Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Borrow poly in ProverQuery so that upstream clients do not have to copy #10

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
21 changes: 9 additions & 12 deletions src/ipa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ use ark_ff::Field;
use ark_ff::PrimeField;
use ark_ff::{One, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use bandersnatch::multi_scalar_mul;
use bandersnatch::EdwardsAffine;
use bandersnatch::EdwardsProjective;
use bandersnatch::Fr;
use itertools::Itertools;

use crate::{IOError, IOErrorKind, IOResult};
use std::io::{Read, Write};

use std::borrow::Borrow;
use std::iter;

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -82,6 +79,7 @@ impl IPAProof {
}
}

#[allow(clippy::needless_borrow)]
pub fn create(
transcript: &mut Transcript,
mut crs: CRS,
Expand Down Expand Up @@ -121,7 +119,7 @@ pub fn create(
let mut L_vec: Vec<EdwardsProjective> = Vec::with_capacity(num_rounds as usize);
let mut R_vec: Vec<EdwardsProjective> = Vec::with_capacity(num_rounds as usize);

for k in 0..num_rounds {
for _ 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 @@ -147,9 +145,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].mul(x_inv.into_repr());
a_L[i] += x * a_R[i];
b_L[i] += x_inv * b_R[i];
G_L[i] += G_R[i].mul(x_inv.into_repr());
}

a = a_L;
Expand All @@ -174,6 +172,7 @@ fn log2(n: usize) -> u32 {
}

impl IPAProof {
#[allow(clippy::needless_borrow)]
pub fn verify(
&self,
transcript: &mut Transcript,
Expand Down Expand Up @@ -228,8 +227,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].mul(x_inv.into_repr());
b_L[i] = b_L[i] + b_R[i] * x_inv;
G_L[i] += G_R[i].mul(x_inv.into_repr());
b_L[i] += b_R[i] * x_inv;
}
G = G_L;
b = b_L;
Expand Down Expand Up @@ -318,6 +317,7 @@ impl IPAProof {
// This is being committed incase someone goes through the git history
// The fully unrolled code is not that intuitive, but maybe this semi
// unrolled version can help you to figure out the gap
#[allow(clippy::needless_borrow)]
pub fn verify_semi_multiexp(
&self,
transcript: &mut Transcript,
Expand Down Expand Up @@ -396,7 +396,6 @@ pub fn slow_vartime_multiscalar_mul<'a>(
scalars: impl Iterator<Item = &'a Fr>,
points: impl Iterator<Item = &'a EdwardsProjective>,
) -> EdwardsProjective {
use ark_ec::group::Group;
use ark_ec::msm::VariableBaseMSM;

let scalars: Vec<_> = scalars.into_iter().map(|s| s.into_repr()).collect();
Expand All @@ -423,11 +422,9 @@ mod tests {
use super::*;
use crate::math_utils::{inner_product, powers_of};
use crate::multiproof::CRS;
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
22 changes: 12 additions & 10 deletions src/lagrange_basis.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use ark_ff::{batch_inversion, batch_inversion_and_mul, Field, One, Zero};
use ark_poly::{domain, univariate::DensePolynomial, Polynomial, UVPolynomial};
use ark_ff::{batch_inversion, batch_inversion_and_mul, Field, Zero};
use bandersnatch::Fr;
use std::{
convert::TryFrom,
ops::{Add, Mul, Sub},
};
#[cfg(test)]
use {
ark_ff::One,
ark_poly::{univariate::DensePolynomial, Polynomial, UVPolynomial},
};

#[derive(Clone, Debug)]
pub struct LagrangeBasis {
Expand All @@ -26,27 +30,23 @@ impl Add<LagrangeBasis> for LagrangeBasis {
self.values
.iter_mut()
.zip(rhs.values.into_iter())
.for_each(|(lhs, rhs)| *lhs = *lhs + rhs);
.for_each(|(lhs, rhs)| *lhs += rhs);
self
}
}
impl Mul<Fr> for LagrangeBasis {
type Output = LagrangeBasis;

fn mul(mut self, rhs: Fr) -> Self::Output {
self.values
.iter_mut()
.for_each(|values| *values = *values * rhs);
self.values.iter_mut().for_each(|values| *values *= rhs);
self
}
}
impl Sub<&Fr> for LagrangeBasis {
type Output = LagrangeBasis;

fn sub(mut self, rhs: &Fr) -> Self::Output {
self.values
.iter_mut()
.for_each(|values| *values = *values - rhs);
self.values.iter_mut().for_each(|values| *values -= rhs);
self
}
}
Expand Down Expand Up @@ -155,8 +155,9 @@ impl LagrangeBasis {
}

// This is only for testing purposes
#[cfg(test)]
pub(crate) fn interpolate(&self) -> DensePolynomial<Fr> {
let domain: Vec<_> = (0..self.domain).map(|i| Fr::from(i as u128)).collect();
let domain = (0..self.domain).map(|i| Fr::from(i as u128));
let points: Vec<_> = domain
.into_iter()
.zip(self.values.iter().cloned())
Expand Down Expand Up @@ -322,6 +323,7 @@ fn simple_division() {
assert_eq!(quotient_expected, quotient_coeff)
}

#[cfg(test)]
// Taken from sapling-crypto -- O(n^2)
fn interpolate(points: &[(Fr, Fr)]) -> Option<Vec<Fr>> {
let max_degree_plus_one = points.len();
Expand Down
7 changes: 4 additions & 3 deletions 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 @@ -17,16 +17,17 @@ pub fn powers_of(point: Fr, n: usize) -> Vec<Fr> {

#[test]
fn simple_vandemonde() {
use ark_ff::Field;
use ark_std::test_rng;
use ark_std::UniformRand;
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]))
}
}
Loading