Skip to content

Commit

Permalink
circuits: Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed May 22, 2024
1 parent 5c80b5d commit b4617bd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
4 changes: 4 additions & 0 deletions circuits/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Change the gadget input to match the order of the circuits public inputs [#177]
- Update `dusk-poseidon` to v0.39 [#179]
- Update `jubjub-schnorr` to v0.4 [#179]
- Update `poseidon-merkle` to v0.6 [#179]

<!-- ISSUES -->
[#179]: https://github.com/dusk-network/phoenix/issues/179
[#177]: https://github.com/dusk-network/phoenix/issues/177
[#171]: https://github.com/dusk-network/phoenix/issues/171
[#169]: https://github.com/dusk-network/phoenix/issues/169
Expand Down
6 changes: 3 additions & 3 deletions circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ exclude = [".github/workflows/dusk-ci.yml", ".gitignore"]
phoenix-core = { path = "../core" }
dusk-plonk = { version = "0.19", default-features = false }
dusk-jubjub = { version = "0.14", default-features = false }
poseidon-merkle = { version = "0.5", features = ["rkyv-impl", "zk", "size_32"] }
dusk-poseidon = { version = "0.33", default-features = false }
jubjub-schnorr = { version = "0.3", default-features = false, features = ["double", "alloc"] }
poseidon-merkle = { version = "0.6", features = ["rkyv-impl", "zk", "size_32"] }
dusk-poseidon = { version = "0.39", features = ["zk"] }
jubjub-schnorr = { version = "0.4", features = ["zk"] }
rand_core = { version = "0.6", default-features = false }
rand = "0.8"

Expand Down
55 changes: 26 additions & 29 deletions circuits/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dusk_jubjub::{
JubJubScalar, GENERATOR, GENERATOR_NUMS, GENERATOR_NUMS_EXTENDED,
};
use dusk_plonk::prelude::*;
use dusk_poseidon::sponge;
use dusk_poseidon::{Domain, Hash, HashGadget};
use jubjub_schnorr::{gadgets, SignatureDouble};
use poseidon_merkle::{zk::opening_gadget, Item, Opening, Tree};

Expand All @@ -25,8 +25,8 @@ const TX_OUTPUT_NOTES: usize = 2;
/// Struct representing a note willing to be spent, in a way
/// suitable for being introduced in the transfer circuit
#[derive(Debug, Clone)]
pub struct TxInputNote<const H: usize, const A: usize> {
pub(crate) merkle_opening: Opening<(), H, A>,
pub struct TxInputNote<const H: usize> {
pub(crate) merkle_opening: Opening<(), H>,
pub(crate) note: Note,
pub(crate) note_pk_p: JubJubAffine,
pub(crate) value: u64,
Expand All @@ -49,15 +49,15 @@ struct WitnessTxInputNote {
signature_r_p: WitnessPoint,
}

impl<const H: usize, const A: usize> TxInputNote<H, A> {
impl<const H: usize> TxInputNote<H> {
/// Create a tx input note
pub fn new(
note: &Note,
merkle_opening: poseidon_merkle::Opening<(), H, A>,
merkle_opening: poseidon_merkle::Opening<(), H>,
sk: &SecretKey,
skeleteon_hash: BlsScalar,
rng: &mut (impl RngCore + CryptoRng),
) -> Result<crate::transaction::TxInputNote<H, A>, PhoenixError> {
) -> Result<crate::transaction::TxInputNote<H>, PhoenixError> {
let note_sk = sk.gen_note_sk(note);
let note_pk_p =
JubJubAffine::from(GENERATOR_NUMS_EXTENDED * note_sk.as_ref());
Expand All @@ -66,11 +66,10 @@ impl<const H: usize, const A: usize> TxInputNote<H, A> {
let value = note.value(Some(&vk))?;
let blinding_factor = note.blinding_factor(Some(&vk))?;

let nullifier = sponge::hash(&[
note_pk_p.get_u(),
note_pk_p.get_v(),
(*note.pos()).into(),
]);
let nullifier = Hash::digest(
Domain::Other,
&[note_pk_p.get_u(), note_pk_p.get_v(), (*note.pos()).into()],
)[0];

let signature = note_sk.sign_double(rng, skeleteon_hash);

Expand Down Expand Up @@ -189,11 +188,11 @@ impl TxOutputNote {
/// - `[output_value_commitment; 2]`
/// - `max_fee`
/// - `crossover`
pub fn gadget<const H: usize, const A: usize, const I: usize>(
pub fn gadget<const H: usize, const I: usize>(
composer: &mut Composer,
skeleton_hash: &BlsScalar,
root: &BlsScalar,
tx_input_notes: &[TxInputNote<H, A>; I],
tx_input_notes: &[TxInputNote<H>; I],
tx_output_notes: &[TxOutputNote; TX_OUTPUT_NOTES],
max_fee: u64,
crossover: u64,
Expand All @@ -220,14 +219,15 @@ pub fn gadget<const H: usize, const A: usize, const I: usize>(
)?;

// COMPUTE AND ASSERT THE NULLIFIER
let nullifier = sponge::gadget(
let nullifier = HashGadget::digest(
composer,
Domain::Other,
&[
*w_tx_input_note.note_pk_p.x(),
*w_tx_input_note.note_pk_p.y(),
w_tx_input_note.pos,
],
);
)[0];
composer.assert_equal(nullifier, w_tx_input_note.nullifier);

// PERFORM A RANGE CHECK ([0, 2^64 - 1]) ON THE VALUE OF THE NOTE
Expand All @@ -251,8 +251,9 @@ pub fn gadget<const H: usize, const A: usize, const I: usize>(
let value_commitment = composer.component_add_point(pc_1, pc_2);

// COMPUTE THE NOTE HASH
let note_hash = sponge::gadget(
let note_hash = HashGadget::digest(
composer,
Domain::Other,
&[
w_tx_input_note.note_type,
*value_commitment.x(),
Expand All @@ -261,7 +262,7 @@ pub fn gadget<const H: usize, const A: usize, const I: usize>(
*w_tx_input_note.note_pk.y(),
w_tx_input_note.pos,
],
);
)[0];

// VERIFY THE MERKLE OPENING
let root =
Expand Down Expand Up @@ -323,25 +324,23 @@ pub fn gadget<const H: usize, const A: usize, const I: usize>(

/// Declaration of the transaction circuit calling the [`gadget`].
#[derive(Debug)]
pub struct TxCircuit<const H: usize, const A: usize, const I: usize> {
tx_input_notes: [TxInputNote<H, A>; I],
pub struct TxCircuit<const H: usize, const I: usize> {
tx_input_notes: [TxInputNote<H>; I],
tx_output_notes: [TxOutputNote; TX_OUTPUT_NOTES],
skeleton_hash: BlsScalar,
root: BlsScalar,
crossover: u64,
max_fee: u64,
}

impl<const H: usize, const A: usize, const I: usize> Default
for TxCircuit<H, A, I>
{
impl<const H: usize, const I: usize> Default for TxCircuit<H, I> {
fn default() -> Self {
let mut rng = StdRng::seed_from_u64(0xbeef);

let sk = SecretKey::random(&mut rng);
let vk = ViewKey::from(&sk);

let mut tree = Tree::<(), H, A>::new();
let mut tree = Tree::<(), H>::new();
let skeleton_hash = BlsScalar::default();

let mut tx_input_notes = Vec::new();
Expand Down Expand Up @@ -388,10 +387,10 @@ impl<const H: usize, const A: usize, const I: usize> Default
}
}

impl<const H: usize, const A: usize, const I: usize> TxCircuit<H, A, I> {
impl<const H: usize, const I: usize> TxCircuit<H, I> {
/// Create a new transfer circuit
pub fn new(
tx_input_notes: [TxInputNote<H, A>; I],
tx_input_notes: [TxInputNote<H>; I],
tx_output_notes: [TxOutputNote; TX_OUTPUT_NOTES],
skeleton_hash: BlsScalar,
root: BlsScalar,
Expand All @@ -409,11 +408,9 @@ impl<const H: usize, const A: usize, const I: usize> TxCircuit<H, A, I> {
}
}

impl<const H: usize, const A: usize, const I: usize> Circuit
for TxCircuit<H, A, I>
{
impl<const H: usize, const I: usize> Circuit for TxCircuit<H, I> {
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
gadget::<H, A, I>(
gadget::<H, I>(
composer,
&self.skeleton_hash,
&self.root,
Expand Down
19 changes: 9 additions & 10 deletions circuits/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ static LABEL: &[u8; 12] = b"dusk-network";
const CAPACITY: usize = 17; // capacity required for the setup

const HEIGHT: usize = 17;
const ARITY: usize = 4;

struct TestingParameters {
sk: SecretKey,
pp: PublicParameters,
tx_input_notes: [TxInputNote<HEIGHT, ARITY>; 4],
tx_input_notes: [TxInputNote<HEIGHT>; 4],
skeleton_hash: BlsScalar,
root: BlsScalar,
crossover: u64,
Expand All @@ -36,7 +35,7 @@ lazy_static! {
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap();
let sk = SecretKey::random(&mut OsRng);

let mut tree = Tree::<(), HEIGHT, ARITY>::new();
let mut tree = Tree::<(), HEIGHT>::new();
let skeleton_hash = BlsScalar::from(1234u64);

// create and insert into the tree 4 testing tx input notes
Expand All @@ -54,7 +53,7 @@ lazy_static! {
}

fn create_and_insert_test_note(
tree: &mut Tree<(), HEIGHT, ARITY>,
tree: &mut Tree<(), HEIGHT>,
pk: &PublicKey,
pos: u64,
value: u64,
Expand All @@ -73,11 +72,11 @@ fn create_and_insert_test_note(
}

fn create_test_tx_input_notes<const I: usize>(
tree: &mut Tree<(), HEIGHT, ARITY>,
tree: &mut Tree<(), HEIGHT>,
sk: &SecretKey,
skeleton_hash: BlsScalar,
rng: &mut (impl RngCore + CryptoRng),
) -> [TxInputNote<HEIGHT, ARITY>; I] {
) -> [TxInputNote<HEIGHT>; I] {
let pk = PublicKey::from(sk);

let mut notes = Vec::new();
Expand Down Expand Up @@ -122,7 +121,7 @@ fn create_test_tx_output_note(
#[test]
fn test_transfer_circuit_1_2() {
let (prover, verifier) =
Compiler::compile::<TxCircuit<HEIGHT, ARITY, 1>>(&TP.pp, LABEL)
Compiler::compile::<TxCircuit<HEIGHT, 1>>(&TP.pp, LABEL)
.expect("failed to compile circuit");

let input_notes = [TP.tx_input_notes[0].clone()];
Expand Down Expand Up @@ -155,7 +154,7 @@ fn test_transfer_circuit_1_2() {
#[test]
fn test_transfer_circuit_2_2() {
let (prover, verifier) =
Compiler::compile::<TxCircuit<HEIGHT, ARITY, 2>>(&TP.pp, LABEL)
Compiler::compile::<TxCircuit<HEIGHT, 2>>(&TP.pp, LABEL)
.expect("failed to compile circuit");

let input_notes =
Expand Down Expand Up @@ -189,7 +188,7 @@ fn test_transfer_circuit_2_2() {
#[test]
fn test_transfer_circuit_3_2() {
let (prover, verifier) =
Compiler::compile::<TxCircuit<HEIGHT, ARITY, 3>>(&TP.pp, LABEL)
Compiler::compile::<TxCircuit<HEIGHT, 3>>(&TP.pp, LABEL)
.expect("failed to compile circuit");

let input_notes = [
Expand Down Expand Up @@ -226,7 +225,7 @@ fn test_transfer_circuit_3_2() {
#[test]
fn test_transfer_circuit_4_2() {
let (prover, verifier) =
Compiler::compile::<TxCircuit<HEIGHT, ARITY, 4>>(&TP.pp, LABEL)
Compiler::compile::<TxCircuit<HEIGHT, 4>>(&TP.pp, LABEL)
.expect("failed to compile circuit");

// create 2 testing tx output notes
Expand Down

0 comments on commit b4617bd

Please sign in to comment.