Skip to content

Commit

Permalink
fix: remove endianness shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker committed Oct 25, 2023
1 parent 06bdefa commit 46b3af6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
40 changes: 10 additions & 30 deletions src/provider/non_hiding_zeromorph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ where
}

debug_assert_eq!(Self::commit(pp, poly).unwrap().0, comm.0);
debug_assert_eq!(poly.evaluate_BE(point), eval.0);
debug_assert_eq!(poly.evaluate(point), eval.0);

let (quotients, remainder) = quotients(poly, point);
debug_assert_eq!(remainder, eval.0);
Expand Down Expand Up @@ -329,7 +329,7 @@ fn quotients<F: PrimeField>(poly: &MultilinearPolynomial<F>, point: &[F]) -> (Ve

let mut remainder = poly.Z.to_vec();
let mut quotients = point
.iter()
.iter().rev() // assume polynomial variables come in LE form
.enumerate()
.rev()
.map(|(num_var, x_i)| {
Expand Down Expand Up @@ -361,8 +361,8 @@ fn quotients<F: PrimeField>(poly: &MultilinearPolynomial<F>, point: &[F]) -> (Ve
}

// TODO : move this somewhere else
fn eval_and_quotient_scalars<F: Field>(y: F, x: F, z: F, u: &[F]) -> (F, Vec<F>) {
let num_vars = u.len();
fn eval_and_quotient_scalars<F: Field>(y: F, x: F, z: F, point: &[F]) -> (F, Vec<F>) {
let num_vars = point.len();

let squares_of_x = iter::successors(Some(x), |&x| Some(x.square()))
.take(num_vars + 1)
Expand Down Expand Up @@ -397,7 +397,7 @@ fn eval_and_quotient_scalars<F: Field>(y: F, x: F, z: F, u: &[F]) -> (F, Vec<F>)
.zip(squares_of_x)
.zip(&vs)
.zip(&vs[1..])
.zip(u)
.zip(point.iter().rev()) // assume variables come in LE form
.map(
|(((((power_of_y, offset_of_x), square_of_x), v_i), v_j), u_i)| {
-(power_of_y * offset_of_x + z * (square_of_x * v_j - *u_i * v_i))
Expand Down Expand Up @@ -437,18 +437,8 @@ where
// TODO: the following two lines will need to change base
let polynomial = MultilinearPolynomial::new(poly.to_vec());

// Nova evaluates in lower endian, the implementation assumes big endian
let rev_point = point.iter().rev().cloned().collect::<Vec<_>>();

let evaluation = ZMEvaluation(*eval);
ZMPCS::open(
pk,
&commitment,
&polynomial,
&rev_point,
&evaluation,
transcript,
)
ZMPCS::open(pk, &commitment, &polynomial, point, &evaluation, transcript)
}

fn verify(
Expand All @@ -462,18 +452,8 @@ where
let commitment = ZMCommitment::from(UVKZGCommitment::from(*comm));
let evaluation = ZMEvaluation(*eval);

// Nova evaluates in lower endian, the implementation assumes big endian
let rev_point = point.iter().rev().cloned().collect::<Vec<_>>();

// TODO: this clone is unsightly!
ZMPCS::verify(
vk,
transcript,
&commitment,
&rev_point,
&evaluation,
arg.clone(),
)?;
ZMPCS::verify(vk, transcript, &commitment, point, &evaluation, arg.clone())?;
Ok(())
}
}
Expand Down Expand Up @@ -527,7 +507,7 @@ mod test {
let point = iter::from_fn(|| transcript.squeeze(b"pt").ok())
.take(num_vars)
.collect::<Vec<_>>();
let eval = ZMEvaluation(poly.evaluate_BE(&point));
let eval = ZMEvaluation(poly.evaluate(&point));

let mut transcript_prover = Keccak256Transcript::<E::G1>::new(b"test");
let proof = ZMPCS::open(&pp, &comm, &poly, &point, &eval, &mut transcript_prover).unwrap();
Expand Down Expand Up @@ -577,11 +557,11 @@ mod test {
}
let (_quotients, remainder) = quotients(&poly, &point);
assert_eq!(
poly.evaluate_BE(&point),
poly.evaluate(&point),
remainder,
"point: {:?}, \n eval: {:?}, remainder:{:?}",
point,
poly.evaluate_BE(&point),
poly.evaluate(&point),
remainder
);
}
Expand Down
26 changes: 0 additions & 26 deletions src/spartan/polys/multilinear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,6 @@ impl<Scalar: PrimeField> MultilinearPolynomial<Scalar> {
}
new_poly
}

/// Evaluate the dense MLE at the given point. The MLE is assumed to be in
/// monomial basis.
///
/// # Example
/// ```
/// use pasta_curves::pallas::Scalar as Fr;
/// use nova_snark::spartan::polys::multilinear::MultilinearPolynomial;
///
/// // The two-variate polynomial x_1 + 3 * x_0 * x_1 + 2 evaluates to [2, 3, 2, 6]
/// // in the lagrange basis: 2*(1 - x_0)(1 - x_1) + 3*(1-x_0)(x_1) + 2*(x_0)(1-x_1) + 6*(x_0)(x_1)
/// let mle = MultilinearPolynomial::new(
/// vec![2, 3, 2, 6].iter().map(|x| Fr::from(*x as u64)).collect()
/// );
///
/// // By the uniqueness of MLEs, `mle` is precisely the above polynomial, which
/// // takes the value 54 at the point (x_1 = 1, x_0 = 17)
/// let eval = mle.evaluate_BE(&[Fr::one(), Fr::from(17)]);
/// assert_eq!(eval, Fr::from(54));
/// ```
pub fn evaluate_BE(&self, point: &[Scalar]) -> Scalar {
assert_eq!(self.num_vars, point.len());
// evaluate requires "lower endian"
let revp = point.iter().cloned().rev().collect::<Vec<_>>();
self.evaluate(&revp)
}
}

impl<Scalar: PrimeField> Index<usize> for MultilinearPolynomial<Scalar> {
Expand Down

0 comments on commit 46b3af6

Please sign in to comment.