Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

circuits: Remove ViewKey from TxOutputNote::new #192

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions circuits/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Remove `ViewKey` from `TxOutputNote::new()` parameters [#191]

## [0.1.0] - 2024-05-22

### Added
Expand All @@ -23,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update `poseidon-merkle` to v0.6 [#179]

<!-- ISSUES -->
[#191]: https://github.com/dusk-network/phoenix/issues/191
[#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
Expand Down
42 changes: 23 additions & 19 deletions circuits/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_jubjub::{
JubJubScalar, GENERATOR, GENERATOR_NUMS, GENERATOR_NUMS_EXTENDED,
JubJubAffine, JubJubScalar, GENERATOR, GENERATOR_EXTENDED, GENERATOR_NUMS,
GENERATOR_NUMS_EXTENDED,
};
use dusk_plonk::prelude::*;
use dusk_poseidon::{Domain, Hash, HashGadget};
Expand Down Expand Up @@ -124,9 +125,9 @@ impl<const H: usize> TxInputNote<H> {
/// suitable for being introduced in the transfer circuit
#[derive(Debug, Clone)]
pub struct TxOutputNote {
pub(crate) value: u64,
pub(crate) value_commitment: JubJubAffine,
pub(crate) blinding_factor: JubJubScalar,
value: u64,
value_commitment: JubJubAffine,
blinding_factor: JubJubScalar,
}

#[derive(Debug, Clone)]
Expand All @@ -137,16 +138,18 @@ struct WitnessTxOutputNote {
}

impl TxOutputNote {
/// Create a tx output note
pub fn new(
note: &Note,
vk: &ViewKey,
) -> Result<crate::transaction::TxOutputNote, PhoenixError> {
Ok(crate::transaction::TxOutputNote {
value: note.value(Some(vk))?,
value_commitment: note.value_commitment().into(),
blinding_factor: note.blinding_factor(Some(vk))?,
})
/// Crate a new `TxOutputNote`.
pub fn new(value: u64, blinding_factor: JubJubScalar) -> Self {
let value_commitment = JubJubAffine::from(
(GENERATOR_EXTENDED * JubJubScalar::from(value))
+ (GENERATOR_NUMS_EXTENDED * blinding_factor),
);

Self {
value,
value_commitment,
blinding_factor,
}
}

fn append_to_circuit(
Expand Down Expand Up @@ -338,7 +341,6 @@ impl<const H: usize, const I: usize> Default for TxCircuit<H, I> {
let mut rng = StdRng::seed_from_u64(0xbeef);

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

let mut tree = Tree::<(), H>::new();
let skeleton_hash = BlsScalar::default();
Expand All @@ -365,10 +367,12 @@ impl<const H: usize, const I: usize> Default for TxCircuit<H, I> {
tx_input_notes.push(tx_input_note);
}

let tx_output_note_1 =
TxOutputNote::new(&note, &vk).expect("Note created properly.");
let tx_output_note_2 =
TxOutputNote::new(&note, &vk).expect("Note created properly.");
let tx_output_note_1 = TxOutputNote {
value: 0,
value_commitment: JubJubAffine::default(),
blinding_factor: JubJubScalar::default(),
};
let tx_output_note_2 = tx_output_note_1.clone();

let tx_output_notes = [tx_output_note_1, tx_output_note_2];

Expand Down
35 changes: 16 additions & 19 deletions circuits/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use rand_core::{CryptoRng, OsRng, RngCore};

use dusk_jubjub::JubJubScalar;
use phoenix_circuits::transaction::{TxCircuit, TxInputNote, TxOutputNote};
use phoenix_core::{Note, PublicKey, SecretKey, ViewKey};
use phoenix_core::{Note, PublicKey, SecretKey};

use dusk_plonk::prelude::*;
use poseidon_merkle::{Item, Tree};
Expand All @@ -21,7 +22,6 @@ const CAPACITY: usize = 17; // capacity required for the setup
const HEIGHT: usize = 17;

struct TestingParameters {
sk: SecretKey,
pp: PublicParameters,
tx_input_notes: [TxInputNote<HEIGHT>; 4],
skeleton_hash: BlsScalar,
Expand All @@ -48,7 +48,7 @@ lazy_static! {
let crossover = 5;
let max_fee = 5;

TestingParameters { sk, pp, tx_input_notes, skeleton_hash, root, crossover, max_fee }
TestingParameters { pp, tx_input_notes, skeleton_hash, root, crossover, max_fee }
};
}

Expand Down Expand Up @@ -108,14 +108,11 @@ fn create_test_tx_input_notes<const I: usize>(
input_notes.try_into().unwrap()
}

fn create_test_tx_output_note(
sk: &SecretKey,
value: u64,
rng: &mut (impl RngCore + CryptoRng),
) -> TxOutputNote {
let note = Note::transparent(rng, &PublicKey::from(sk), value);
TxOutputNote::new(&note, &ViewKey::from(&TP.sk))
.expect("Note created properly.")
// we don't care if the test output notes are spendable
fn create_test_tx_output_note(value: u64) -> TxOutputNote {
let blinding_factor = JubJubScalar::from(42u64);

TxOutputNote::new(value, blinding_factor)
}

#[test]
Expand All @@ -128,8 +125,8 @@ fn test_transfer_circuit_1_2() {

// create 2 testing tx output notes
let tx_output_notes = [
create_test_tx_output_note(&TP.sk, 10, &mut OsRng),
create_test_tx_output_note(&TP.sk, 5, &mut OsRng),
create_test_tx_output_note(10),
create_test_tx_output_note(5),
];

let (proof, public_inputs) = prover
Expand Down Expand Up @@ -162,8 +159,8 @@ fn test_transfer_circuit_2_2() {

// create 2 testing tx output notes
let tx_output_notes = [
create_test_tx_output_note(&TP.sk, 35, &mut OsRng),
create_test_tx_output_note(&TP.sk, 5, &mut OsRng),
create_test_tx_output_note(35),
create_test_tx_output_note(5),
];

let (proof, public_inputs) = prover
Expand Down Expand Up @@ -199,8 +196,8 @@ fn test_transfer_circuit_3_2() {

// create 2 testing tx output notes
let tx_output_notes = [
create_test_tx_output_note(&TP.sk, 35, &mut OsRng),
create_test_tx_output_note(&TP.sk, 30, &mut OsRng),
create_test_tx_output_note(35),
create_test_tx_output_note(30),
];

let (proof, public_inputs) = prover
Expand Down Expand Up @@ -230,8 +227,8 @@ fn test_transfer_circuit_4_2() {

// create 2 testing tx output notes
let tx_output_notes = [
create_test_tx_output_note(&TP.sk, 60, &mut OsRng),
create_test_tx_output_note(&TP.sk, 30, &mut OsRng),
create_test_tx_output_note(60),
create_test_tx_output_note(30),
];

let (proof, public_inputs) = prover
Expand Down