switch from arkworks to crypto-bigint to reduce cylces

This commit is contained in:
thomaslavaur 2024-09-06 14:37:57 +02:00
parent d2127d494b
commit c6f13496a5
6 changed files with 50 additions and 31 deletions

View File

@ -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"

View File

@ -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<u8>,
pub y_0: [u8; 32]
pub y_0: U256
}
impl EquivalencePublic {
pub fn new(
da_commitment: Vec<u8>,
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<U256>
}
impl EquivalencePrivate {
pub fn new(
coefficients: Vec<[u8; 32]>,
coefficients: Vec<U256>,
) -> Self {
Self {
coefficients,

View File

@ -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"]

View File

@ -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<UniversalParams<Bls12_381>> = Lazy::new(|| {
let mut rng = rand::thread_rng();
KZG10::<Bls12_381, DensePolynomial<Fr>>::setup(32, true, &mut rng).unwrap()
KZG10::<Bls12_381, DensePolynomial<Fr>>::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

View File

@ -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]

View File

@ -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<Fr> = 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);