Skip to content

Commit e7cf080

Browse files
committed
fix bn254_muladd test on rng
1 parent 672dc73 commit e7cf080

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed
68.1 KB
Binary file not shown.
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_main]
22
sp1_zkvm::entrypoint!(main);
33

4-
use num::{BigUint, One};
4+
use num::{BigUint, Num, One};
55
use rand::Rng;
66
use sp1_zkvm::syscalls::sys_bn254_muladd;
77

@@ -28,33 +28,31 @@ fn biguint_to_bytes_le(x: BigUint) -> [u8; 32] {
2828
#[sp1_derive::cycle_tracker]
2929
pub fn main() {
3030
// Test with random numbers.
31-
// let mut rng = rand::thread_rng();
32-
// let mut x: [u8; 32] = rng.gen();
33-
// let mut y: [u8; 32] = rng.gen();
34-
// let z: [u8; 32] = rng.gen();
35-
36-
// // Convert byte arrays to BigUint
37-
// let z_big = BigUint::from_bytes_le(&z);
38-
// let x_big = BigUint::from_bytes_le(&x);
39-
// // x = biguint_to_bytes_le(x_big.clone());
40-
// let y_big = BigUint::from_bytes_le(&y);
41-
// // y = biguint_to_bytes_le(y_big.clone());
42-
43-
let mut x: [u8; 32] = [0; 32];
44-
x[0] = 2;
45-
let mut y: [u8; 32] = [0; 32];
46-
y[0] = 3;
47-
let mut z: [u8; 32] = [0; 32];
48-
z[0] = 4;
31+
let mut rng = rand::thread_rng();
32+
let mut x: [u8; 32] = rng.gen();
33+
let mut y: [u8; 32] = rng.gen();
34+
let mut z: [u8; 32] = rng.gen();
35+
36+
//bn254 scalar field modulus
37+
let modulus = BigUint::from_str_radix(
38+
"21888242871839275222246405745257275088548364400416034343698204186575808495617",
39+
10,
40+
)
41+
.unwrap();
42+
4943
// Convert byte arrays to BigUint
5044
let z_big = BigUint::from_bytes_le(&z);
5145
let x_big = BigUint::from_bytes_le(&x);
5246
let y_big = BigUint::from_bytes_le(&y);
5347

48+
x = biguint_to_bytes_le(&x_big % &modulus);
49+
y = biguint_to_bytes_le(&y_big % &modulus);
50+
z = biguint_to_bytes_le(&z_big % &modulus);
51+
5452
let result_bytes = uint256_muladd(&x, &y, &z);
5553

56-
let result = (x_big * y_big) + z_big;
54+
let result = ((x_big * y_big) + z_big) % modulus;
5755
let result_syscall = BigUint::from_bytes_le(&result_bytes);
5856

59-
assert_eq!(result, result_syscall); //10
57+
assert_eq!(result, result_syscall);
6058
}

0 commit comments

Comments
 (0)