Skip to content

Commit

Permalink
Update derivation of the rho value in Issue Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstanceBeguier committed Jan 2, 2025
1 parent fe15076 commit 91e816c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/issuance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,37 @@ impl<T: IssueAuth> IssueBundle<T> {
authorization: map_auth(authorization),
}
}

/// Returns the reference notes for the `IssueBundle`.
pub fn get_reference_notes(self) -> HashMap<AssetBase, Note> {
let mut reference_notes = HashMap::new();
self.actions.iter().for_each(|action| {
action.notes.iter().for_each(|note| {
if (note.recipient() == ReferenceKeys::recipient())
&& (note.value() == NoteValue::zero())
{
reference_notes.insert(note.asset(), *note);
}
})
});
reference_notes
}

/// Compute the correct `\rho` value for each note in the bundle.
pub fn finalize(&mut self, nullifier: Nullifier) {
self.actions
.iter_mut()
.enumerate()
.for_each(|(index_action, action)| {
action
.notes
.iter_mut()
.enumerate()
.for_each(|(index_note, note)| {
note.update_rho_for_issuance_note(nullifier, index_action, index_note);
});
});
}
}

impl IssueBundle<Unauthorized> {
Expand Down Expand Up @@ -468,23 +499,6 @@ impl IssueBundle<Unauthorized> {
}
}

impl<T: IssueAuth> IssueBundle<T> {
/// Returns the reference notes for the `IssueBundle`.
pub fn get_reference_notes(self) -> HashMap<AssetBase, Note> {
let mut reference_notes = HashMap::new();
self.actions.iter().for_each(|action| {
action.notes.iter().for_each(|note| {
if (note.recipient() == ReferenceKeys::recipient())
&& (note.value() == NoteValue::zero())
{
reference_notes.insert(note.asset(), *note);
}
})
});
reference_notes
}
}

fn create_reference_note(asset: AssetBase, mut rng: impl RngCore) -> Note {
Note::new(
ReferenceKeys::recipient(),
Expand Down
28 changes: 28 additions & 0 deletions src/note.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Data structures used for note construction.
use blake2b_simd::Params;
use core::fmt;
use memuse::DynamicUsage;

Expand All @@ -22,6 +23,8 @@ pub use self::commitment::{ExtractedNoteCommitment, NoteCommitment};
pub(crate) mod nullifier;
pub use self::nullifier::Nullifier;

const ZSA_ISSUE_NOTE_RHO_PERSONALIZATION: &[u8; 16] = b"ZSA_IssueNoteRho";

/// The randomness used to construct a note.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Rho(pallas::Base);
Expand Down Expand Up @@ -345,6 +348,31 @@ impl Note {
..self
}
}

/// Update the rho value of the issuance note.
pub(crate) fn update_rho_for_issuance_note(
&mut self,
nullifier: Nullifier,
index_action: usize,
index_note: usize,
) {
let index_action_bytes: [u8; 4] = (index_action.try_into().unwrap() as u32).to_le_bytes();
let index_note_bytes: [u8; 4] = (index_note.try_into().unwrap() as u32).to_le_bytes();
self.rho = Rho(to_base(
Params::new()
.hash_length(64)
.personal(ZSA_ISSUE_NOTE_RHO_PERSONALIZATION)
.to_state()
.update(&nullifier.to_bytes())
.update(&[0x84])
.update(index_action_bytes.as_ref())
.update(index_note_bytes.as_ref())
.finalize()
.as_bytes()
.try_into()
.unwrap(),
));
}
}

/// An encrypted note.
Expand Down

0 comments on commit 91e816c

Please sign in to comment.