Skip to content

Commit

Permalink
Merge pull request #726 from dusk-network/mocello/ecc_tests
Browse files Browse the repository at this point in the history
Add more tests for `assert_equal_point`
  • Loading branch information
moCello authored Jan 25, 2023
2 parents 5c4dc47 + 8690aa9 commit c162d2b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 59 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add tests for `assert_equal_point` [#725]

### Removed

- Remove `bit_iterator` macro [#632]

### Fixed

- Fixed negation of public input values when using `composer.append_public` [#717]
- Fix negation of public input values when using `composer.append_public` [#717]
- Fix `assert_equal_point` method [#721]

## [0.13.1] - 2022-10-26

Expand Down Expand Up @@ -452,6 +457,8 @@ is necessary since `rkyv/validation` was required as a bound.
- Proof system module.

<!-- ISSUES -->
[#725]: https://github.com/dusk-network/plonk/issues/725
[#721]: https://github.com/dusk-network/plonk/issues/721
[#717]: https://github.com/dusk-network/plonk/issues/717
[#709]: https://github.com/dusk-network/plonk/issues/709
[#697]: https://github.com/dusk-network/plonk/issues/697
Expand Down
3 changes: 2 additions & 1 deletion src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,8 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
self.append_gate(constraint);
}

/// Asserts `a == b` by appending two gates
/// Asserts that the coordinates of the two points `a` and `b` are the same
/// by appending two gates
fn assert_equal_point(&mut self, a: WitnessPoint, b: WitnessPoint) {
self.assert_equal(*a.x(), *b.x());
self.assert_equal(*a.y(), *b.y());
Expand Down
57 changes: 0 additions & 57 deletions tests/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,60 +111,3 @@ fn circuit_with_all_gates() {
.verify(&proof, &public_inputs)
.expect("failed to verify proof");
}

#[test]
fn proving_fails_when_different_points_are_asserted_to_be_equal() {
let rng = &mut StdRng::seed_from_u64(8349u64);

let n = 1 << 12;
let label = b"demo";
let pp = PublicParameters::setup(n, rng).expect("failed to create pp");

pub struct DummyCircuit {
p1: JubJubExtended,
p2: JubJubExtended,
}

impl Default for DummyCircuit {
fn default() -> Self {
Self {
p1: JubJubExtended::from_raw_unchecked(
BlsScalar::zero(),
BlsScalar::one(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
),
p2: JubJubExtended::from_raw_unchecked(
BlsScalar::zero(),
BlsScalar::zero(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
),
}
}
}

impl Circuit for DummyCircuit {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
let w_x = composer.append_point(self.p1);
let w_y = composer.append_point(self.p2);
composer.assert_equal_point(w_x, w_y);
Ok(())
}
}

let (prover, _verifier) = Compiler::compile::<DummyCircuit>(&pp, label)
.expect("failed to compile circuit");

let proving_result = prover.prove(rng, &Default::default());

assert!(
proving_result.is_err(),
"proving should fail because the points are not equal"
);
}
126 changes: 126 additions & 0 deletions tests/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,129 @@ fn mul_point_works() {
.expect_err("circuit is not satisfied");
}
}

#[test]
fn assert_equal_point_works() {
let rng = &mut StdRng::seed_from_u64(8349u64);

let n = 1 << 4;
let label = b"demo";
let pp = PublicParameters::setup(n, rng).expect("failed to create pp");

pub struct DummyCircuit {
p1: JubJubAffine,
p2: JubJubAffine,
}

impl DummyCircuit {
pub fn new(p1: JubJubAffine, p2: JubJubAffine) -> Self {
Self { p1, p2 }
}
}

impl Default for DummyCircuit {
fn default() -> Self {
Self {
p1: dusk_jubjub::GENERATOR,
p2: dusk_jubjub::GENERATOR,
}
}
}

impl Circuit for DummyCircuit {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
let w_p1 = composer.append_point(self.p1);
let w_p2 = composer.append_point(self.p2);
composer.assert_equal_point(w_p1, w_p2);

Ok(())
}
}

let (prover, verifier) = Compiler::compile::<DummyCircuit>(&pp, label)
.expect("failed to compile circuit");

// Test default works:
// GENERATOR = GENERATOR
{
let (proof, public_inputs) = prover
.prove(rng, &Default::default())
.expect("prover shouldn't fail");

assert_eq!(public_inputs.len(), 0);

verifier
.verify(&proof, &public_inputs)
.expect("Default circuit verification should pass");
}

// Test sanity:
// 42 * GENERATOR = 42 * GENERATOR
{
let scalar = JubJubScalar::from(42u64);
let p1 = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let p2 = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let circuit = DummyCircuit::new(p1.into(), p2.into());

let (proof, public_inputs) =
prover.prove(rng, &circuit).expect("prover shouldn't fail");

assert_eq!(public_inputs.len(), 0);

verifier
.verify(&proof, &public_inputs)
.expect("Circuit verification with equal points should pass");
}

// Test:
// GENERATOR != 42 * GENERATOR
{
let scalar = JubJubScalar::from(42u64);
let p1 = dusk_jubjub::GENERATOR;
let p2 = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let circuit = DummyCircuit::new(p1, p2.into());

prover
.prove(rng, &circuit)
.expect_err("prover should fail because the points are not equal");
}

// Test:
// assertion of points with different x-coordinates fails
{
let p1 = JubJubAffine::from_raw_unchecked(
BlsScalar::one(),
BlsScalar::one(),
);
let p2 = JubJubAffine::from_raw_unchecked(
BlsScalar::zero(),
BlsScalar::one(),
);
let circuit = DummyCircuit::new(p1, p2);

prover
.prove(rng, &circuit)
.expect_err("prover should fail because the x-coordinates of the points are not equal");
}

// Test:
// assertion of points with different y-coordinates fails
{
let p1 = JubJubAffine::from_raw_unchecked(
BlsScalar::one(),
BlsScalar::one(),
);
let p2 = JubJubAffine::from_raw_unchecked(
BlsScalar::one(),
BlsScalar::zero(),
);
let circuit = DummyCircuit::new(p1, p2);

prover
.prove(rng, &circuit)
.expect_err("prover should fail because the y-coordinates of the points are not equal");
}
}

0 comments on commit c162d2b

Please sign in to comment.