Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mst efficiency improvement #188

Merged
Merged
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e0e8ce2
feat: tree building rules
enricobottazzi Nov 21, 2023
134020c
feat: add `get_middle_node_hash_preimage` and `get_middle_node_hash_p…
enricobottazzi Nov 21, 2023
b236bfa
feat: update `generate_proof`
enricobottazzi Nov 21, 2023
698dce6
fix: `generate_proof`
enricobottazzi Nov 21, 2023
d62f231
fix: modify middle nodes hashing logic in circuit
enricobottazzi Nov 21, 2023
63f89d0
feat: update `MerkleProof`
enricobottazzi Nov 21, 2023
d95feef
feat: add further constraint on `MstInclusionCircuit`
enricobottazzi Nov 21, 2023
68ab601
feat: rm `test_balance_not_in_range`
enricobottazzi Nov 21, 2023
eda4ad2
refactor: update `MerkleProof`
enricobottazzi Nov 21, 2023
41f2257
chore: fix `backend`
enricobottazzi Nov 21, 2023
efe6b87
test: fix `backend`
enricobottazzi Nov 21, 2023
ac0cecd
fix: testing
enricobottazzi Nov 21, 2023
e3ba8e6
fix: modify `MstInclusionCircuit` struct
enricobottazzi Nov 21, 2023
8d9ee86
Revert "fix: modify `MstInclusionCircuit` struct"
enricobottazzi Nov 21, 2023
11f533f
chore: update `benches`
enricobottazzi Nov 21, 2023
fc44e01
fix: move poseidon hasher APIs to `Node`
enricobottazzi Nov 27, 2023
94404ed
chore: add guide on `MerkleProof `
enricobottazzi Nov 27, 2023
fe9a72f
chore: update `where` clause in backend
enricobottazzi Nov 27, 2023
1775ca7
chore: update comments
enricobottazzi Nov 27, 2023
06a15e4
chore: move `generate_leaf_hash` outside of `zk_prover`
enricobottazzi Nov 27, 2023
559ac3a
chore: add `ethers` legacy feature
enricobottazzi Nov 27, 2023
cc7d02e
Merge branch 'v1-improvements-and-consolidation' into mst-efficiency-…
enricobottazzi Nov 27, 2023
79cbe2c
chore: update verifier contract
enricobottazzi Nov 27, 2023
d84aa36
fix: removed minting erc20 on the backend test
sifnoc Nov 27, 2023
be15a56
chore: rm unused imports
enricobottazzi Nov 28, 2023
ae539a2
feat: remove `Node::leaf()`
enricobottazzi Nov 28, 2023
ac396ce
feat: rm `Node::middle()`
enricobottazzi Nov 28, 2023
69d049c
Revert "feat: remove `Node::leaf()`"
enricobottazzi Nov 28, 2023
5dea9ed
chore: DRY code
enricobottazzi Nov 28, 2023
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
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.

5 changes: 3 additions & 2 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -13,12 +13,13 @@ futures = "0.3.28"
num-bigint = "0.4.3"
serde = { version = "1.0.166", features = ["derive"] }
snark-verifier-sdk = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", version = "0.1.1" }
ethers = { version = "2.0.7", default-features = false, features = ["ethers-solc"] }
ethers = { version = "2.0.7", default-features = false, features = ["ethers-solc", "legacy"] }
reqwest = { version = "0.11", features = ["json"] }
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"] }
ethers = { version = "2.0.7", default-features = false, features = ["ethers-solc", "legacy"] }
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ We have provided a bash script to automate the process of updating the verifier
Ensure you have the necessary permissions to execute the script.

```
backend $ chmod +x scripts/update_verifier_contract.sh
backend $ scripts/update_verifier_contract.sh
```

## Summa solvency flow example
7 changes: 4 additions & 3 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
@@ -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;
@@ -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`.
9 changes: 7 additions & 2 deletions backend/scripts/update_verifier_contract.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -6,8 +6,13 @@ echo "1. Building verifier contracts"
cd ../zk_prover
cargo run --release --example gen_inclusion_verifier

# Generate Commitment for Merkle Sum Tree
echo "2. Generate Commitment for Merkle Sum Tree"
cd ../zk_prover
cargo run --release --example gen_commitment

