Skip to content

Commit 718e95a

Browse files
committed
Cache default zkapp hash
This reduces time to compute the merkle root hash of the full ledger (devnet) from 11 seconds to 6 seconds.
1 parent b4256cc commit 718e95a

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

ledger/src/account/account.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use mina_p2p_messages::{
77
v2,
88
};
99
use mina_signer::CompressedPubKey;
10-
use once_cell::sync::OnceCell;
10+
use once_cell::sync::{Lazy, OnceCell};
1111
use openmina_core::constants::PROTOCOL_VERSION;
1212
use rand::{prelude::ThreadRng, seq::SliceRandom, Rng};
1313
use serde::{Deserialize, Serialize};
@@ -601,6 +601,16 @@ impl std::fmt::Debug for ZkAppUri {
601601
}
602602
}
603603

604+
fn default_zkapp_uri_hash() -> Fp {
605+
static HASH: Lazy<Fp> = Lazy::new(|| {
606+
let mut inputs = Inputs::new();
607+
inputs.append(&Fp::zero());
608+
inputs.append(&Fp::zero());
609+
hash_with_kimchi(&MINA_ZKAPP_URI, &inputs.to_fields())
610+
});
611+
*HASH
612+
}
613+
604614
impl ZkAppUri {
605615
#[allow(clippy::new_without_default)]
606616
pub fn new() -> Self {
@@ -617,23 +627,16 @@ impl ZkAppUri {
617627
}
618628

619629
fn opt_to_field(opt: Option<&ZkAppUri>) -> Fp {
630+
let Some(zkapp_uri) = opt else {
631+
return default_zkapp_uri_hash();
632+
};
620633
let mut inputs = Inputs::new();
621-
622-
match opt {
623-
Some(zkapp_uri) => {
624-
for c in zkapp_uri.0.as_slice() {
625-
for j in 0..8 {
626-
inputs.append_bool((c & (1 << j)) != 0);
627-
}
628-
}
629-
inputs.append_bool(true);
630-
}
631-
None => {
632-
inputs.append_field(Fp::zero());
633-
inputs.append_field(Fp::zero());
634+
for c in zkapp_uri.0.as_slice() {
635+
for j in 0..8 {
636+
inputs.append_bool((c & (1 << j)) != 0);
634637
}
635638
}
636-
639+
inputs.append_bool(true);
637640
hash_with_kimchi(&MINA_ZKAPP_URI, &inputs.to_fields())
638641
}
639642
}
@@ -1635,6 +1638,14 @@ impl Account {
16351638
}
16361639
}
16371640

1641+
pub fn default_zkapp_hash() -> Fp {
1642+
static HASH: Lazy<Fp> = Lazy::new(|| {
1643+
let default = ZkAppAccount::default();
1644+
default.hash()
1645+
});
1646+
*HASH
1647+
}
1648+
16381649
impl ToInputs for Account {
16391650
fn to_inputs(&self, inputs: &mut Inputs) {
16401651
let Self {
@@ -1652,11 +1663,11 @@ impl ToInputs for Account {
16521663
} = self;
16531664

16541665
// Self::zkapp
1655-
let field_zkapp = {
1656-
let zkapp = MyCow::borrow_or_default(zkapp);
1657-
zkapp.hash()
1666+
let field_zkapp = match zkapp.as_ref() {
1667+
Some(zkapp) => zkapp.hash(),
1668+
None => default_zkapp_hash(),
16581669
};
1659-
inputs.append_field(field_zkapp);
1670+
inputs.append(&field_zkapp);
16601671
inputs.append(permissions);
16611672

16621673
// Self::timing

ledger/src/proofs/to_field_elements.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::borrow::Cow;
22

3-
use crate::proofs::{public_input::plonk_checks::ShiftingValue, util::four_u64_to_field};
3+
use crate::{
4+
default_zkapp_hash,
5+
proofs::{public_input::plonk_checks::ShiftingValue, util::four_u64_to_field},
6+
};
47
use ark_ff::{Field, One, Zero};
58
use kimchi::proof::{PointEvaluations, ProofEvaluations, ProverCommitments, ProverProof};
69
use mina_curves::pasta::Fq;
@@ -704,9 +707,11 @@ impl ToFieldElements<Fp> for Box<Account> {
704707
voting_for.to_field_elements(fields);
705708
timing.to_field_elements(fields);
706709
permissions.to_field_elements(fields);
707-
MyCow::borrow_or_default(zkapp)
708-
.hash()
709-
.to_field_elements(fields);
710+
match zkapp.as_ref() {
711+
Some(zkapp) => zkapp.hash(),
712+
None => default_zkapp_hash(),
713+
}
714+
.to_field_elements(fields);
710715
}
711716
}
712717

0 commit comments

Comments
 (0)