diff --git a/patch-testing/bls12-381/program/Cargo.toml b/patch-testing/bls12-381/program/Cargo.toml index 8d92d809e9..3c34451fef 100644 --- a/patch-testing/bls12-381/program/Cargo.toml +++ b/patch-testing/bls12-381/program/Cargo.toml @@ -28,7 +28,21 @@ path = "bin/test_bls_add.rs" name = "bls12_381_ec_double_test" path = "bin/test_bls_double.rs" -# todo explicit mul and add tests +[[bin]] +name = "bls12_381_fp_test_mul" +path = "bin/test_mul.rs" + +[[bin]] +name = "bls12_381_fp_test_add" +path = "bin/test_add.rs" + +[[bin]] +name = "bls12_381_fp2_test_mul" +path = "bin/test_mul_fp2.rs" + +[[bin]] +name = "bls12_381_fp2_test_add" +path = "bin/test_add_fp2.rs" [dependencies] sp1-lib = { path = "../../../crates/zkvm/lib" } diff --git a/patch-testing/bls12-381/program/bin/test_add.rs b/patch-testing/bls12-381/program/bin/test_add.rs new file mode 100644 index 0000000000..25d4dfb552 --- /dev/null +++ b/patch-testing/bls12-381/program/bin/test_add.rs @@ -0,0 +1,20 @@ +#![no_main] +sp1_zkvm::entrypoint!(main); + +pub fn main() { + use bls12_381::fp::Fp; + + let times = sp1_lib::io::read::(); + + for _ in 0..times { + let val1: Vec = sp1_lib::io::read(); + let val2: Vec = sp1_lib::io::read(); + + let val1 = Fp::from_bytes(&val1.try_into().expect("[u8; 48] for fp")).unwrap(); + let val2 = Fp::from_bytes(&val2.try_into().expect("[u8; 48] for fp")).unwrap(); + + let sum = val1 + val2; + + sp1_lib::io::commit(&sum.to_bytes().to_vec()); + } +} \ No newline at end of file diff --git a/patch-testing/bls12-381/program/bin/test_add_fp2.rs b/patch-testing/bls12-381/program/bin/test_add_fp2.rs new file mode 100644 index 0000000000..5423e1ea2a --- /dev/null +++ b/patch-testing/bls12-381/program/bin/test_add_fp2.rs @@ -0,0 +1,20 @@ +#![no_main] +sp1_zkvm::entrypoint!(main); + +pub fn main() { + use bls12_381::fp2::Fp2; + + let times = sp1_lib::io::read::(); + + for _ in 0..times { + let val1: Vec = sp1_lib::io::read(); + let val2: Vec = sp1_lib::io::read(); + + let val1 = Fp2::from_bytes(&val1.try_into().expect("[u8; 96] for fp2")).unwrap(); + let val2 = Fp2::from_bytes(&val2.try_into().expect("[u8; 96] for fp2")).unwrap(); + + let sum = val1 + val2; + + sp1_lib::io::commit(&sum.to_bytes().to_vec()); + } +} \ No newline at end of file diff --git a/patch-testing/bls12-381/program/bin/test_mul.rs b/patch-testing/bls12-381/program/bin/test_mul.rs new file mode 100644 index 0000000000..887df88bb3 --- /dev/null +++ b/patch-testing/bls12-381/program/bin/test_mul.rs @@ -0,0 +1,20 @@ +#![no_main] +sp1_zkvm::entrypoint!(main); + +pub fn main() { + use bls12_381::fp::Fp; + + let times = sp1_lib::io::read::(); + + for _ in 0..times { + let val1: Vec = sp1_lib::io::read(); + let val2: Vec = sp1_lib::io::read(); + + let val1 = Fp::from_bytes(&val1.try_into().expect("[u8; 48] for fp")).unwrap(); + let val2 = Fp::from_bytes(&val2.try_into().expect("[u8; 48] for fp")).unwrap(); + + let product = val1 * val2; + + sp1_lib::io::commit(&product.to_bytes().to_vec()); + } +} \ No newline at end of file diff --git a/patch-testing/bls12-381/program/bin/test_mul_fp2.rs b/patch-testing/bls12-381/program/bin/test_mul_fp2.rs new file mode 100644 index 0000000000..0bad5eec7d --- /dev/null +++ b/patch-testing/bls12-381/program/bin/test_mul_fp2.rs @@ -0,0 +1,20 @@ +#![no_main] +sp1_zkvm::entrypoint!(main); + +pub fn main() { + use bls12_381::fp2::Fp2; + + let times = sp1_lib::io::read::(); + + for _ in 0..times { + let val1: Vec = sp1_lib::io::read(); + let val2: Vec = sp1_lib::io::read(); + + let val1 = Fp2::from_bytes(&val1.try_into().expect("[u8; 96] for fp2")).unwrap(); + let val2 = Fp2::from_bytes(&val2.try_into().expect("[u8; 96] for fp2")).unwrap(); + + let product = val1 * val2; + + sp1_lib::io::commit(&product.to_bytes().to_vec()); + } +} \ No newline at end of file diff --git a/patch-testing/bls12-381/src/lib.rs b/patch-testing/bls12-381/src/lib.rs index e029ccd0a7..8a00680a44 100644 --- a/patch-testing/bls12-381/src/lib.rs +++ b/patch-testing/bls12-381/src/lib.rs @@ -114,8 +114,7 @@ pub fn test_inverse_fp2_100( #[sp1_test::sp1_test("bls12_381_ec_add_test", syscalls = [BLS12381_DOUBLE, BLS12381_ADD, BLS12381_FP_MUL, BLS12381_FP_SUB], gpu, prove)] pub fn test_bls_add_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk::SP1PublicValues) { - use bls12_381::g1::G1Affine; - use bls12_381::g1::G1Projective; + use bls12_381::g1::{G1Affine, G1Projective}; use group::Group; let times: u8 = 100; @@ -150,8 +149,7 @@ pub fn test_bls_add_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk::S #[sp1_test::sp1_test("bls12_381_ec_double_test", syscalls = [BLS12381_DOUBLE, BLS12381_ADD, BLS12381_FP_ADD, BLS12381_FP_MUL, BLS12381_FP_SUB], gpu, prove)] pub fn test_bls_double_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk::SP1PublicValues) { - use bls12_381::g1::G1Affine; - use bls12_381::g1::G1Projective; + use bls12_381::g1::{G1Affine, G1Projective}; use group::Group; let times: u8 = 100; @@ -180,3 +178,115 @@ pub fn test_bls_double_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk } } } + +#[sp1_test::sp1_test("bls12_381_fp_test_add", syscalls = [BLS12381_FP_ADD], gpu, prove)] +pub fn test_add_fp_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk::SP1PublicValues) { + use bls12_381::fp::Fp; + + let times: u8 = 100; + stdin.write(×); + + let mut unpatched_results: Vec> = Vec::new(); + + while unpatched_results.len() < times as usize { + let val1 = Fp::random(&mut rand::thread_rng()); + let val2 = Fp::random(&mut rand::thread_rng()); + + stdin.write(&val1.to_bytes().to_vec()); + stdin.write(&val2.to_bytes().to_vec()); + + let sum = val1 + val2; + unpatched_results.push(sum.to_bytes().to_vec()); + } + + |mut public| { + for res in unpatched_results { + let zk_res = public.read::>(); + assert_eq!(res, zk_res); + } + } +} + +#[sp1_test::sp1_test("bls12_381_fp_test_mul", syscalls = [BLS12381_FP_MUL], gpu, prove)] +pub fn test_mul_fp_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk::SP1PublicValues) { + use bls12_381::fp::Fp; + + let times: u8 = 100; + stdin.write(×); + + let mut unpatched_results: Vec> = Vec::new(); + + while unpatched_results.len() < times as usize { + let val1 = Fp::random(&mut rand::thread_rng()); + let val2 = Fp::random(&mut rand::thread_rng()); + + stdin.write(&val1.to_bytes().to_vec()); + stdin.write(&val2.to_bytes().to_vec()); + + let product = val1 * val2; + unpatched_results.push(product.to_bytes().to_vec()); + } + + |mut public| { + for res in unpatched_results { + let zk_res = public.read::>(); + assert_eq!(res, zk_res); + } + } +} + +#[sp1_test::sp1_test("bls12_381_fp2_test_add", syscalls = [BLS12381_FP2_ADD], gpu, prove)] +pub fn test_add_fp2_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk::SP1PublicValues) { + use bls12_381::fp2::Fp2; + + let times: u8 = 100; + stdin.write(×); + + let mut unpatched_results: Vec> = Vec::new(); + + while unpatched_results.len() < times as usize { + let val1 = Fp2::random(&mut rand::thread_rng()); + let val2 = Fp2::random(&mut rand::thread_rng()); + + stdin.write(&val1.to_bytes().to_vec()); + stdin.write(&val2.to_bytes().to_vec()); + + let sum = val1 + val2; + unpatched_results.push(sum.to_bytes().to_vec()); + } + + |mut public| { + for res in unpatched_results { + let zk_res = public.read::>(); + assert_eq!(res, zk_res); + } + } +} + +#[sp1_test::sp1_test("bls12_381_fp2_test_mul", syscalls = [BLS12381_FP_MUL, BLS12381_FP2_MUL], gpu, prove)] +pub fn test_mul_fp2_100(stdin: &mut sp1_sdk::SP1Stdin) -> impl FnOnce(sp1_sdk::SP1PublicValues) { + use bls12_381::fp2::Fp2; + + let times: u8 = 100; + stdin.write(×); + + let mut unpatched_results: Vec> = Vec::new(); + + while unpatched_results.len() < times as usize { + let val1 = Fp2::random(&mut rand::thread_rng()); + let val2 = Fp2::random(&mut rand::thread_rng()); + + stdin.write(&val1.to_bytes().to_vec()); + stdin.write(&val2.to_bytes().to_vec()); + + let product = val1 * val2; + unpatched_results.push(product.to_bytes().to_vec()); + } + + |mut public| { + for res in unpatched_results { + let zk_res = public.read::>(); + assert_eq!(res, zk_res); + } + } +}