Skip to content

Commit

Permalink
Mantainance (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricobottazzi authored Dec 1, 2023
1 parent 0ce4a0d commit c2a3ee2
Show file tree
Hide file tree
Showing 45 changed files with 573 additions and 562 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
8 changes: 4 additions & 4 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 @@ -43,7 +43,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.await?;

// Each CEX prepares its own `signature` CSV file.
let signature_csv_path = "src/apis/csv/signatures.csv";
let signature_csv_path = "../csv/signatures.csv";
let mut address_ownership_client = AddressOwnership::new(&signer, signature_csv_path).unwrap();

// Dispatch the proof of address ownership.
Expand All @@ -58,7 +58,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
//
// Initialize the `Round` instance to submit the liability commitment.
let params_path = "ptau/hermez-raw-11";
let entry_csv = "../zk_prover/src/merkle_sum_tree/csv/entry_16.csv";
let entry_csv = "../csv/entry_16.csv";
let mst = MerkleSumTree::new(entry_csv).unwrap();

// Using the `round` instance, the commitment is dispatched to the Summa contract with the `dispatch_commitment` method.
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
2 changes: 1 addition & 1 deletion backend/src/apis/csv_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod tests {

#[test]
fn test_parse_csv_to_signature() {
let path = "src/apis/csv/signatures.csv";
let path = "../csv/signatures.csv";
let address_ownership = parse_signature_csv(path).unwrap();

let first_address_ownership = AddressOwnershipProof {
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 c2a3ee2

Please sign in to comment.