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

feat: Tree trait #179

Merged
merged 20 commits into from
Nov 10, 2023
Merged

Conversation

enricobottazzi
Copy link
Member

@enricobottazzi enricobottazzi commented Nov 6, 2023

Replacement of #177

  • Added Tree trait
  • MerkleSumTree now implements Tree trait
  • Modified build_merkle_tree_from_entries utils function to build_merkle_tree_from_leaves to make it more generic.
  • Modified MerkleProof struct to take leaf instead of entry. This makes it more generic.
  • Modified MstInclusion Circuit constructor function to take MerkleProof rather than whole MerkleSumTree

to do @sifnoc :

  • Round should no longer take a csv_path as input. Instead, take an object that implements the Tree trait.
  • generate_proof_of_inclusion API on round should be modified to support the new
  • Update test and example accordingly
  • Import the summa_solvency::MerkleSumTree module inside backend module (First, you include the external library in your Cargo.toml file and then use use statement to bring the module into scope in your library code). Then, expose the imported module to other users of summa_backend library by using pub use statement. In this way, the aggregation-tree repo would only need to use summa_backend as dependency while still being able to access the API needed to build the MerkleSumTree, which is needed for the workers.

sifnoc and others added 6 commits November 3, 2023 13:04
* feat: create bash script for updating verifier interface files in backend

* fix: error propagation with try operator; remove unnecessaries

* refactor: changed data type in 'MstInclusionProof'

* fix: generate solvency verifier contract

* chore: remove left over

* chore: update README

* fix: remove left over; assert term

* fix: update README; small fixes

