Skip to content

Commit

Permalink
Merge pull request #71 from dusk-network/main-rc
Browse files Browse the repository at this point in the history
Reuse test code to eliminate repetition
  • Loading branch information
miloszm authored Jul 12, 2023
2 parents 6484496 + b3f903c commit 99f8a64
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 136 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add Utils implementation

### Changed

- Updated poseidon_merkle from 0.2 to 0.2.1-rc.0

## [0.4.0] - 2023-06-28

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MPL-2.0"
[dependencies]
dusk-bytes = "0.1"
dusk-poseidon = { version = "0.30", default-features = false, features = ["rkyv-impl", "alloc"] }
poseidon-merkle = { version = "0.2", features = ["rkyv-impl", "zk", "size_32"] }
poseidon-merkle = { version = "0.2.1-rc.0", features = ["rkyv-impl", "zk", "size_32"] }
dusk-plonk = { version = "0.14", default-features = false, features = ["rkyv-impl", "alloc"] }
dusk-bls12_381 = { version = "0.11", default-features = false }
dusk-jubjub = { version = "0.12", default-features = false }
Expand Down
78 changes: 16 additions & 62 deletions benches/citadel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_jubjub::GENERATOR_EXTENDED;

use poseidon_merkle::{Item, Tree};

use dusk_pki::{PublicSpendKey, SecretSpendKey};
use dusk_plonk::prelude::*;
use dusk_poseidon::sponge;

use zk_citadel::gadgets;
use zk_citadel::license::{
CitadelProverParameters, License, Request, SessionCookie, ShelterProverParameters,
};
use zk_citadel::license::{CitadelProverParameters, SessionCookie, ShelterProverParameters};

use criterion::{criterion_group, criterion_main, Criterion};
use rand_core::{CryptoRng, OsRng, RngCore};
use rand_core::OsRng;
use zk_citadel::utils::CitadelUtils;

static mut CONSTRAINTS_CITADEL: usize = 0;
static mut CONSTRAINTS_SHELTER: usize = 0;
Expand All @@ -33,8 +27,7 @@ const ARITY: usize = 4; // arity of the Merkle tree
#[macro_use]
extern crate lazy_static;

// Example values
const USER_ATTRIBUTES: u64 = 112233445566778899u64;
// Example value
const CHALLENGE: u64 = 20221126u64;

pub struct Keys {
Expand Down Expand Up @@ -122,36 +115,14 @@ impl Circuit for Shelter {
}
}

fn compute_random_license<R: RngCore + CryptoRng>(rng: &mut R) -> License {
// First, the user computes these values and requests a License
let lsa = KEYS.psk.gen_stealth_address(&JubJubScalar::random(rng));
let lsk = KEYS.ssk.sk_r(&lsa);
let k_lic =
JubJubAffine::from(GENERATOR_EXTENDED * sponge::truncated::hash(&[(*lsk.as_ref()).into()]));
let req = Request::new(&KEYS.psk_lp, &lsa, &k_lic, rng);

// Second, the LP computes these values and grants the License
let attr = JubJubScalar::from(USER_ATTRIBUTES);
let mut lic = License::new(&attr, &KEYS.ssk_lp, &req, rng);
lic.pos = 0;

lic
}

