Skip to content

Commit

Permalink
Implement ToAdviceInputs for MmrPeaks (#328)
Browse files Browse the repository at this point in the history
* MmrPeaks ToAdviceInputs

* use MmrPeaks::to_advice_inputs
  • Loading branch information
plafer authored Nov 30, 2023
1 parent c57448a commit b8daebf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
35 changes: 27 additions & 8 deletions objects/src/advice.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{utils::collections::Vec, AdviceInputs, Felt, Word};
use crate::crypto::merkle::InnerNodeInfo;
use assembly::utils::IntoBytes;
use miden_crypto::{merkle::MmrPeaks, ZERO};

/// [AdviceInputsBuilder] trait specifies the interface for building advice inputs.
/// The trait provides three methods for building advice inputs:
Expand All @@ -18,14 +19,6 @@ pub trait AdviceInputsBuilder {
fn add_merkle_nodes<I: Iterator<Item = InnerNodeInfo>>(&mut self, nodes: I);
}

/// ToAdviceInputs trait specifies the interface for converting a rust object into advice inputs.
pub trait ToAdviceInputs {
/// Converts the rust object into advice inputs and pushes them onto the given advice inputs
/// builder.
fn to_advice_inputs<T: AdviceInputsBuilder>(&self, target: &mut T);
}

/// Implement the `AdviceInputsBuilder` trait on `AdviceInputs`.
impl AdviceInputsBuilder for AdviceInputs {
fn push_onto_stack(&mut self, values: &[Felt]) {
self.extend_stack(values.iter().copied());
Expand All @@ -39,3 +32,29 @@ impl AdviceInputsBuilder for AdviceInputs {
self.extend_merkle_store(nodes);
}
}

/// ToAdviceInputs trait specifies the interface for converting a rust object into advice inputs.
pub trait ToAdviceInputs {
/// Converts the rust object into advice inputs and pushes them onto the given advice inputs
/// builder.
fn to_advice_inputs<T: AdviceInputsBuilder>(&self, target: &mut T);
}

// ToAdviceInputs IMPLEMENTATIONS
// =================================================================================================

impl ToAdviceInputs for MmrPeaks {
fn to_advice_inputs<T: AdviceInputsBuilder>(&self, target: &mut T) {
// create the vector of items to insert into the map
// The vector is in the following format:
// elements[0] = number of leaves in the Mmr
// elements[1..4] = padding ([Felt::ZERO; 3])
// elements[4..] = Mmr peak roots
let mut elements = vec![Felt::new(self.num_leaves() as u64), ZERO, ZERO, ZERO];
elements.extend(self.flatten_and_pad_peaks());

// insert the Mmr accumulator vector into the advice map against the Mmr root, which acts
// as the key.
target.insert_into_map(self.hash_peaks().into(), elements);
}
}
16 changes: 3 additions & 13 deletions objects/src/chain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{crypto::merkle::Mmr, AdviceInputsBuilder, Felt, ToAdviceInputs, ZERO};
use super::{crypto::merkle::Mmr, AdviceInputsBuilder, ToAdviceInputs};

// TODO: Consider using a PartialMmr that only contains the Mmr nodes that are relevant to the
// transaction being processed.
Expand Down Expand Up @@ -33,18 +33,8 @@ impl ToAdviceInputs for &ChainMmr {
target.add_merkle_nodes(self.0.inner_nodes());

// Extract Mmr accumulator
let accumulator = self.0.peaks(self.0.forest()).unwrap();
let peaks = self.0.peaks(self.0.forest()).unwrap();

// create the vector of items to insert into the map
// The vector is in the following format:
// elements[0] = number of leaves in the Mmr
// elements[1..4] = padding ([Felt::ZERO; 3])
// elements[4..] = Mmr peak roots
let mut elements = vec![Felt::new(accumulator.num_leaves() as u64), ZERO, ZERO, ZERO];
elements.extend(accumulator.flatten_and_pad_peaks());

// insert the Mmr accumulator vector into the advice map against the Mmr root, which acts
// as the key.
target.insert_into_map(accumulator.hash_peaks().into(), elements);
peaks.to_advice_inputs(target);
}
}

0 comments on commit b8daebf

Please sign in to comment.