mirror of
https://github.com/Rate-Limiting-Nullifier/pmtree.git
synced 2026-01-10 13:17:57 -05:00
chore: implement new function for Merkle Tree
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
//! # pmtree
|
||||
//! Persistent Merkle Tree in Rust
|
||||
//!
|
||||
//! ## How it stored
|
||||
//! { [0, 0, ..., 0] : depth }
|
||||
//! { [0, 0, ..., 1] : next_index}
|
||||
//! { Position (tuple, converted to DBKey) : Value}
|
||||
|
||||
pub mod database;
|
||||
pub mod hasher;
|
||||
@@ -17,7 +22,7 @@ pub struct Error(String);
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Denotes keys in a database
|
||||
pub type DBKey = [u8; 32];
|
||||
pub type DBKey = [u8; 8];
|
||||
|
||||
/// Denotes values in a database
|
||||
pub type Value = Vec<u8>;
|
||||
|
||||
46
src/tree.rs
46
src/tree.rs
@@ -1,5 +1,20 @@
|
||||
use crate::*;
|
||||
|
||||
// db[DEPTH_KEY] = depth
|
||||
const DEPTH_KEY: DBKey = [0; 8];
|
||||
|
||||
// db[NEXT_INDEX_KEY] = next_index;
|
||||
const NEXT_INDEX_KEY: DBKey = [1; 8];
|
||||
|
||||
// Denotes keys (depth, index) in Merkle Tree. Can be converted to DBKey
|
||||
struct Key(usize, usize);
|
||||
impl From<Key> for DBKey {
|
||||
fn from(key: Key) -> DBKey {
|
||||
let cantor_pairing = (key.0 + key.1) * (key.0 + key.1 + 1) / 2 + key.1;
|
||||
cantor_pairing.to_be_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// Merkle Tree implementation
|
||||
pub struct MerkleTree<D, H>
|
||||
where
|
||||
@@ -19,7 +34,32 @@ where
|
||||
{
|
||||
/// Creates new `MerkleTree` and store it to the specified path/db
|
||||
pub fn new(depth: usize, dbpath: &str) -> Result<Self> {
|
||||
todo!()
|
||||
// Create new db instance
|
||||
let mut db = D::new(dbpath);
|
||||
|
||||
// Insert depth val into db
|
||||
let depth_val = depth.to_be_bytes().to_vec();
|
||||
db.put(DEPTH_KEY, depth_val)?;
|
||||
|
||||
// Insert next_index val into db
|
||||
let next_index = 0usize;
|
||||
let next_index_val = next_index.to_be_bytes().to_vec();
|
||||
db.put(NEXT_INDEX_KEY, next_index_val)?;
|
||||
|
||||
// Initialize one branch of the `Merkle Tree` from bottom to top
|
||||
let mut prev = H::default_leaf();
|
||||
db.put(Key(depth, 0).into(), prev.into())?;
|
||||
for i in (0..depth).rev() {
|
||||
prev = H::hash(&[prev, prev]);
|
||||
db.put(Key(i + 1, 0).into(), prev.into())?;
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
db,
|
||||
h: H::new(),
|
||||
depth,
|
||||
next_index,
|
||||
})
|
||||
}
|
||||
|
||||
/// Loads existing Merkle Tree from the specified path/db
|
||||
@@ -28,7 +68,7 @@ where
|
||||
}
|
||||
|
||||
/// Inserts a leaf to the next available index
|
||||
pub fn insert(&mut self, leaf: H::Fr) {
|
||||
pub fn insert(&mut self, leaf: H::Fr) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
@@ -38,7 +78,7 @@ where
|
||||
}
|
||||
|
||||
/// Deletes a leaf at the `key` by setting it to its default value
|
||||
pub fn delete(&mut self, key: usize) {
|
||||
pub fn delete(&mut self, key: usize) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user