goas: hack to see if rayon parallelism helps

This commit is contained in:
David Rusu 2024-08-23 23:47:11 +04:00
parent da18cef987
commit 9f5966d1df
3 changed files with 82 additions and 62 deletions

View File

@ -14,8 +14,6 @@ use std::collections::BTreeMap;
// TODO: sparse merkle tree // TODO: sparse merkle tree
pub const MAX_BALANCES: usize = 1 << 8; pub const MAX_BALANCES: usize = 1 << 8;
pub const MAX_TXS: usize = 1 << 8;
pub const MAX_EVENTS: usize = 1 << 8;
// state of the zone // state of the zone
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
@ -105,14 +103,19 @@ impl StateWitness {
} }
pub fn balances_root(&self) -> [u8; 32] { pub fn balances_root(&self) -> [u8; 32] {
let balance_bytes = Vec::from_iter(self.balances.iter().map(|(owner, balance)| { let mut hasher = Sha256::new();
let mut bytes: Vec<u8> = vec![];
bytes.extend(owner); for (owner, balance) in self.balances.iter() {
bytes.extend(balance.to_le_bytes()); hasher.update(owner);
bytes hasher.update(balance.to_le_bytes());
})); }
let balance_merkle_leaves = cl::merkle::padded_leaves(&balance_bytes);
merkle::root::<MAX_BALANCES>(balance_merkle_leaves) for _ in self.balances.len()..MAX_BALANCES {
hasher.update([0u8; PUBLIC_KEY_LENGTH]);
hasher.update(0u64.to_le_bytes());
}
hasher.finalize().into()
} }
pub fn total_balance(&self) -> u64 { pub fn total_balance(&self) -> u64 {

View File

@ -21,3 +21,4 @@ cl = { path = "../../cl/cl" }
ledger = { path = "../../cl/ledger" } ledger = { path = "../../cl/ledger" }
ledger_proof_statements = { path = "../../cl/ledger_proof_statements" } ledger_proof_statements = { path = "../../cl/ledger_proof_statements" }
goas_proof_statements = { path = "../proof_statements" } goas_proof_statements = { path = "../proof_statements" }
rayon = "1.10.0"

View File

@ -1,10 +1,13 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use cl::{BalanceWitness, BundleWitness, NoteWitness, NullifierNonce}; use cl::{BalanceWitness, BundleWitness, NoteWitness, Nullifier, NullifierNonce};
use common::{new_account, BoundTx, Deposit, SignedBoundTx, Tx, Withdraw}; use common::{new_account, BoundTx, Deposit, SignedBoundTx, Tx, Withdraw};
use executor::ZoneNotes; use executor::ZoneNotes;
use goas_proof_statements::user_note::{UserAtomicTransfer, UserIntent}; use goas_proof_statements::user_note::{UserAtomicTransfer, UserIntent};
use ledger::DeathProof;
use rayon::prelude::*;
#[test] #[test]
fn test_atomic_transfer() { fn test_atomic_transfer() {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
@ -86,8 +89,9 @@ fn test_atomic_transfer() {
&mut alice, &mut alice,
); );
let death_proofs = BTreeMap::from_iter([ let death_proof = |idx: usize| -> (Nullifier, DeathProof) {
( match idx {
0 => (
alice_intent_in.nullifier(), alice_intent_in.nullifier(),
executor::prove_user_atomic_transfer(UserAtomicTransfer { executor::prove_user_atomic_transfer(UserAtomicTransfer {
user_note: atomic_transfer_ptx.input_witness(0), user_note: atomic_transfer_ptx.input_witness(0),
@ -96,10 +100,11 @@ fn test_atomic_transfer() {
zone_b: atomic_transfer_ptx.output_witness(2), zone_b: atomic_transfer_ptx.output_witness(2),
zone_a_roots: zone_a_end.state.state_roots(), zone_a_roots: zone_a_end.state.state_roots(),
zone_b_roots: zone_b_end.state.state_roots(), zone_b_roots: zone_b_end.state.state_roots(),
withdraw_tx: withdraw_inclusion, withdraw_tx: withdraw_inclusion.clone(),
deposit_tx: deposit_inclusion, deposit_tx: deposit_inclusion.clone(),
}), }),
), ),
1 => {
( (
zone_a_start.state_input_witness().nullifier(), zone_a_start.state_input_witness().nullifier(),
executor::prove_zone_stf( executor::prove_zone_stf(
@ -109,7 +114,9 @@ fn test_atomic_transfer() {
atomic_transfer_ptx.output_witness(0), // output state note atomic_transfer_ptx.output_witness(0), // output state note
atomic_transfer_ptx.output_witness(1), // output funds note atomic_transfer_ptx.output_witness(1), // output funds note
), ),
), )
}
2 => {
( (
zone_a_start.fund_input_witness().nullifier(), zone_a_start.fund_input_witness().nullifier(),
executor::prove_zone_fund_constraint( executor::prove_zone_fund_constraint(
@ -117,7 +124,9 @@ fn test_atomic_transfer() {
atomic_transfer_ptx.output_witness(0), // output state note atomic_transfer_ptx.output_witness(0), // output state note
&zone_a_end.state, &zone_a_end.state,
), ),
), )
}
3 => {
( (
zone_b_start.state_input_witness().nullifier(), zone_b_start.state_input_witness().nullifier(),
executor::prove_zone_stf( executor::prove_zone_stf(
@ -127,7 +136,9 @@ fn test_atomic_transfer() {
atomic_transfer_ptx.output_witness(2), // output state note atomic_transfer_ptx.output_witness(2), // output state note
atomic_transfer_ptx.output_witness(3), // output funds note atomic_transfer_ptx.output_witness(3), // output funds note
), ),
), )
}
4 => {
( (
zone_b_start.fund_input_witness().nullifier(), zone_b_start.fund_input_witness().nullifier(),
executor::prove_zone_fund_constraint( executor::prove_zone_fund_constraint(
@ -135,8 +146,13 @@ fn test_atomic_transfer() {
atomic_transfer_ptx.output_witness(2), // output state note (output #0) atomic_transfer_ptx.output_witness(2), // output state note (output #0)
&zone_b_end.state, &zone_b_end.state,
), ),
), )
]); }
_ => panic!("Invalid idx"),
}
};
let death_proofs = (0..5).into_par_iter().map(death_proof).collect();
let user_ptx_proof = let user_ptx_proof =
ledger::partial_tx::ProvedPartialTx::prove(&user_ptx, BTreeMap::new(), &[]) ledger::partial_tx::ProvedPartialTx::prove(&user_ptx, BTreeMap::new(), &[])