# Deploy contracts to local environment
echo "2. Deploying contracts to local environment"
echo "3. Deploying contracts to local environment"
cd ../contracts
npm install
npx hardhat node &
@@ -16,7 +21,7 @@ sleep 5
npx hardhat run scripts/deploy.ts --network localhost

# Generate interface files for Backend
echo "3. Generating interface files for Backend"
echo "4. Generating interface files for Backend"
cd ../backend
cargo build

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()
}
30 changes: 20 additions & 10 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
@@ -55,20 +55,23 @@ impl<const LEVELS: usize, const N_ASSETS: usize, const N_BYTES: usize>
Round<'_, LEVELS, N_ASSETS, N_BYTES>
where
[usize; N_ASSETS + 1]: Sized,
[usize; 2 * (1 + N_ASSETS)]: Sized,
[usize; N_ASSETS + 2]: Sized,
{
pub fn new<'a>(
signer: &'a SummaSigner,
mst: Box<dyn Tree<N_ASSETS, N_BYTES>>,
asset_csv_path: &str,
params_path: &str,
timestamp: u64,
) -> Result<Round<'a, LEVELS, N_ASSETS, N_BYTES>, Box<dyn Error>> {
) -> Result<Round<'a, LEVELS, N_ASSETS, N_BYTES>, Box<dyn Error>>
sifnoc marked this conversation as resolved.
Show resolved Hide resolved
where
[(); N_ASSETS + 2]: Sized,
{
Ok(Round {
timestamp,
snapshot: Snapshot::<LEVELS, N_ASSETS, N_BYTES>::new(mst, asset_csv_path, params_path)
.unwrap(),
signer: &signer,
signer,
})
}

@@ -102,7 +105,10 @@ where
pub fn get_proof_of_inclusion(
&self,
user_index: usize,
) -> Result<MstInclusionProof, &'static str> {
) -> Result<MstInclusionProof, &'static str>
where
[(); N_ASSETS + 2]: Sized,
{
Ok(self
.snapshot
.generate_proof_of_inclusion(user_index)
@@ -114,13 +120,16 @@ impl<const LEVELS: usize, const N_ASSETS: usize, const N_BYTES: usize>
Snapshot<LEVELS, N_ASSETS, N_BYTES>
where
[usize; N_ASSETS + 1]: Sized,
[usize; 2 * (1 + N_ASSETS)]: Sized,
[usize; N_ASSETS + 2]: Sized,
{
pub fn new(
mst: Box<dyn Tree<N_ASSETS, N_BYTES>>,
asset_csv_path: &str,
params_path: &str,
) -> Result<Snapshot<LEVELS, N_ASSETS, N_BYTES>, Box<dyn std::error::Error>> {
) -> Result<Snapshot<LEVELS, N_ASSETS, N_BYTES>, Box<dyn std::error::Error>>
where
[(); N_ASSETS + 2]: Sized,
sifnoc marked this conversation as resolved.
Show resolved Hide resolved
{
let assets_state = parse_asset_csv::<&str, N_ASSETS>(asset_csv_path).unwrap();

let mst_inclusion_circuit = MstInclusionCircuit::<LEVELS, N_ASSETS, N_BYTES>::init_empty();
@@ -143,11 +152,12 @@ where
pub fn generate_proof_of_inclusion(
&self,
user_index: usize,
) -> Result<MstInclusionProof, &'static str> {
) -> Result<MstInclusionProof, &'static str>
where
[(); N_ASSETS + 2]: Sized,
{
let merkle_proof = self.mst.generate_proof(user_index).unwrap();
let user_entry = self.mst.get_entry(user_index).clone();
let circuit =
MstInclusionCircuit::<LEVELS, N_ASSETS, N_BYTES>::init(merkle_proof, user_entry);
let circuit = MstInclusionCircuit::<LEVELS, N_ASSETS, N_BYTES>::init(merkle_proof);

// Currently, default manner of generating a inclusion proof for solidity-verifier.
let calldata = gen_proof_solidity_calldata(
2 changes: 1 addition & 1 deletion backend/src/contracts/abi/InclusionVerifier.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/src/contracts/abi/Summa.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/src/contracts/deployments.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"31337":{"address":"0xcf7ed3acca5a467e9e704c703e8d87f634fb0fc9"}}
{"31337":{"address":"0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"}}
Loading