Skip to content

Commit

Permalink
fix: test_commit() passes
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed May 17, 2024
1 parent fc249a8 commit cb47e02
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
9 changes: 9 additions & 0 deletions src/curve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ impl<C: EllipticCurve> AddAssign for AffinePoint<C> {
fn add_assign(&mut self, rhs: Self) { *self = *self + rhs; }
}

impl<C: EllipticCurve> Sum for AffinePoint<C> {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.reduce(|x, y| x + y).unwrap_or(AffinePoint::Infinity)
}
}

impl<C: EllipticCurve> Neg for AffinePoint<C> {
type Output = AffinePoint<C>;

Expand All @@ -115,6 +121,9 @@ impl<C: EllipticCurve> Mul<u32> for AffinePoint<C> {
type Output = AffinePoint<C>;

fn mul(mut self, scalar: u32) -> Self::Output {
if scalar == 0 {
return AffinePoint::Infinity;
}
let val = self;
for _ in 1..scalar {
self += val;
Expand Down
11 changes: 1 addition & 10 deletions src/kzg/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,9 @@ pub fn commit(
coefs: Vec<PlutoScalarField>,
g1_srs: Vec<AffinePoint<PlutoExtendedCurve>>,
) -> AffinePoint<PlutoExtendedCurve> {
// commit to a polynomial
// - given a polynomial, commit to it
assert!(g1_srs.len() >= coefs.len());
// Todo implement multiplication with field elements as scalar mult.
// Maybe having the scalar mult be around the base field like colin suggested is better

let mut commitment = AffinePoint::Infinity;
for (coef, point) in coefs.iter().zip(g1_srs) {
let res = point * *coef;
commitment += res;
}
commitment
g1_srs.into_iter().zip(coefs).map(|(g1, coef)| g1 * coef).sum::<AffinePoint<PlutoExtendedCurve>>()
}

/// Open the commitment
Expand Down
19 changes: 8 additions & 11 deletions src/kzg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ fn test_setup() {
assert_eq!(g2srs, expected_g2srs);
}

/// always right polynomial with degree 0 term first
#[test]
fn test_commit() {
println!("FIRST COMMIT");
let (g1srs, _) = setup();
// p(x) = (x-1)(x-2)(x-3)
// p(x) = - 6 + 11x -6x^2 + x^3

// (X^2 - 3x +2)(x-3)
// x^3 -3x^2 - 3x^2 + 9x + 2x - 6
// x^3 - 6x^2 + 11x - 6
// p(x) = 11 + 11x + 11x^2 + x^3 mod 17

// -> -6 mod 17 is 11 so this is [11, 11, 11, 1]
let coefficients = vec![
Expand All @@ -71,6 +68,7 @@ fn test_commit() {
let commit_1 = commit(coefficients, g1srs.clone());
assert_eq!(commit_1, AffinePoint::<PlutoExtendedCurve>::Infinity);

println!("\n\nSECOND COMMIT");
// p(x) = (x-1)(x-2)(x-3)(x-4)
// p(x) = 24 - 50x + 35x^2 - 10x^3
// -> 24 mod 17 is 7
Expand All @@ -87,27 +85,26 @@ fn test_commit() {
// g1srs[0] * 7 + g1srs[1] * 16 + g1srs[2] * 1 + g1srs[3] * 11 + g1srs[4] * 1
let commit_2 = commit(coefficients, g1srs.clone());

/// point not on curve
assert_eq!(
commit_2,
AffinePoint::<PlutoExtendedCurve>::new(
PlutoBaseFieldExtension::new([PlutoBaseField::new(32), PlutoBaseField::new(59)]),
PlutoBaseFieldExtension::new([PlutoBaseField::ZERO, PlutoBaseField::ZERO]),
PlutoBaseFieldExtension::from(32usize),
PlutoBaseFieldExtension::from(59usize),
)
);

println!("\n\nTHIRD COMMIT");
// p(x) = 3 + 2x + x^2
let coefficients =
vec![PlutoScalarField::new(3), PlutoScalarField::new(2), PlutoScalarField::new(1)];
// g1srs[0] * 3 + g1srs[1] * 2 + g1srs[2] * 1
let commit_3 = commit(coefficients, g1srs);
/// point not on curve

assert_eq!(
commit_3,
AffinePoint::<PlutoExtendedCurve>::new(
PlutoBaseFieldExtension::new([PlutoBaseField::new(32), PlutoBaseField::new(59)]),
PlutoBaseFieldExtension::new([PlutoBaseField::ZERO, PlutoBaseField::ZERO]),
PlutoBaseFieldExtension::from(32usize),
PlutoBaseFieldExtension::from(59usize),
)
);
}
Expand Down

0 comments on commit cb47e02

Please sign in to comment.