-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add MST leaf update function * Implement code review changes * Fix documentation
- Loading branch information
Showing
12 changed files
with
291 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
username;balances | ||
dxGaEAii;11888,41163 | ||
MBlfbBGI;67823,18651 | ||
lAhWlEWZ;18651,2087 | ||
nuZweYtO;22073,55683 | ||
gbdSwiuY;34897,83296 | ||
RZNneNuP;83296,16881 | ||
YsscHXkp;31699,35479 | ||
RkLzkDun;2086,79732 | ||
HlQlnEYI;30605,11888 | ||
RqkZOFYe;16881,14874 | ||
NjCSRAfD;41163,67823 | ||
pHniJMQY;14874,22073 | ||
dOGIMzKR;10032,10032 | ||
HfMDmNLp;55683,34897 | ||
xPLKzCBl;79731,30605 | ||
AtwIxZHo;35479,31699 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use halo2_proofs::halo2curves::bn256::Fr as Fp; | ||
use num_bigint::BigUint; | ||
|
||
use super::{ | ||
big_uint_to_fp, | ||
utils::{poseidon_entry, poseidon_node}, | ||
}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Node<const N_ASSETS: usize> { | ||
pub hash: Fp, | ||
pub balances: [Fp; N_ASSETS], | ||
} | ||
impl<const N_ASSETS: usize> Node<N_ASSETS> { | ||
/// Builds a "middle" (non-leaf-level) node of the MST | ||
pub fn middle(child_l: &Node<N_ASSETS>, child_r: &Node<N_ASSETS>) -> Node<N_ASSETS> | ||
where | ||
[usize; 2 * (1 + N_ASSETS)]: Sized, | ||
{ | ||
let mut balances_sum = [Fp::zero(); N_ASSETS]; | ||
for (i, balance) in balances_sum.iter_mut().enumerate() { | ||
*balance = child_l.balances[i] + child_r.balances[i]; | ||
} | ||
|
||
Node { | ||
hash: poseidon_node( | ||
child_l.hash, | ||
child_l.balances, | ||
child_r.hash, | ||
child_r.balances, | ||
), | ||
balances: balances_sum, | ||
} | ||
} | ||
|
||
/// Builds a leaf-level node of the MST | ||
pub fn leaf(username: &BigUint, balances: &[BigUint; N_ASSETS]) -> Node<N_ASSETS> | ||
where | ||
[usize; N_ASSETS + 1]: Sized, | ||
{ | ||
Node { | ||
hash: poseidon_entry::<N_ASSETS>( | ||
big_uint_to_fp(username), | ||
balances | ||
.iter() | ||
.map(big_uint_to_fp) | ||
.collect::<Vec<Fp>>() | ||
.try_into() | ||
.unwrap(), | ||
), | ||
//Map the array of balances using big_int_to_fp: | ||
balances: balances | ||
.iter() | ||
.map(big_uint_to_fp) | ||
.collect::<Vec<Fp>>() | ||
.try_into() | ||
.unwrap(), | ||
} | ||
} | ||
} |
Oops, something went wrong.