move code for better cycle analysis

This commit is contained in:
thomaslavaur 2024-09-06 08:18:47 +02:00
parent b71e7457a3
commit 83139f9f12
5 changed files with 46 additions and 43 deletions

View File

@ -6,9 +6,4 @@ edition = "2021"
[dependencies]
cl = { path = "../../cl/cl" }
serde = { version = "1.0", features = ["derive"] }
crypto-bigint = { version = "0.5.5", features = ["serde"] }
sha2 = "0.10"
ark-bls12-381 = "0.4.0"
ark-poly = "0.4.0"
ark-ff = "0.4.0"
ark-serialize = "0.4.0"
crypto-bigint = { version = "0.5.5", features = ["serde"] }

View File

@ -1,11 +1,4 @@
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use ark_poly::univariate::DensePolynomial;
use ark_bls12_381::Fr;
use ark_ff::{PrimeField};
use ark_poly::{DenseUVPolynomial, Polynomial};
const BLOB_SIZE: usize = 32;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EquivalencePublic {
@ -32,31 +25,10 @@ pub struct EquivalencePrivate {
impl EquivalencePrivate {
pub fn new(
coefficients: Vec<[u8; 32]>,
coefficients: Vec<Fr>,
) -> Self {
Self {
coefficients,
}
}
pub fn get_random_point(&self, da_commitment: Vec<u8>) -> [u8; 32] {
assert_eq!(da_commitment.len(), 48);
let mut hasher = Sha256::new();
hasher.update(da_commitment);
for i in 0..self.coefficients.len() {
hasher.update(&self.coefficients[i]);
}
hasher.finalize().into()
}
pub fn evaluate(&self, point: [u8;32]) -> Fr {
assert_eq!(self.coefficients.len(), BLOB_SIZE);
// TODO: Optimize this part
let mut bls_coefficients = vec![];
for i in 0..BLOB_SIZE {
bls_coefficients.push(Fr::from_be_bytes_mod_order(&self.coefficients[i]));
}
let bls_point = Fr::from_be_bytes_mod_order(&point);
let polynomial = DensePolynomial::from_coefficients_vec(bls_coefficients);
polynomial.evaluate(&bls_point)
}
}

View File

@ -56,7 +56,7 @@ mod test {
static GLOBAL_PARAMETERS: Lazy<UniversalParams<Bls12_381>> = Lazy::new(|| {
let mut rng = rand::thread_rng();
KZG10::<Bls12_381, DensePolynomial<Fr>>::setup(64, true, &mut rng).unwrap()
KZG10::<Bls12_381, DensePolynomial<Fr>>::setup(32, true, &mut rng).unwrap()
});
#[test]
@ -80,9 +80,6 @@ mod test {
//recover x_0
let mut hasher = Sha256::new();
hasher.update(da_commitment.clone());
for i in 0..coefficients.clone().len() {
hasher.update(coefficients[i].clone());
}
let x_0 = Fr::from_be_bytes_mod_order(&hasher.finalize());
let y_0 = bls_polynomial.evaluate(&x_0); // EVAL OF x0

View File

@ -14,6 +14,8 @@ 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]
# add RISC Zero accelerator support for all downstream usages of the following crates.

View File

@ -2,7 +2,10 @@
use equivalence_proof_statements::{EquivalencePrivate, EquivalencePublic};
use risc0_zkvm::guest::env;
use ark_bls12_381::Fr;
use ark_ff::{PrimeField};
use ark_ff::{PrimeField, BigInteger};
use sha2::{Digest, Sha256};
use ark_poly::univariate::DensePolynomial;
use ark_poly::{DenseUVPolynomial, Polynomial};
fn main() {
let public_inputs: EquivalencePublic = env::read();
@ -12,12 +15,46 @@ fn main() {
} = env::read();
let private_inputs = EquivalencePrivate { coefficients };
//compute random point
let random_point = private_inputs.get_random_point(public_inputs.da_commitment.clone());
let start = env::cycle_count();
let mut hasher = Sha256::new();
hasher.update(public_inputs.da_commitment.clone());
let x_0 : [u8; 32] = hasher.finalize().into();
let end = env::cycle_count();
eprintln!("draw random point: {}", end - start);
//evaluate the polynomial over BLS
let evaluation = private_inputs.evaluate(random_point);
assert_eq!(evaluation, Fr::from_be_bytes_mod_order(&public_inputs.y_0));
let start = env::cycle_count();
let bls_point = Fr::from_be_bytes_mod_order(&x_0);
let end = env::cycle_count();
eprintln!("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 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));
let end = env::cycle_count();
eprintln!("last assertion: {}", end - start);
let start = env::cycle_count();
env::commit(&public_inputs);
let end = env::cycle_count();
eprintln!("public input: {}", end - start);
}