-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.c
105 lines (85 loc) · 1.83 KB
/
core.c
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
/* relic setup */
#include "core.h"
#include "include/types.h"
#include <sodium.h>
#include <stdbool.h>
static bool core_init_run = false;
static bn_t order;
static bn_t one;
unsigned int order_size;
__attribute__((constructor)) static void init_relic(void) {
if (sodium_init() == -1) {
// TODO: handle!
}
if (!core_get()) {
core_init();
core_init_run = true;
}
ep_param_set_any_pairf();
bn_null(order);
bn_null(one);
bn_new(order);
bn_new(one);
ep_curve_get_ord(order);
order_size = bn_size_bin(order);
}
__attribute__((destructor)) static void clean_relic(void) {
if (core_init_run) {
bn_free(new);
bn_free(order);
core_init_run = false;
core_clean();
}
}
static bool bn_is_one(const bn_t a) {
return bn_cmp(a, one) == RLC_EQ;
}
void zp_rand(bn_t b) {
bn_rand_mod(b, order);
}
void zp_add(bn_t c, const bn_t a, const bn_t b) {
bn_add(c, a, b);
bn_mod(c, c, order);
}
void zp_sub(bn_t c, const bn_t a, const bn_t b) {
bn_sub(c, a, b);
bn_mod(c, c, order);
if (bn_sign(c) == RLC_NEG) {
bn_add(c, c, order);
}
}
void zp_mul(bn_t c, const bn_t a, const bn_t b) {
bn_mul(c, a, b);
if (bn_sign(c) == RLC_NEG) {
bn_add(c, c, order);
} else {
bn_mod(c, c, order);
}
}
void zp_div(bn_t c, const bn_t a, const bn_t b) {
bn_t s;
bn_null(s);
RLC_TRY {
bn_new(s);
bn_gcd_ext(s, c, NULL, b, order);
if (bn_sign(c) == RLC_NEG) {
bn_add(c, c, order);
}
if (!bn_is_one(a)) {
bn_new(s);
bn_mul(s, a, c);
bn_div_rem(s, c, s, order);
}
}
RLC_FINALLY {
bn_free(s);
}
}
void hash_squeeze_zp(bn_t bn, Keccak_HashInstance* ctx) {
uint8_t buffer[MAX_ORDER_SIZE];
Keccak_HashSqueeze(ctx, buffer, order_size * 8);
bn_read_bin(bn, buffer, order_size);
if (bn_cmp(bn, order) == RLC_GT) {
bn_mod(bn, bn, order);
}
}