Debian Codegraph @

crypto_kem/8192128pcf/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_gf(unsigned char *dest, uint16_t a)
17 {
18 dest[0] = a & 0xFF;
19 dest[1] = a >> 8;
20 }
21
22 static inline uint16_t load_gf(const unsigned char *src)
23 {
24 uint16_t a;
25
26 a = src[1];
27 a <<= 8;
28 a |= src[0];
29
30 return a & GFMASK;
31 }
32
33 static inline uint32_t load4(const unsigned char *src)
34 {
35 uint32_t a;
36
37 a = src[3]; a <<= 8;
38 a |= src[2]; a <<= 8;
39 a |= src[1]; a <<= 8;
40 a |= src[0];
41
42 return a;
43 }
44
45 static inline void irr_load(vec128 * out, const unsigned char * in)
46 {
47 int i, j;
48 uint64_t v0 = 0, v1 = 0;
49 uint16_t irr[ SYS_T ];
50
51 for (i = 0; i < SYS_T; i++)
52 irr[i] = load_gf(in + i*2);
53
54 for (i = 0; i < GFBITS; i++)
55 {
56 for (j = 63; j >= 0; j--)
57 {
58 v0 <<= 1;
59 v1 <<= 1;
60 v0 |= crypto_int64_bitmod_01(irr[j], i);
61 v1 |= crypto_int64_bitmod_01(irr[j+64], i);
62 }
63
64 out[i] = vec128_set2x(v0, v1);
65 }
66 }
67
68 static inline void store8(unsigned char *out, uint64_t in)
69 {
70 out[0] = (in >> 0x00) & 0xFF;
71 out[1] = (in >> 0x08) & 0xFF;
72 out[2] = (in >> 0x10) & 0xFF;
73 out[3] = (in >> 0x18) & 0xFF;
74 out[4] = (in >> 0x20) & 0xFF;
75 out[5] = (in >> 0x28) & 0xFF;
76 out[6] = (in >> 0x30) & 0xFF;
77 out[7] = (in >> 0x38) & 0xFF;
78 }
79
80 static inline uint64_t load8(const unsigned char * in)
81 {
82 int i;
83 uint64_t ret = in[7];
84
85 for (i = 6; i >= 0; i--)
86 {
87 ret <<= 8;
88 ret |= in[i];
89 }
90
91 return ret;
92 }
93
94 static inline vec128 load16(const unsigned char * in)
95 {
96 return vec128_set2x( load8(in), load8(in+8) );
97 }
98
99 static inline void store16(unsigned char * out, vec128 in)
100 {
101 store8(out+0, vec128_extract(in, 0));
102 store8(out+8, vec128_extract(in, 1));
103 }
104
105 #endif
106