Skip to content
Open
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
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"ledger",
"macros",
"mina-p2p-messages",
"mina-tx-type",
"node",
"node/account",
"node/common",
Expand Down Expand Up @@ -147,6 +148,7 @@ mina-p2p-messages = { path = "mina-p2p-messages" }
mina-poseidon = { git = "https://github.com/o1-labs/proof-systems", tag = "0.2.0" }
mina-signer = { git = "https://github.com/o1-labs/proof-systems", tag = "0.2.0" }
mina-transport = { path = "tools/transport" }
mina-tx-type = { path = "mina-tx-type" }
mio = { version = "1.0.4", features = ["os-poll", "net"] }
multiaddr = "0.18.1"
multihash = { version = "0.18.1", features = ["blake2b"] }
Expand Down
1 change: 1 addition & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ COPY genesis_ledgers ./genesis_ledgers
COPY ledger ./ledger
COPY macros ./macros
COPY mina-p2p-messages ./mina-p2p-messages
COPY mina-tx-type ./mina-tx-type
COPY node ./node
COPY p2p ./p2p
COPY poseidon ./poseidon
Expand Down
1 change: 1 addition & 0 deletions ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mina-macros = { workspace = true }
mina-p2p-messages = { workspace = true }
mina-poseidon = { workspace = true }
mina-signer = { workspace = true }
mina-tx-type = { workspace = true }
num-bigint = { workspace = true }
o1-utils = { workspace = true }
once_cell = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions ledger/src/account/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
numbers::{
currency::{CheckedAmount, CheckedBalance},
nat::{CheckedSlot, CheckedSlotSpan},
AmountToChecked, BalanceToChecked, SlotSpanToChecked, SlotToChecked,
},
to_field_elements::ToFieldElements,
},
Expand Down
100 changes: 46 additions & 54 deletions ledger/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
//! Hash computation types and traits for the ledger.
//!
//! This module provides the [`ToInputs`] trait for converting types to hash
//! inputs, enabling Poseidon hash computation for transactions and other
//! protocol data structures.

use mina_curves::pasta::Fp;
use mina_signer::CompressedPubKey;

use crate::{proofs::witness::Witness, scan_state::currency};
use poseidon::hash::{hash_with_kimchi, Inputs, LazyParam};

/// Trait for types that can be converted to hash inputs.
///
/// This trait is the foundation for computing Poseidon hashes of protocol
/// data structures. Types implementing this trait can be hashed using
/// the Mina-compatible Kimchi hash function.
pub trait ToInputs {
/// Appends the hash inputs for this value to the given input buffer.
fn to_inputs(&self, inputs: &mut Inputs);

/// Creates a new input buffer containing this value's hash inputs.
fn to_inputs_owned(&self) -> Inputs {
let mut inputs = Inputs::new();
self.to_inputs(&mut inputs);
inputs
}

/// Computes the Poseidon hash of this value with the given parameter.
fn hash_with_param(&self, param: &LazyParam) -> Fp {
let mut inputs = Inputs::new();
self.to_inputs(&mut inputs);
hash_with_kimchi(param, &inputs.to_fields())
}

/// Computes the Poseidon hash with circuit witness generation.
fn checked_hash_with_param(&self, param: &LazyParam, w: &mut Witness<Fp>) -> Fp {
use crate::proofs::transaction::transaction_snark::checked_hash;

Expand Down Expand Up @@ -73,7 +88,9 @@ where
}
}

/// Extension trait for appending [`ToInputs`] values to an input buffer.
pub trait AppendToInputs {
/// Appends a value implementing [`ToInputs`] to this input buffer.
fn append<T>(&mut self, value: &T)
where
T: ToInputs;
Expand All @@ -88,36 +105,37 @@ impl AppendToInputs for Inputs {
}
}

