Skip to content

Commit

Permalink
Merge branch 'master' into rusk-wallet-core-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksh14 committed Sep 4, 2024
2 parents ca6f704 + a7fa425 commit 4b2c740
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 112 deletions.
1 change: 0 additions & 1 deletion contracts/stake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ rusk-prover = { version = "0.3", path = "../../rusk-prover/" }
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
hex = "0.4"
rand = "0.8"
poseidon-merkle = { version = "0.7", features = ["rkyv-impl"] }
ff = { version = "0.13", default-features = false }
criterion = "0.5"

Expand Down
18 changes: 7 additions & 11 deletions contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

use std::sync::mpsc;

use poseidon_merkle::Opening as PoseidonOpening;
use rand::rngs::StdRng;

use execution_core::{
transfer::{
contract_exec::ContractExec,
phoenix::{
Note, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
Transaction as PhoenixTransaction, TreeLeaf,
ViewKey as PhoenixViewKey, NOTES_TREE_DEPTH,
Note, NoteLeaf, NoteOpening, NoteTreeItem,
PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
Transaction as PhoenixTransaction, ViewKey as PhoenixViewKey,
},
Transaction, TRANSFER_CONTRACT,
},
Expand All @@ -29,7 +28,7 @@ const POINT_LIMIT: u64 = 0x100000000;
pub fn leaves_from_height(
session: &mut Session,
height: u64,
) -> Result<Vec<TreeLeaf>, PiecrustError> {
) -> Result<Vec<NoteLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -49,7 +48,7 @@ pub fn leaves_from_height(
pub fn leaves_from_pos(
session: &mut Session,
pos: u64,
) -> Result<Vec<TreeLeaf>, PiecrustError> {
) -> Result<Vec<NoteLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand Down Expand Up @@ -81,7 +80,7 @@ pub fn root(session: &mut Session) -> Result<BlsScalar, PiecrustError> {
pub fn opening(
session: &mut Session,
pos: u64,
) -> Result<Option<PoseidonOpening<(), NOTES_TREE_DEPTH>>, PiecrustError> {
) -> Result<Option<NoteOpening>, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "opening", &pos, POINT_LIMIT)
.map(|r| r.data)
Expand Down Expand Up @@ -177,10 +176,7 @@ pub fn create_transaction<const I: usize>(
.expect("An opening should exist for a note in the tree");

// sanity check of the merkle opening
assert!(opening.verify(poseidon_merkle::Item::new(
rusk_abi::poseidon_hash(note.hash_inputs().to_vec()),
()
)));
assert!(opening.verify(NoteTreeItem::new(note.hash(), ())));

inputs.push((note.clone(), opening));
}
Expand Down
1 change: 0 additions & 1 deletion contracts/transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
execution-core = { version = "0.1.0", path = "../../execution-core" }
dusk-bytes = "0.1"
poseidon-merkle = { version = "0.7", features = ["rkyv-impl"] }
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
ringbuffer = "0.15"

Expand Down
12 changes: 4 additions & 8 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use alloc::collections::{BTreeMap, BTreeSet};
use alloc::vec::Vec;

use dusk_bytes::Serializable;
use poseidon_merkle::Opening as PoseidonOpening;
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};

use execution_core::{
Expand All @@ -22,8 +21,8 @@ use execution_core::{
transfer::{
moonlight::{AccountData, Transaction as MoonlightTransaction},
phoenix::{
Note, Sender, Transaction as PhoenixTransaction, TreeLeaf,
NOTES_TREE_DEPTH,
Note, NoteLeaf, NoteOpening, Sender,
Transaction as PhoenixTransaction,
},
withdraw::{
Withdraw, WithdrawReceiver, WithdrawReplayToken, WithdrawSignature,
Expand Down Expand Up @@ -517,7 +516,7 @@ impl TransferState {
/// Note: the method `update_root` needs to be called after the last note is
/// pushed.
pub fn push_note(&mut self, block_height: u64, note: Note) -> Note {
let tree_leaf = TreeLeaf { block_height, note };
let tree_leaf = NoteLeaf { block_height, note };
let pos = self.tree.push(tree_leaf.clone());
rusk_abi::emit("TREE_LEAF", (pos, tree_leaf));
self.get_note(pos)
Expand Down Expand Up @@ -573,10 +572,7 @@ impl TransferState {
}

/// Get the opening
pub fn opening(
&self,
pos: u64,
) -> Option<PoseidonOpening<(), NOTES_TREE_DEPTH>> {
pub fn opening(&self, pos: u64) -> Option<NoteOpening> {
self.tree.opening(pos)
}

Expand Down
29 changes: 11 additions & 18 deletions contracts/transfer/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,38 @@

use alloc::vec::Vec;

use poseidon_merkle::{
Item as PoseidonItem, Opening as PoseidonOpening, Tree as PoseidonTree,
};

use execution_core::{
transfer::phoenix::{Note, TreeLeaf, NOTES_TREE_DEPTH},
transfer::phoenix::{Note, NoteLeaf, NoteOpening, NoteTreeItem, NotesTree},
BlsScalar,
};

pub struct Tree {
tree: PoseidonTree<(), NOTES_TREE_DEPTH>,
tree: NotesTree,
// Since `dusk-merkle` does not include data blocks with the tree, we do it
// here.
leaves: Vec<TreeLeaf>,
leaves: Vec<NoteLeaf>,
}

impl Tree {
pub const fn new() -> Self {
Self {
tree: PoseidonTree::new(),
tree: NotesTree::new(),
leaves: Vec::new(),
}
}

pub fn get(&self, pos: u64) -> Option<TreeLeaf> {
pub fn get(&self, pos: u64) -> Option<NoteLeaf> {
self.leaves.get(pos as usize).cloned()
}

pub fn push(&mut self, mut leaf: TreeLeaf) -> u64 {
pub fn push(&mut self, mut leaf: NoteLeaf) -> u64 {
// update the position before computing the hash
let pos = self.leaves.len() as u64;
leaf.note.set_pos(pos);

// compute the item that goes in the leaf of the tree
let hash = rusk_abi::poseidon_hash(leaf.note.hash_inputs().to_vec());
let item = PoseidonItem { hash, data: () };
let item = NoteTreeItem { hash, data: () };

self.tree.insert(pos, item);
self.leaves.push(leaf);
Expand All @@ -57,7 +53,7 @@ impl Tree {
for note in notes {
// skip transparent notes with a value of 0
if !note.value(None).is_ok_and(|value| value == 0) {
self.push(TreeLeaf { block_height, note });
self.push(NoteLeaf { block_height, note });
}
}
}
Expand All @@ -68,7 +64,7 @@ impl Tree {

/// Return an iterator through the leaves in the tree, starting from a given
/// `height`.
pub fn leaves(&self, height: u64) -> impl Iterator<Item = &TreeLeaf> {
pub fn leaves(&self, height: u64) -> impl Iterator<Item = &NoteLeaf> {
// We can do this since we know the leaves are strictly increasing in
// block height. If this ever changes - such as in the case of a
// sparsely populated tree - we should annotate the tree and use
Expand All @@ -80,7 +76,7 @@ impl Tree {

/// Return an iterator through the leaves in the tree, starting from a given
/// `position`.
pub fn leaves_pos(&self, pos: u64) -> impl Iterator<Item = &TreeLeaf> {
pub fn leaves_pos(&self, pos: u64) -> impl Iterator<Item = &NoteLeaf> {
// We can do this since we know the leaves are strictly increasing in
// block height. If this ever changes - such as in the case of a
// sparsely populated tree - we should annotate the tree and use
Expand All @@ -92,10 +88,7 @@ impl Tree {
self.leaves[pos..].iter()
}

pub fn opening(
&self,
pos: u64,
) -> Option<PoseidonOpening<(), NOTES_TREE_DEPTH>> {
pub fn opening(&self, pos: u64) -> Option<NoteOpening> {
self.tree.opening(pos)
}

Expand Down
16 changes: 6 additions & 10 deletions contracts/transfer/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use execution_core::{
contract_exec::ContractExec,
moonlight::AccountData,
phoenix::{
Note, PublicKey, SecretKey, Transaction as PhoenixTransaction,
TreeLeaf, ViewKey, NOTES_TREE_DEPTH,
Note, NoteLeaf, NoteOpening, NoteTreeItem, PublicKey, SecretKey,
Transaction as PhoenixTransaction, ViewKey,
},
Transaction, TRANSFER_CONTRACT,
},
Expand All @@ -22,15 +22,14 @@ use execution_core::{
use rusk_abi::{CallReceipt, PiecrustError, Session};
use rusk_prover::LocalProver;

use poseidon_merkle::Opening as PoseidonOpening;
use rand::rngs::StdRng;

const GAS_LIMIT: u64 = 0x10_000_000;

pub fn leaves_from_height(
session: &mut Session,
height: u64,
) -> Result<Vec<TreeLeaf>, PiecrustError> {
) -> Result<Vec<NoteLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -50,7 +49,7 @@ pub fn leaves_from_height(
pub fn leaves_from_pos(
session: &mut Session,
pos: u64,
) -> Result<Vec<TreeLeaf>, PiecrustError> {
) -> Result<Vec<NoteLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand Down Expand Up @@ -106,7 +105,7 @@ pub fn contract_balance(
pub fn opening(
session: &mut Session,
pos: u64,
) -> Result<Option<PoseidonOpening<(), NOTES_TREE_DEPTH>>, PiecrustError> {
) -> Result<Option<NoteOpening>, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "opening", &pos, GAS_LIMIT)
.map(|r| r.data)
Expand Down Expand Up @@ -212,10 +211,7 @@ pub fn create_phoenix_transaction<const I: usize>(
.expect("An opening should exist for a note in the tree");

// sanity check of the merkle opening
assert!(opening.verify(poseidon_merkle::Item::new(
rusk_abi::poseidon_hash(note.hash_inputs().to_vec()),
()
)));
assert!(opening.verify(NoteTreeItem::new(note.hash(), ())));

inputs.push((note.clone(), opening));
}
Expand Down
37 changes: 22 additions & 15 deletions execution-core/src/transfer/phoenix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use bytecheck::CheckBytes;
use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use dusk_poseidon::{Domain, Hash};
use ff::Field;
use poseidon_merkle::Opening;
use rand::{CryptoRng, RngCore};
use rkyv::{Archive, Deserialize, Serialize};

Expand All @@ -29,20 +28,37 @@ use crate::{
};

// phoenix types
pub use phoenix_circuits::{InputNoteInfo, OutputNoteInfo, TxCircuit};
pub use phoenix_core::{
value_commitment, Error as CoreError, Note, PublicKey, SecretKey, Sender,
StealthAddress, TxSkeleton, ViewKey, NOTE_VAL_ENC_SIZE, OUTPUT_NOTES,
};

pub use phoenix_circuits::{InputNoteInfo, OutputNoteInfo, TxCircuit};
/// The depth of the merkle tree of notes stored in the transfer-contract.
pub const NOTES_TREE_DEPTH: usize = 17;
/// The arity of the merkle tree of notes stored in the transfer-contract.
pub use poseidon_merkle::ARITY as NOTES_TREE_ARITY;
/// The merkle tree of notes stored in the transfer-contract.
pub type NotesTree = poseidon_merkle::Tree<(), NOTES_TREE_DEPTH>;
/// The merkle opening for a note-hash in the merkle tree of notes.
pub type NoteOpening = poseidon_merkle::Opening<(), NOTES_TREE_DEPTH>;
/// the tree item for the merkle-tree of notes stored in the transfer-contract.
pub type NoteTreeItem = poseidon_merkle::Item<()>;

/// A leaf of the merkle tree of notes stored in the transfer-contract.
#[derive(Debug, Clone, PartialEq, Eq, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
pub struct NoteLeaf {
/// The height of the block when the note was inserted in the tree.
pub block_height: u64,
/// The note inserted in the tree.
pub note: Note,
}

/// Label used for the ZK transcript initialization. Must be the same for prover
/// and verifier.
pub const TRANSCRIPT_LABEL: &[u8] = b"dusk-network";

/// The depth of the transfer tree.
pub const NOTES_TREE_DEPTH: usize = 17;

/// Phoenix transaction.
#[derive(Debug, Clone, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
Expand Down Expand Up @@ -79,7 +95,7 @@ impl Transaction {
sender_sk: &SecretKey,
change_pk: &PublicKey,
receiver_pk: &PublicKey,
inputs: Vec<(Note, Opening<(), NOTES_TREE_DEPTH>)>,
inputs: Vec<(Note, NoteOpening)>,
root: BlsScalar,
transfer_value: u64,
obfuscated_transaction: bool,
Expand Down Expand Up @@ -835,15 +851,6 @@ impl Serializable<SIZE> for Fee {
})
}
}
/// A leaf of the transfer tree.
#[derive(Debug, Clone, PartialEq, Eq, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
pub struct TreeLeaf {
/// The height of the block when the note was inserted in the tree.
pub block_height: u64,
/// The note inserted in the tree.
pub note: Note,
}

/// This struct mimics the [`TxCircuit`] but is not generic over the amount of
/// input-notes.
Expand Down
10 changes: 5 additions & 5 deletions execution-core/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ use execution_core::{
ContractBytecode, ContractCall, ContractDeploy, ContractExec,
},
phoenix::{
Note, Prove, PublicKey as PhoenixPublicKey,
SecretKey as PhoenixSecretKey, TxCircuitVec, NOTES_TREE_DEPTH,
Note, NoteTreeItem, NotesTree, Prove,
PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
TxCircuitVec,
},
Transaction,
},
BlsScalar, Error, JubJubScalar,
};
use ff::Field;
use poseidon_merkle::{Item, Tree};
use rand::rngs::StdRng;
use rand::{CryptoRng, Rng, RngCore, SeedableRng};

Expand Down Expand Up @@ -86,9 +86,9 @@ fn new_phoenix_tx<R: RngCore + CryptoRng>(
input_2.set_pos(2);
let notes = vec![input_0, input_1, input_2];

let mut notes_tree = Tree::<(), NOTES_TREE_DEPTH>::new();
let mut notes_tree = NotesTree::new();
for note in notes.iter() {
let item = Item {
let item = NoteTreeItem {
hash: note.hash(),
data: (),
};
Expand Down
1 change: 0 additions & 1 deletion rusk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ dirs = "4"
blake3 = "1"
blake2b_simd = { version = "1", default-features = false }

poseidon-merkle = { version = "0.7", features = ["rkyv-impl", "size_32"] }
sha3 = "0.10"
dusk-bytes = "0.1"
kadcast = "0.7.0-rc"
Expand Down
Loading

0 comments on commit 4b2c740

Please sign in to comment.