Skip to content

Commit 6047338

Browse files
committed
add tests
1 parent 8fcec0c commit 6047338

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/scalar_field.nr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ impl<let N: u32> std::convert::Into<Field> for ScalarField<N> {
165165
**/
166166
fn into(self: Self) -> Field {
167167
// TODO: This is susceptible to overflow when N is large!
168+
if !std::runtime::is_unconstrained() {
169+
if N >= 64 {
170+
compare_scalar_field_to_bignum(self);
171+
}
172+
}
168173
let mut acc: Field = 0;
169174
for i in 0..N {
170175
acc = acc * 16;
@@ -372,4 +377,48 @@ mod tests {
372377
],
373378
);
374379
}
380+
#[test(should_fail)]
381+
fn test_get_modulus_slices_fail_64() {
382+
//13th nibble is 14, which is greater than the modulus
383+
let modulus_slices: [u8; 64] = [
384+
9, 8, 3, 2, 2, 7, 3, 9, 7, 0, 9, 8, 14, 0, 1, 4, 13, 12, 2, 8, 2, 2, 13, 11, 4, 0, 12,
385+
0, 10, 12, 2, 14, 9, 4, 1, 9, 15, 4, 2, 4, 3, 12, 13, 12, 11, 8, 4, 8, 10, 1, 15, 0, 15,
386+
10, 12, 9, 15, 8, 0, 0, 0, 0, 0, 0,
387+
];
388+
let mut result: ScalarField<64> = ScalarField { base4_slices: [0; 64], skew: true };
389+
for i in 0..64 {
390+
result.base4_slices[i] = modulus_slices[i];
391+
}
392+
let _ = result.into();
393+
}
394+
#[test(should_fail)]
395+
fn test_get_modulus_slices_fail_65() {
396+
//modulus slice represents a number that is greater than the modulus
397+
let modulus_slices2 = [
398+
8, 1, 8, 3, 2, 2, 7, 3, 10, 8, 0, 9, 8, 13, 0, 1, 4, 13, 12, 2, 8, 2, 2, 13, 11, 4, 0,
399+
12, 0, 10, 12, 2, 14, 9, 4, 1, 9, 15, 4, 2, 4, 3, 12, 13, 12, 11, 8, 4, 8, 10, 1, 15, 0,
400+
15, 10, 12, 9, 15, 8, 0, 0, 0, 0, 0, 0,
401+
];
402+
let mut result: ScalarField<65> = ScalarField { base4_slices: [0; 65], skew: true };
403+
for i in 0..64 {
404+
result.base4_slices[i] = modulus_slices2[i];
405+
}
406+
let _ = result.into();
407+
}
408+
409+
#[test(should_fail)]
410+
fn test_get_modulus_slices_fail_68() {
411+
//modulus slice represents a number that is greater than the modulus
412+
let modulus_slices3 = [
413+
8, 0, 0, 0, 2, 8, 3, 2, 2, 7, 3, 9, 7, 0, 9, 8, 13, 0, 1, 4, 13, 12, 2, 8, 2, 2, 13, 11,
414+
4, 0, 12, 0, 10, 12, 2, 14, 9, 4, 1, 9, 15, 4, 2, 4, 3, 12, 13, 12, 11, 8, 4, 8, 10, 1,
415+
15, 0, 15, 10, 12, 9, 15, 8, 0, 0, 0, 0, 0, 0,
416+
];
417+
let mut result: ScalarField<68> = ScalarField { base4_slices: [0; 68], skew: true };
418+
for i in 0..68 {
419+
result.base4_slices[i] = modulus_slices3[i];
420+
}
421+
let _ = result.into();
422+
}
423+
375424
}

0 commit comments

Comments
 (0)