-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
244 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// 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 rand_core::{CryptoRng, OsRng, RngCore}; | ||
|
||
use phoenix_core::transfer::{gadget, InputNote, OutputNote}; | ||
use phoenix_core::{Note, PublicKey, SecretKey, ViewKey}; | ||
use poseidon_merkle::{Item, Tree}; | ||
|
||
use dusk_plonk::prelude::*; | ||
|
||
static LABEL: &[u8; 12] = b"dusk-network"; | ||
const CAPACITY: usize = 15; // capacity required for the setup | ||
const OUTPUT: usize = 2; | ||
const H: usize = 2; | ||
const A: usize = 4; | ||
const I: usize = 2; | ||
|
||
#[derive(Debug)] | ||
pub struct TransferCircuit { | ||
input_notes: [InputNote<H, A>; I], | ||
output_notes: [OutputNote; OUTPUT], | ||
skeleton_hash: BlsScalar, | ||
tree: Tree<(), H, A>, | ||
} | ||
|
||
impl Default for TransferCircuit { | ||
fn default() -> Self { | ||
let mut rng = OsRng; | ||
let sk = SecretKey::random(&mut rng); | ||
|
||
let mut tree = Tree::<(), H, A>::new(); | ||
|
||
let input_note_1 = create_test_input_note(&mut tree, 0, &sk, &mut rng); | ||
let input_note_2 = create_test_input_note(&mut tree, 1, &sk, &mut rng); | ||
|
||
let input_notes = [input_note_1, input_note_2]; | ||
|
||
let output_note_1 = create_test_output_note(&sk, &mut rng); | ||
let output_note_2 = create_test_output_note(&sk, &mut rng); | ||
|
||
let output_notes = [output_note_1, output_note_2]; | ||
|
||
let skeleton_hash = BlsScalar::default(); | ||
|
||
Self { | ||
input_notes, | ||
output_notes, | ||
skeleton_hash, | ||
tree, | ||
} | ||
} | ||
} | ||
|
||
impl TransferCircuit { | ||
pub fn new( | ||
input_notes: [InputNote<H, A>; I], | ||
output_notes: [OutputNote; OUTPUT], | ||
skeleton_hash: BlsScalar, | ||
tree: Tree<(), H, A>, | ||
) -> Self { | ||
Self { | ||
input_notes, | ||
output_notes, | ||
skeleton_hash, | ||
tree, | ||
} | ||
} | ||
} | ||
|
||
impl Circuit for TransferCircuit { | ||
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> { | ||
gadget::<H, A, I>( | ||
composer, | ||
&self.input_notes, | ||
&self.output_notes, | ||
self.skeleton_hash, | ||
&self.tree, | ||
); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn create_test_input_note<const H: usize, const A: usize>( | ||
tree: &mut Tree<(), H, A>, | ||
pos: u64, | ||
sk: &SecretKey, | ||
rng: &mut (impl RngCore + CryptoRng), | ||
) -> InputNote<H, A> { | ||
let pk = PublicKey::from(sk); | ||
let value = 25; | ||
|
||
let mut note = Note::transparent(rng, &pk, value); | ||
|
||
note.set_pos(pos); | ||
|
||
let item = Item { | ||
hash: note.hash(), | ||
data: (), | ||
}; | ||
|
||
tree.insert(*note.pos(), item); | ||
|
||
let skeleton_hash = BlsScalar::from(1234u64); | ||
|
||
note.create_input_note(&sk, skeleton_hash, rng) | ||
} | ||
|
||
fn create_test_output_note( | ||
sk: &SecretKey, | ||
rng: &mut (impl RngCore + CryptoRng), | ||
) -> OutputNote { | ||
let note = Note::transparent(rng, &PublicKey::from(sk), 25); | ||
note.create_output_note(&ViewKey::from(sk)) | ||
} | ||
|
||
#[test] | ||
fn test_transfer_circuit() { | ||
let mut rng = OsRng; | ||
let sk = SecretKey::random(&mut rng); | ||
|
||
let mut tree = Tree::<(), H, A>::new(); | ||
|
||
let input_note_1 = create_test_input_note(&mut tree, 0, &sk, &mut rng); | ||
let input_note_2 = create_test_input_note(&mut tree, 1, &sk, &mut rng); | ||
|
||
let input_notes = [input_note_1, input_note_2]; | ||
|
||
let output_note_1 = create_test_output_note(&sk, &mut rng); | ||
let output_note_2 = create_test_output_note(&sk, &mut rng); | ||
|
||
let output_notes = [output_note_1, output_note_2]; | ||
|
||
let skeleton_hash = BlsScalar::from(1234u64); | ||
|
||
let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap(); | ||
|
||
let (prover, verifier) = Compiler::compile::<TransferCircuit>(&pp, LABEL) | ||
.expect("failed to compile circuit"); | ||
|
||
let (proof, public_inputs) = prover | ||
.prove( | ||
&mut OsRng, | ||
&TransferCircuit::new( | ||
input_notes, | ||
output_notes, | ||
skeleton_hash, | ||
tree, | ||
), | ||
) | ||
.expect("failed to prove"); | ||
|
||
verifier | ||
.verify(&proof, &public_inputs) | ||
.expect("failed to verify proof"); | ||
} |