Просмотр исходного кода

smt2: replace generic_const_exprs unstable rust feature with temp workaround that can easily be changed once the feature exists in rust proper

zero 2 лет назад
Родитель
Сommit
030d532222
3 измененных файлов с 28 добавлено и 19 удалено
  1. 16 12
      src/sdk/src/crypto/smt2/mod.rs
  2. 12 6
      src/sdk/src/crypto/smt2/test.rs
  3. 0 1
      src/sdk/src/lib.rs

+ 16 - 12
src/sdk/src/crypto/smt2/mod.rs

@@ -84,7 +84,7 @@ pub struct MemoryStorage<F: FieldElement> {
 }
 
 impl<F: FieldElement> MemoryStorage<F> {
-    fn new() -> Self {
+    pub fn new() -> Self {
         Self { tree: HashMap::new() }
     }
 }
@@ -110,27 +110,31 @@ impl<F: FieldElement> StorageAdapter for MemoryStorage<F> {
 #[derive(Debug)]
 pub struct SparseMerkleTree<
     const N: usize,
+    // M = N + 1
+    const M: usize,
     F: FieldElement,
     H: FieldHasher<F, 2>,
     S: StorageAdapter<Value = F>,
-> where
-    [(); N + 1]:,
-{
+> {
     /// A map from leaf indices to leaf data stored as field elements.
     store: S,
     /// The hasher used to build the Merkle tree.
     hasher: H,
     /// An array of empty hashes hashed with themselves `N` times.
-    empty_nodes: [F; N + 1],
+    empty_nodes: [F; M],
 }
 
-impl<const N: usize, F: FieldElement, H: FieldHasher<F, 2>, S: StorageAdapter<Value = F>>
-    SparseMerkleTree<N, F, H, S>
-where
-    [(); N + 1]:,
+impl<
+        const N: usize,
+        const M: usize,
+        F: FieldElement,
+        H: FieldHasher<F, 2>,
+        S: StorageAdapter<Value = F>,
+    > SparseMerkleTree<N, M, F, H, S>
 {
     /// Creates a new SMT
     pub fn new(store: S, hasher: H, empty_leaf: F) -> Self {
+        assert_eq!(M, N + 1);
         let empty_nodes = gen_empty_nodes(&hasher, empty_leaf);
 
         Self { store, hasher, empty_nodes }
@@ -258,11 +262,11 @@ impl<const N: usize, F: FieldElement, H: FieldHasher<F, 2>> Path<N, F, H> {
 /// of the SMT.
 ///
 /// Ordering is depth-wise starting from root going down.
-pub fn gen_empty_nodes<const N: usize, F: FieldElement, H: FieldHasher<F, 2>>(
+pub fn gen_empty_nodes<const M: usize, F: FieldElement, H: FieldHasher<F, 2>>(
     hasher: &H,
     empty_leaf: F,
-) -> [F; N + 1] {
-    let mut empty_nodes = [F::ZERO; N + 1];
+) -> [F; M] {
+    let mut empty_nodes = [F::ZERO; M];
 
     let mut empty_node = empty_leaf;
     for item in empty_nodes.iter_mut().rev() {

+ 12 - 6
src/sdk/src/crypto/smt2/test.rs

@@ -25,7 +25,7 @@ use rand::rngs::OsRng;
 fn empties() {
     let hasher = Poseidon::<Fp, 2>::new();
     let empty_leaf = Fp::from(0);
-    let empty_nodes = gen_empty_nodes::<3, _, _>(&hasher, empty_leaf);
+    let empty_nodes = gen_empty_nodes::<{ 3 + 1 }, _, _>(&hasher, empty_leaf);
 
     let empty_node1 = hasher.hash([empty_leaf, empty_leaf]);
     let empty_node2 = hasher.hash([empty_node1, empty_node1]);
@@ -44,8 +44,11 @@ fn poseidon_smt() {
     let empty_leaf = Fp::from(0);
 
     let store = MemoryStorage::<Fp>::new();
-    let mut smt =
-        SparseMerkleTree::<HEIGHT, _, _, _>::new(store, hasher.clone(), empty_leaf.clone());
+    let mut smt = SparseMerkleTree::<HEIGHT, { HEIGHT + 1 }, _, _, _>::new(
+        store,
+        hasher.clone(),
+        empty_leaf.clone(),
+    );
 
     // Both reprs should match
     assert_eq!(Fp::from(1).as_biguint(), BigUint::from(1u32));
@@ -58,7 +61,7 @@ fn poseidon_smt() {
     ];
     smt.insert_batch(leaves.clone());
 
-    let empty_nodes = gen_empty_nodes::<HEIGHT, _, _>(&hasher, empty_leaf);
+    let empty_nodes = gen_empty_nodes::<{ HEIGHT + 1 }, _, _>(&hasher, empty_leaf);
 
     let hash1 = leaves[0].1;
     let hash2 = leaves[1].1;
@@ -114,8 +117,11 @@ fn poseidon_smt_incl_proof() {
     let empty_leaf = Fp::from(0);
 
     let store = MemoryStorage::<Fp>::new();
-    let mut smt =
-        SparseMerkleTree::<HEIGHT, _, _, _>::new(store, hasher.clone(), empty_leaf.clone());
+    let mut smt = SparseMerkleTree::<HEIGHT, { HEIGHT + 1 }, _, _, _>::new(
+        store,
+        hasher.clone(),
+        empty_leaf.clone(),
+    );
 
     let leaves = vec![
         (Fp::from(1), Fp::random(&mut OsRng)),

+ 0 - 1
src/sdk/src/lib.rs

@@ -15,7 +15,6 @@
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
-#![feature(generic_const_exprs)]
 
 pub use bridgetree;
 pub use num_bigint;