Skip to content

Commit

Permalink
chore: replace N_ASSETS with N_CURRENCIES
Browse files Browse the repository at this point in the history
  • Loading branch information
enricobottazzi committed Dec 1, 2023
1 parent a9e4893 commit ce39b18
Show file tree
Hide file tree
Showing 30 changed files with 463 additions and 15,700 deletions.
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ cargo test --release -- --nocapture

### Generating and updating verifier contract for Backend

The verifier contract in the backend were generated using a predefined set of parameters: `N_ASSETS = 2` and `N_BYTES=14`, as indicated [here](https://github.com/summa-dev/summa-solvency/blob/master/zk_prover/examples/gen_inclusion_verifier.rs#L21-L22).
The verifier contract in the backend were generated using a predefined set of parameters: `N_CURRENCIES = 2` and `N_BYTES=14`, as indicated [here](https://github.com/summa-dev/summa-solvency/blob/master/zk_prover/examples/gen_inclusion_verifier.rs#L21-L22).
If you intend to work with different parameters, you'll need to adjust these hard-coded values and then generate new verifier contract.

The process described below assists in both generating the verifier and updating the Summa contract, which integrates the new verifier as constructors.
Expand Down
4 changes: 2 additions & 2 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use summa_backend::{
};
use summa_solvency::merkle_sum_tree::MerkleSumTree;

const N_ASSETS: usize = 2;
const N_CURRENCIES: usize = 2;
const USER_INDEX: usize = 0;

#[tokio::main]
Expand Down Expand Up @@ -109,7 +109,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let leaf_hash = public_inputs[0];
assert_eq!(
leaf_hash,
leaf_hash_from_inputs::<N_ASSETS>(user_name.clone(), balances.clone())
leaf_hash_from_inputs::<N_CURRENCIES>(user_name.clone(), balances.clone())
);

// Get `mst_root` from contract. the `mst_root` is disptached by CEX with specific time `snapshot_time`.
Expand Down
9 changes: 6 additions & 3 deletions backend/src/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ 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
pub fn leaf_hash_from_inputs<const N_CURRENCIES: usize>(
username: String,
balances: Vec<String>,
) -> U256
where
[usize; N_ASSETS + 1]: Sized,
[usize; N_CURRENCIES + 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();
let entry: Entry<N_CURRENCIES> = Entry::new(username, balances.try_into().unwrap()).unwrap();

// Convert Fp to U256
let hash_str = format!("{:?}", entry.compute_leaf().hash);
Expand Down
44 changes: 22 additions & 22 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,35 @@ impl MstInclusionProof {
}
}

pub struct Snapshot<const LEVELS: usize, const N_ASSETS: usize, const N_BYTES: usize> {
pub mst: Box<dyn Tree<N_ASSETS, N_BYTES>>,
pub struct Snapshot<const LEVELS: usize, const N_CURRENCIES: usize, const N_BYTES: usize> {
pub mst: Box<dyn Tree<N_CURRENCIES, N_BYTES>>,
trusted_setup: SetupArtifacts,
}

pub struct Round<'a, const LEVELS: usize, const N_ASSETS: usize, const N_BYTES: usize> {
pub struct Round<'a, const LEVELS: usize, const N_CURRENCIES: usize, const N_BYTES: usize> {
timestamp: u64,
snapshot: Snapshot<LEVELS, N_ASSETS, N_BYTES>,
snapshot: Snapshot<LEVELS, N_CURRENCIES, N_BYTES>,
signer: &'a SummaSigner,
}

impl<const LEVELS: usize, const N_ASSETS: usize, const N_BYTES: usize>
Round<'_, LEVELS, N_ASSETS, N_BYTES>
impl<const LEVELS: usize, const N_CURRENCIES: usize, const N_BYTES: usize>
Round<'_, LEVELS, N_CURRENCIES, N_BYTES>
where
[usize; N_ASSETS + 1]: Sized,
[usize; N_ASSETS + 2]: Sized,
[usize; N_CURRENCIES + 1]: Sized,
[usize; N_CURRENCIES + 2]: Sized,
{
pub fn new<'a>(
signer: &'a SummaSigner,
mst: Box<dyn Tree<N_ASSETS, N_BYTES>>,
mst: Box<dyn Tree<N_CURRENCIES, N_BYTES>>,
params_path: &str,
timestamp: u64,
) -> Result<Round<'a, LEVELS, N_ASSETS, N_BYTES>, Box<dyn Error>>
) -> Result<Round<'a, LEVELS, N_CURRENCIES, N_BYTES>, Box<dyn Error>>
where
[(); N_ASSETS + 2]: Sized,
[(); N_CURRENCIES + 2]: Sized,
{
Ok(Round {
timestamp,
snapshot: Snapshot::<LEVELS, N_ASSETS, N_BYTES>::new(mst, params_path).unwrap(),
snapshot: Snapshot::<LEVELS, N_CURRENCIES, N_BYTES>::new(mst, params_path).unwrap(),
signer: &signer,
})
}
Expand Down Expand Up @@ -114,7 +114,7 @@ where
user_index: usize,
) -> Result<MstInclusionProof, &'static str>
where
[(); N_ASSETS + 2]: Sized,
[(); N_CURRENCIES + 2]: Sized,
{
Ok(self
.snapshot
Expand All @@ -123,17 +123,17 @@ where
}
}

impl<const LEVELS: usize, const N_ASSETS: usize, const N_BYTES: usize>
Snapshot<LEVELS, N_ASSETS, N_BYTES>
impl<const LEVELS: usize, const N_CURRENCIES: usize, const N_BYTES: usize>
Snapshot<LEVELS, N_CURRENCIES, N_BYTES>
where
[usize; N_ASSETS + 1]: Sized,
[usize; N_ASSETS + 2]: Sized,
[usize; N_CURRENCIES + 1]: Sized,
[usize; N_CURRENCIES + 2]: Sized,
{
pub fn new(
mst: Box<dyn Tree<N_ASSETS, N_BYTES>>,
mst: Box<dyn Tree<N_CURRENCIES, N_BYTES>>,
params_path: &str,
) -> Result<Snapshot<LEVELS, N_ASSETS, N_BYTES>, Box<dyn std::error::Error>> {
let mst_inclusion_circuit = MstInclusionCircuit::<LEVELS, N_ASSETS, N_BYTES>::init_empty();
) -> Result<Snapshot<LEVELS, N_CURRENCIES, N_BYTES>, Box<dyn std::error::Error>> {
let mst_inclusion_circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init_empty();

// get k from ptau file name
let parts: Vec<&str> = params_path.split("-").collect();
Expand All @@ -154,10 +154,10 @@ where
user_index: usize,
) -> Result<MstInclusionProof, &'static str>
where
[(); N_ASSETS + 2]: Sized,
[(); N_CURRENCIES + 2]: Sized,
{
let merkle_proof = self.mst.generate_proof(user_index).unwrap();
let circuit = MstInclusionCircuit::<LEVELS, N_ASSETS, N_BYTES>::init(merkle_proof);
let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);

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

Large diffs are not rendered by default.

Loading

0 comments on commit ce39b18

Please sign in to comment.