Debian Codegraph @

crypto_kem/348864/avx/util.h

raw
1 /*
2 This file is for loading/storing data in a little-endian fashion
3 */
4 // 20240805 djb: more use of cryptoint
5 // 20240508 djb: remove unused bitrev
6
7 #ifndef UTIL_H
8 #define UTIL_H
9
10 #include "gf.h"
11 #include "params.h"
12 #include "vec128.h"
13
14 #include <stdint.h>
15 #include "crypto_int64.h"
16
17 static inline void store_i(unsigned char *out, uint64_t in, int i)
18 {
19 int j;
20
21 for (j = 0; j < i; j++)
22 out[j] = (in >> (j * 8)) & 0xFF;
23 }
24
25 static inline void store_gf(unsigned char *dest, gf a)
26 {
27 dest[0] = a & 0xFF;
28 dest[1] = a >> 8;
29 }
30
31 static inline uint16_t load_gf(const unsigned char *src)
32 {
33 uint16_t a;
34
35 a = src[1];
36 a <<= 8;
37 a |= src[0];
38
39 return a & GFMASK;
40 }
41
42 static inline uint32_t load4(const unsigned char *src)
43 {
44 uint32_t a;
45
46 a = src[3]; a <<= 8;
47 a |= src[2]; a <<= 8;
48 a |= src[1]; a <<= 8;
49 a |= src[0];
50
51 return a;
52 }
53
54 static inline void irr_load(uint64_t * out, const unsigned char * in)
55 {
56 int i, j;
57 uint16_t irr[ SYS_T + 1 ];
58
59 for (i = 0; i < SYS_T; i++)
60 irr[i] = load_gf(in + i*2);
61
62 irr[ SYS_T ] = 1;
63
64 for (i = 0; i < GFBITS; i++)
65 out[i] = 0;
66
67 for (i = SYS_T; i >= 0; i--)
68 for (j = 0; j < GFBITS; j++)
69 {
70 out[j] <<= 1;
71 out[j] |= crypto_int64_bitmod_01(irr[i], j);
72 }
73 }
74
75 static inline void store8(unsigned char *out, uint64_t in)
76 {
77 out[0] = (in >> 0x00) & 0xFF;
78 out[1] = (in >> 0x08) & 0xFF;
79 out[2] = (in >> 0x10) & 0xFF;
80 out[3] = (in >> 0x18) & 0xFF;
81 out[4] = (in >> 0x20) & 0xFF;
82 out[5] = (in >> 0x28) & 0xFF;
83 out[6] = (in >> 0x30) & 0xFF;
84 out[7] = (in >> 0x38) & 0xFF;
85 }
86
87 static inline uint64_t load8(const unsigned char * in)
88 {
89 int i;
90 uint64_t ret = in[7];
91
92 for (i = 6; i >= 0; i--)
93 {
94 ret <<= 8;
95 ret |= in[i];
96 }
97
98 return ret;
99 }
100
101 static inline vec128 load16(const unsigned char * in)
102 {
103 return vec128_set2x( load8(in), load8(in+8) );
104 }
105
106 static inline void store16(unsigned char * out, vec128 in)
107 {
108 store8(out+0, vec128_extract(in, 0));
109 store8(out+8, vec128_extract(in, 1));
110 }
111
112 #endif
113