Skip to content

Commit

Permalink
core: Add circuit module
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle committed Nov 25, 2024
1 parent 2698bc2 commit ccfafd8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
14 changes: 5 additions & 9 deletions core/benches/license_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,26 @@ use ff::Field;
use phoenix_core::{PublicKey, SecretKey};
use poseidon_merkle::{Item, Tree};

use zk_citadel::{gadgets, License, Request, SessionCookie};
use zk_citadel::{circuit, gadgets, License, Request, SessionCookie};

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

static mut CONSTRAINTS: usize = 0;

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

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

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

#[derive(Default, Debug)]
pub struct LicenseCircuit {
gp: gadgets::GadgetParameters<DEPTH>,
gp: gadgets::GadgetParameters<{ circuit::DEPTH }>,
sc: SessionCookie,
}

impl LicenseCircuit {
pub fn new(gp: &gadgets::GadgetParameters<DEPTH>, sc: &SessionCookie) -> Self {
pub fn new(gp: &gadgets::GadgetParameters<{ circuit::DEPTH }>, sc: &SessionCookie) -> Self {
Self { gp: *gp, sc: *sc }
}
}
Expand All @@ -55,7 +51,7 @@ fn license_circuit_benchmark(crit: &mut Criterion) {
let pk = PublicKey::from(&sk);
let sk_lp = SecretKey::random(&mut OsRng);
let pk_lp = PublicKey::from(&sk_lp);
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();
let pp = PublicParameters::setup(1 << circuit::CAPACITY, &mut OsRng).unwrap();
let (prover, verifier) =
Compiler::compile::<LicenseCircuit>(&pp, LABEL).expect("failed to compile circuit");

Expand All @@ -70,7 +66,7 @@ fn license_circuit_benchmark(crit: &mut Criterion) {
let lic =
License::new(&attr_data, &sk_lp, &req, &mut OsRng).expect("License correctly computed.");

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

let item = Item {
Expand Down
31 changes: 31 additions & 0 deletions core/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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::{gadgets, SessionCookie};
use dusk_plonk::prelude::*;

#[allow(dead_code)]
pub const CAPACITY: usize = 15; // capacity required for the setup
pub const DEPTH: usize = 16; // depth of the n-ary Merkle tree

#[derive(Default, Debug)]
pub struct LicenseCircuit {
gp: gadgets::GadgetParameters<DEPTH>,
sc: SessionCookie,
}

impl LicenseCircuit {
pub fn new(gp: &gadgets::GadgetParameters<DEPTH>, sc: &SessionCookie) -> Self {
Self { gp: *gp, sc: *sc }
}
}

impl Circuit for LicenseCircuit {
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
gadgets::use_license(composer, &self.gp, &self.sc)?;
Ok(())
}
}
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod license;
mod request;
mod session;

pub mod circuit;
pub mod gadgets;

pub use license::License;
Expand Down
34 changes: 6 additions & 28 deletions core/tests/citadel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,14 @@ use phoenix_core::{PublicKey, SecretKey};
use poseidon_merkle::{Item, Tree};
use rand_core::OsRng;

use zk_citadel::{gadgets, License, Request, Session, SessionCookie};
use zk_citadel::{circuit, gadgets, License, Request, Session, SessionCookie};

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

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

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

#[derive(Default, Debug)]
pub struct LicenseCircuit {
gp: gadgets::GadgetParameters<DEPTH>,
sc: SessionCookie,
}

impl LicenseCircuit {
pub fn new(gp: &gadgets::GadgetParameters<DEPTH>, sc: &SessionCookie) -> Self {
Self { gp: *gp, sc: *sc }
}
}

impl Circuit for LicenseCircuit {
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
gadgets::use_license(composer, &self.gp, &self.sc)?;
Ok(())
}
}

#[test]
fn test_full_citadel() {
// These are the keys of the user
Expand All @@ -53,9 +31,9 @@ fn test_full_citadel() {
let pk_lp = PublicKey::from(&sk_lp);

// Now we generate the ProverKey and VerifierKey for the license circuit
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();
let (prover, verifier) =
Compiler::compile::<LicenseCircuit>(&pp, LABEL).expect("failed to compile circuit");
let pp = PublicParameters::setup(1 << circuit::CAPACITY, &mut OsRng).unwrap();
let (prover, verifier) = Compiler::compile::<circuit::LicenseCircuit>(&pp, LABEL)
.expect("failed to compile circuit");

// To use Citadel, the user first computes these values and requests a License
let lsa = pk.gen_stealth_address(&JubJubScalar::random(&mut OsRng));
Expand All @@ -70,7 +48,7 @@ fn test_full_citadel() {
let lic =
License::new(&attr_data, &sk_lp, &req, &mut OsRng).expect("License correctly computed.");

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

let item = Item {
Expand Down Expand Up @@ -98,7 +76,7 @@ fn test_full_citadel() {
.expect("Parameters computed correctly.");

let (proof, public_inputs) = prover
.prove(&mut OsRng, &LicenseCircuit::new(&gp, &sc))
.prove(&mut OsRng, &circuit::LicenseCircuit::new(&gp, &sc))
.expect("failed to prove");

// The network verifies the proof received from the user
Expand Down

0 comments on commit ccfafd8

Please sign in to comment.