-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathbigint.nr
406 lines (372 loc) · 12.5 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::cmp::Eq;
use crate::ops::{Add, Div, Mul, Sub};
global bn254_fq: [u8] = &[
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: [u8] = &[
1, 0, 0, 240, 147, 245, 225, 67, 145, 112, 185, 121, 72, 232, 51, 40, 93, 88, 129, 129, 182, 69,
80, 184, 41, 160, 49, 225, 114, 78, 100, 48,
];
global secpk1_fr: [u8] = &[
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: [u8] = &[
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: [u8] = &[
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: [u8] = &[
81, 37, 99, 252, 194, 202, 185, 243, 132, 158, 23, 167, 173, 250, 230, 188, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255,
];
// docs:start:big_int_definition
pub struct BigInt {
pointer: u32,
modulus: u32,
}
// docs:end:big_int_definition
impl BigInt {
#[foreign(bigint_add)]
fn bigint_add(self, other: BigInt) -> BigInt {}
#[foreign(bigint_sub)]
fn bigint_sub(self, other: BigInt) -> BigInt {}
#[foreign(bigint_mul)]
fn bigint_mul(self, other: BigInt) -> BigInt {}
#[foreign(bigint_div)]
fn bigint_div(self, other: BigInt) -> BigInt {}
#[foreign(bigint_from_le_bytes)]
fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {}
#[foreign(bigint_to_le_bytes)]
fn to_le_bytes(self) -> [u8; 32] {}
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
}
}
pub trait BigField {
fn from_le_bytes(bytes: [u8]) -> Self;
fn from_le_bytes_32(bytes: [u8; 32]) -> Self;
fn to_le_bytes(self) -> [u8];
}
pub struct Secpk1Fq {
array: [u8; 32],
}
impl BigField for Secpk1Fq {
fn from_le_bytes(bytes: [u8]) -> Secpk1Fq {
assert(bytes.len() <= 32);
let mut array = [0; 32];
for i in 0..bytes.len() {
array[i] = bytes[i];
}
Secpk1Fq { array }
}
fn from_le_bytes_32(bytes: [u8; 32]) -> Secpk1Fq {
Secpk1Fq { array: bytes }
}
fn to_le_bytes(self) -> [u8] {
self.array
}
}
impl Add for Secpk1Fq {
fn add(self: Self, other: Secpk1Fq) -> Secpk1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fq);
Secpk1Fq { array: a.bigint_add(b).to_le_bytes() }
}
}
impl Sub for Secpk1Fq {
fn sub(self: Self, other: Secpk1Fq) -> Secpk1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fq);
Secpk1Fq { array: a.bigint_sub(b).to_le_bytes() }
}
}
impl Mul for Secpk1Fq {
fn mul(self: Self, other: Secpk1Fq) -> Secpk1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fq);
Secpk1Fq { array: a.bigint_mul(b).to_le_bytes() }
}
}
impl Div for Secpk1Fq {
fn div(self: Self, other: Secpk1Fq) -> Secpk1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fq);
Secpk1Fq { array: a.bigint_div(b).to_le_bytes() }
}
}
impl Eq for Secpk1Fq {
fn eq(self: Self, other: Secpk1Fq) -> bool {
self.array == other.array
}
}
pub struct Secpk1Fr {
array: [u8; 32],
}
impl BigField for Secpk1Fr {
fn from_le_bytes(bytes: [u8]) -> Secpk1Fr {
assert(bytes.len() <= 32);
let mut array = [0; 32];
for i in 0..bytes.len() {
array[i] = bytes[i];
}
Secpk1Fr { array }
}
fn from_le_bytes_32(bytes: [u8; 32]) -> Secpk1Fr {
Secpk1Fr { array: bytes }
}
fn to_le_bytes(self) -> [u8] {
self.array
}
}
impl Add for Secpk1Fr {
fn add(self: Self, other: Secpk1Fr) -> Secpk1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fr);
Secpk1Fr { array: a.bigint_add(b).to_le_bytes() }
}
}
impl Sub for Secpk1Fr {
fn sub(self: Self, other: Secpk1Fr) -> Secpk1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fr);
Secpk1Fr { array: a.bigint_sub(b).to_le_bytes() }
}
}
impl Mul for Secpk1Fr {
fn mul(self: Self, other: Secpk1Fr) -> Secpk1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fr);
Secpk1Fr { array: a.bigint_mul(b).to_le_bytes() }
}
}
impl Div for Secpk1Fr {
fn div(self: Self, other: Secpk1Fr) -> Secpk1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpk1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpk1_fr);
Secpk1Fr { array: a.bigint_div(b).to_le_bytes() }
}
}
impl Eq for Secpk1Fr {
fn eq(self: Self, other: Secpk1Fr) -> bool {
self.array == other.array
}
}
pub struct Bn254Fr {
array: [u8; 32],
}
impl BigField for Bn254Fr {
fn from_le_bytes(bytes: [u8]) -> Bn254Fr {
assert(bytes.len() <= 32);
let mut array = [0; 32];
for i in 0..bytes.len() {
array[i] = bytes[i];
}
Bn254Fr { array }
}
fn from_le_bytes_32(bytes: [u8; 32]) -> Bn254Fr {
Bn254Fr { array: bytes }
}
fn to_le_bytes(self) -> [u8] {
self.array
}
}
impl Add for Bn254Fr {
fn add(self: Self, other: Bn254Fr) -> Bn254Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fr);
Bn254Fr { array: a.bigint_add(b).to_le_bytes() }
}
}
impl Sub for Bn254Fr {
fn sub(self: Self, other: Bn254Fr) -> Bn254Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fr);
Bn254Fr { array: a.bigint_sub(b).to_le_bytes() }
}
}
impl Mul for Bn254Fr {
fn mul(self: Self, other: Bn254Fr) -> Bn254Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fr);
Bn254Fr { array: a.bigint_mul(b).to_le_bytes() }
}
}
impl Div for Bn254Fr {
fn div(self: Self, other: Bn254Fr) -> Bn254Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fr);
Bn254Fr { array: a.bigint_div(b).to_le_bytes() }
}
}
impl Eq for Bn254Fr {
fn eq(self: Self, other: Bn254Fr) -> bool {
self.array == other.array
}
}
pub struct Bn254Fq {
array: [u8; 32],
}
impl BigField for Bn254Fq {
fn from_le_bytes(bytes: [u8]) -> Bn254Fq {
assert(bytes.len() <= 32);
let mut array = [0; 32];
for i in 0..bytes.len() {
array[i] = bytes[i];
}
Bn254Fq { array }
}
fn from_le_bytes_32(bytes: [u8; 32]) -> Bn254Fq {
Bn254Fq { array: bytes }
}
fn to_le_bytes(self) -> [u8] {
self.array
}
}
impl Add for Bn254Fq {
fn add(self: Self, other: Bn254Fq) -> Bn254Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fq);
Bn254Fq { array: a.bigint_add(b).to_le_bytes() }
}
}
impl Sub for Bn254Fq {
fn sub(self: Self, other: Bn254Fq) -> Bn254Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fq);
Bn254Fq { array: a.bigint_sub(b).to_le_bytes() }
}
}
impl Mul for Bn254Fq {
fn mul(self: Self, other: Bn254Fq) -> Bn254Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fq);
Bn254Fq { array: a.bigint_mul(b).to_le_bytes() }
}
}
impl Div for Bn254Fq {
fn div(self: Self, other: Bn254Fq) -> Bn254Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), bn254_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), bn254_fq);
Bn254Fq { array: a.bigint_div(b).to_le_bytes() }
}
}
impl Eq for Bn254Fq {
fn eq(self: Self, other: Bn254Fq) -> bool {
self.array == other.array
}
}
pub struct Secpr1Fq {
array: [u8; 32],
}
impl BigField for Secpr1Fq {
fn from_le_bytes(bytes: [u8]) -> Secpr1Fq {
assert(bytes.len() <= 32);
let mut array = [0; 32];
for i in 0..bytes.len() {
array[i] = bytes[i];
}
Secpr1Fq { array }
}
fn from_le_bytes_32(bytes: [u8; 32]) -> Secpr1Fq {
Secpr1Fq { array: bytes }
}
fn to_le_bytes(self) -> [u8] {
self.array
}
}
impl Add for Secpr1Fq {
fn add(self: Self, other: Secpr1Fq) -> Secpr1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fq);
Secpr1Fq { array: a.bigint_add(b).to_le_bytes() }
}
}
impl Sub for Secpr1Fq {
fn sub(self: Self, other: Secpr1Fq) -> Secpr1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fq);
Secpr1Fq { array: a.bigint_sub(b).to_le_bytes() }
}
}
impl Mul for Secpr1Fq {
fn mul(self: Self, other: Secpr1Fq) -> Secpr1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fq);
Secpr1Fq { array: a.bigint_mul(b).to_le_bytes() }
}
}
impl Div for Secpr1Fq {
fn div(self: Self, other: Secpr1Fq) -> Secpr1Fq {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fq);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fq);
Secpr1Fq { array: a.bigint_div(b).to_le_bytes() }
}
}
impl Eq for Secpr1Fq {
fn eq(self: Self, other: Secpr1Fq) -> bool {
self.array == other.array
}
}
pub struct Secpr1Fr {
array: [u8; 32],
}
impl BigField for Secpr1Fr {
fn from_le_bytes(bytes: [u8]) -> Secpr1Fr {
assert(bytes.len() <= 32);
let mut array = [0; 32];
for i in 0..bytes.len() {
array[i] = bytes[i];
}
Secpr1Fr { array }
}
fn from_le_bytes_32(bytes: [u8; 32]) -> Secpr1Fr {
Secpr1Fr { array: bytes }
}
fn to_le_bytes(self) -> [u8] {
self.array
}
}
impl Add for Secpr1Fr {
fn add(self: Self, other: Secpr1Fr) -> Secpr1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fr);
Secpr1Fr { array: a.bigint_add(b).to_le_bytes() }
}
}
impl Sub for Secpr1Fr {
fn sub(self: Self, other: Secpr1Fr) -> Secpr1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fr);
Secpr1Fr { array: a.bigint_sub(b).to_le_bytes() }
}
}
impl Mul for Secpr1Fr {
fn mul(self: Self, other: Secpr1Fr) -> Secpr1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fr);
Secpr1Fr { array: a.bigint_mul(b).to_le_bytes() }
}
}
impl Div for Secpr1Fr {
fn div(self: Self, other: Secpr1Fr) -> Secpr1Fr {
let a = BigInt::from_le_bytes(self.array.as_slice(), secpr1_fr);
let b = BigInt::from_le_bytes(other.array.as_slice(), secpr1_fr);
Secpr1Fr { array: a.bigint_div(b).to_le_bytes() }
}
}
impl Eq for Secpr1Fr {
fn eq(self: Self, other: Secpr1Fr) -> bool {
self.array == other.array
}
}