fn shelter_benchmark(crit: &mut Criterion) {
let lic = compute_random_license(&mut OsRng);

let mut tree = Tree::<(), DEPTH_SHELTER, ARITY>::new();
let lpk = JubJubAffine::from(lic.lsa.pk_r().as_ref());

let item = Item {
hash: sponge::hash(&[lpk.get_x(), lpk.get_y()]),
data: (),
};

tree.insert(lic.pos, item);

let merkle_proof = tree.opening(lic.pos).expect("Tree was read successfully");
let (lic, merkle_proof) = CitadelUtils::compute_random_license::<OsRng, DEPTH_SHELTER, ARITY>(
&mut OsRng,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
);

let c = JubJubScalar::from(CHALLENGE);
let spp = ShelterProverParameters::compute_parameters(
Expand Down Expand Up @@ -189,29 +160,12 @@ fn shelter_benchmark(crit: &mut Criterion) {
}

fn citadel_benchmark(crit: &mut Criterion) {
let lic = compute_random_license(&mut OsRng);

let mut tree = Tree::<(), DEPTH_CITADEL, ARITY>::new();
let lpk = JubJubAffine::from(lic.lsa.pk_r().as_ref());

let item = Item {
hash: sponge::hash(&[lpk.get_x(), lpk.get_y()]),
data: (),
};

tree.insert(lic.pos, item);

let merkle_proof = tree.opening(lic.pos).expect("Tree was read successfully");

let c = JubJubScalar::from(CHALLENGE);
let (cpp, sc) = CitadelProverParameters::compute_parameters(
&KEYS.ssk,
&lic,
&KEYS.psk_lp,
&KEYS.psk_lp,
&c,
let (cpp, sc) = CitadelUtils::compute_citadel_parameters::<OsRng, DEPTH_CITADEL, ARITY>(
&mut OsRng,
merkle_proof,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
);

unsafe {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

pub mod gadgets;
pub mod license;
pub mod utils;
86 changes: 86 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::license::{CitadelProverParameters, License, Request, SessionCookie};
use dusk_jubjub::{JubJubAffine, JubJubScalar, GENERATOR_EXTENDED};
use dusk_pki::{PublicSpendKey, SecretSpendKey};
use dusk_poseidon::sponge;
use poseidon_merkle::{Item, Opening, Tree};
use rand_core::{CryptoRng, RngCore};

// Example values
const USER_ATTRIBUTES: u64 = 112233445566778899u64;
const CHALLENGE: u64 = 20221126u64;

pub struct CitadelUtils {}

impl CitadelUtils {
pub fn compute_random_license<
R: RngCore + CryptoRng,
const DEPTH: usize,
const ARITY: usize,
>(
rng: &mut R,
ssk: SecretSpendKey,
psk: PublicSpendKey,
ssk_lp: SecretSpendKey,
psk_lp: PublicSpendKey,
) -> (License, Opening<(), DEPTH, ARITY>) {
// First, the user computes these values and requests a License
let lsa = psk.gen_stealth_address(&JubJubScalar::random(rng));
let lsk = ssk.sk_r(&lsa);
let k_lic = JubJubAffine::from(
GENERATOR_EXTENDED * sponge::truncated::hash(&[(*lsk.as_ref()).into()]),
);
let req = Request::new(&psk_lp, &lsa, &k_lic, rng);

// Second, the LP computes these values and grants the License
let attr = JubJubScalar::from(USER_ATTRIBUTES);
let mut lic = License::new(&attr, &ssk_lp, &req, rng);

let mut tree = Tree::<(), DEPTH, ARITY>::new();
let lpk = JubJubAffine::from(lic.lsa.pk_r().as_ref());

let item = Item {
hash: sponge::hash(&[lpk.get_x(), lpk.get_y()]),
data: (),
};

lic.pos = 0;
tree.insert(lic.pos, item);

let merkle_proof = tree.opening(lic.pos).expect("Tree was read successfully");

(lic, merkle_proof)
}

pub fn compute_citadel_parameters<
R: RngCore + CryptoRng,
const DEPTH: usize,
const ARITY: usize,
>(
rng: &mut R,
ssk: SecretSpendKey,
psk: PublicSpendKey,
ssk_lp: SecretSpendKey,
psk_lp: PublicSpendKey,
) -> (CitadelProverParameters<DEPTH, ARITY>, SessionCookie) {
let (lic, merkle_proof) =
Self::compute_random_license::<R, DEPTH, ARITY>(rng, ssk, psk, ssk_lp, psk_lp);

let c = JubJubScalar::from(CHALLENGE);
let (cpp, sc) = CitadelProverParameters::compute_parameters(
&ssk,
&lic,
&psk_lp,
&psk_lp,
&c,
rng,
merkle_proof,
);
(cpp, sc)
}
}
106 changes: 33 additions & 73 deletions tests/citadel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_jubjub::GENERATOR_EXTENDED;

use poseidon_merkle::{Item, Opening, Tree};

use dusk_pki::{PublicSpendKey, SecretSpendKey};
use dusk_plonk::prelude::*;
use dusk_poseidon::sponge;

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

Expand All @@ -20,16 +15,16 @@ const ARITY: usize = 4; // arity of the Merkle tree

use zk_citadel::gadgets;
use zk_citadel::license::{
CitadelProverParameters, License, Request, Session, SessionCookie, ShelterProverParameters,
CitadelProverParameters, Session, SessionCookie, ShelterProverParameters,
};

use rand_core::{CryptoRng, OsRng, RngCore};
use rand_core::OsRng;
use zk_citadel::utils::CitadelUtils;

#[macro_use]
extern crate lazy_static;

// Example values
const USER_ATTRIBUTES: u64 = 112233445566778899u64;
// Example value
const CHALLENGE: u64 = 20221126u64;

pub struct Keys {
Expand Down Expand Up @@ -112,51 +107,14 @@ impl Circuit for Shelter {
}
}

fn compute_random_license<R: RngCore + CryptoRng>(
rng: &mut R,
) -> (License, Opening<(), DEPTH, ARITY>) {
// First, the user computes these values and requests a License
let lsa = KEYS.psk.gen_stealth_address(&JubJubScalar::random(rng));
let lsk = KEYS.ssk.sk_r(&lsa);
let k_lic =
JubJubAffine::from(GENERATOR_EXTENDED * sponge::truncated::hash(&[(*lsk.as_ref()).into()]));
let req = Request::new(&KEYS.psk_lp, &lsa, &k_lic, rng);

// Second, the LP computes these values and grants the License
let attr = JubJubScalar::from(USER_ATTRIBUTES);
let mut lic = License::new(&attr, &KEYS.ssk_lp, &req, rng);

let mut tree = Tree::<(), DEPTH, ARITY>::new();
let lpk = JubJubAffine::from(lic.lsa.pk_r().as_ref());

let item = Item {
hash: sponge::hash(&[lpk.get_x(), lpk.get_y()]),
data: (),
};

lic.pos = 0;
tree.insert(lic.pos, item);

let merkle_proof = tree.opening(lic.pos).expect("Tree was read successfully");

(lic, merkle_proof)
}

#[test]
fn test_full_citadel() {
// We generate a random license and merkle proof for testing
let (lic, merkle_proof) = compute_random_license(&mut OsRng);

// The user computes these values to use a license
let c = JubJubScalar::from(CHALLENGE);
let (cpp, sc) = CitadelProverParameters::compute_parameters(
&KEYS.ssk,
&lic,
&KEYS.psk_lp,
&KEYS.psk_lp,
&c,
let (cpp, sc) = CitadelUtils::compute_citadel_parameters::<OsRng, DEPTH, ARITY>(
&mut OsRng,
merkle_proof,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
);

// Then, the user generates the proof
Expand All @@ -179,7 +137,13 @@ fn test_full_citadel() {
#[test]
fn test_full_shelter() {
// We generate a random license and merkle proof for testing
let (lic, merkle_proof) = compute_random_license(&mut OsRng);
let (lic, merkle_proof) = CitadelUtils::compute_random_license::<OsRng, DEPTH, ARITY>(
&mut OsRng,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
);

// The user computes these values to use a license
let c = JubJubScalar::from(CHALLENGE);
Expand All @@ -206,17 +170,12 @@ fn test_full_shelter() {
#[test]
#[should_panic]
fn test_citadel_false_public_input() {
let (lic, merkle_proof) = compute_random_license(&mut OsRng);

let c = JubJubScalar::from(CHALLENGE);
let (cpp, sc) = CitadelProverParameters::compute_parameters(
&KEYS.ssk,
&lic,
&KEYS.psk_lp,
&KEYS.psk_lp,
&c,
let (cpp, sc) = CitadelUtils::compute_citadel_parameters::<OsRng, DEPTH, ARITY>(
&mut OsRng,
merkle_proof,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
);

let (proof, public_inputs) = KEYS
Expand All @@ -236,7 +195,13 @@ fn test_citadel_false_public_input() {
#[test]
#[should_panic]
fn test_shelter_false_public_input() {
let (lic, merkle_proof) = compute_random_license(&mut OsRng);
let (lic, merkle_proof) = CitadelUtils::compute_random_license::<OsRng, DEPTH, ARITY>(
&mut OsRng,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
);

let c = JubJubScalar::from(CHALLENGE);
let spp = ShelterProverParameters::compute_parameters(
Expand Down Expand Up @@ -264,17 +229,12 @@ fn test_shelter_false_public_input() {
#[test]
#[should_panic]
fn test_citadel_false_session_cookie() {
let (lic, merkle_proof) = compute_random_license(&mut OsRng);

let c = JubJubScalar::from(CHALLENGE);
let (cpp, sc) = CitadelProverParameters::compute_parameters(
&KEYS.ssk,
&lic,
&KEYS.psk_lp,
&KEYS.psk_lp,
&c,
let (cpp, sc) = CitadelUtils::compute_citadel_parameters::<OsRng, DEPTH, ARITY>(
&mut OsRng,
merkle_proof,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
);

let (_proof, public_inputs) = KEYS
Expand Down

0 comments on commit 99f8a64

Please sign in to comment.