Skip to content

Commit

Permalink
chore: move generate_leaf_hash outside of zk_prover
Browse files Browse the repository at this point in the history
  • Loading branch information
enricobottazzi committed Nov 27, 2023
1 parent 1775ca7 commit 06a15e4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions backend/Cargo.lock

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

1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde_json = "1.0.64"
tokio = { version = "1.7.1", features = ["full"] }
base64 = "0.13"
bincode = "1.3.3"
num-traits = "0.2.14"

[build-dependencies]
ethers = { version = "2.0.7", default-features = false, features = ["ethers-solc"] }
7 changes: 4 additions & 3 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use serde_json::{from_reader, to_string_pretty};
use summa_backend::{
apis::{
address_ownership::AddressOwnership,
leaf_hash_from_inputs,
round::{MstInclusionProof, Round},
},
contracts::signer::{AddressInput, SummaSigner},
tests::initialize_test_env,
};
use summa_solvency::merkle_sum_tree::{utils::generate_leaf_hash, MerkleSumTree};
use summa_solvency::merkle_sum_tree::MerkleSumTree;

const N_ASSETS: usize = 2;
const USER_INDEX: usize = 0;
Expand Down Expand Up @@ -111,12 +112,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
// It's assumed that both `user_name` and `balances` are provided by the CEX.
// The `balances` represent the user's balances on the CEX at `snapshot_time`.
let user_name = "dxGaEAii".to_string();
let balances = vec![11888, 41163];
let balances = vec!["11888".to_string(), "41163".to_string()];

let leaf_hash = public_inputs[0];
assert_eq!(
leaf_hash,
generate_leaf_hash::<N_ASSETS>(user_name.clone(), balances.clone())
leaf_hash_from_inputs::<N_ASSETS>(user_name.clone(), balances.clone())
);

// Get `mst_root` from contract. the `mst_root` is disptached by CEX with specific time `snapshot_time`.
Expand Down
22 changes: 22 additions & 0 deletions backend/src/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
pub mod address_ownership;
pub mod csv_parser;
pub mod round;

use ethers::types::U256;
use num_bigint::BigUint;
use num_traits::Num;
use summa_solvency::merkle_sum_tree::Entry;

pub fn leaf_hash_from_inputs<const N_ASSETS: usize>(username: String, balances: Vec<String>) -> U256
where
[usize; N_ASSETS + 1]: Sized,
{
// Convert balances to BigUint
let balances: Vec<BigUint> = balances
.iter()
.map(|balance| BigUint::from_str_radix(balance, 10).unwrap())
.collect();

let entry: Entry<N_ASSETS> = Entry::new(username, balances.try_into().unwrap()).unwrap();

// Convert Fp to U256
let hash_str = format!("{:?}", entry.compute_leaf().hash);
U256::from_str_radix(&hash_str, 16).unwrap()
}
19 changes: 0 additions & 19 deletions zk_prover/src/merkle_sum_tree/utils/generate_leaf_hash.rs

This file was deleted.

2 changes: 0 additions & 2 deletions zk_prover/src/merkle_sum_tree/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod build_tree;
mod csv_parser;
mod generate_leaf_hash;
mod operation_helpers;

pub use build_tree::{build_leaves_from_entries, build_merkle_tree_from_leaves};
pub use csv_parser::parse_csv_to_entries;
pub use generate_leaf_hash::generate_leaf_hash;
pub use operation_helpers::*;

0 comments on commit 06a15e4

Please sign in to comment.