Browse Source

sdk/crypto/smt: Code style fixes

parazyd 2 years ago
parent
commit
5f922f836f

+ 11 - 9
src/sdk/src/crypto/smt/mod.rs

@@ -58,6 +58,14 @@
 //!   calculated as `leaf_idx = final_level_start_idx + position`.
 //! * **node** - either the leaf values or parent nodes `hash(left, right)`.
 
+use num_bigint::BigUint;
+use std::collections::HashMap;
+// Only used for the type aliases below
+use pasta_curves::pallas;
+
+use crate::error::{ContractError, ContractResult};
+use util::{FieldElement, FieldHasher};
+
 mod empty;
 pub use empty::EMPTY_NODES_FP;
 
@@ -69,14 +77,6 @@ pub use util::Poseidon;
 
 pub mod wasmdb;
 
-use num_bigint::BigUint;
-use std::collections::HashMap;
-// Only used for the type aliases below
-use pasta_curves::pallas;
-
-use crate::error::{ContractError, ContractResult};
-use util::{FieldElement, FieldHasher};
-
 // Bit size for Fp (and Fq)
 pub const SMT_FP_DEPTH: usize = 255;
 pub type PoseidonFp = Poseidon<pallas::Base, 2>;
@@ -119,6 +119,7 @@ impl<F: FieldElement> StorageAdapter for MemoryStorage<F> {
         self.tree.insert(key, value);
         true
     }
+
     fn get(&self, key: &BigUint) -> Option<F> {
         self.tree.get(key).copied()
     }
@@ -207,6 +208,7 @@ impl<
 
             dirty_idxs = new_dirty_idxs;
         }
+
         Ok(())
     }
 
@@ -304,8 +306,8 @@ pub fn gen_empty_nodes<const M: usize, F: FieldElement, H: FieldHasher<F, 2>>(
     empty_leaf: F,
 ) -> [F; M] {
     let mut empty_nodes = [F::ZERO; M];
-
     let mut empty_node = empty_leaf;
+
     for item in empty_nodes.iter_mut().rev() {
         *item = empty_node;
         empty_node = hasher.hash([empty_node, empty_node]);

+ 2 - 1
src/sdk/src/crypto/smt/test.rs

@@ -16,11 +16,12 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use super::*;
 use halo2_proofs::arithmetic::Field;
 use pasta_curves::Fp;
 use rand::rngs::OsRng;
 
+use super::*;
+
 #[test]
 fn check_empties() {
     use empty::EMPTY_NODES_FP;

+ 4 - 1
src/sdk/src/crypto/smt/util.rs

@@ -16,23 +16,26 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use std::marker::PhantomData;
+
 use halo2_gadgets::poseidon::{
     primitives as poseidon,
     primitives::{ConstantLength, P128Pow5T3, Spec},
 };
 use num_bigint::BigUint;
 use pasta_curves::group::ff::{PrimeField, WithSmallOrderMulGroup};
-use std::marker::PhantomData;
 
 pub trait FieldElement: WithSmallOrderMulGroup<3> + Ord + PrimeField {
     fn as_biguint(&self) -> BigUint;
 }
+
 impl FieldElement for pasta_curves::Fp {
     fn as_biguint(&self) -> BigUint {
         let repr = self.to_repr();
         BigUint::from_bytes_le(&repr)
     }
 }
+
 impl FieldElement for pasta_curves::Fq {
     fn as_biguint(&self) -> BigUint {
         let repr = self.to_repr();

+ 4 - 7
src/sdk/src/crypto/smt/wasmdb.rs

@@ -16,6 +16,8 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use num_bigint::BigUint;
+
 use super::{PoseidonFp, SparseMerkleTree, StorageAdapter, SMT_FP_DEPTH};
 use crate::{
     crypto::pasta_prelude::*,
@@ -23,7 +25,6 @@ use crate::{
     msg,
     pasta::pallas,
 };
-use num_bigint::BigUint;
 
 pub type SmtWasmFp = SparseMerkleTree<
     'static,
@@ -61,11 +62,7 @@ impl StorageAdapter for SmtWasmDbStorage {
 
         let mut repr = [0; 32];
         repr.copy_from_slice(&value);
-        let value = pallas::Base::from_repr(repr);
-        if value.is_none().into() {
-            None
-        } else {
-            Some(value.unwrap())
-        }
+
+        pallas::Base::from_repr(repr).into()
     }
 }