Skip to content

Commit

Permalink
Merge pull request #15 from dusk-network/note-storage
Browse files Browse the repository at this point in the history
Implement storage logic for Note
  • Loading branch information
jules authored Oct 3, 2020
2 parents 93e0d5f + 29b5518 commit 6167a90
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dusk-plonk = { version = "0.2.11", features = ["trace-print"] }

[dev-dependencies]
assert_matches = "1.3"
anyhow = "1.0.32"

[profile.release]
opt-level = 3
Expand Down
13 changes: 13 additions & 0 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use dusk_pki::{PublicSpendKey, SecretSpendKey, StealthAddress, ViewKey};
use dusk_plonk::jubjub::{dhke, GENERATOR_EXTENDED, GENERATOR_NUMS_EXTENDED};
use poseidon252::cipher::PoseidonCipher;
use poseidon252::sponge::sponge::sponge_hash;
use poseidon252::StorageScalar;

use crate::{BlsScalar, Error, JubJubAffine, JubJubExtended, JubJubScalar};

Expand Down Expand Up @@ -370,3 +371,15 @@ impl<H: ByteHash> Content<H> for Note {
})
}
}

impl From<&Note> for StorageScalar {
fn from(value: &Note) -> Self {
StorageScalar(value.hash())
}
}

impl From<Note> for StorageScalar {
fn from(value: Note) -> Self {
(&value).into()
}
}
64 changes: 64 additions & 0 deletions tests/note_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ use phoenix_core::{Crossover, Error, Fee, Note, NoteType};
use std::io::{Read, Write};

use dusk_pki::{PublicSpendKey, SecretSpendKey};
use dusk_plonk::bls12_381::Scalar as BlsScalar;
use dusk_plonk::jubjub::Fr as JubJubScalar;
use dusk_plonk::prelude::*;

use kelvin::Blake2b;
use poseidon252::merkle_proof::merkle_opening_gadget;
use poseidon252::{PoseidonAnnotation, PoseidonTree};

use anyhow::Result;
use assert_matches::*;

#[test]
Expand Down Expand Up @@ -199,3 +206,60 @@ fn transparent_from_fee_remainder_with_invalid_consumed() -> Result<(), Error> {

Ok(())
}

#[test]
fn note_tree_storage() -> Result<()> {
let ssk = SecretSpendKey::default();
let psk = ssk.public_key();
let value = 25;

let note = Note::transparent(&psk, value);

// Store the note in the tree
let mut tree = PoseidonTree::<Note, PoseidonAnnotation, Blake2b>::new(4);
let idx = tree.push(note.into()).unwrap();

// Fetch the note from the tree
let branch = tree.poseidon_branch(idx).unwrap().unwrap();

// Now, let's see if we can make a valid merkle opening proof.
let pub_params = PublicParameters::setup(1 << 14, &mut rand::thread_rng())?;
let (ck, vk) = pub_params.trim(1 << 13)?;

let mut prover = Prover::new(b"NoteTest");
let hash = prover.mut_cs().add_input(note.hash());
let root = merkle_opening_gadget(
prover.mut_cs(),
branch.clone(),
hash,
branch.root.clone(),
);
prover.mut_cs().constrain_to_constant(
root,
BlsScalar::zero(),
-branch.root,
);

prover.preprocess(&ck).unwrap();
let proof = prover.prove(&ck).unwrap();

let mut verifier = Verifier::new(b"NoteTest");
let hash = verifier.mut_cs().add_input(note.hash());
let root = merkle_opening_gadget(
verifier.mut_cs(),
branch.clone(),
hash,
branch.root.clone(),
);
verifier.mut_cs().constrain_to_constant(
root,
BlsScalar::zero(),
-branch.root,
);

verifier.preprocess(&ck).unwrap();
let pi = verifier.mut_cs().public_inputs.clone();
verifier.verify(&proof, &vk, &pi).unwrap();

Ok(())
}

0 comments on commit 6167a90

Please sign in to comment.