Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion patch-testing/bls12-381/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
20 changes: 20 additions & 0 deletions patch-testing/bls12-381/program/bin/test_add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

pub fn main() {
use bls12_381::fp::Fp;

let times = sp1_lib::io::read::<u8>();

for _ in 0..times {
let val1: Vec<u8> = sp1_lib::io::read();
let val2: Vec<u8> = 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());
}
}
20 changes: 20 additions & 0 deletions patch-testing/bls12-381/program/bin/test_add_fp2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

pub fn main() {
use bls12_381::fp2::Fp2;

let times = sp1_lib::io::read::<u8>();

for _ in 0..times {
let val1: Vec<u8> = sp1_lib::io::read();
let val2: Vec<u8> = 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());
}
}
20 changes: 20 additions & 0 deletions patch-testing/bls12-381/program/bin/test_mul.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

pub fn main() {
use bls12_381::fp::Fp;

let times = sp1_lib::io::read::<u8>();

for _ in 0..times {
let val1: Vec<u8> = sp1_lib::io::read();
let val2: Vec<u8> = 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());
}
}
20 changes: 20 additions & 0 deletions patch-testing/bls12-381/program/bin/test_mul_fp2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

pub fn main() {
use bls12_381::fp2::Fp2;

let times = sp1_lib::io::read::<u8>();

for _ in 0..times {
let val1: Vec<u8> = sp1_lib::io::read();
let val2: Vec<u8> = 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());
}
}
118 changes: 114 additions & 4 deletions patch-testing/bls12-381/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(&times);

let mut unpatched_results: Vec<Vec<u8>> = 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::<Vec<u8>>();
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(&times);

let mut unpatched_results: Vec<Vec<u8>> = 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::<Vec<u8>>();
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(&times);

let mut unpatched_results: Vec<Vec<u8>> = 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::<Vec<u8>>();
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(&times);

let mut unpatched_results: Vec<Vec<u8>> = 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::<Vec<u8>>();
assert_eq!(res, zk_res);
}
}
}
Loading