Skip to content

Commit

Permalink
fix: backend for updated v2 contract
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Feb 27, 2024
1 parent 627e382 commit 6dbc747
Show file tree
Hide file tree
Showing 18 changed files with 36,740 additions and 52,259 deletions.
15 changes: 15 additions & 0 deletions backend/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ fn main() {
"Summa",
"Summa",
),
(
"src/contracts/generated/verifying_key.rs",
"Halo2VerifyingKey",
"VerifyingKey",
),
(
"src/contracts/generated/snark_verifier.rs",
"Verifier",
"SnarkVerifier",
),
(
"src/contracts/generated/grandsum_verifier.rs",
"GrandSumVerifier",
"GrandSumVerifier",
),
(
"src/contracts/generated/inclusion_verifier.rs",
"InclusionVerifier",
Expand Down
76 changes: 48 additions & 28 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
use std::{error::Error, fs::File, io::BufReader, io::Write};

use ethers::types::U256;
use halo2_proofs::halo2curves::bn256::{Fr as Fp, G1Affine};
use halo2_proofs::halo2curves::bn256::Fr as Fp;
use serde_json::{from_reader, to_string_pretty};

use summa_backend::{
apis::{address_ownership::AddressOwnership, round::Round},
apis::{
address_ownership::AddressOwnership,
round::{KZGProof, Round},
},
contracts::signer::{AddressInput, SummaSigner},
tests::initialize_test_env,
};
Expand All @@ -17,7 +20,7 @@ use summa_solvency::{
},
cryptocurrency::Cryptocurrency,
entry::Entry,
utils::{big_uint_to_fp, parse_csv_to_entries},
utils::parse_csv_to_entries,
};

const K: u32 = 17;
Expand Down Expand Up @@ -65,7 +68,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
// 2. Submit Commitment
//
// Initialize the `Round` instance to submit the liability commitment.
let params_path = "ptau/hermez-raw-17";
let entry_csv = "../csv/entry_16.csv";
let mut entries: Vec<Entry<N_CURRENCIES>> = vec![Entry::init_empty(); N_USERS];
let mut cryptos = vec![Cryptocurrency::init_empty(); N_CURRENCIES];
Expand All @@ -77,12 +79,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
UnivariateGrandSumConfig<N_CURRENCIES, N_USERS>,
>::init(entries.to_vec());

// This ptau file is also utilized in the generation of the verifier contract.
// It corresponds to the same file used in the `gen_verifier.rs` script.
let params_path = "../backend/ptau/hermez-raw-17";
let (params, pk, vk) =
generate_setup_artifacts(K, None, &univariate_grand_sum_circuit).unwrap();
generate_setup_artifacts(K, Some(params_path), &univariate_grand_sum_circuit).unwrap();

// Create a proof
let instances = vec![Fp::one(); 1]; // This instance is necessary to verify proof on solidity verifier.
let (zk_snark_proof, advice_polys, _omega) = full_prover(
let (zk_snark_proof, advice_polys, _) = full_prover(
&params,
&pk,
univariate_grand_sum_circuit.clone(),
Expand All @@ -91,12 +96,17 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Using the `round` instance, the commitment is dispatched to the Summa contract with the `dispatch_commitment` method.
let timestamp = 1u64;
let mut round =
Round::<N_CURRENCIES, N_POINTS, N_USERS>::new(&signer, advice_polys, params, vk, 1)
.unwrap();
let mut round = Round::<N_CURRENCIES, N_POINTS, N_USERS>::new(
&signer,
zk_snark_proof,
advice_polys,
params,
vk,
timestamp,
);

// // Sends the commitment, which should ideally complete without errors.
// round.dispatch_commitment().await?;
// Sends the commitment, which should ideally complete without errors.
round.dispatch_commitment().await?;

println!("2. Commitment is submitted successfully!");

Expand All @@ -118,33 +128,43 @@ async fn main() -> Result<(), Box<dyn Error>> {

// 4. Verify Inclusion Proof
//
// The `snapshot_time` denotes the specific moment when entries were created for the Merkle sum tree.
// The `snapshot_time` denotes the specific moment when entries were created for polynomal encoding.
// This timestamp is established during the initialization of the Round instance.
let snapshot_time = U256::from(1);
let snapshot_time = U256::from(timestamp);

// When verifying the inclusion proof from the user's perspective, the user have to fetch `proof`.
// Assume that the `proof` file has been downloaded from the CEX.
let proof_file = File::open(format!("user_{}_proof.json", USER_INDEX))?;
let reader = BufReader::new(proof_file);
// let downloaded_inclusion_proof: KZGInclusionProof = from_reader(reader)?;

// TODO: fix after the contract is concrete
// // Get `mst_root` from contract. the `mst_root` is disptached by CEX with specific time `snapshot_time`.
// let commitment = summa_contract.commitments(snapshot_time).call().await?;
let downloaded_inclusion_proof: KZGProof = from_reader(reader)?;

// Fetch commitment data from the contract with timestamp, `snapshot_time`.
let commitment = summa_contract.commitments(snapshot_time).call().await?;

// // Match the `mst_root` with the `root_hash` derived from the proof.
// assert_eq!(commitment, public_inputs[1]);
// Ensure the length of the commitment matches the expected size for the number of points.
assert_eq!(commitment.to_vec().len(), 0x40 * N_POINTS);

// // Validate the inclusion proof using the contract verifier.
// let proof = inclusion_proof.get_proof();
// let verification_result = summa_contract
// .verify_inclusion_proof(proof.clone(), public_inputs.clone(), snapshot_time)
// .await?;
// Validate the inclusion proof using the contract verifier.
let mut verification_result = false;

// println!(
// "4. Verifying the proof on contract veirifer for User #{}: {}",
// USER_INDEX, verification_result
// );
if let Some(challenges) = downloaded_inclusion_proof.get_challenge().as_ref() {
verification_result = summa_contract
.verify_inclusion_proof(
snapshot_time,
inclusion_proof.get_proof().clone(),
challenges.clone(),
inclusion_proof.get_input_values().clone(),
)
.await?;
} else {
eprintln!("No challenges found in the proof, This may not a inclusion proof");
}

println!(
"4. Verifying the proof on contract veirifer for User #{}: {}",
USER_INDEX, verification_result
);

// Wrapping up
drop(anvil);
Expand Down
10 changes: 5 additions & 5 deletions backend/scripts/update_verifier_contract.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash
set -e

# Build the verifier contracts
# Build the verifier contract
echo "1. Building verifier contracts"
cd ../zk_prover
cargo run --release --example gen_inclusion_verifier
cargo run --release --bin generate_verifier

# Generate Commitment for Merkle Sum Tree
echo "2. Generate Commitment for Merkle Sum Tree"
# Generate Commitment and Proofs for Encoded Polynomials
echo "2. Generate Commitment and Proofs for Encoded Polynomials"
cd ../zk_prover
cargo run --release --example gen_commitment
cargo run --release --bin generate_commitment_and_proofs

# Deploy contracts to local environment
echo "3. Deploying contracts to local environment"
Expand Down
Loading

0 comments on commit 6dbc747

Please sign in to comment.