Skip to content

Commit

Permalink
feat: redefine Hash as a struct so we can impl Display on it, and pri…
Browse files Browse the repository at this point in the history
…nt as base64
  • Loading branch information
dan-da committed May 27, 2021
1 parent 1539367 commit b540203
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ mod tests {
// Valid mint signatures BUT signing wrong message
for _ in 0..n_wrong_msg_sigs.coerce() {
if let Some(input) = repeating_inputs.next() {
let wrong_msg_sig = genesis.key_mgr.sign(&[0u8; 32]);
let wrong_msg_sig = genesis.key_mgr.sign(&Hash([0u8; 32]));
fuzzed_transaction_sigs.insert(input.name(), (genesis.public_key(), wrong_msg_sig));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dbc_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
use threshold_crypto::PublicKeySet;
use tiny_keccak::{Hasher, Sha3};

use crate::DbcContentHash;
use crate::{DbcContentHash, Hash};

#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct DbcContent {
Expand Down Expand Up @@ -56,6 +56,6 @@ impl DbcContent {

let mut hash = [0; 32];
sha3.finalize(&mut hash);
hash
Hash(hash)
}
}
14 changes: 9 additions & 5 deletions src/dbc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl DbcTransaction {

let mut hash = [0; 32];
sha3.finalize(&mut hash);
hash
Hash(hash)
}
}

Expand All @@ -54,10 +54,14 @@ mod tests {
#[quickcheck]
fn prop_hash_is_independent_of_order(inputs: Vec<u64>, outputs: Vec<u64>) {
// This test is here to protect us in the case that someone swaps out the BTreeSet for inputs/outputs for something else
let input_hashes: Vec<DbcContentHash> =
inputs.iter().map(|i| sha3_256(&i.to_be_bytes())).collect();
let output_hashes: Vec<DbcContentHash> =
outputs.iter().map(|i| sha3_256(&i.to_be_bytes())).collect();
let input_hashes: Vec<DbcContentHash> = inputs
.iter()
.map(|i| Hash(sha3_256(&i.to_be_bytes())))
.collect();
let output_hashes: Vec<DbcContentHash> = outputs
.iter()
.map(|i| Hash(sha3_256(&i.to_be_bytes())))
.collect();

let forward_hash = DbcTransaction::new(
input_hashes.iter().cloned().collect(),
Expand Down
9 changes: 7 additions & 2 deletions src/key_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ impl PublicKey {

let mut hash = [0; 32];
sha3.finalize(&mut hash);
hash
Hash(hash)
}

pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}

pub fn ed(&self) -> EdPublicKey {
Expand All @@ -65,7 +69,7 @@ pub fn ed25519_keypair() -> Keypair {
Keypair::generate(&mut rand::thread_rng())
}

#[derive(Default)]
#[derive(Debug, Default)]
pub struct KeyCache(HashSet<PublicKey>);

impl KeyCache {
Expand Down Expand Up @@ -100,6 +104,7 @@ pub struct ChainNode {
prev_mint_sig: Signature,
}

#[derive(Debug)]
pub struct KeyManager {
keypair: Keypair,
genesis: PublicKey,
Expand Down
44 changes: 41 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
// permissions and limitations relating to use of the SAFE Network Software.
#![allow(clippy::from_iter_instead_of_collect)]

use serde::{Deserialize, Serialize};
use std::ops::Deref;
#[cfg(test)]
use tiny_keccak::{Hasher, Sha3};
/// These typdefs are to simplify algorithm for now and will be removed for production.
pub(crate) type Hash = [u8; 32];
pub(crate) type DbcContentHash = [u8; 32];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Hash([u8; 32]);
pub(crate) type DbcContentHash = Hash;
mod dbc;
mod dbc_content;
mod dbc_transaction;
Expand All @@ -28,6 +31,41 @@ pub use crate::{
mint::{Mint, MintRequest, MintTransaction},
};

impl From<[u8; 32]> for Hash {
fn from(val: [u8; 32]) -> Hash {
Hash(val)
}
}

impl Deref for Hash {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl AsRef<[u8]> for Hash {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}

#[cfg(test)]
use rand::distributions::{Distribution, Standard};

#[cfg(test)]
use rand::Rng;

#[cfg(test)]
/// used when fuzzing DBC's in testing.
impl Distribution<Hash> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Hash {
Hash(rng.gen())
}
}

#[cfg(test)]
pub(crate) fn bls_dkg_id() -> bls_dkg::outcome::Outcome {
use std::collections::BTreeSet;
Expand Down Expand Up @@ -57,7 +95,7 @@ pub(crate) fn bls_dkg_id() -> bls_dkg::outcome::Outcome {
}

#[cfg(test)]
fn sha3_256(input: &[u8]) -> Hash {
fn sha3_256(input: &[u8]) -> [u8; 32] {
let mut sha3 = Sha3::v256();
let mut output = [0; 32];
sha3.update(input);
Expand Down
7 changes: 4 additions & 3 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use crate::{
Dbc, DbcContent, DbcContentHash, DbcTransaction, Error, KeyCache, KeyManager, PublicKey,
Dbc, DbcContent, DbcContentHash, DbcTransaction, Error, Hash, KeyCache, KeyManager, PublicKey,
Result, Signature,
};

pub type InputSignatures = BTreeMap<DbcContentHash, (PublicKey, Signature)>;

#[derive(Default)]
#[derive(Debug, Default)]
struct SpendBook {
transactions: BTreeMap<DbcContentHash, DbcTransaction>,
}
Expand Down Expand Up @@ -116,6 +116,7 @@ pub struct MintRequest {
pub input_ownership_proofs: HashMap<DbcContentHash, threshold_crypto::Signature>,
}

#[derive(Debug)]
pub struct Mint {
pub(crate) key_mgr: KeyManager,
spendbook: SpendBook,
Expand All @@ -125,7 +126,7 @@ impl Mint {
pub fn genesis(genesis_key: threshold_crypto::PublicKeySet, amount: u64) -> (Self, Dbc) {
let key_mgr = KeyManager::new_genesis();

let genesis_input = [0u8; 32];
let genesis_input = Hash([0u8; 32]);

let parents = vec![genesis_input].into_iter().collect();
let content = DbcContent::new(parents, amount, 0, genesis_key);
Expand Down

0 comments on commit b540203

Please sign in to comment.