Skip to content

Commit

Permalink
Fix snarks (#55)
Browse files Browse the repository at this point in the history
* fix: first tests

* fix: snark test
  • Loading branch information
sebasti810 authored Jul 13, 2024
1 parent 8d8bada commit 195a581
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
5 changes: 2 additions & 3 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 = "0.3.1"
indexed-merkle-tree = { git = "https://github.com/deltadevsde/indexed-merkle-tree", rev = "5f1836a99789c707f4636fe379b60e6294753aaf" }
dotenvy = "0.15.7"
ahash = "0.8.7"
celestia-rpc = "0.2.0"
Expand Down
12 changes: 6 additions & 6 deletions src/da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,10 @@ mod da_tests {
let prev_commitment = tree.get_commitment().unwrap();

// insert a first node
let node_1 = create_node("test1", "test2");
let mut node_1 = create_node("test1", "test2");

// generate proof for the first insert
let first_insert_proof = tree.insert_node(&node_1).unwrap();
let first_insert_proof = tree.insert_node(&mut node_1).unwrap();
let first_insert_zk_snark = Proof::Insert(first_insert_proof);

// create bls12 proof for posting
Expand All @@ -530,12 +530,12 @@ mod da_tests {
let prev_commitment = tree.get_commitment().unwrap();

// insert a second and third node
let node_2 = create_node("test3", "test4");
let node_3 = create_node("test5", "test6");
let mut node_2 = create_node("test3", "test4");
let mut node_3 = create_node("test5", "test6");

// generate proof for the second and third insert
let second_insert_proof = tree.insert_node(&node_2).unwrap();
let third_insert_proof = tree.insert_node(&node_3).unwrap();
let second_insert_proof = tree.insert_node(&mut node_2).unwrap();
let third_insert_proof = tree.insert_node(&mut node_3).unwrap();
let second_insert_zk_snark = Proof::Insert(second_insert_proof);
let third_insert_zk_snark = Proof::Insert(third_insert_proof);

Expand Down
38 changes: 29 additions & 9 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, NonMembershipProof, Proof, UpdateProof};
use indexed_merkle_tree::tree::{InsertProof, MerkleProof, 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::<(NonMembershipProof, UpdateProof, UpdateProof)>(&proof_value)
serde_json::from_str::<(MerkleProof, UpdateProof, UpdateProof)>(&proof_value)
{
let insertion_proof = InsertProof {
non_membership_proof,
Expand Down Expand Up @@ -210,24 +210,44 @@ mod tests {

#[test]
fn test_validate_epoch_valid_proof() {
let mut tree = IndexedMerkleTree::new_with_size(4).unwrap();
let mut tree = IndexedMerkleTree::new_with_size(8).unwrap();
let prev_commitment = tree.get_commitment().unwrap();

let ryan = sha256(&"Ryan".to_string());
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 ryans_node = Node::new_leaf(true, true, ryan, ford, Node::TAIL.to_string());
let sebastians_node = Node::new_leaf(true, true, sebastian, pusch, Node::TAIL.to_string());
let mut ethans_node =
Node::new_leaf(true, true, ethan, triple_zero, Node::TAIL.to_string());
let mut ryans_node = Node::new_leaf(true, true, ryan, ford, Node::TAIL.to_string());
let mut sebastians_node =
Node::new_leaf(true, true, sebastian.clone(), pusch, Node::TAIL.to_string());

let first_insert_proof = tree.insert_node(&ryans_node).unwrap();
let second_insert_proof = tree.insert_node(&sebastians_node).unwrap();
let first_insert_proof = tree.insert_node(&mut ryans_node).unwrap();
let third_insert_proof = tree.insert_node(&mut ethans_node).unwrap();
let second_insert_proof = tree.insert_node(&mut sebastians_node).unwrap();

let first_insert_zk_snark = Proof::Insert(first_insert_proof);
let second_insert_zk_snark = Proof::Insert(second_insert_proof);

let proofs = vec![first_insert_zk_snark, second_insert_zk_snark];
let third_insert_zk_snark = Proof::Insert(third_insert_proof.clone());

let updated_seb = sha256(&"Sebastian".to_string());
sebastians_node =
Node::new_leaf(true, true, sebastian, updated_seb, Node::TAIL.to_string());
let index = tree.find_node_index(&sebastians_node).unwrap();
let update_proof = tree.update_node(index, sebastians_node).unwrap();

let update_zk_snark = Proof::Update(update_proof);

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

let batched_proof =
Expand Down
4 changes: 2 additions & 2 deletions src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async fn update_entry(
Ok(_) => {
let new_tree = session.create_tree().unwrap();
let hashed_id = sha256(&signature_with_key.id);
let node = new_tree.find_leaf_by_label(&hashed_id).unwrap();
let mut node = new_tree.find_leaf_by_label(&hashed_id).unwrap();

let proofs = if update_proof {
let new_index = tree.clone().find_node_index(&node).unwrap();
Expand All @@ -126,7 +126,7 @@ async fn update_entry(
format!(r#"{{"Update":{}}}"#, pre_processed_string)
} else {
let pre_processed_string =
serde_json::to_string(&tree.clone().insert_node(&node).unwrap()).unwrap();
serde_json::to_string(&tree.clone().insert_node(&mut node).unwrap()).unwrap();
format!(r#"{{"Insert":{}}}"#, pre_processed_string)
};

Expand Down
26 changes: 13 additions & 13 deletions src/zk_snark/mod.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> {
if !proof.path.is_empty() {
let scalar_root = hex_to_scalar(proof.root_hash.as_str()).map_err(DeimosError::General)?;
Ok((scalar_root, &proof.path))
} else {
Err(DeimosError::Proof(ProofError::ProofUnpackError(format!(
"proof path is empty for root hash {}",
proof.root_hash
))))
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 "
)))),
}
}

Expand Down Expand Up @@ -218,12 +218,12 @@ mod tests {
let ford = sha256(&"Ford".to_string());
let sebastian = sha256(&"Sebastian".to_string());
let pusch = sha256(&"Pusch".to_string());
let ryans_node = Node::new_leaf(true, true, ryan, ford, TAIL.to_string());
let sebastians_node = Node::new_leaf(true, true, sebastian, pusch, TAIL.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());

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

// create zkSNARKs for the two proofs
let first_insert_zk_snark = Proof::Insert(first_insert_proof);
Expand Down Expand Up @@ -602,7 +602,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.merkle_proof)?;
unpack_and_process(&proof.non_membership_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 195a581

Please sign in to comment.