Skip to content

Commit

Permalink
prog
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasti810 authored and distractedm1nd committed Jul 18, 2024
1 parent 63c18f1 commit d2e571c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 122 deletions.
2 changes: 0 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 @@ -52,7 +52,7 @@ clap = { version = "4.3.2", features = ["derive"] }
config = "0.14.0"
fs2 = "0.4.3"
thiserror = "1.0.50"
indexed-merkle-tree = "0.4.1"
indexed-merkle-tree = "0.5.0"
dotenvy = "0.15.7"
ahash = "0.8.7"
celestia-rpc = "0.2.0"
Expand Down
5 changes: 3 additions & 2 deletions src/da/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
};
use async_trait::async_trait;
use ed25519::Signature;
use indexed_merkle_tree::Hash;
use serde::{Deserialize, Serialize};
use std::{self, str::FromStr};

Expand All @@ -14,8 +15,8 @@ pub mod mock;
#[derive(Serialize, Deserialize, Clone)]
pub struct EpochJson {
pub height: u64,
pub prev_commitment: String,
pub current_commitment: String,
pub prev_commitment: Hash,
pub current_commitment: Hash,
pub proof: Bls12Proof,
pub verifying_key: VerifyingKey,
pub signature: Option<String>,
Expand Down
26 changes: 12 additions & 14 deletions src/node_types/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use async_trait::async_trait;
use ed25519_dalek::{Signer, SigningKey};
use indexed_merkle_tree::{node::Node, sha256_mod, tree::IndexedMerkleTree};
use indexed_merkle_tree::{node::Node, sha256_mod, tree::IndexedMerkleTree, Hash};
use std::{self, sync::Arc, time::Duration};
use tokio::{
sync::{
Expand Down Expand Up @@ -222,7 +222,9 @@ impl Sequencer {
let prev_commitment = if epoch > 0 {
let prev_epoch = epoch - 1;
match self.db.get_commitment(&prev_epoch) {
Ok(commitment) => commitment,
Ok(commitment) => {
Hash::from_hex(commitment.as_str()).map_err(DeimosError::MerkleTree)?
}
Err(e) => {
return Err(DatabaseError::ReadError(format!(
"commitment for prev epoch {:?}: {:?}",
Expand Down Expand Up @@ -286,13 +288,9 @@ impl Sequencer {
.db
.get_derived_value(&key.to_string())
.map_err(|e| DatabaseError::ReadError(e.to_string()))?;
Ok(Node::new_leaf(
true,
true,
key.clone(),
value,
Node::TAIL.to_string(),
))
let hash_key = Hash::from_hex(&key).unwrap();
let hash_value = Hash::from_hex(&value).unwrap();
Ok(Node::new_leaf(true, true, hash_key, hash_value, Node::TAIL))
})
.collect();

Expand Down Expand Up @@ -335,8 +333,8 @@ impl Sequencer {
.iter()
.enumerate() // use index
.find(|(_, k)| {
// without dereferencing we compare &&string with &string
label.clone().is_some_and(|l| *k == &l)
let k = Hash::from_hex(k).unwrap();
label.clone().is_some_and(|l| k == l)
})
.map(|(k, _)| k)
});
Expand All @@ -346,9 +344,9 @@ impl Sequencer {
nodes.push(Node::new_leaf(
false,
true,
Node::HEAD.to_string(),
Node::HEAD.to_string(),
Node::TAIL.to_string(),
Node::HEAD,
Node::HEAD,
Node::TAIL,
));
}

Expand Down
36 changes: 19 additions & 17 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ impl Display for Operation {

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct ChainEntry {
pub hash: String,
pub previous_hash: String,
pub hash: Hash,
pub previous_hash: Hash,
pub operation: Operation,
pub value: String,
pub value: Hash,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -99,10 +99,10 @@ pub trait Database: Send + Sync {
&self,
epoch: &u64,
epoch_operation: &u64,
commitment: &str,
commitment: &Hash,
proofs: &str,
) -> DeimosResult<()>;
fn add_commitment(&self, epoch: &u64, commitment: &str) -> DeimosResult<()>;
fn add_commitment(&self, epoch: &u64, commitment: &Hash) -> DeimosResult<()>;
fn initialize_derived_dict(&self) -> DeimosResult<()>;
fn flush_database(&self) -> DeimosResult<()>;
}
Expand Down Expand Up @@ -338,8 +338,10 @@ impl Database for RedisConnections {
) -> DeimosResult<()> {
let mut con = self.lock_connection(&self.derived_dict)?;
let mut input_con = self.lock_connection(&self.input_order)?;
let hashed_key = sha256_mod(&incoming_entry.id);
con.set::<&String, &String, String>(&hashed_key, &value.hash)
let hashed_key = sha256_mod(&incoming_entry.id.as_bytes());
// TODO: @distractedm1nd thought about saving the raw bytes of the hash for space effiency but it seems like redis needs at least the key to be a string and for consistency we should probably save then both value as a string wdyt?
// to_string() Method works here because i've implemented the Display trait for Hash in indexed_merkle_tree crate
con.set::<&String, &String, String>(&hashed_key.to_string(), &value.hash.to_string())
.map_err(|_| {
DeimosError::Database(DatabaseError::WriteError(format!(
"derived dict update for key: {}",
Expand All @@ -349,7 +351,7 @@ impl Database for RedisConnections {

if new {
input_con
.rpush::<&'static str, &String, u32>("input_order", &hashed_key)
.rpush::<&'static str, &String, u32>("input_order", &hashed_key.to_string())
.map_err(|_| {
DeimosError::Database(DatabaseError::WriteError(format!(
"input order update for key: {}",
Expand Down Expand Up @@ -389,7 +391,7 @@ impl Database for RedisConnections {
&self,
epoch: &u64,
epoch_operation: &u64,
commitment: &str,
commitment: &Hash,
proofs: &str,
) -> DeimosResult<()> {
let mut con = self.lock_connection(&self.merkle_proofs)?;
Expand All @@ -403,7 +405,7 @@ impl Database for RedisConnections {
})
}

fn add_commitment(&self, epoch: &u64, commitment: &str) -> DeimosResult<()> {
fn add_commitment(&self, epoch: &u64, commitment: &Hash) -> DeimosResult<()> {
let mut con = self.lock_connection(&self.commitments)?;
con.set::<&String, &String, ()>(&format!("epoch_{}", epoch), &commitment.to_string())
.map_err(|_| {
Expand Down Expand Up @@ -484,10 +486,10 @@ mod tests {

fn create_mock_chain_entry() -> ChainEntry {
ChainEntry {
hash: "test_hash".to_string(),
previous_hash: "test_previous_hash".to_string(),
hash: sha256_mod(b"test_hash"),
previous_hash: sha256_mod(b"test_previous_hash"),
operation: Operation::Add,
value: "test_value".to_string(),
value: sha256_mod(b"test_value"),
}
}

Expand Down Expand Up @@ -644,9 +646,9 @@ mod tests {

// check if the returned keys are correct
let expected_keys: Vec<String> = vec![
sha256_mod("test_key1"),
sha256_mod("test_key2"),
sha256_mod("test_key3"),
sha256_mod(b"test_key1").to_string(),
sha256_mod(b"test_key2").to_string(),
sha256_mod(b"test_key3").to_string(),
];
let returned_keys: Vec<String> = keys;

Expand Down Expand Up @@ -759,7 +761,7 @@ mod tests {
}

let hashchain = redis_connections.get_hashchain(&incoming_entry.id).unwrap();
assert_eq!(hashchain[0].hash, "test_hash");
assert_eq!(hashchain[0].hash, sha256_mod(b"test_hash"));
assert_eq!(hashchain.len(), 1);

teardown(&redis_connections);
Expand Down
39 changes: 18 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::{
error::{DeimosError, DeimosResult, GeneralError, ProofError},
storage::{ChainEntry, Operation},
zk_snark::{hex_to_scalar, ProofVariantCircuit},
zk_snark::{hash_to_scalar, ProofVariantCircuit},
};
use base64::{engine::general_purpose::STANDARD as engine, Engine as _};
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::Proof;
use indexed_merkle_tree::{tree::Proof, Hash};
use rand::rngs::OsRng;

/// Checks if a given public key in the list of `ChainEntry` objects has been revoked.
Expand All @@ -22,7 +22,7 @@ use rand::rngs::OsRng;
///
/// `true` if the value was not revoked, otherwise `false`.
/// TODO(@distractedm1nd): is_revoked > is_not_revoked, for readability
pub fn is_not_revoked(entries: &[ChainEntry], value: String) -> bool {
pub fn is_not_revoked(entries: &[ChainEntry], value: Hash) -> bool {
for entry in entries {
if entry.value == value && matches!(entry.operation, Operation::Revoke) {
return false;
Expand Down Expand Up @@ -80,17 +80,17 @@ pub fn create_and_verify_snark(
}

pub fn validate_epoch(
previous_commitment: &str,
current_commitment: &str,
previous_commitment: &Hash,
current_commitment: &Hash,
proof: groth16::Proof<Bls12>,
verifying_key: VerifyingKey<Bls12>,
) -> Result<groth16::Proof<Bls12>, DeimosError> {
trace!("validate_epoch: preparing verifying key for zkSNARK");
let pvk = groth16::prepare_verifying_key(&verifying_key);

let scalars: Result<Vec<Scalar>, _> = vec![
hex_to_scalar(previous_commitment),
hex_to_scalar(current_commitment),
hash_to_scalar(previous_commitment),
hash_to_scalar(current_commitment),
]
.into_iter()
.collect();
Expand Down Expand Up @@ -191,18 +191,16 @@ mod tests {
let mut tree = IndexedMerkleTree::new_with_size(8).unwrap();
let prev_commitment = tree.get_commitment().unwrap();

let ryan = sha256_mod("Ryan");
let ford = sha256_mod("Ford");
let sebastian = sha256_mod("Sebastian");
let pusch = sha256_mod("Pusch");
let ethan = sha256_mod("Ethan");
let triple_zero = sha256_mod("000");
let ryan = sha256_mod(b"Ryan");
let ford = sha256_mod(b"Ford");
let sebastian = sha256_mod(b"Sebastian");
let pusch = sha256_mod(b"Pusch");
let ethan = sha256_mod(b"Ethan");
let triple_zero = sha256_mod(b"000");

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

let first_insert_proof = tree.insert_node(&mut ryans_node).unwrap();
let second_insert_proof = tree.insert_node(&mut sebastians_node).unwrap();
Expand All @@ -212,9 +210,8 @@ mod tests {
let second_insert_zk_snark = Proof::Insert(second_insert_proof);
let third_insert_zk_snark = Proof::Insert(third_insert_proof);

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

Expand Down
2 changes: 1 addition & 1 deletion src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async fn update_entry(
.json(format!("Error creating new tree: {}", e))
}
};
let hashed_id = sha256_mod(&incoming_entry.id);
let hashed_id = sha256_mod(incoming_entry.id.as_bytes());
let mut node = match new_tree.find_leaf_by_label(&hashed_id) {
Some(n) => n,
None => return HttpResponse::InternalServerError().json("Error finding leaf"),
Expand Down
Loading

0 comments on commit d2e571c

Please sign in to comment.