* feat: Signer accepts address or file path for init
[usize; N_ASSETS + 1]: Sized,
[usize; 2 * (1 + N_ASSETS)]: Sized,
{
verify_proof(proof)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for miscommunication, but I meant that the method implementation should live in the trait directly. There's no benefit of putting it into a separate file. It would be convenient for any developer to click on a method name and see the implementation right away, no need to click through one more level. I suggested to move all the "util" methods to the tree trait as they are common for both trees and also represent the fire functionality of the Merkle tree (not auxiliary utility code).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied the changes in b3990eb. I believe that this can only apply to verify_proof while I cannot define the logic of generate_proof within the Tree trait as this will be different between MerkleSumTree and AggregationMerkleSumTree

Copy link
Contributor

@alxkzmn alxkzmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@enricobottazzi enricobottazzi changed the base branch from master to v1-improvements-and-consolidation November 7, 2023 07:51
@sifnoc
Copy link
Member

sifnoc commented Nov 7, 2023

  • generate_proof_of_inclusion API on round should be modified to support the new

Would you give me some hint to understand about this?

@enricobottazzi
Copy link
Member Author

@sifnoc sorry about that. I think that my comment was cut off. What I meant was to update generate_proof_of_inclusion to reflect the new API of the MstInclusionCircuit => https://github.com/summa-dev/summa-solvency/blob/2f14b3633f4fd0dd6ad9a8f75f82807109e9e57d/zk_prover/src/circuits/merkle_sum_tree.rs#L72

@sifnoc sifnoc force-pushed the enrico-tree-trait branch from 8b44b2f to 3e8c855 Compare November 8, 2023 09:02
@@ -85,6 +85,15 @@ contract Summa is Ownable {
inclusionVerifier = _inclusionVerifier;
}

/*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this @alxkzmn @sifnoc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's leftover from my audit comment, I will remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed c575a29

let signature_csv_path = "src/apis/csv/signatures.csv";

// The signer instance would be shared with `address_ownership` and `round` instances
//
// Using AddressInput::Address to directly provide the summa_contract's address.
// For deployed contracts, if the address is stored in a config file,
// you can alternatively use AddressInput::Path to specify the file's path.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an example of such config file? Can you add it to the comments if possible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is "backend/src/contracts/deployments.json".
I will add comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated c575a29

amount: U256::from(556863),
},
];
assert_eq!(liability_commitment_logs.len(), 0);

// Send sovlecy proof to contract
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed at 5a9120f

1,
)
.unwrap();
let entries = get_sample_entries();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add entry.csv file back here? I think there was a misunderstanding in one of my previous comments. I didn't mean to remove the usage of entry.csv. I just meants that this should no longer be an input for the Round builder. But it's still ok to me to build the mst using let merkle_tree =MerkleSumTree::new("src/merkle_sum_tree/csv/entry_16.csv") and then pass it to Round

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated at 5a9120f

use num_bigint::ToBigUint;
use summa_solvency::merkle_sum_tree::Entry;

pub fn get_sample_entries() -> Vec<Entry<2>> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed at 5a9120f

rpc_url: &str,
summa_address_input: AddressInput,
entry_csv_path: &str,
pub fn new<'a>(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main point of the PR for me was that Round should accept as input an object binded to the Tree trait. Similarly to what you applied to Solvency::init. But I don't know why you applied it to Solvency which is now dismissed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's what you were looking for. Honestly, I was trying to figure out how to define the Tree trait as a concrete type in the SolvencyCircuit struct, but I realized it's impossible without using Box, which is kind of smart pointer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It because the merkle_sum_tree in the solvency::init evaluates some values, like root_hash, not assign into SolvencyCircuit. Anyway I fixed like this:

pub struct Snapshot<const LEVELS: usize, const N_ASSETS: usize, const N_BYTES: usize> {
    mst: Box<dyn Tree<N_ASSETS, N_BYTES>>, // Instead of MerkleSumTree<N_ASSETS, N_BYTES>,
    assets_state: [Asset; N_ASSETS],
    trusted_setup: SetupArtifacts,
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated at 5a9120f

})
}

pub fn generate_proof_of_inclusion(
&self,
user_index: usize,
) -> Result<MstInclusionProof, &'static str> {
let merkle_proof = self.mst.generate_proof(user_index).unwrap();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As soon as you modify Snapshot to take an object with a Binded Trait Tree this object will no longer have the get_entry method as this method doesn't live on the Tree trait. I don't know what's the best way to handle this

Copy link
Member

@sifnoc sifnoc Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, as you already know I added get_entry method in Tree trait. and it works well.
but for removing get_entry from Tree trait, we have to add username and balances fields on the MerkleProof.
the entry is used to get username and balances In the MstInclusionCircuit.

// Assign the entry username
let username = self.assign_value_to_witness(
layouter.namespace(|| "assign entry username"),
big_uint_to_fp(self.entry.username_as_big_uint()),
"entry username",
config.advices[0],
)?;
// Assign the entry balances
let mut current_balances = vec![];
for i in 0..N_ASSETS {
let balance = self.assign_value_to_witness(
layouter.namespace(|| format!("assign entry balance {}", i)),
big_uint_to_fp(&self.entry.balances()[i]),
"entry balance",
config.advices[1],
)?;
current_balances.push(balance);
}

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok let's keep it as it is for now!

If something comes up during the AggregationMerkleSumTree implementation, we can just come back to summa-solvency and fix that

@enricobottazzi enricobottazzi linked an issue Nov 9, 2023 that may be closed by this pull request
proof.public_inputs[0],
.submit_commitment(
mst_root,
root_sums,
self.snapshot.assets_state.to_vec(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to think about for the future: assets come from the same source as the user balances. E.g., when the CEX is building the tree, it knows that it's now querying the ETH balances or BTC balances. That's why I don't think that the assets would live in a separate CSV file. Imagine that when the CEX is querying the balances, it specifies the assets in the query. If we're sticking to CSV, I would imagine a CSV that looks like userId,balance_ETH_ETH,balance_USDT_ETH,.... Won't request changes now but Interesting to know what you think @enricobottazzi @sifnoc

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really an asset "state" per se, it's kind of a mapping of what assets are submitted in the MST to leave the record inside the transaction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It used to be a state when it also contained the asset_balances. Now it doesn't make sense for asset.csv to live in a separate file. Opening an issue.

@enricobottazzi enricobottazzi marked this pull request as ready for review November 10, 2023 08:27
@enricobottazzi enricobottazzi merged commit 978a645 into v1-improvements-and-consolidation Nov 10, 2023
4 checks passed
@enricobottazzi enricobottazzi deleted the enrico-tree-trait branch November 21, 2023 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add AggregationMerkleSumTree to summa-aggregation
3 participants