From c6f13496a5d74b65ebd553d09007091b989a59f2 Mon Sep 17 00:00:00 2001 From: thomaslavaur Date: Fri, 6 Sep 2024 14:37:57 +0200 Subject: [PATCH] switch from arkworks to crypto-bigint to reduce cylces --- Cargo.toml | 4 +- .../proof_statements/src/lib.rs | 9 ++-- proof_of_equivalence/risc0/prover/Cargo.toml | 1 + proof_of_equivalence/risc0/prover/src/lib.rs | 11 ++-- .../proof_of_equivalence/Cargo.toml | 3 -- .../proof_of_equivalence/src/main.rs | 53 ++++++++++++------- 6 files changed, 50 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 691b8fa0..d16782ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,5 +32,7 @@ members = [ "cl/cl", "tests", ] -exclude = ["proof_of_leadership/risc0/risc0_proofs"] +exclude = ["proof_of_leadership/risc0/risc0_proofs", +"proof_of_leadership/proof_statements", +"proof_of_equivalence/proof_statements"] resolver = "2" \ No newline at end of file diff --git a/proof_of_equivalence/proof_statements/src/lib.rs b/proof_of_equivalence/proof_statements/src/lib.rs index c8f92096..2227f98f 100644 --- a/proof_of_equivalence/proof_statements/src/lib.rs +++ b/proof_of_equivalence/proof_statements/src/lib.rs @@ -1,15 +1,16 @@ use serde::{Deserialize, Serialize}; +use crypto_bigint::U256; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct EquivalencePublic { pub da_commitment: Vec, - pub y_0: [u8; 32] + pub y_0: U256 } impl EquivalencePublic { pub fn new( da_commitment: Vec, - y_0: [u8; 32] + y_0: U256 ) -> Self { Self { da_commitment, @@ -20,12 +21,12 @@ impl EquivalencePublic { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct EquivalencePrivate { - pub coefficients: Vec<[u8; 32]> + pub coefficients: Vec } impl EquivalencePrivate { pub fn new( - coefficients: Vec<[u8; 32]>, + coefficients: Vec, ) -> Self { Self { coefficients, diff --git a/proof_of_equivalence/risc0/prover/Cargo.toml b/proof_of_equivalence/risc0/prover/Cargo.toml index fc15d602..8a551267 100644 --- a/proof_of_equivalence/risc0/prover/Cargo.toml +++ b/proof_of_equivalence/risc0/prover/Cargo.toml @@ -23,6 +23,7 @@ ark-poly = "0.4.0" ark-serialize = "0.4.0" sha2 = "0.10" ark-ec = "0.4.0" +crypto-bigint = { version = "0.5.5", features = ["serde"] } [features] metal = ["risc0-zkvm/metal"] \ No newline at end of file diff --git a/proof_of_equivalence/risc0/prover/src/lib.rs b/proof_of_equivalence/risc0/prover/src/lib.rs index c079c973..2136bee4 100644 --- a/proof_of_equivalence/risc0/prover/src/lib.rs +++ b/proof_of_equivalence/risc0/prover/src/lib.rs @@ -51,12 +51,13 @@ mod test { use sha2::{Digest, Sha256}; use std::ops::{Mul, Neg}; use ark_ec::pairing::Pairing; + use crypto_bigint::{U256}; const BLOB_SIZE: usize = 32; static GLOBAL_PARAMETERS: Lazy> = Lazy::new(|| { let mut rng = rand::thread_rng(); - KZG10::>::setup(32, true, &mut rng).unwrap() + KZG10::>::setup(BLOB_SIZE, true, &mut rng).unwrap() }); #[test] @@ -71,6 +72,10 @@ mod test { for i in 0..BLOB_SIZE { bls_coefficients.push(Fr::from_be_bytes_mod_order(&coefficients[i])); } + let mut u256_coefficients = vec![]; + for i in 0..BLOB_SIZE { + u256_coefficients.push(U256::from_be_slice(&coefficients[i])); + } let mut da_commitment = Vec::new(); let bls_polynomial = DensePolynomial::from_coefficients_vec(bls_coefficients); @@ -102,11 +107,11 @@ mod test { let expected_public_inputs = EquivalencePublic::new( da_commitment.clone(), - y_0.into_bigint().to_bytes_be().try_into().unwrap() + U256::from_be_slice(&y_0.into_bigint().to_bytes_be().as_slice()), ); let private_inputs = EquivalencePrivate::new( - coefficients.clone() + u256_coefficients, ); // Zone STF diff --git a/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/Cargo.toml b/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/Cargo.toml index 3a4c6572..caaefb42 100644 --- a/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/Cargo.toml +++ b/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/Cargo.toml @@ -12,9 +12,6 @@ cl = { path = "../../../../cl/cl" } equivalence_proof_statements = { path = "../../../proof_statements" } sha2 = "0.10" crypto-bigint = "0.5.5" -ark-bls12-381 = "0.4.0" -ark-ff = "0.4.0" -ark-poly = "0.4.0" [patch.crates-io] diff --git a/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/src/main.rs b/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/src/main.rs index f72e2b67..07419377 100644 --- a/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/src/main.rs +++ b/proof_of_equivalence/risc0/risc0_proofs/proof_of_equivalence/src/main.rs @@ -1,20 +1,39 @@ /// Proof of Equivalence use equivalence_proof_statements::{EquivalencePrivate, EquivalencePublic}; use risc0_zkvm::guest::env; -use ark_bls12_381::Fr; -use ark_ff::{PrimeField, BigInteger}; use sha2::{Digest, Sha256}; -use ark_poly::univariate::DensePolynomial; -use ark_poly::{DenseUVPolynomial, Polynomial}; +use crypto_bigint::{U256, impl_modulus, const_residue, modular::constant_mod::ResidueParams}; + +const BLOB_SIZE: usize = 32; + +impl_modulus!( + Fr, + U256, + "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001" +); + +fn mul_mod(a: U256, b: U256) -> U256 { + let a = const_residue!(a, Fr); + let b = const_residue!(b, Fr); + a.mul(&b).retrieve() +} fn main() { + let start = env::cycle_count(); let public_inputs: EquivalencePublic = env::read(); let EquivalencePrivate { coefficients, } = env::read(); let private_inputs = EquivalencePrivate { coefficients }; + let end = env::cycle_count(); + eprintln!("inputs load: {}", end - start); + let start = env::cycle_count(); + // BLS scalar field modulus + let modulus = U256::from_be_slice(&[115, 237, 167, 83, 41, 157, 125, 72, 51, 57, 216, 8, 9, 161, 216, 5, 83, 189, 164, 2, 255, 254, 91, 254, 255, 255, 255, 255, 0, 0, 0, 1]); + let end = env::cycle_count(); + eprintln!("modulus conversion from u8: {}", end - start); //compute random point let start = env::cycle_count(); @@ -24,31 +43,25 @@ fn main() { let end = env::cycle_count(); eprintln!("draw random point: {}", end - start); + //evaluate the polynomial over BLS let start = env::cycle_count(); - let bls_point = Fr::from_be_bytes_mod_order(&x_0); + let bls_point = U256::from_be_slice(&x_0); let end = env::cycle_count(); - eprintln!("point conversion from u8: {}", end - start); + eprintln!("evaluation point conversion from u8: {}", end - start); let start = env::cycle_count(); - let mut bls_coefficients : Vec = vec![]; - for i in 0..private_inputs.coefficients.len() { - bls_coefficients.push(Fr::from_be_bytes_mod_order(&private_inputs.coefficients[i])); + let mut evaluation = private_inputs.coefficients[BLOB_SIZE-1]; + for i in 1..BLOB_SIZE { + let mul = mul_mod(evaluation, bls_point); + evaluation = private_inputs.coefficients[BLOB_SIZE-1-i].add_mod(&mul, &modulus); } let end = env::cycle_count(); - eprintln!("coefficients conversion from u8: {}", end - start); - - let start = env::cycle_count(); - let polynomial = DensePolynomial::from_coefficients_vec(bls_coefficients); - let end = env::cycle_count(); - eprintln!("polynomial construction: {}", end - start); - - let start = env::cycle_count(); - let evaluation = polynomial.evaluate(&bls_point); - let end = env::cycle_count(); eprintln!("point evaluation: {}", end - start); + + let start = env::cycle_count(); - assert_eq!(evaluation, Fr::from_be_bytes_mod_order(&public_inputs.y_0)); + assert_eq!(evaluation, public_inputs.y_0); let end = env::cycle_count(); eprintln!("last assertion: {}", end - start);