Skip to content

Commit

Permalink
feat: using latest indexed merkle tree crate (#58)
Browse files Browse the repository at this point in the history
* feat: using latest indexed merkle tree crate using non membership proofs for effiency

* fix: forgot to add new file
  • Loading branch information
sebasti810 authored Jul 14, 2024
1 parent accacf2 commit 2d11b7b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ clap = { version = "4.3.2", features = ["derive"] }
config = "0.14.0"
fs2 = "0.4.3"
thiserror = "1.0.50"
indexed-merkle-tree = { git = "https://github.com/deltadevsde/indexed-merkle-tree", rev = "5f1836a99789c707f4636fe379b60e6294753aaf" }
indexed-merkle-tree = "0.3.2"
dotenvy = "0.15.7"
ahash = "0.8.7"
celestia-rpc = "0.2.0"
Expand Down
25 changes: 13 additions & 12 deletions src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use base64::engine::{general_purpose, Engine as _};
use ed25519::Signature;
use indexed_merkle_tree::{node::Node, sha256, tree::Proof};
use mockall::predicate::*;
use mockall::*;
use mockall::{predicate::*, *};
use redis::{Client, Commands, Connection};
use serde::{Deserialize, Serialize};
use std::process::Command;
use std::sync::MutexGuard;
use std::thread::sleep;
use std::time::Duration;
use std::{self, fmt::Display, sync::Mutex};

use crate::cfg::RedisConfig;
use crate::utils::Signable;
use std::{
self,
fmt::Display,
process::Command,
sync::{Mutex, MutexGuard},
thread::sleep,
time::Duration,
};

use crate::{
cfg::RedisConfig,
error::{DatabaseError, DeimosError, DeimosResult, GeneralError},
utils::parse_json_to_proof,
utils::{parse_json_to_proof, Signable},
};

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
Expand Down Expand Up @@ -455,7 +456,7 @@ impl Database for RedisConnections {

// add the empty hash to the input order as first node
input_con
.rpush::<&str, String, u32>("input_order", empty_hash.clone())
.rpush::<String, String, u32>("input_order".to_string(), empty_hash.clone())
.map_err(|_| {
DatabaseError::WriteError(format!("empty hash as first entry in input order"))
})?;
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bellman::groth16::{self, VerifyingKey};
use bls12_381::{Bls12, Scalar};
use ed25519::Signature;
use ed25519_dalek::{Verifier, VerifyingKey as Ed25519VerifyingKey};
use indexed_merkle_tree::tree::{InsertProof, MerkleProof, Proof, UpdateProof};
use indexed_merkle_tree::tree::{InsertProof, NonMembershipProof, Proof, UpdateProof};
use rand::rngs::OsRng;

/// Checks if a given public key in the list of `ChainEntry` objects has been revoked.
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn decode_public_key(pub_key_str: &String) -> DeimosResult<Ed25519VerifyingK

pub fn validate_proof(proof_value: String) -> DeimosResult<()> {
if let Ok((non_membership_proof, first_proof, second_proof)) =
serde_json::from_str::<(MerkleProof, UpdateProof, UpdateProof)>(&proof_value)
serde_json::from_str::<(NonMembershipProof, UpdateProof, UpdateProof)>(&proof_value)
{
let insertion_proof = InsertProof {
non_membership_proof,
Expand Down
30 changes: 20 additions & 10 deletions src/zk_snark/mod.rs → src/zk_snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ pub fn decode_and_convert_to_g2affine(encoded_data: &String) -> Result<G2Affine,
}

fn unpack_and_process(proof: &MerkleProof) -> Result<(Scalar, &Vec<Node>), DeimosError> {
match (&proof.root_hash, &proof.path) {
(Some(hex_root), Some(path)) if !path.is_empty() => {
let scalar_root = hex_to_scalar(hex_root).map_err(DeimosError::General)?;
Ok((scalar_root, path))
}
_ => Err(DeimosError::Proof(ProofError::ProofUnpackError(format!(
"proof path is empty for root hash "
)))),
if !proof.path.is_empty() {
let root = hex_to_scalar(&proof.root_hash)?;
Ok((root, &proof.path))
} else {
Err(DeimosError::Proof(ProofError::ProofUnpackError(format!(
"proof path is empty for root hash {}",
proof.root_hash
))))
}
}

Expand Down Expand Up @@ -213,18 +213,28 @@ mod tests {
let ford = sha256(&"Ford".to_string());
let sebastian = sha256(&"Sebastian".to_string());
let pusch = sha256(&"Pusch".to_string());
let ethan = sha256(&"Ethan".to_string());
let triple_zero = sha256(&"000".to_string());

let mut ryans_node = Node::new_leaf(true, true, ryan, ford, TAIL.to_string());
let mut sebastians_node = Node::new_leaf(true, true, sebastian, pusch, TAIL.to_string());
let mut ethans_node = Node::new_leaf(true, true, ethan, triple_zero, TAIL.to_string());

// generate proofs for the two nodes
let first_insert_proof = tree.insert_node(&mut ryans_node).unwrap();
let second_insert_proof = tree.insert_node(&mut sebastians_node).unwrap();
let third_insert_proof = tree.insert_node(&mut ethans_node).unwrap();

// create zkSNARKs for the two proofs
let first_insert_zk_snark = Proof::Insert(first_insert_proof);
let second_insert_zk_snark = Proof::Insert(second_insert_proof);
let third_insert_zk_snark = Proof::Insert(third_insert_proof);

let proofs = vec![first_insert_zk_snark, second_insert_zk_snark];
let proofs = vec![
first_insert_zk_snark,
second_insert_zk_snark,
third_insert_zk_snark,
];
let current_commitment = tree.get_commitment().unwrap();

let batched_proof =
Expand Down Expand Up @@ -607,7 +617,7 @@ impl Circuit<Scalar> for HashChainEntryCircuit {
impl InsertMerkleProofCircuit {
pub fn new(proof: &InsertProof) -> Result<InsertMerkleProofCircuit, DeimosError> {
let (non_membership_root, non_membership_path) =
unpack_and_process(&proof.non_membership_proof)?;
unpack_and_process(&proof.non_membership_proof.merkle_proof)?;

let first_merkle_circuit = UpdateMerkleProofCircuit::new(&proof.first_proof)?;
let second_merkle_circuit = UpdateMerkleProofCircuit::new(&proof.second_proof)?;
Expand Down

0 comments on commit 2d11b7b

Please sign in to comment.