-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbignum.nr
375 lines (318 loc) · 13.1 KB
/
bignum.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
use crate::utils::map::map;
use crate::params::BigNumParamsGetter;
use crate::fns::{
constrained_ops::{
add, assert_is_not_equal, conditional_select, derive_from_seed, div, eq, mul, neg, sub,
udiv, udiv_mod, umod, validate_in_field, validate_in_range,
}, expressions::{__compute_quadratic_expression, evaluate_quadratic_expression},
serialization::{from_be_bytes, to_le_bytes},
unconstrained_ops::{
__add, __batch_invert, __batch_invert_slice, __derive_from_seed, __div, __eq, __invmod,
__is_zero, __mul, __neg, __pow, __sub, __tonelli_shanks_sqrt, __udiv_mod,
},
};
pub struct BigNum<let N: u32, let MOD_BITS: u32, Params> {
pub limbs: [Field; N],
}
// We aim to avoid needing to add a generic parameter to this trait, for this reason we do not allow
// accessing the limbs of the bignum except through slices.
pub trait BigNumTrait {
// TODO: this crashes the compiler? v0.32
// fn default() -> Self { std::default::Default::default () }
pub fn new() -> Self;
pub fn one() -> Self;
pub fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
pub unconstrained fn __derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
pub fn from_slice(limbs: [Field]) -> Self;
pub fn from_be_bytes<let NBytes: u32>(x: [u8; NBytes]) -> Self;
pub fn to_le_bytes<let NBytes: u32>(self) -> [u8; NBytes];
pub fn modulus() -> Self;
pub fn modulus_bits(self) -> u32;
pub fn num_limbs(self) -> u32;
pub fn get_limbs_slice(self) -> [Field];
pub fn get_limb(self, idx: u32) -> Field;
pub fn set_limb(&mut self, idx: u32, value: Field);
pub unconstrained fn __eq(self, other: Self) -> bool;
pub unconstrained fn __is_zero(self) -> bool;
pub unconstrained fn __neg(self) -> Self;
pub unconstrained fn __add(self, other: Self) -> Self;
pub unconstrained fn __sub(self, other: Self) -> Self;
pub unconstrained fn __mul(self, other: Self) -> Self;
pub unconstrained fn __div(self, other: Self) -> Self;
pub unconstrained fn __udiv_mod(self, divisor: Self) -> (Self, Self);
pub unconstrained fn __invmod(self) -> Self;
pub unconstrained fn __pow(self, exponent: Self) -> Self;
pub unconstrained fn __batch_invert<let M: u32>(to_invert: [Self; M]) -> [Self; M];
pub unconstrained fn __batch_invert_slice<let M: u32>(to_invert: [Self]) -> [Self];
pub unconstrained fn __tonelli_shanks_sqrt(self) -> std::option::Option<Self>;
pub unconstrained fn __compute_quadratic_expression<let LHS_N: u32, let RHS_N: u32, let NUM_PRODUCTS: u32, let ADD_N: u32>(
lhs: [[Self; LHS_N]; NUM_PRODUCTS],
lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS],
rhs: [[Self; RHS_N]; NUM_PRODUCTS],
rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS],
add: [Self; ADD_N],
add_flags: [bool; ADD_N],
) -> (Self, Self);
pub fn evaluate_quadratic_expression<let LHS_N: u32, let RHS_N: u32, let NUM_PRODUCTS: u32, let ADD_N: u32>(
lhs: [[Self; LHS_N]; NUM_PRODUCTS],
lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS],
rhs: [[Self; RHS_N]; NUM_PRODUCTS],
rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS],
add: [Self; ADD_N],
add_flags: [bool; ADD_N],
);
pub fn eq(self, other: Self) -> bool {
self == other
}
pub fn assert_is_not_equal(self, other: Self);
pub fn validate_in_range(self);
pub fn validate_in_field(self);
pub fn neg(self) -> Self;
pub fn add(self, other: Self) -> Self {
self + other
}
pub fn sub(self, other: Self) -> Self {
self - other
}
pub fn mul(self, other: Self) -> Self {
self * other
}
pub fn div(self, other: Self) -> Self {
self / other
}
pub fn udiv_mod(self, divisor: Self) -> (Self, Self);
pub fn udiv(self, divisor: Self) -> Self;
pub fn umod(self, divisor: Self) -> Self;
pub fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self;
}
impl<let N: u32, let MOD_BITS: u32, Params> BigNumTrait for BigNum<N, MOD_BITS, Params>
where
Params: BigNumParamsGetter<N, MOD_BITS>,
{
fn new() -> Self {
Self { limbs: [0; N] }
}
fn one() -> Self {
let mut result = BigNum::new();
result.limbs[0] = 1;
result
}
fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self {
let params = Params::get_params();
Self { limbs: derive_from_seed::<_, MOD_BITS, _>(params, seed) }
}
unconstrained fn __derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self {
let params = Params::get_params();
Self { limbs: __derive_from_seed::<_, MOD_BITS, _>(params, seed) }
}
fn from_slice(limbs: [Field]) -> Self {
Self { limbs: limbs.as_array() }
}
fn from_be_bytes<let NBytes: u32>(x: [u8; NBytes]) -> Self {
Self { limbs: from_be_bytes::<_, MOD_BITS, _>(x) }
}
fn to_le_bytes<let NBytes: u32>(self) -> [u8; NBytes] {
to_le_bytes::<_, MOD_BITS, _>(self.limbs)
}
fn modulus() -> Self {
Self { limbs: Params::get_params().modulus }
}
fn modulus_bits(_: Self) -> u32 {
MOD_BITS
}
fn num_limbs(_: Self) -> u32 {
N
}
fn get_limbs_slice(self) -> [Field] {
self.limbs
}
fn get_limb(self, idx: u32) -> Field {
self.limbs[idx]
}
fn set_limb(&mut self, idx: u32, value: Field) {
self.limbs[idx] = value;
}
unconstrained fn __eq(self, other: Self) -> bool {
__eq(self.limbs, other.limbs)
}
unconstrained fn __is_zero(self) -> bool {
__is_zero(self.limbs)
}
unconstrained fn __neg(self) -> Self {
let params = Params::get_params();
Self::from_slice(__neg(params, self.limbs))
}
unconstrained fn __add(self, other: Self) -> Self {
let params = Params::get_params();
Self::from_slice(__add(params, self.limbs, other.limbs))
}
unconstrained fn __sub(self, other: Self) -> Self {
let params = Params::get_params();
Self::from_slice(__sub(params, self.limbs, other.limbs))
}
unconstrained fn __mul(self, other: Self) -> Self {
let params = Params::get_params();
Self::from_slice(__mul::<_, MOD_BITS>(params, self.limbs, other.limbs))
}
unconstrained fn __div(self, divisor: Self) -> Self {
let params = Params::get_params();
Self::from_slice(__div::<_, MOD_BITS>(params, self.limbs, divisor.limbs))
}
unconstrained fn __udiv_mod(self, divisor: Self) -> (Self, Self) {
let (q, r) = __udiv_mod(self.limbs, divisor.limbs);
(Self { limbs: q }, Self { limbs: r })
}
unconstrained fn __invmod(self) -> Self {
let params = Params::get_params();
assert(params.has_multiplicative_inverse);
Self { limbs: __invmod::<_, MOD_BITS>(params, self.limbs) }
}
unconstrained fn __pow(self, exponent: Self) -> Self {
let params = Params::get_params();
Self { limbs: __pow::<_, MOD_BITS>(params, self.limbs, exponent.limbs) }
}
unconstrained fn __batch_invert<let M: u32>(x: [Self; M]) -> [Self; M] {
let params = Params::get_params();
assert(params.has_multiplicative_inverse);
__batch_invert::<_, MOD_BITS, _>(params, x.map(|bn: Self| bn.limbs)).map(|limbs| {
Self { limbs }
})
}
unconstrained fn __batch_invert_slice<let M: u32>(x: [Self]) -> [Self] {
let params = Params::get_params();
assert(params.has_multiplicative_inverse);
__batch_invert_slice::<_, MOD_BITS>(params, x.map(|bn: Self| bn.limbs)).map(|limbs| {
Self { limbs }
})
}
unconstrained fn __tonelli_shanks_sqrt(self) -> std::option::Option<Self> {
let params = Params::get_params();
let maybe_limbs = unsafe { __tonelli_shanks_sqrt(params, self.limbs) };
maybe_limbs.map(|limbs| Self { limbs })
}
unconstrained fn __compute_quadratic_expression<let LHS_N: u32, let RHS_N: u32, let NUM_PRODUCTS: u32, let ADD_N: u32>(
lhs_terms: [[Self; LHS_N]; NUM_PRODUCTS],
lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS],
rhs_terms: [[Self; RHS_N]; NUM_PRODUCTS],
rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS],
linear_terms: [Self; ADD_N],
linear_flags: [bool; ADD_N],
) -> (Self, Self) {
let params = Params::get_params();
let (q_limbs, r_limbs) = __compute_quadratic_expression::<_, MOD_BITS, _, _, _, _>(
params,
map(lhs_terms, |bns| map(bns, |bn: Self| bn.limbs)),
lhs_flags,
map(rhs_terms, |bns| map(bns, |bn: Self| bn.limbs)),
rhs_flags,
map(linear_terms, |bn: Self| bn.limbs),
linear_flags,
);
(Self { limbs: q_limbs }, Self { limbs: r_limbs })
}
fn evaluate_quadratic_expression<let LHS_N: u32, let RHS_N: u32, let NUM_PRODUCTS: u32, let ADD_N: u32>(
lhs_terms: [[Self; LHS_N]; NUM_PRODUCTS],
lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS],
rhs_terms: [[Self; RHS_N]; NUM_PRODUCTS],
rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS],
linear_terms: [Self; ADD_N],
linear_flags: [bool; ADD_N],
) {
let params = Params::get_params();
evaluate_quadratic_expression::<_, MOD_BITS, _, _, _, _>(
params,
map(lhs_terms, |bns| map(bns, |bn: Self| bn.limbs)),
lhs_flags,
map(rhs_terms, |bns| map(bns, |bn: Self| bn.limbs)),
rhs_flags,
map(linear_terms, |bn: Self| bn.limbs),
linear_flags,
)
}
fn validate_in_field(self: Self) {
let params = Params::get_params();
validate_in_field::<_, MOD_BITS>(params, self.limbs);
}
fn validate_in_range(self) {
validate_in_range::<_, MOD_BITS>(self.limbs);
}
fn assert_is_not_equal(self, other: Self) {
let params = Params::get_params();
assert_is_not_equal(params, self.limbs, other.limbs);
}
fn neg(self) -> Self {
let params = Params::get_params();
Self { limbs: neg::<_, MOD_BITS>(params, self.limbs) }
}
fn udiv_mod(self, divisor: Self) -> (Self, Self) {
let params = Params::get_params();
let (q, r) = udiv_mod::<_, MOD_BITS>(params, self.limbs, divisor.limbs);
(Self { limbs: q }, Self { limbs: r })
}
fn udiv(self, divisor: Self) -> Self {
let params = Params::get_params();
Self { limbs: udiv::<_, MOD_BITS>(params, self.limbs, divisor.limbs) }
}
fn umod(self, divisor: Self) -> Self {
let params = Params::get_params();
Self { limbs: umod::<_, MOD_BITS>(params, self.limbs, divisor.limbs) }
}
fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self {
Self { limbs: conditional_select(lhs.limbs, rhs.limbs, predicate) }
}
}
// impl<let N: u32, Params> BigNumTrait<N, Params> for BigNum<N, Params> where Params: BigNumParamsGetter<N> {}
impl<let N: u32, let MOD_BITS: u32, Params> std::ops::Add for BigNum<N, MOD_BITS, Params>
where
Params: BigNumParamsGetter<N, MOD_BITS>,
{
// Note: this method is expensive! Try to craft quadratic relations and directly evaluate them
// via evaluate_quadratic_expression
fn add(self, other: Self) -> Self {
let params = Params::get_params();
Self { limbs: add::<_, MOD_BITS>(params, self.limbs, other.limbs) }
}
}
impl<let N: u32, let MOD_BITS: u32, Params> std::ops::Sub for BigNum<N, MOD_BITS, Params>
where
Params: BigNumParamsGetter<N, MOD_BITS>,
{
// Note: this method is expensive! Try to craft quadratic relations and directly evaluate them
// via evaluate_quadratic_expression
fn sub(self, other: Self) -> Self {
let params = Params::get_params();
Self { limbs: sub::<_, MOD_BITS>(params, self.limbs, other.limbs) }
}
}
impl<let N: u32, let MOD_BITS: u32, Params> std::ops::Mul for BigNum<N, MOD_BITS, Params>
where
Params: BigNumParamsGetter<N, MOD_BITS>,
{
// Note: this method is expensive! Try to craft quadratic relations and directly evaluate them
// via evaluate_quadratic_expression
// e.g. performing a sum of multiple multiplications and additions via `evaluate_quadratic_expression`
// will create much fewer constraints than calling `mul` and `add` directly
fn mul(self, other: Self) -> Self {
let params = Params::get_params();
Self { limbs: mul::<_, MOD_BITS>(params, self.limbs, other.limbs) }
}
}
impl<let N: u32, let MOD_BITS: u32, Params> std::ops::Div for BigNum<N, MOD_BITS, Params>
where
Params: BigNumParamsGetter<N, MOD_BITS>,
{
// Note: this method is expensive! Witness computation is extremely expensive as it requires modular exponentiation
fn div(self, other: Self) -> Self {
let params = Params::get_params();
Self { limbs: div::<_, MOD_BITS>(params, self.limbs, other.limbs) }
}
}
impl<let N: u32, let MOD_BITS: u32, Params> std::cmp::Eq for BigNum<N, MOD_BITS, Params>
where
Params: BigNumParamsGetter<N, MOD_BITS>,
{
fn eq(self, other: Self) -> bool {
let params = Params::get_params();
eq::<_, MOD_BITS>(params, self.limbs, other.limbs)
}
}