Skip to content

Commit d89fd77

Browse files
committed
fix: remove endianness shenanigans
1 parent 06bdefa commit d89fd77

File tree

2 files changed

+15
-61
lines changed

2 files changed

+15
-61
lines changed

src/provider/non_hiding_zeromorph.rs

+15-35
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ where
189189
}
190190

191191
debug_assert_eq!(Self::commit(pp, poly).unwrap().0, comm.0);
192-
debug_assert_eq!(poly.evaluate_BE(point), eval.0);
192+
debug_assert_eq!(poly.evaluate(point), eval.0);
193193

194194
let (quotients, remainder) = quotients(poly, point);
195195
debug_assert_eq!(remainder, eval.0);
@@ -325,15 +325,15 @@ where
325325
/// (n - k) variables at, respectively, point'' = (point_k + 1, point_{k+1}, ..., point_{n-1}) and
326326
/// point' = (point_k, ..., point_{n-1}).
327327
fn quotients<F: PrimeField>(poly: &MultilinearPolynomial<F>, point: &[F]) -> (Vec<Vec<F>>, F) {
328-
assert_eq!(poly.get_num_vars(), point.len());
328+
let num_var = poly.get_num_vars();
329+
assert_eq!(num_var, point.len());
329330

330331
let mut remainder = poly.Z.to_vec();
331332
let mut quotients = point
332-
.iter()
333+
.iter() // assume polynomial variables come in LE form
333334
.enumerate()
334-
.rev()
335-
.map(|(num_var, x_i)| {
336-
let (remainder_lo, remainder_hi) = remainder.split_at_mut(1 << num_var);
335+
.map(|(idx, x_i)| {
336+
let (remainder_lo, remainder_hi) = remainder.split_at_mut(1 << (num_var - 1 - idx));
337337
let mut quotient = vec![F::ZERO; remainder_lo.len()];
338338

339339
quotient
@@ -350,7 +350,7 @@ fn quotients<F: PrimeField>(poly: &MultilinearPolynomial<F>, point: &[F]) -> (Ve
350350
*r_lo += (*r_hi - r_lo as &_) * x_i;
351351
});
352352

353-
remainder.truncate(1 << num_var);
353+
remainder.truncate(1 << (num_var - 1 - idx));
354354

355355
quotient
356356
})
@@ -361,8 +361,8 @@ fn quotients<F: PrimeField>(poly: &MultilinearPolynomial<F>, point: &[F]) -> (Ve
361361
}
362362

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

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

440-
// Nova evaluates in lower endian, the implementation assumes big endian
441-
let rev_point = point.iter().rev().cloned().collect::<Vec<_>>();
442-
443440
let evaluation = ZMEvaluation(*eval);
444-
ZMPCS::open(
445-
pk,
446-
&commitment,
447-
&polynomial,
448-
&rev_point,
449-
&evaluation,
450-
transcript,
451-
)
441+
ZMPCS::open(pk, &commitment, &polynomial, point, &evaluation, transcript)
452442
}
453443

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

465-
// Nova evaluates in lower endian, the implementation assumes big endian
466-
let rev_point = point.iter().rev().cloned().collect::<Vec<_>>();
467-
468455
// TODO: this clone is unsightly!
469-
ZMPCS::verify(
470-
vk,
471-
transcript,
472-
&commitment,
473-
&rev_point,
474-
&evaluation,
475-
arg.clone(),
476-
)?;
456+
ZMPCS::verify(vk, transcript, &commitment, point, &evaluation, arg.clone())?;
477457
Ok(())
478458
}
479459
}
@@ -527,7 +507,7 @@ mod test {
527507
let point = iter::from_fn(|| transcript.squeeze(b"pt").ok())
528508
.take(num_vars)
529509
.collect::<Vec<_>>();
530-
let eval = ZMEvaluation(poly.evaluate_BE(&point));
510+
let eval = ZMEvaluation(poly.evaluate(&point));
531511

532512
let mut transcript_prover = Keccak256Transcript::<E::G1>::new(b"test");
533513
let proof = ZMPCS::open(&pp, &comm, &poly, &point, &eval, &mut transcript_prover).unwrap();
@@ -577,11 +557,11 @@ mod test {
577557
}
578558
let (_quotients, remainder) = quotients(&poly, &point);
579559
assert_eq!(
580-
poly.evaluate_BE(&point),
560+
poly.evaluate(&point),
581561
remainder,
582562
"point: {:?}, \n eval: {:?}, remainder:{:?}",
583563
point,
584-
poly.evaluate_BE(&point),
564+
poly.evaluate(&point),
585565
remainder
586566
);
587567
}

src/spartan/polys/multilinear.rs

-26
Original file line numberDiff line numberDiff line change
@@ -130,32 +130,6 @@ impl<Scalar: PrimeField> MultilinearPolynomial<Scalar> {
130130
}
131131
new_poly
132132
}
133-
134-
/// Evaluate the dense MLE at the given point. The MLE is assumed to be in
135-
/// monomial basis.
136-
///
137-
/// # Example
138-
/// ```
139-
/// use pasta_curves::pallas::Scalar as Fr;
140-
/// use nova_snark::spartan::polys::multilinear::MultilinearPolynomial;
141-
///
142-
/// // The two-variate polynomial x_1 + 3 * x_0 * x_1 + 2 evaluates to [2, 3, 2, 6]
143-
/// // 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)
144-
/// let mle = MultilinearPolynomial::new(
145-
/// vec![2, 3, 2, 6].iter().map(|x| Fr::from(*x as u64)).collect()
146-
/// );
147-
///
148-
/// // By the uniqueness of MLEs, `mle` is precisely the above polynomial, which
149-
/// // takes the value 54 at the point (x_1 = 1, x_0 = 17)
150-
/// let eval = mle.evaluate_BE(&[Fr::one(), Fr::from(17)]);
151-
/// assert_eq!(eval, Fr::from(54));
152-
/// ```
153-
pub fn evaluate_BE(&self, point: &[Scalar]) -> Scalar {
154-
assert_eq!(self.num_vars, point.len());
155-
// evaluate requires "lower endian"
156-
let revp = point.iter().cloned().rev().collect::<Vec<_>>();
157-
self.evaluate(&revp)
158-
}
159133
}
160134

161135
impl<Scalar: PrimeField> Index<usize> for MultilinearPolynomial<Scalar> {

0 commit comments

Comments
 (0)