Skip to content

Commit

Permalink
clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Dec 29, 2023
1 parent e97c368 commit 2c8cf31
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 72 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = ["verkle-db", "verkle-trie", "verkle-spec", "ipa-multipoint"]
resolver = "2"

[profile.bench]
debug = true
Expand Down
2 changes: 1 addition & 1 deletion ipa-multipoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"

[dependencies]
rand = "0.6"
criterion = "0.3.4"
criterion = "0.5.1"
bandersnatch = "0.1.1"
ark-ff = { version = "^0.3.0", default-features = false }
ark-ec = { version = "^0.3.0", default-features = false }
Expand Down
20 changes: 9 additions & 11 deletions ipa-multipoint/src/crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,19 @@ impl std::ops::Index<usize> for CRS {
}

fn generate_random_elements(num_required_points: usize, seed: &'static [u8]) -> Vec<Element> {
use ark_ec::group::Group;

use ark_ff::PrimeField;
use bandersnatch::Fq;
use sha2::{Digest, Sha256};

let choose_largest = false;
let _choose_largest = false;

(0u64..)
.into_iter()
// Hash the seed + i to get a possible x value
.map(|i| {
let mut hasher = Sha256::new();
hasher.update(seed);
hasher.update(&i.to_be_bytes());
hasher.update(i.to_be_bytes());
let bytes: Vec<u8> = hasher.finalize().to_vec();
bytes
})
Expand All @@ -76,8 +75,7 @@ fn generate_random_elements(num_required_points: usize, seed: &'static [u8]) ->
bytes
})
// Deserialise the x-cordinate to get a valid banderwagon element
.map(|bytes| Element::from_bytes(&bytes))
.filter_map(|point| point)
.filter_map(|bytes| Element::from_bytes(&bytes))
.take(num_required_points)
.collect()
}
Expand All @@ -88,22 +86,22 @@ fn crs_consistency() {
// TODO is a bit different
// See: https://hackmd.io/1RcGSMQgT4uREaq1CCx_cg#Methodology
use ark_serialize::CanonicalSerialize;
use bandersnatch::Fq;

use sha2::{Digest, Sha256};

let points = generate_random_elements(256, b"eth_verkle_oct_2021");

let mut bytes = [0u8; 32];
points[0].serialize(&mut bytes[..]).unwrap();
assert_eq!(
hex::encode(&bytes),
hex::encode(bytes),
"01587ad1336675eb912550ec2a28eb8923b824b490dd2ba82e48f14590a298a0",
"the first point is incorrect"
);
let mut bytes = [0u8; 32];
points[255].serialize(&mut bytes[..]).unwrap();
assert_eq!(
hex::encode(&bytes),
hex::encode(bytes),
"3de2be346b539395b0c0de56a5ccca54a317f1b5c80107b0802af9a62276a4d8",
"the 256th (last) point is incorrect"
);
Expand All @@ -112,11 +110,11 @@ fn crs_consistency() {
for point in &points {
let mut bytes = [0u8; 32];
point.serialize(&mut bytes[..]).unwrap();
hasher.update(&bytes);
hasher.update(bytes);
}
let bytes = hasher.finalize().to_vec();
assert_eq!(
hex::encode(&bytes),
hex::encode(bytes),
"1fcaea10bf24f750200e06fa473c76ff0468007291fa548e2d99f09ba9256fdb",
"unexpected point encountered"
);
Expand Down
16 changes: 8 additions & 8 deletions ipa-multipoint/src/ipa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn create(
let mut L_vec: Vec<Element> = Vec::with_capacity(num_rounds as usize);
let mut R_vec: Vec<Element> = Vec::with_capacity(num_rounds as usize);

for k in 0..num_rounds {
for _k in 0..num_rounds {
let (a_L, a_R) = halve(a);
let (b_L, b_R) = halve(b);
let (G_L, G_R) = halve(G);
Expand All @@ -140,9 +140,9 @@ pub fn create(
let x = transcript.challenge_scalar(b"x");
let x_inv = x.inverse().unwrap();
for i in 0..a_L.len() {
a_L[i] = a_L[i] + x * a_R[i];
b_L[i] = b_L[i] + x_inv * b_R[i];
G_L[i] = G_L[i] + G_R[i] * x_inv;
a_L[i] += x * a_R[i];
b_L[i] += x_inv * b_R[i];
G_L[i] += G_R[i] * x_inv;
}

a = a_L;
Expand Down Expand Up @@ -221,8 +221,8 @@ impl IPAProof {
let (b_L, b_R) = halve(b);

for i in 0..G_L.len() {
G_L[i] = G_L[i] + G_R[i] * *x_inv;
b_L[i] = b_L[i] + b_R[i] * x_inv;
G_L[i] += G_R[i] * *x_inv;
b_L[i] += b_R[i] * x_inv;
}
G = G_L;
b = b_L;
Expand Down Expand Up @@ -413,11 +413,11 @@ mod tests {
use super::*;
use crate::crs::CRS;
use crate::math_utils::{inner_product, powers_of};
use ark_std::rand;

use ark_std::rand::SeedableRng;
use ark_std::UniformRand;
use rand_chacha::ChaCha20Rng;
use std::iter;

#[test]
fn test_create_IPAProof_proof() {
let n = 8;
Expand Down
14 changes: 9 additions & 5 deletions ipa-multipoint/src/lagrange_basis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ark_ff::{batch_inversion, batch_inversion_and_mul, Field, One, Zero};
use ark_poly::{univariate::DensePolynomial, Polynomial, UVPolynomial};
use ark_poly::{univariate::DensePolynomial, UVPolynomial};
use bandersnatch::Fr;
use std::{
convert::TryFrom,
Expand All @@ -25,8 +25,8 @@ impl Add<LagrangeBasis> for LagrangeBasis {
}
self.values
.iter_mut()
.zip(rhs.values.into_iter())
.for_each(|(lhs, rhs)| *lhs = *lhs + rhs);
.zip(rhs.values)
.for_each(|(lhs, rhs)| *lhs += rhs);
self
}
}
Expand All @@ -36,7 +36,7 @@ impl Mul<Fr> for LagrangeBasis {
fn mul(mut self, rhs: Fr) -> Self::Output {
self.values
.iter_mut()
.for_each(|values| *values = *values * rhs);
.for_each(|values| *values *= rhs);
self
}
}
Expand All @@ -46,7 +46,7 @@ impl Sub<&Fr> for LagrangeBasis {
fn sub(mut self, rhs: &Fr) -> Self::Output {
self.values
.iter_mut()
.for_each(|values| *values = *values - rhs);
.for_each(|values| *values -= rhs);
self
}
}
Expand Down Expand Up @@ -261,6 +261,8 @@ impl LagrangeBasis {

#[test]
fn basic_interpolation() {
use ark_poly::Polynomial;

let p1 = Fr::from(8u128);
let p2 = Fr::from(2u128);
let lag_poly = LagrangeBasis::new(vec![p1, p2]);
Expand All @@ -276,6 +278,8 @@ fn basic_interpolation() {

#[test]
fn simple_eval_outside_domain() {
use ark_poly::Polynomial;

let numerator_lag =
LagrangeBasis::new(vec![-Fr::from(2), Fr::from(0), Fr::from(12), Fr::from(40)]);
let numerator_coeff = numerator_lag.interpolate();
Expand Down
2 changes: 1 addition & 1 deletion ipa-multipoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod math_utils;
pub mod multiproof;
pub mod transcript;

pub(crate) use ipa::slow_vartime_multiscalar_mul;


pub mod lagrange_basis;

Expand Down
8 changes: 5 additions & 3 deletions ipa-multipoint/src/math_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ark_ff::{Field, One};
use ark_ff::{One};
use bandersnatch::Fr;
/// Computes the inner product between two scalar vectors
pub fn inner_product(a: &[Fr], b: &[Fr]) -> Fr {
Expand All @@ -19,14 +19,16 @@ pub fn powers_of(point: Fr, n: usize) -> Vec<Fr> {
fn simple_vandemonde() {
use ark_std::test_rng;
use ark_std::UniformRand;
use ark_ff::Field;

let rand_fr = Fr::rand(&mut test_rng());
let n = 100;
let powers = powers_of(rand_fr, n);

assert_eq!(powers[0], Fr::one());
assert_eq!(powers[n - 1], rand_fr.pow(&[(n - 1) as u64]));
assert_eq!(powers[n - 1], rand_fr.pow([(n - 1) as u64]));

for (i, power) in powers.into_iter().enumerate() {
assert_eq!(power, rand_fr.pow(&[i as u64]))
assert_eq!(power, rand_fr.pow([i as u64]))
}
}
43 changes: 25 additions & 18 deletions ipa-multipoint/src/multiproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
#![allow(non_snake_case)]

use crate::crs::CRS;
use crate::ipa::{self, slow_vartime_multiscalar_mul, IPAProof};
use crate::ipa::{slow_vartime_multiscalar_mul, IPAProof};
use crate::lagrange_basis::{LagrangeBasis, PrecomputedWeights};
use crate::math_utils::inner_product;

use crate::math_utils::powers_of;
use crate::transcript::Transcript;
use crate::transcript::TranscriptProtocol;
use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ff::PrimeField;
use ark_ff::{batch_inversion, Field};
use ark_ff::{One, Zero};
use ark_poly::{Polynomial, UVPolynomial};


use ark_ff::{batch_inversion};
use ark_ff::{Zero};

use std::collections::HashMap;

use banderwagon::{multi_scalar_mul, Element, Fr};
use banderwagon::{Element, Fr};
pub struct MultiPoint;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -132,7 +132,7 @@ impl MultiPoint {

let g1_x = aggregated_queries
.into_iter()
.zip(g1_den.into_iter())
.zip(g1_den)
.map(|((_, agg_f_x), den_inv)| {
let term: Vec<_> = agg_f_x
.values()
Expand Down Expand Up @@ -160,7 +160,7 @@ impl MultiPoint {

MultiPointProof {
open_proof: g_3_ipa,
g_x_comm: g_x_comm,
g_x_comm,
}
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ impl MultiPointProof {

let helper_scalars: Vec<_> = powers_of_r
.iter()
.zip(g2_den.into_iter())
.zip(g2_den)
.map(|(r_i, den_inv)| den_inv * r_i)
.collect();

Expand All @@ -246,7 +246,7 @@ impl MultiPointProof {
.sum();

//4. Compute [g_1(X)] = E
let comms: Vec<_> = queries.into_iter().map(|query| query.commitment).collect();
let comms: Vec<_> = queries.iter().map(|query| query.commitment).collect();
let g1_comm = slow_vartime_multiscalar_mul(helper_scalars.iter(), comms.iter());

transcript.append_point(b"E", &g1_comm);
Expand All @@ -255,7 +255,7 @@ impl MultiPointProof {
let g3_comm = g1_comm - self.g_x_comm;

// Check IPA
let b = LagrangeBasis::evaluate_lagrange_coefficients(&precomp, crs.n, t); // TODO: we could put this as a method on PrecomputedWeights
let b = LagrangeBasis::evaluate_lagrange_coefficients(precomp, crs.n, t); // TODO: we could put this as a method on PrecomputedWeights

self.open_proof
.verify_multiexp(transcript, crs, b, g3_comm, t, g2_t)
Expand All @@ -280,6 +280,8 @@ pub(crate) fn open_point_outside_of_domain(

#[test]
fn open_multiproof_lagrange() {
use ark_std::One;

let poly = LagrangeBasis::new(vec![
Fr::one(),
Fr::from(10u128),
Expand Down Expand Up @@ -318,6 +320,8 @@ fn open_multiproof_lagrange() {

#[test]
fn open_multiproof_lagrange_2_polys() {
use ark_std::One;

let poly = LagrangeBasis::new(vec![
Fr::one(),
Fr::from(10u128),
Expand All @@ -342,7 +346,7 @@ fn open_multiproof_lagrange_2_polys() {
};
let prover_query_j = ProverQuery {
commitment: poly_comm,
poly: poly,
poly,
point: x_j,
result: y_j,
};
Expand All @@ -369,11 +373,12 @@ fn open_multiproof_lagrange_2_polys() {
}
#[test]
fn test_ipa_consistency() {
use crate::math_utils::inner_product;
use ark_serialize::CanonicalSerialize;
let n = 256;
let crs = CRS::new(n, b"eth_verkle_oct_2021");
let precomp = PrecomputedWeights::new(n);
let input_point = Fr::from(2101 as u128);
let input_point = Fr::from(2101_u128);

let poly: Vec<Fr> = (0..n).map(|i| Fr::from(((i % 32) + 1) as u128)).collect();
let polynomial = LagrangeBasis::new(poly.clone());
Expand All @@ -398,7 +403,7 @@ fn test_ipa_consistency() {
let mut bytes = [0u8; 32];
p_challenge.serialize(&mut bytes[..]).unwrap();
assert_eq!(
hex::encode(&bytes),
hex::encode(bytes),
"0a81881cbfd7d7197a54ebd67ed6a68b5867f3c783706675b34ece43e85e7306"
);

Expand Down Expand Up @@ -437,6 +442,8 @@ fn test_ipa_consistency() {

#[test]
fn multiproof_consistency() {
use ark_std::One;

use ark_serialize::CanonicalSerialize;
let n = 256;
let crs = CRS::new(n, b"eth_verkle_oct_2021");
Expand All @@ -456,7 +463,7 @@ fn multiproof_consistency() {
let y_a = Fr::one();

let point_b = 0;
let y_b = Fr::from(32 as u128);
let y_b = Fr::from(32_u128);

let poly_comm_a = crs.commit_lagrange_poly(&polynomial_a);
let poly_comm_b = crs.commit_lagrange_poly(&polynomial_b);
Expand Down Expand Up @@ -486,7 +493,7 @@ fn multiproof_consistency() {
let mut bytes = [0u8; 32];
p_challenge.serialize(&mut bytes[..]).unwrap();
assert_eq!(
hex::encode(&bytes),
hex::encode(bytes),
"eee8a80357ff74b766eba39db90797d022e8d6dee426ded71234241be504d519"
);

Expand Down
4 changes: 2 additions & 2 deletions ipa-multipoint/src/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod tests {
#[test]
fn test_vector_2() {
let mut tr = Transcript::new(b"simple_protocol");
let five = Fr::from(5 as u128);
let five = Fr::from(5_u128);

tr.append_scalar(b"five", &five);
tr.append_scalar(b"five again", &five);
Expand All @@ -102,7 +102,7 @@ mod tests {
#[test]
fn test_vector_3() {
let mut tr = Transcript::new(b"simple_protocol");
let one = Fr::from(1 as u128);
let one = Fr::from(1_u128);
let minus_one = -one;

tr.append_scalar(b"-1", &minus_one);
Expand Down
Loading

0 comments on commit 2c8cf31

Please sign in to comment.