Skip to content

Commit

Permalink
Merge pull request #56 from dusk-network/test_improve
Browse files Browse the repository at this point in the history
Improve tests efficiency
  • Loading branch information
xevisalle committed Jun 13, 2023
2 parents 6a161c0 + 2582923 commit 83f20b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rkyv = { version = "0.7", optional = true, default-features = false }

[dev-dependencies]
criterion = "0.3"
lazy_static = "1.4"

[[bench]]
name = "citadel"
Expand Down
2 changes: 1 addition & 1 deletion benches/citadel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static LABEL: &[u8; 12] = b"dusk-network";

const CAPACITY: usize = 16; // capacity required for the setup
const DEPTH: usize = 17; // depth of the n-ary Merkle tree
pub const ARITY: usize = 4; // arity of the Merkle tree
const ARITY: usize = 4; // arity of the Merkle tree

#[derive(Default, Debug)]
pub struct Citadel {
Expand Down
47 changes: 28 additions & 19 deletions tests/citadel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@ use dusk_poseidon::sponge;

static LABEL: &[u8; 12] = b"dusk-network";

const CAPACITY: usize = 16; // capacity required for the setup
const DEPTH: usize = 17; // depth of the n-ary Merkle tree
const CAPACITY: usize = 15; // capacity required for the setup
const DEPTH: usize = 9; // depth of the n-ary Merkle tree
const ARITY: usize = 4; // arity of the Merkle tree

use zk_citadel::gadget;
use zk_citadel::license::{License, LicenseProverParameters, Request, Session, SessionCookie};

use rand_core::{CryptoRng, OsRng, RngCore};

#[macro_use]
extern crate lazy_static;

pub struct Keys {
prover: Prover<Citadel>,
verifier: Verifier<Citadel>,
}

lazy_static! {
static ref KEYS: Keys = {
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();
let (prover, verifier) =
Compiler::compile::<Citadel>(&pp, LABEL).expect("failed to compile circuit");

Keys { prover, verifier }
};
}

#[derive(Default, Debug)]
pub struct Citadel {
lpp: LicenseProverParameters<DEPTH, ARITY>,
Expand Down Expand Up @@ -95,16 +113,13 @@ fn compute_random_license<R: RngCore + CryptoRng>(

#[test]
fn test_full_citadel() {
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();
let (prover, verifier) =
Compiler::compile::<Citadel>(&pp, LABEL).expect("failed to compile circuit");

let (_lic, lpp, sc) = compute_random_license(&mut OsRng);
let (proof, public_inputs) = prover
let (proof, public_inputs) = KEYS
.prover
.prove(&mut OsRng, &Citadel::new(&lpp, &sc))
.expect("failed to prove");

verifier
KEYS.verifier
.verify(&proof, &public_inputs)
.expect("failed to verify proof");

Expand All @@ -116,33 +131,27 @@ fn test_full_citadel() {
#[test]
#[should_panic]
fn test_use_license_circuit_false_public_input() {
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();
let (prover, verifier) =
Compiler::compile::<Citadel>(&pp, LABEL).expect("failed to compile circuit");

let (_lic, lpp, sc) = compute_random_license(&mut OsRng);
let (proof, public_inputs) = prover
let (proof, public_inputs) = KEYS
.prover
.prove(&mut OsRng, &Citadel::new(&lpp, &sc))
.expect("failed to prove");

// set a false public input
let mut false_public_inputs = public_inputs;
false_public_inputs[0] = BlsScalar::random(&mut OsRng);

verifier
KEYS.verifier
.verify(&proof, &false_public_inputs)
.expect("failed to verify proof");
}

#[test]
#[should_panic]
fn test_verify_license_false_session_cookie() {
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();
let (prover, _verifier) =
Compiler::compile::<Citadel>(&pp, LABEL).expect("failed to compile circuit");

let (_lic, lpp, sc) = compute_random_license(&mut OsRng);
let (_proof, public_inputs) = prover
let (_proof, public_inputs) = KEYS
.prover
.prove(&mut OsRng, &Citadel::new(&lpp, &sc))
.expect("failed to prove");

Expand Down

0 comments on commit 83f20b2

Please sign in to comment.