Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
- Update jubjub-schnorr and phoenix-core dependencies
- Remove utils module since it is only used for tests
  • Loading branch information
moCello committed Apr 25, 2024
1 parent 5528344 commit 4000adf
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 179 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Update `jubjub-schnorr` dependency to "0.2"
- Update `phoenix-core` dependency to "0.26"

### Removed

- Remove `utils` module as it was only used for testing

## [0.11.0] - 2024-04-10

### Changed
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ dusk-plonk = { version = "0.19", default-features = false, features = ["rkyv-imp
dusk-bls12_381 = { version = "0.13", default-features = false, features = ["rkyv-impl", "alloc"] }
dusk-jubjub = { version = "0.14", default-features = false, features = ["rkyv-impl", "alloc"] }
ff = { version = "0.13", default-features = false }
jubjub-schnorr = { version = "0.2", features = ["rkyv-impl", "alloc", "double"] }
phoenix-core = { version = "0.26", features = ["rkyv-impl", "alloc"] }
jubjub-schnorr = { version = "0.3", features = ["rkyv-impl", "alloc", "double"] }
phoenix-core = { version = "0.27", features = ["rkyv-impl", "alloc"] }
rand_core = { version = "0.6", default-features=false, features = ["getrandom"] }
nstack = { version = "0.16" }
rkyv = { version = "0.7", default-features = false }
Expand Down
111 changes: 83 additions & 28 deletions benches/citadel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,119 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_jubjub::{JubJubAffine, JubJubScalar, GENERATOR_EXTENDED};
use dusk_plonk::prelude::*;
use phoenix_core::{PublicKey as PublicSpendKey, SecretKey as SecretSpendKey};
use dusk_poseidon::sponge;
use ff::Field;
use phoenix_core::{PublicKey, SecretKey};
use poseidon_merkle::{Item, Opening, Tree};

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

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

static mut CONSTRAINTS_CITADEL: usize = 0;

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

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

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

#[macro_use]
extern crate lazy_static;

pub struct Keys {
ssk: SecretSpendKey,
psk: PublicSpendKey,
fn compute_random_license(
rng: &mut OsRng,
sk: &SecretKey,
sk_lp: &SecretKey,
pk_lp: &PublicKey,
) -> (License, Opening<(), DEPTH, ARITY>) {
let pk = PublicKey::from(sk);

// First, the user computes these values and requests a License
let lsa = pk.gen_stealth_address(&JubJubScalar::random(&mut *rng));
let lsk = sk.gen_note_sk(&lsa);
let k_lic =
JubJubAffine::from(GENERATOR_EXTENDED * sponge::truncated::hash(&[(*lsk.as_ref()).into()]));
let req = Request::new(pk_lp, &lsa, &k_lic, rng);

// Second, the LP computes these values and grants the License
let attr_data = JubJubScalar::from(ATTRIBUTE_DATA);
let lic = License::new(&attr_data, sk_lp, &req, rng);

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

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

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

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

(lic, merkle_proof)
}

fn compute_citadel_parameters(
rng: &mut OsRng,
sk: &SecretKey,
pk_lp: &PublicKey,
lic: &License,
merkle_proof: Opening<(), DEPTH, ARITY>,
) -> (CitadelProverParameters<DEPTH, ARITY>, SessionCookie) {
let c = JubJubScalar::from(CHALLENGE);
let (cpp, sc) =
CitadelProverParameters::compute_parameters(sk, lic, pk_lp, pk_lp, &c, rng, merkle_proof);
(cpp, sc)
}

struct Keys {
sk: SecretKey,

ssk_lp: SecretSpendKey,
psk_lp: PublicSpendKey,
sk_lp: SecretKey,
pk_lp: PublicKey,

citadel_prover: Prover,
citadel_verifier: Verifier,
}

lazy_static! {
static ref KEYS: Keys = {
static ref TEST_KEYS: Keys = {
// These are the keys of the user
let ssk = SecretSpendKey::random(&mut OsRng);
let psk = PublicSpendKey::from(ssk);
let sk = SecretKey::random(&mut OsRng);

// These are the keys of the LP
let ssk_lp = SecretSpendKey::random(&mut OsRng);
let psk_lp = PublicSpendKey::from(ssk_lp);
let sk_lp = SecretKey::random(&mut OsRng);
let pk_lp = PublicKey::from(&sk_lp);

// Now we generate the ProverKey and VerifierKey for Citadel
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();

let (citadel_prover, citadel_verifier) =
Compiler::compile::<Citadel>(&pp, LABEL).expect("failed to compile circuit");

Keys { ssk, psk, ssk_lp, psk_lp, citadel_prover, citadel_verifier }
Keys { sk, sk_lp, pk_lp, citadel_prover, citadel_verifier }
};
}

#[derive(Default, Debug)]
pub struct Citadel {
cpp: CitadelProverParameters<DEPTH_CITADEL, ARITY>,
cpp: CitadelProverParameters<DEPTH, ARITY>,
sc: SessionCookie,
}

impl Citadel {
pub fn new(cpp: &CitadelProverParameters<DEPTH_CITADEL, ARITY>, sc: &SessionCookie) -> Self {
pub fn new(cpp: &CitadelProverParameters<DEPTH, ARITY>, sc: &SessionCookie) -> Self {
Self { cpp: *cpp, sc: *sc }
}
}
Expand All @@ -78,18 +132,17 @@ impl Circuit for Citadel {
}

fn citadel_benchmark(crit: &mut Criterion) {
let (lic, merkle_proof) = CitadelUtils::compute_random_license::<OsRng, DEPTH_CITADEL, ARITY>(
let (lic, merkle_proof) = compute_random_license(
&mut OsRng,
KEYS.ssk,
KEYS.psk,
KEYS.ssk_lp,
KEYS.psk_lp,
&TEST_KEYS.sk,
&TEST_KEYS.sk_lp,
&TEST_KEYS.pk_lp,
);

let (cpp, sc) = CitadelUtils::compute_citadel_parameters::<OsRng, DEPTH_CITADEL, ARITY>(
let (cpp, sc) = compute_citadel_parameters(
&mut OsRng,
KEYS.ssk,
KEYS.psk_lp,
&TEST_KEYS.sk,
&TEST_KEYS.pk_lp,
&lic,
merkle_proof,
);
Expand All @@ -98,21 +151,23 @@ fn citadel_benchmark(crit: &mut Criterion) {
let log = &format!("Citadel Prover ({} constraints)", CONSTRAINTS_CITADEL);
crit.bench_function(log, |b| {
b.iter(|| {
KEYS.citadel_prover
TEST_KEYS
.citadel_prover
.prove(&mut OsRng, &Citadel::new(&cpp, &sc))
.expect("failed to prove")
})
});

// Benchmark the verifier
let (proof, public_inputs) = KEYS
let (proof, public_inputs) = TEST_KEYS
.citadel_prover
.prove(&mut OsRng, &Citadel::new(&cpp, &sc))
.expect("failed to prove");
let log = &format!("Citadel Verifier ({} constraints)", CONSTRAINTS_CITADEL);
crit.bench_function(log, |b| {
b.iter(|| {
KEYS.citadel_verifier
TEST_KEYS
.citadel_verifier
.verify(&proof, &public_inputs)
.expect("failed to verify proof")
})
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@

pub mod gadgets;
pub mod license;
pub mod utils;
38 changes: 20 additions & 18 deletions src/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use dusk_jubjub::{dhke, GENERATOR_EXTENDED, GENERATOR_NUMS_EXTENDED};
use dusk_poseidon::cipher::PoseidonCipher;
use dusk_poseidon::sponge;
use ff::Field;
use jubjub_schnorr::{PublicKey, SecretKey, Signature, SignatureDouble};
use phoenix_core::{PublicKey as PublicSpendKey, SecretKey as SecretSpendKey, StealthAddress};
use jubjub_schnorr::{
PublicKey as NotePublicKey, SecretKey as NoteSecretKey, Signature, SignatureDouble,
};
use phoenix_core::{PublicKey, SecretKey, StealthAddress};
use poseidon_merkle::{Item, Opening, Tree};
use rand_core::{CryptoRng, RngCore};

Expand All @@ -37,7 +39,7 @@ pub struct Request {

impl Request {
pub fn new<R: RngCore + CryptoRng>(
psk_lp: &PublicSpendKey,
pk_lp: &PublicKey,
lsa: &StealthAddress,
k_lic: &JubJubAffine,
mut rng: &mut R,
Expand All @@ -46,12 +48,12 @@ impl Request {
let nonce_2 = BlsScalar::random(&mut rng);
let nonce_3 = BlsScalar::random(&mut rng);

let lpk = JubJubAffine::from(*lsa.pk_r().as_ref());
let lpk = JubJubAffine::from(*lsa.note_pk().as_ref());
let r = JubJubAffine::from(*lsa.R());

let r_dh = JubJubScalar::random(rng);
let rsa = psk_lp.gen_stealth_address(&r_dh);
let k_dh = dhke(&r_dh, psk_lp.A());
let rsa = pk_lp.gen_stealth_address(&r_dh);
let k_dh = dhke(&r_dh, pk_lp.A());

let enc_1 = PoseidonCipher::encrypt(&[lpk.get_u(), lpk.get_v()], &k_dh, &nonce_1);

Expand Down Expand Up @@ -185,11 +187,11 @@ pub struct License {
impl License {
pub fn new<R: RngCore + CryptoRng>(
attr_data: &JubJubScalar,
ssk_lp: &SecretSpendKey,
sk_lp: &SecretKey,
req: &Request,
mut rng: &mut R,
) -> Self {
let k_dh = dhke(ssk_lp.a(), req.rsa.R());
let k_dh = dhke(sk_lp.a(), req.rsa.R());

let dec_1 = req
.enc_1
Expand All @@ -212,7 +214,7 @@ impl License {

let message = sponge::hash(&[lpk.get_u(), lpk.get_v(), BlsScalar::from(*attr_data)]);

let sig_lic = SecretKey::from(ssk_lp.a()).sign(rng, message);
let sig_lic = NoteSecretKey::from(sk_lp.a()).sign(rng, message);
let sig_lic_r = JubJubAffine::from(sig_lic.R());

let nonce_1 = BlsScalar::random(&mut rng);
Expand All @@ -230,7 +232,7 @@ impl License {
Self {
lsa: StealthAddress::from_raw_unchecked(
JubJubExtended::from(r),
PublicKey::from_raw_unchecked(JubJubExtended::from(lpk)),
NotePublicKey::from_raw_unchecked(JubJubExtended::from(lpk)),
),
enc_1,
nonce_1,
Expand Down Expand Up @@ -288,15 +290,15 @@ impl<const DEPTH: usize, const ARITY: usize> Default for CitadelProverParameters
impl<const DEPTH: usize, const ARITY: usize> CitadelProverParameters<DEPTH, ARITY> {
#[allow(clippy::too_many_arguments)]
pub fn compute_parameters<R: RngCore + CryptoRng>(
ssk: &SecretSpendKey,
sk: &SecretKey,
lic: &License,
psk_lp: &PublicSpendKey,
psk_sp: &PublicSpendKey,
pk_lp: &PublicKey,
pk_sp: &PublicKey,
c: &JubJubScalar,
mut rng: &mut R,
merkle_proof: Opening<(), DEPTH, ARITY>,
) -> (Self, SessionCookie) {
let lsk = ssk.sk_r(&lic.lsa);
let lsk = sk.gen_note_sk(lic.lsa);
let k_lic = JubJubAffine::from(
GENERATOR_EXTENDED * sponge::truncated::hash(&[(*lsk.as_ref()).into()]),
);
Expand All @@ -323,16 +325,16 @@ impl<const DEPTH: usize, const ARITY: usize> CitadelProverParameters<DEPTH, ARIT
)
.unwrap();

let lpk = JubJubAffine::from(*lic.lsa.pk_r().as_ref());
let lpk = JubJubAffine::from(*lic.lsa.note_pk().as_ref());

let lsk = ssk.sk_r(&lic.lsa);
let lsk = sk.gen_note_sk(lic.lsa);
let lpk_p = JubJubAffine::from(GENERATOR_NUMS_EXTENDED * lsk.as_ref());

let s_0 = BlsScalar::random(&mut rng);
let s_1 = JubJubScalar::random(&mut rng);
let s_2 = JubJubScalar::random(&mut rng);

let pk_sp = JubJubAffine::from(*psk_sp.A());
let pk_sp = JubJubAffine::from(*pk_sp.A());
let r = BlsScalar::random(&mut rng);

let session_hash = sponge::hash(&[pk_sp.get_u(), pk_sp.get_v(), r]);
Expand All @@ -341,7 +343,7 @@ impl<const DEPTH: usize, const ARITY: usize> CitadelProverParameters<DEPTH, ARIT

let session_id = sponge::hash(&[lpk_p.get_u(), lpk_p.get_v(), BlsScalar::from(*c)]);

let pk_lp = JubJubAffine::from(*psk_lp.A());
let pk_lp = JubJubAffine::from(*pk_lp.A());

let com_0 = sponge::hash(&[pk_lp.get_u(), pk_lp.get_v(), s_0]);
let com_1 = (GENERATOR_EXTENDED * attr_data) + (GENERATOR_NUMS_EXTENDED * s_1);
Expand Down

0 comments on commit 4000adf

Please sign in to comment.