#[cfg(test)]
mod tests {
use o1_utils::FieldHelpers;

use poseidon::hash::param_to_field;
#[cfg(target_family = "wasm")]
use wasm_bindgen_test::wasm_bindgen_test as test;
// ============================================================================
// ToInputs implementations for currency types
// ============================================================================

macro_rules! impl_to_inputs {
(32: { $($name32:ident,)* }, 64: { $($name64:ident,)* },) => {
$(
impl ToInputs for currency::$name32 {
fn to_inputs(&self, inputs: &mut Inputs) {
inputs.append_u32(self.as_u32());
}
}
)*
$(
impl ToInputs for currency::$name64 {
fn to_inputs(&self, inputs: &mut Inputs) {
inputs.append_u64(self.as_u64());
}
}
)*
};
}

use super::*;
impl_to_inputs!(
32: { Length, Slot, Nonce, Index, SlotSpan, TxnVersion, Epoch, },
64: { Amount, Balance, Fee, BlockTime, BlockTimeSpan, N, },
);

#[test]
fn test_param() {
for (s, hex) in [
(
"",
"2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000",
),
(
"hello",
"68656c6c6f2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000",
),
(
"aaaaaaaaaaaaaaaaaaaa",
"6161616161616161616161616161616161616161000000000000000000000000",
),
] {
let field = param_to_field(s);
assert_eq!(field.to_hex(), hex);
}
}
#[cfg(test)]
mod tests {
use poseidon::hash::Inputs;

#[test]
fn test_inputs() {
Expand All @@ -132,31 +150,5 @@ mod tests {

elog!("INPUTS={:?}", inputs);
elog!("FIELDS={:?}", inputs.to_fields());

// // Self::timing
// match self.timing {
// Timing::Untimed => {
// roi.append_bool(false);
// roi.append_u64(0); // initial_minimum_balance
// roi.append_u32(0); // cliff_time
// roi.append_u64(0); // cliff_amount
// roi.append_u32(1); // vesting_period
// roi.append_u64(0); // vesting_increment
// }
// Timing::Timed {
// initial_minimum_balance,
// cliff_time,
// cliff_amount,
// vesting_period,
// vesting_increment,
// } => {
// roi.append_bool(true);
// roi.append_u64(initial_minimum_balance);
// roi.append_u32(cliff_time);
// roi.append_u64(cliff_amount);
// roi.append_u32(vesting_period);
// roi.append_u64(vesting_increment);
// }
// }
}
}
3 changes: 2 additions & 1 deletion ledger/src/proofs/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
wrap::{wrap, WrapParams},
},
scan_state::{
currency,
currency::{self, Magnitude},
fee_excess::{self, FeeExcess},
pending_coinbase::{PendingCoinbase, PendingCoinbaseWitness, Stack},
protocol_state::MinaHash,
Expand All @@ -47,6 +47,7 @@ use super::{
numbers::{
currency::CheckedAmount,
nat::{CheckedBlockTime, CheckedBlockTimeSpan, CheckedLength},
AmountToChecked, LengthToChecked, SignedAmountToChecked,
},
step::{step, InductiveRule, OptFlag, PreviousProofStatement, StepParams, StepProof},
to_field_elements::ToFieldElements,
Expand Down
1 change: 1 addition & 0 deletions ledger/src/proofs/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
use super::{
constants::WrapMergeProof,
field::{Boolean, CircuitVar, FieldWitness},
numbers::{SignedAmountToChecked, SignedFeeToChecked},
public_input::plonk_checks::PlonkMinimal,
step::{
extract_recursion_challenges, InductiveRule, OptFlag, PreviousProofStatement, StepProof,
Expand Down
89 changes: 73 additions & 16 deletions ledger/src/proofs/numbers/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,22 +510,6 @@ macro_rules! impl_currency {
}
}

impl $unchecked {
pub fn to_checked<F: FieldWitness>(&self) -> $name<F> {
$name::from_inner(*self)
}
}

impl Signed<$unchecked> {
pub fn to_checked<F: FieldWitness>(&self) -> CheckedSigned<F, $name<F>> {
CheckedSigned {
magnitude: self.magnitude.to_checked(),
sgn: CircuitVar::Var(self.sgn),
value: Cell::new(None),
}
}
}

impl<F: FieldWitness> ForZkappCheck<F> for $unchecked {
type CheckedType = $name<F>;
fn checked_from_field(field: F) -> Self::CheckedType {
Expand All @@ -543,3 +527,76 @@ impl_currency!(
{CheckedFee, Fee},
{CheckedBalance, Balance}
);

// Extension traits for to_checked conversion
pub trait AmountToChecked {
fn to_checked<F: FieldWitness>(&self) -> CheckedAmount<F>;
}

impl AmountToChecked for Amount {
fn to_checked<F: FieldWitness>(&self) -> CheckedAmount<F> {
CheckedAmount::from_inner(*self)
}
}

pub trait FeeToChecked {
fn to_checked<F: FieldWitness>(&self) -> CheckedFee<F>;
}

impl FeeToChecked for Fee {
fn to_checked<F: FieldWitness>(&self) -> CheckedFee<F> {
CheckedFee::from_inner(*self)
}
}

pub trait BalanceToChecked {
fn to_checked<F: FieldWitness>(&self) -> CheckedBalance<F>;
}

impl BalanceToChecked for Balance {
fn to_checked<F: FieldWitness>(&self) -> CheckedBalance<F> {
CheckedBalance::from_inner(*self)
}
}

pub trait SignedAmountToChecked {
fn to_checked<F: FieldWitness>(&self) -> CheckedSigned<F, CheckedAmount<F>>;
}

impl SignedAmountToChecked for Signed<Amount> {
fn to_checked<F: FieldWitness>(&self) -> CheckedSigned<F, CheckedAmount<F>> {
CheckedSigned {
magnitude: AmountToChecked::to_checked::<F>(&self.magnitude),
sgn: CircuitVar::Var(self.sgn),
value: Cell::new(None),
}
}
}

pub trait SignedFeeToChecked {
fn to_checked<F: FieldWitness>(&self) -> CheckedSigned<F, CheckedFee<F>>;
}

impl SignedFeeToChecked for Signed<Fee> {
fn to_checked<F: FieldWitness>(&self) -> CheckedSigned<F, CheckedFee<F>> {
CheckedSigned {
magnitude: FeeToChecked::to_checked::<F>(&self.magnitude),
sgn: CircuitVar::Var(self.sgn),
value: Cell::new(None),
}
}
}

pub trait SignedBalanceToChecked {
fn to_checked<F: FieldWitness>(&self) -> CheckedSigned<F, CheckedBalance<F>>;
}

impl SignedBalanceToChecked for Signed<Balance> {
fn to_checked<F: FieldWitness>(&self) -> CheckedSigned<F, CheckedBalance<F>> {
CheckedSigned {
magnitude: BalanceToChecked::to_checked::<F>(&self.magnitude),
sgn: CircuitVar::Var(self.sgn),
value: Cell::new(None),
}
}
}
10 changes: 10 additions & 0 deletions ledger/src/proofs/numbers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
pub mod common;
pub mod currency;
pub mod nat;

// Re-export extension traits for to_checked conversion
pub use currency::{
AmountToChecked, BalanceToChecked, FeeToChecked, SignedAmountToChecked, SignedBalanceToChecked,
SignedFeeToChecked,
};
pub use nat::{
BlockTimeSpanToChecked, BlockTimeToChecked, IndexToChecked, LengthToChecked, NonceToChecked,
SlotSpanToChecked, SlotToChecked, TxnVersionToChecked,
};
Loading
Loading