Debian Codegraph @

crypto_kem/6688128pcf/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: include vec128_gf.h
6
7 #ifndef UTIL_H
8 #define UTIL_H
9
10 #include "vec128_gf.h"
11 #include "params.h"
12
13 #include <stdint.h>
14 #include "crypto_int64.h"
15
16 static inline void store_i(unsigned char *out, uint64_t in, int i)
17 {
18 int j;
19
20 for (j = 0; j < i; j++)
21 out[j] = (in >> (j * 8)) & 0xFF;
22 }
23
24 static inline void store_gf(unsigned char *dest, uint16_t a)
25 {
26 dest[0] = a & 0xFF;
27 dest[1] = a >> 8;
28 }
29
30 static inline uint16_t load_gf(const unsigned char *src)
31 {
32 uint16_t a;
33
34 a = src[1];
35 a <<= 8;
36 a |= src[0];
37
38 return a & GFMASK;
39 }
40
41 static inline uint32_t load4(const unsigned char *src)
42 {
43 uint32_t a;
44
45 a = src[3]; a <<= 8;
46 a |= src[2]; a <<= 8;
47 a |= src[1]; a <<= 8;
48 a |= src[0];
49
50 return a;
51 }
52
53 static inline void irr_load(vec128 * out, const unsigned char * in)
54 {
55 int i, j;
56 uint64_t v[2];
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 {
66 v[0] = v[1] = 0;
67
68 for (j = 63; j >= 0; j--) {v[0] <<= 1; v[0] |= crypto_int64_bitmod_01(irr[j], i);}
69 for (j = SYS_T; j >= 64; j--) {v[1] <<= 1; v[1] |= crypto_int64_bitmod_01(irr[j], i);}
70
71 out[i] = vec128_set2x(v[0], v[1]);
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