-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathmod.nr
494 lines (444 loc) · 17 KB
/
mod.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
pub mod bn254;
use crate::{runtime::is_unconstrained, static_assert};
use bn254::lt as bn254_lt;
impl Field {
/// Asserts that `self` can be represented in `bit_size` bits.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^{bit_size}`.
// docs:start:assert_max_bit_size
pub fn assert_max_bit_size<let BIT_SIZE: u32>(self) {
// docs:end:assert_max_bit_size
static_assert(
BIT_SIZE < modulus_num_bits() as u32,
"BIT_SIZE must be less than modulus_num_bits",
);
self.__assert_max_bit_size(BIT_SIZE);
}
#[builtin(apply_range_constraint)]
fn __assert_max_bit_size(self, bit_size: u32) {}
/// Decomposes `self` into its little endian bit decomposition as a `[u1; N]` array.
/// This slice will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_le_bits)]
fn _to_le_bits<let N: u32>(self: Self) -> [u1; N] {}
/// Decomposes `self` into its big endian bit decomposition as a `[u1; N]` array.
/// This array will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_be_bits)]
fn _to_be_bits<let N: u32>(self: Self) -> [u1; N] {}
/// Decomposes `self` into its little endian bit decomposition as a `[u1; N]` array.
/// This slice will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// The bit decomposition returned is canonical and is guaranteed to not overflow the modulus.
// docs:start:to_le_bits
pub fn to_le_bits<let N: u32>(self: Self) -> [u1; N] {
// docs:end:to_le_bits
let bits = self._to_le_bits();
if !is_unconstrained() {
// Ensure that the byte decomposition does not overflow the modulus
let p = modulus_le_bits();
assert(bits.len() <= p.len());
let mut ok = bits.len() != p.len();
for i in 0..N {
if !ok {
if (bits[N - 1 - i] != p[N - 1 - i]) {
assert(p[N - 1 - i] == 1);
ok = true;
}
}
}
assert(ok);
}
bits
}
/// Decomposes `self` into its big endian bit decomposition as a `[u1; N]` array.
/// This array will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// The bit decomposition returned is canonical and is guaranteed to not overflow the modulus.
// docs:start:to_be_bits
pub fn to_be_bits<let N: u32>(self: Self) -> [u1; N] {
// docs:end:to_be_bits
let bits = self._to_be_bits();
if !is_unconstrained() {
// Ensure that the decomposition does not overflow the modulus
let p = modulus_be_bits();
assert(bits.len() <= p.len());
let mut ok = bits.len() != p.len();
for i in 0..N {
if !ok {
if (bits[i] != p[i]) {
assert(p[i] == 1);
ok = true;
}
}
}
assert(ok);
}
bits
}
/// Decomposes `self` into its little endian byte decomposition as a `[u8;N]` array
/// This array will be zero padded should not all bytes be necessary to represent `self`.
///
/// # Failures
/// The length N of the array must be big enough to contain all the bytes of the 'self',
/// and no more than the number of bytes required to represent the field modulus
///
/// # Safety
/// The result is ensured to be the canonical decomposition of the field element
// docs:start:to_le_bytes
pub fn to_le_bytes<let N: u32>(self: Self) -> [u8; N] {
// docs:end:to_le_bytes
static_assert(
N <= modulus_le_bytes().len(),
"N must be less than or equal to modulus_le_bytes().len()",
);
// Compute the byte decomposition
let bytes = self.to_le_radix(256);
if !is_unconstrained() {
// Ensure that the byte decomposition does not overflow the modulus
let p = modulus_le_bytes();
assert(bytes.len() <= p.len());
let mut ok = bytes.len() != p.len();
for i in 0..N {
if !ok {
if (bytes[N - 1 - i] != p[N - 1 - i]) {
assert(bytes[N - 1 - i] < p[N - 1 - i]);
ok = true;
}
}
}
assert(ok);
}
bytes
}
/// Decomposes `self` into its big endian byte decomposition as a `[u8;N]` array of length required to represent the field modulus
/// This array will be zero padded should not all bytes be necessary to represent `self`.
///
/// # Failures
/// The length N of the array must be big enough to contain all the bytes of the 'self',
/// and no more than the number of bytes required to represent the field modulus
///
/// # Safety
/// The result is ensured to be the canonical decomposition of the field element
// docs:start:to_be_bytes
pub fn to_be_bytes<let N: u32>(self: Self) -> [u8; N] {
// docs:end:to_be_bytes
static_assert(
N <= modulus_le_bytes().len(),
"N must be less than or equal to modulus_le_bytes().len()",
);
// Compute the byte decomposition
let bytes = self.to_be_radix(256);
if !is_unconstrained() {
// Ensure that the byte decomposition does not overflow the modulus
let p = modulus_be_bytes();
assert(bytes.len() <= p.len());
let mut ok = bytes.len() != p.len();
for i in 0..N {
if !ok {
if (bytes[i] != p[i]) {
assert(bytes[i] < p[i]);
ok = true;
}
}
}
assert(ok);
}
bytes
}
// docs:start:to_le_radix
pub fn to_le_radix<let N: u32>(self: Self, radix: u32) -> [u8; N] {
// Brillig does not need an immediate radix
if !crate::runtime::is_unconstrained() {
static_assert(1 < radix, "radix must be greater than 1");
static_assert(radix <= 256, "radix must be less than or equal to 256");
static_assert(radix & (radix - 1) == 0, "radix must be a power of 2");
}
self.__to_le_radix(radix)
}
// docs:end:to_le_radix
// docs:start:to_be_radix
pub fn to_be_radix<let N: u32>(self: Self, radix: u32) -> [u8; N] {
// Brillig does not need an immediate radix
if !crate::runtime::is_unconstrained() {
crate::assert_constant(radix);
}
self.__to_be_radix(radix)
}
// docs:end:to_be_radix
// `_radix` must be less than 256
#[builtin(to_le_radix)]
fn __to_le_radix<let N: u32>(self, radix: u32) -> [u8; N] {}
// `_radix` must be less than 256
#[builtin(to_be_radix)]
fn __to_be_radix<let N: u32>(self, radix: u32) -> [u8; N] {}
// Returns self to the power of the given exponent value.
// Caution: we assume the exponent fits into 32 bits
// using a bigger bit size impacts negatively the performance and should be done only if the exponent does not fit in 32 bits
pub fn pow_32(self, exponent: Field) -> Field {
let mut r: Field = 1;
let b: [u1; 32] = exponent.to_le_bits();
for i in 1..33 {
r *= r;
r = (b[32 - i] as Field) * (r * self) + (1 - b[32 - i] as Field) * r;
}
r
}
// Parity of (prime) Field element, i.e. sgn0(x mod p) = 0 if x `elem` {0, ..., p-1} is even, otherwise sgn0(x mod p) = 1.
pub fn sgn0(self) -> u1 {
self as u1
}
pub fn lt(self, another: Field) -> bool {
if crate::compat::is_bn254() {
bn254_lt(self, another)
} else {
lt_fallback(self, another)
}
}
/// Convert a little endian byte array to a field element.
/// If the provided byte array overflows the field modulus then the Field will silently wrap around.
pub fn from_le_bytes<let N: u32>(bytes: [u8; N]) -> Field {
static_assert(
N <= modulus_le_bytes().len(),
"N must be less than or equal to modulus_le_bytes().len()",
);
let mut v = 1;
let mut result = 0;
for i in 0..N {
result += (bytes[i] as Field) * v;
v = v * 256;
}
result
}
/// Convert a big endian byte array to a field element.
/// If the provided byte array overflows the field modulus then the Field will silently wrap around.
pub fn from_be_bytes<let N: u32>(bytes: [u8; N]) -> Field {
let mut v = 1;
let mut result = 0;
for i in 0..N {
result += (bytes[N - 1 - i] as Field) * v;
v = v * 256;
}
result
}
}
#[builtin(modulus_num_bits)]
pub comptime fn modulus_num_bits() -> u64 {}
#[builtin(modulus_be_bits)]
pub comptime fn modulus_be_bits() -> [u1] {}
#[builtin(modulus_le_bits)]
pub comptime fn modulus_le_bits() -> [u1] {}
#[builtin(modulus_be_bytes)]
pub comptime fn modulus_be_bytes() -> [u8] {}
#[builtin(modulus_le_bytes)]
pub comptime fn modulus_le_bytes() -> [u8] {}
/// An unconstrained only built in to efficiently compare fields.
#[builtin(field_less_than)]
unconstrained fn __field_less_than(x: Field, y: Field) -> bool {}
pub(crate) unconstrained fn field_less_than(x: Field, y: Field) -> bool {
__field_less_than(x, y)
}
// Convert a 32 byte array to a field element by modding
pub fn bytes32_to_field(bytes32: [u8; 32]) -> Field {
// Convert it to a field element
let mut v = 1;
let mut high = 0 as Field;
let mut low = 0 as Field;
for i in 0..16 {
high = high + (bytes32[15 - i] as Field) * v;
low = low + (bytes32[16 + 15 - i] as Field) * v;
v = v * 256;
}
// Abuse that a % p + b % p = (a + b) % p and that low < p
low + high * v
}
fn lt_fallback(x: Field, y: Field) -> bool {
if is_unconstrained() {
/// Safety: unconstrained context
unsafe {
field_less_than(x, y)
}
} else {
let x_bytes: [u8; 32] = x.to_le_bytes();
let y_bytes: [u8; 32] = y.to_le_bytes();
let mut x_is_lt = false;
let mut done = false;
for i in 0..32 {
if (!done) {
let x_byte = x_bytes[32 - 1 - i] as u8;
let y_byte = y_bytes[32 - 1 - i] as u8;
let bytes_match = x_byte == y_byte;
if !bytes_match {
x_is_lt = x_byte < y_byte;
done = true;
}
}
}
x_is_lt
}
}
mod tests {
use crate::{panic::panic, runtime};
use super::field_less_than;
#[test]
// docs:start:to_be_bits_example
fn test_to_be_bits() {
let field = 2;
let bits: [u1; 8] = field.to_be_bits();
assert_eq(bits, [0, 0, 0, 0, 0, 0, 1, 0]);
}
// docs:end:to_be_bits_example
#[test]
// docs:start:to_le_bits_example
fn test_to_le_bits() {
let field = 2;
let bits: [u1; 8] = field.to_le_bits();
assert_eq(bits, [0, 1, 0, 0, 0, 0, 0, 0]);
}
// docs:end:to_le_bits_example
#[test]
// docs:start:to_be_bytes_example
fn test_to_be_bytes() {
let field = 2;
let bytes: [u8; 8] = field.to_be_bytes();
assert_eq(bytes, [0, 0, 0, 0, 0, 0, 0, 2]);
assert_eq(Field::from_be_bytes::<8>(bytes), field);
}
// docs:end:to_be_bytes_example
#[test]
// docs:start:to_le_bytes_example
fn test_to_le_bytes() {
let field = 2;
let bytes: [u8; 8] = field.to_le_bytes();
assert_eq(bytes, [2, 0, 0, 0, 0, 0, 0, 0]);
assert_eq(Field::from_le_bytes::<8>(bytes), field);
}
// docs:end:to_le_bytes_example
#[test]
// docs:start:to_be_radix_example
fn test_to_be_radix() {
// 259, in base 256, big endian, is [1, 3].
// i.e. 3 * 256^0 + 1 * 256^1
let field = 259;
// The radix (in this example, 256) must be a power of 2.
// The length of the returned byte array can be specified to be
// >= the amount of space needed.
let bytes: [u8; 8] = field.to_be_radix(256);
assert_eq(bytes, [0, 0, 0, 0, 0, 0, 1, 3]);
assert_eq(Field::from_be_bytes::<8>(bytes), field);
}
// docs:end:to_be_radix_example
#[test]
// docs:start:to_le_radix_example
fn test_to_le_radix() {
// 259, in base 256, little endian, is [3, 1].
// i.e. 3 * 256^0 + 1 * 256^1
let field = 259;
// The radix (in this example, 256) must be a power of 2.
// The length of the returned byte array can be specified to be
// >= the amount of space needed.
let bytes: [u8; 8] = field.to_le_radix(256);
assert_eq(bytes, [3, 1, 0, 0, 0, 0, 0, 0]);
assert_eq(Field::from_le_bytes::<8>(bytes), field);
}
// docs:end:to_le_radix_example
#[test(should_fail_with = "radix must be greater than 1")]
fn test_to_le_radix_1() {
// this test should only fail in constrained mode
if !runtime::is_unconstrained() {
let field = 2;
let _: [u8; 8] = field.to_le_radix(1);
} else {
panic(f"radix must be greater than 1");
}
}
// TODO: Update this test to account for the Brillig restriction that the radix must be greater than 2
//#[test]
//fn test_to_le_radix_brillig_1() {
// // this test should only fail in constrained mode
// if runtime::is_unconstrained() {
// let field = 1;
// let out: [u8; 8] = field.to_le_radix(1);
// crate::println(out);
// let expected = [0; 8];
// assert(out == expected, "unexpected result");
// }
//}
#[test(should_fail_with = "radix must be a power of 2")]
fn test_to_le_radix_3() {
// this test should only fail in constrained mode
if !runtime::is_unconstrained() {
let field = 2;
let _: [u8; 8] = field.to_le_radix(3);
} else {
panic(f"radix must be a power of 2");
}
}
#[test]
fn test_to_le_radix_brillig_3() {
// this test should only fail in constrained mode
if runtime::is_unconstrained() {
let field = 1;
let out: [u8; 8] = field.to_le_radix(3);
let mut expected = [0; 8];
expected[0] = 1;
assert(out == expected, "unexpected result");
}
}
#[test(should_fail_with = "radix must be less than or equal to 256")]
fn test_to_le_radix_512() {
// this test should only fail in constrained mode
if !runtime::is_unconstrained() {
let field = 2;
let _: [u8; 8] = field.to_le_radix(512);
} else {
panic(f"radix must be less than or equal to 256")
}
}
// TODO: Update this test to account for the Brillig restriction that the radix must be less than 512
//#[test]
//fn test_to_le_radix_brillig_512() {
// // this test should only fail in constrained mode
// if runtime::is_unconstrained() {
// let field = 1;
// let out: [u8; 8] = field.to_le_radix(512);
// let mut expected = [0; 8];
// expected[0] = 1;
// assert(out == expected, "unexpected result");
// }
//}
#[test]
unconstrained fn test_field_less_than() {
assert(field_less_than(0, 1));
assert(field_less_than(0, 0x100));
assert(field_less_than(0x100, 0 - 1));
assert(!field_less_than(0 - 1, 0));
}
}