-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
128 lines (102 loc) · 3.03 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef UTILS_H
#define UTILS_H
#include <endian.h>
#include <stdint.h>
#include <string.h>
#include <relic/relic.h>
#if !defined(RLC_TRY)
// error-handling macros have been renamed
#define RLC_TRY TRY
#define RLC_CATCH_ANY CATCH_ANY
#define RLC_FINALLY FINALLY
#endif
#include "FIPS202-opt64/KeccakHash.h"
#define G1_SIZE_COMPRESSED (1 + RLC_FP_BYTES)
#define G2_SIZE_COMPRESSED (1 + 2 * RLC_FP_BYTES)
#define GT_SIZE_COMPRESSED (8 * RLC_FP_BYTES)
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
/* helper functions for (de)serialization */
static inline void write_u8(uint8_t** dst, uint8_t v) {
**dst = v;
*dst += sizeof(v);
}
static inline uint8_t read_u8(const uint8_t** src) {
uint8_t v = **src;
*src += sizeof(v);
return v;
}
static inline void write_u32(uint8_t** dst, uint32_t v) {
v = htole32(v);
memcpy(*dst, &v, sizeof(v));
*dst += sizeof(v);
}
static inline uint32_t read_u32(const uint8_t** src) {
uint32_t v;
memcpy(&v, *src, sizeof(v));
*src += sizeof(v);
return le32toh(v);
}
static inline void write_u64(uint8_t** dst, uint64_t v) {
v = htole64(v);
memcpy(*dst, &v, sizeof(v));
*dst += sizeof(v);
}
static inline uint64_t read_u64(const uint8_t** src) {
uint64_t v;
memcpy(&v, *src, sizeof(v));
*src += sizeof(v);
return le64toh(v);
}
static inline void write_bn(uint8_t** dst, const bn_t v) {
bn_write_bin(*dst, RLC_BN_SIZE, v);
*dst += RLC_BN_SIZE;
}
static inline void read_bn(bn_t v, const uint8_t** src) {
bn_read_bin(v, *src, RLC_BN_SIZE);
*src += RLC_BN_SIZE;
}
static inline void write_g1(uint8_t** dst, const g1_t v) {
g1_write_bin(*dst, G1_SIZE_COMPRESSED, v, 1);
*dst += G1_SIZE_COMPRESSED;
}
static inline void read_g1(g1_t v, const uint8_t** src) {
g1_read_bin(v, *src, G1_SIZE_COMPRESSED);
*src += G1_SIZE_COMPRESSED;
}
static inline void write_g2(uint8_t** dst, g2_t v) {
g2_write_bin(*dst, G2_SIZE_COMPRESSED, v, 1);
*dst += G2_SIZE_COMPRESSED;
}
static inline void read_g2(g2_t v, const uint8_t** src) {
g2_read_bin(v, *src, G2_SIZE_COMPRESSED);
*src += G2_SIZE_COMPRESSED;
}
static inline void write_gt(uint8_t** dst, gt_t v) {
gt_write_bin(*dst, GT_SIZE_COMPRESSED, v, 1);
*dst += GT_SIZE_COMPRESSED;
}
static inline void read_gt(gt_t v, const uint8_t** src) {
gt_read_bin(v, *src, GT_SIZE_COMPRESSED);
*src += GT_SIZE_COMPRESSED;
}
static inline void hash_update_g1(Keccak_HashInstance* ctx, const g1_t v) {
uint8_t buffer[G1_SIZE_COMPRESSED];
g1_write_bin(buffer, G1_SIZE_COMPRESSED, v, 1);
Keccak_HashUpdate(ctx, buffer, G1_SIZE_COMPRESSED * 8);
}
static inline void hash_update_g2(Keccak_HashInstance* ctx, g2_t v) {
uint8_t buffer[G2_SIZE_COMPRESSED];
g2_write_bin(buffer, G2_SIZE_COMPRESSED, v, 1);
Keccak_HashUpdate(ctx, buffer, G2_SIZE_COMPRESSED * 8);
}
static inline void hash_update_gt(Keccak_HashInstance* ctx, gt_t v) {
uint8_t buffer[GT_SIZE_COMPRESSED];
gt_write_bin(buffer, GT_SIZE_COMPRESSED, v, 1);
Keccak_HashUpdate(ctx, buffer, GT_SIZE_COMPRESSED * 8);
}
#endif