-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfp.h
More file actions
201 lines (172 loc) · 4.6 KB
/
fp.h
File metadata and controls
201 lines (172 loc) · 4.6 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#pragma once
#include "constants.h"
#include "mod.h"
#include "types.h"
#include "globals.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static inline void fp_set_small(fp_t *x, const uint64_t val) {
modint((int)val, *x);
}
static inline void fp_mul_small(fp_t *x, const fp_t *a, const uint32_t val) {
modmli(*a, (int)val, *x);
}
static inline void fp_set_zero(fp_t *x) {
modzer(*x);
}
static inline void fp_set_one(fp_t *x) {
modone(*x);
}
static inline uint32_t fp_is_equal(const fp_t *a, const fp_t *b) {
return -(uint32_t)modcmp(*a, *b);
}
static inline uint32_t fp_is_zero(const fp_t *a) {
return -(uint32_t)modis0(*a);
}
static inline void fp_copy(fp_t *out, const fp_t *a) {
modcpy(*a, *out);
}
static inline void fp_cswap(fp_t *a, fp_t *b, uint32_t ctl) {
modcsw((int)(ctl & 0x1), *a, *b);
}
static inline void fp_add(fp_t *out, const fp_t *a, const fp_t *b) {
modadd(*a, *b, *out);
}
static inline void fp_sub(fp_t *out, const fp_t *a, const fp_t *b) {
modsub(*a, *b, *out);
}
static inline void fp_neg(fp_t *out, const fp_t *a) {
modneg(*a, *out);
}
static inline void fp_sqr(fp_t *out, const fp_t *a) {
modsqr(*a, *out);
}
static inline void fp_mul(fp_t *out, const fp_t *a, const fp_t *b) {
modmul(*a, *b, *out);
}
static inline void fp_inv(
#if DEBUG_MODINV
const char *file_name, int line_num,
#endif
fp_t *x) {
modinv(
#if DEBUG_MODINV
file_name, line_num,
#endif
*x, NULL, *x);
}
static inline uint32_t fp_is_square(const fp_t *a) {
return -(uint32_t)modqr(NULL, *a);
}
static inline void fp_sqrt(fp_t *a) {
modsqrt(*a, NULL, *a);
}
static inline void fp_half(fp_t *out, const fp_t *a) {
modmul(TWO_INV, *a, *out);
}
static inline void fp_exp3div4(fp_t *out, const fp_t *a) {
modpro(*a, *out);
}
static inline void fp_div3(fp_t *out, const fp_t *a) {
modmul(THREE_INV, *a, *out);
}
static inline void fp_select(fp_t *d, const fp_t *a0, const fp_t *a1, uint32_t ctl) {
uint64_t cw = (int32_t)ctl;
for (unsigned int i = 0; i < NWORDS_FIELD; i++) {
(*d)[i] = (*a0)[i] ^ (cw & ((*a0)[i] ^ (*a1)[i]));
}
}
static inline void fp_encode(void *dst, const fp_t *a) {
int i;
uint64_t c[5];
redc(*a, c);
for (i = 0; i < 32; i++) {
((char *)dst)[i] = c[0] & (uint64_t)0xff;
(void)modshr(8, c);
}
}
static inline uint32_t fp_decode(fp_t *d, const void *src) {
int i;
uint64_t res;
const unsigned char *b = src;
for (i = 0; i < 5; i++) {
(*d)[i] = 0;
}
for (i = 31; i >= 0; i--) {
modshl(8, *d);
(*d)[0] += (uint64_t)b[i];
}
res = (uint64_t)-modfsb(*d);
nres(*d, *d);
for (i = 0; i < 5; i++) {
(*d)[i] &= res;
}
return (uint32_t)res;
}
static inline unsigned char add_carry(unsigned char cc, uint64_t a, uint64_t b, uint64_t *d) {
__uint128_t t = (__uint128_t)a + (__uint128_t)b + cc;
*d = (uint64_t)t;
return (unsigned char)(t >> WORD_LEN);
}
static inline void partial_reduce(uint64_t *out, const uint64_t *src) {
uint64_t h, l, quo, rem;
unsigned char cc;
h = src[3] >> 56;
l = src[3] & 0x00FFFFFFFFFFFFFF;
quo = (h * 0xCD) >> 10;
rem = h - (5 * quo);
cc = add_carry(0, src[0], quo, &out[0]);
cc = add_carry(cc, src[1], 0, &out[1]);
cc = add_carry(cc, src[2], 0, &out[2]);
(void)add_carry(cc, l, rem << 56, &out[3]);
}
static inline void enc64le(void *dst, uint64_t x) {
uint8_t *buf = dst;
buf[0] = (uint8_t)x;
buf[1] = (uint8_t)(x >> 8);
buf[2] = (uint8_t)(x >> 16);
buf[3] = (uint8_t)(x >> 24);
buf[4] = (uint8_t)(x >> 32);
buf[5] = (uint8_t)(x >> 40);
buf[6] = (uint8_t)(x >> 48);
buf[7] = (uint8_t)(x >> 56);
}
static inline uint64_t dec64le(const void *src) {
const uint8_t *buf = src;
return (uint64_t)buf[0] | ((uint64_t)buf[1] << 8) | ((uint64_t)buf[2] << 16) | ((uint64_t)buf[3] << 24) |
((uint64_t)buf[4] << 32) | ((uint64_t)buf[5] << 40) | ((uint64_t)buf[6] << 48) | ((uint64_t)buf[7] << 56);
}
static inline void fp_decode_reduce(fp_t *d, const void *src, size_t len) {
uint64_t t[4];
uint8_t tmp[32];
const uint8_t *b = src;
fp_set_zero(d);
if (len == 0) {
return;
}
size_t rem = len % 32;
if (rem != 0) {
size_t k = len - rem;
memcpy(tmp, b + k, len - k);
memset(tmp + len - k, 0, (sizeof tmp) - (len - k));
fp_decode(d, tmp);
len = k;
}
while (len > 0) {
fp_mul(d, d, &R2);
len -= 32;
t[0] = dec64le(b + len);
t[1] = dec64le(b + len + 8);
t[2] = dec64le(b + len + 16);
t[3] = dec64le(b + len + 24);
partial_reduce(t, t);
enc64le(tmp, t[0]);
enc64le(tmp + 8, t[1]);
enc64le(tmp + 16, t[2]);
enc64le(tmp + 24, t[3]);
fp_t a;
fp_decode(&a, tmp);
fp_add(d, d, &a);
}
}