Debian Codegraph @

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