use dusk_plonk::prelude::*;
use rand::rngs::StdRng;
use rand::SeedableRng;
mod common;
use common::{check_satisfied_circuit, check_unsatisfied_circuit};
#[test]
fn assert_equal_point() {
pub struct TestCircuit {
p1: JubJubAffine,
p2: JubJubAffine,
}
impl TestCircuit {
pub fn new(p1: JubJubAffine, p2: JubJubAffine) -> Self {
Self { p1, p2 }
}
}
impl Default for TestCircuit {
fn default() -> Self {
Self {
p1: dusk_jubjub::GENERATOR,
p2: dusk_jubjub::GENERATOR,
}
}
}
impl Circuit for TestCircuit {
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
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 label = b"assert_equal_point";
let rng = &mut StdRng::seed_from_u64(0xdecaf);
let capacity = 1 << 4;
let pp = PublicParameters::setup(capacity, rng)
.expect("Creation of public parameter shouldn't fail");
let (prover, verifier) = Compiler::compile::<TestCircuit>(&pp, label)
.expect("Circuit should compile");
let msg = "Default circuit verification should pass";
let circuit = TestCircuit::default();
let pi = vec![];
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg);
let msg = "Circuit verification with equal points should pass";
let scalar = JubJubScalar::from(42u64);
let p1 = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let p2 = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let circuit = TestCircuit::new(p1.into(), p2.into());
let pi = vec![];
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg);
let msg = "prover should fail because the points are not equal";
let scalar = JubJubScalar::from(42u64);
let p1 = dusk_jubjub::GENERATOR;
let p2 = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let circuit = TestCircuit::new(p1, p2.into());
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
let msg = "prover should fail because the x-coordinates of the points are not equal";
let p1 =
JubJubAffine::from_raw_unchecked(BlsScalar::one(), BlsScalar::one());
let p2 =
JubJubAffine::from_raw_unchecked(BlsScalar::zero(), BlsScalar::one());
let circuit = TestCircuit::new(p1, p2);
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
let msg = "prover should fail because the y-coordinates of the points are not equal";
let p1 =
JubJubAffine::from_raw_unchecked(BlsScalar::one(), BlsScalar::one());
let p2 =
JubJubAffine::from_raw_unchecked(BlsScalar::one(), BlsScalar::zero());
let circuit = TestCircuit::new(p1, p2);
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
}
#[test]
fn assert_equal_public_point() {
pub struct TestCircuit {
point: JubJubAffine,
public: JubJubAffine,
}
impl TestCircuit {
pub fn new(point: JubJubAffine, public: JubJubAffine) -> Self {
Self { point, public }
}
}
impl Default for TestCircuit {
fn default() -> Self {
Self {
point: dusk_jubjub::GENERATOR,
public: dusk_jubjub::GENERATOR,
}
}
}
impl Circuit for TestCircuit {
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
let w_point = composer.append_point(self.point);
composer.assert_equal_public_point(w_point, self.public);
Ok(())
}
}
let label = b"assert_equal_public_point";
let rng = &mut StdRng::seed_from_u64(0xfeed);
let capacity = 1 << 4;
let pp = PublicParameters::setup(capacity, rng)
.expect("Creation of public parameter shouldn't fail");
let (prover, verifier) = Compiler::compile::<TestCircuit>(&pp, label)
.expect("Circuit should compile");
let msg = "Default circuit verification should pass";
let circuit = TestCircuit::default();
let pi = vec![
dusk_jubjub::GENERATOR.get_u(),
dusk_jubjub::GENERATOR.get_v(),
];
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg);
let msg = "Circuit verification with equal points should pass";
let scalar = JubJubScalar::from(42u64);
let point = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let public = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let circuit = TestCircuit::new(point.into(), public.into());
let public_affine: JubJubAffine = public.into();
let pi = vec![public_affine.get_u(), public_affine.get_v()];
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg);
let msg = "prover should fail because the points are not equal";
let scalar = JubJubScalar::from(42u64);
let point = dusk_jubjub::GENERATOR;
let public = dusk_jubjub::GENERATOR_EXTENDED * &scalar;
let circuit = TestCircuit::new(point, public.into());
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
let msg = "prover should fail because the x-coordinates of the points are not equal";
let point =
JubJubAffine::from_raw_unchecked(BlsScalar::one(), BlsScalar::one());
let public =
JubJubAffine::from_raw_unchecked(BlsScalar::zero(), BlsScalar::one());
let circuit = TestCircuit::new(point, public);
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
let msg = "prover should fail because the y-coordinates of the points are not equal";
let point =
JubJubAffine::from_raw_unchecked(BlsScalar::one(), BlsScalar::one());
let public =
JubJubAffine::from_raw_unchecked(BlsScalar::one(), BlsScalar::zero());
let circuit = TestCircuit::new(point, public);
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
}