-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathbigint.nr
351 lines (323 loc) · 8.8 KB
/
bigint.nr
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use crate::ops::{Add, Sub, Mul, Div};
use crate::cmp::Eq;
global bn254_fq = &[0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
0x5d, 0x58, 0x81, 0x81, 0xb6, 0x45, 0x50, 0xb8, 0x29, 0xa0, 0x31, 0xe1, 0x72, 0x4e, 0x64, 0x30];
global bn254_fr = &[0x01, 0x00, 0x00, 0x00, 0x3F, 0x59, 0x1F, 0x43, 0x09, 0x97, 0xB9, 0x79, 0x48, 0xE8, 0x33, 0x28,
0x5D, 0x58, 0x81, 0x81, 0xB6, 0x45, 0x50, 0xB8, 0x29, 0xA0, 0x31, 0xE1, 0x72, 0x4E, 0x64, 0x30];
global secpk1_fr = &[0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA,
0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
global secpk1_fq = &[0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
global secpr1_fq = &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF];
global secpr1_fr = &[0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3, 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,0xFF, 0xFF, 0xFF, 0xFF];
// docs:start:big_int_definition
struct BigInt {
pointer: u32,
modulus: u32,
}
// docs:end:big_int_definition
impl BigInt {
#[builtin(bigint_add)]
fn bigint_add(self, other: BigInt) -> BigInt {}
#[builtin(bigint_sub)]
fn bigint_sub(self, other: BigInt) -> BigInt {}
#[builtin(bigint_mul)]
fn bigint_mul(self, other: BigInt) -> BigInt {}
#[builtin(bigint_div)]
fn bigint_div(self, other: BigInt) -> BigInt {}
#[builtin(bigint_from_le_bytes)]
fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {}
#[builtin(bigint_to_le_bytes)]
fn to_le_bytes(self) -> [u8] {}
fn check_32_bytes(self: Self, other: BigInt) -> bool {
let bytes = self.to_le_bytes();
let o_bytes = other.to_le_bytes();
let mut result = true;
for i in 0..32 {
result = result & (bytes[i] == o_bytes[i]);
}
result
}
}
trait BigField {
fn from_le_bytes(bytes: [u8]) -> Self;
fn to_le_bytes(self) -> [u8];
}
struct Secpk1Fq {
inner: BigInt,
}
impl BigField for Secpk1Fq {
fn from_le_bytes(bytes: [u8]) -> Secpk1Fq {
Secpk1Fq {
inner: BigInt::from_le_bytes(bytes, secpk1_fq)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}
impl Add for Secpk1Fq {
fn add(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Secpk1Fq {
fn sub(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpk1Fq {
fn mul(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_mul(other.inner)
}
}
}
impl Div for Secpk1Fq {
fn div(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Secpk1Fq {
fn eq(self: Self, other: Secpk1Fq) -> bool {
self.inner.check_32_bytes(other.inner)
}
}
struct Secpk1Fr {
inner: BigInt,
}
impl BigField for Secpk1Fr {
fn from_le_bytes(bytes: [u8]) -> Secpk1Fr {
Secpk1Fr {
inner: BigInt::from_le_bytes(bytes, secpk1_fr)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}
impl Add for Secpk1Fr {
fn add(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Secpk1Fr {
fn sub(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpk1Fr {
fn mul(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_mul(other.inner)
}
}
}
impl Div for Secpk1Fr {
fn div(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Secpk1Fr {
fn eq(self: Self, other: Secpk1Fr) -> bool {
self.inner.check_32_bytes(other.inner)
}
}
struct Bn254Fr {
inner: BigInt,
}
impl BigField for Bn254Fr {
fn from_le_bytes(bytes: [u8]) -> Bn254Fr {
Bn254Fr {
inner: BigInt::from_le_bytes(bytes, bn254_fr)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}
impl Add for Bn254Fr {
fn add(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Bn254Fr {
fn sub(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Bn254Fr {
fn mul(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_mul(other.inner)
}
}
}
impl Div for Bn254Fr {
fn div(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Bn254Fr {
fn eq(self: Self, other: Bn254Fr) -> bool {
self.inner.check_32_bytes(other.inner)
}
}
struct Bn254Fq {
inner: BigInt,
}
impl BigField for Bn254Fq {
fn from_le_bytes(bytes: [u8]) -> Bn254Fq {
Bn254Fq {
inner: BigInt::from_le_bytes(bytes, bn254_fq)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}
impl Add for Bn254Fq {
fn add(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Bn254Fq {
fn sub(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Bn254Fq {
fn mul(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_mul(other.inner)
}
}
}
impl Div for Bn254Fq {
fn div(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Bn254Fq {
fn eq(self: Self, other: Bn254Fq) -> bool {
self.inner.check_32_bytes(other.inner)
}
}
struct Secpr1Fq {
inner: BigInt,
}
impl BigField for Secpr1Fq {
fn from_le_bytes(bytes: [u8]) -> Secpr1Fq {
Secpr1Fq {
inner: BigInt::from_le_bytes(bytes, secpr1_fq)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}
impl Add for Secpr1Fq {
fn add(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Secpr1Fq {
fn sub(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpr1Fq {
fn mul(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_mul(other.inner)
}
}
}
impl Div for Secpr1Fq {
fn div(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Secpr1Fq {
fn eq(self: Self, other: Secpr1Fq) -> bool {
self.inner.check_32_bytes(other.inner)
}
}
struct Secpr1Fr {
inner: BigInt,
}
impl BigField for Secpr1Fr {
fn from_le_bytes(bytes: [u8]) -> Secpr1Fr {
Secpr1Fr {
inner: BigInt::from_le_bytes(bytes, secpr1_fr)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}
impl Add for Secpr1Fr {
fn add(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Secpr1Fr {
fn sub(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpr1Fr {
fn mul(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_mul(other.inner)
}
}
}
impl Div for Secpr1Fr {
fn div(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Secpr1Fr {
fn eq(self: Self, other: Secpr1Fr) -> bool {
self.inner.check_32_bytes(other.inner)
}
}