Skip to content

Commit

Permalink
feat: functionality to work with Hidden types (#148)
Browse files Browse the repository at this point in the history
* Functionality to work with `Hidden` types

* Formatting
  • Loading branch information
AaronFeickert committed Nov 11, 2022
1 parent 27a9472 commit 086d164
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
38 changes: 36 additions & 2 deletions src/hashing.rs
Expand Up @@ -31,7 +31,7 @@
use std::{marker::PhantomData, ops::Deref};

use blake2::VarBlake2b;
use digest::{Digest, Output, Update};
use digest::{Digest, FixedOutput, Output, Update};
use sha3::Sha3_256;
use tari_utilities::ByteArray;

Expand Down Expand Up @@ -306,6 +306,20 @@ pub trait AsFixedBytes<const I: usize>: AsRef<[u8]> {

impl<const I: usize, D: Digest> AsFixedBytes<I> for DomainSeparatedHash<D> {}

impl<TInnerDigest: FixedOutput, TDomain: DomainSeparation> FixedOutput
for DomainSeparatedHasher<TInnerDigest, TDomain>
{
type OutputSize = TInnerDigest::OutputSize;

fn finalize_into(self, out: &mut digest::generic_array::GenericArray<u8, Self::OutputSize>) {
self.inner.finalize_into(out);
}

fn finalize_into_reset(&mut self, out: &mut digest::generic_array::GenericArray<u8, Self::OutputSize>) {
self.inner.finalize_into_reset(out);
}
}

/// Implements Digest so that it can be used for other crates
impl<TInnerDigest: Digest, TDomain: DomainSeparation> Digest for DomainSeparatedHasher<TInnerDigest, TDomain> {
type OutputSize = TInnerDigest::OutputSize;
Expand Down Expand Up @@ -571,7 +585,7 @@ pub fn create_hasher<D: Digest, HD: DomainSeparation>() -> DomainSeparatedHasher
#[cfg(test)]
mod test {
use blake2::Blake2b;
use digest::Digest;
use digest::{generic_array::GenericArray, Digest, FixedOutput};
use tari_utilities::hex::{from_hex, to_hex};

use crate::{
Expand Down Expand Up @@ -628,6 +642,26 @@ mod test {
assert_eq!(MacDomain::domain_separation_tag("test"), "com.tari.mac.v1.test");
}

#[test]
fn finalize_into() {
hash_domain!(TestHasher, "com.example.test");
let mut hasher = DomainSeparatedHasher::<Blake256, TestHasher>::new();
hasher.update([0, 0, 0]);

let mut output = GenericArray::<u8, <Blake256 as Digest>::OutputSize>::default();
hasher.finalize_into(&mut output);
}

#[test]
fn finalize_into_reset() {
hash_domain!(TestHasher, "com.example.test");
let mut hasher = DomainSeparatedHasher::<Blake256, TestHasher>::new();
hasher.update([0, 0, 0]);

let mut output = GenericArray::<u8, <Blake256 as Digest>::OutputSize>::default();
hasher.finalize_into_reset(&mut output);
}

#[test]
fn dst_hasher() {
hash_domain!(GenericHashDomain, "com.tari.generic");
Expand Down
5 changes: 3 additions & 2 deletions src/keys.rs
Expand Up @@ -11,6 +11,7 @@ use std::ops::Add;
use rand::{CryptoRng, Rng};
use serde::{de::DeserializeOwned, ser::Serialize};
use tari_utilities::ByteArray;
use zeroize::Zeroize;

/// A trait specifying common behaviour for representing `SecretKey`s. Specific elliptic curve
/// implementations need to implement this trait for them to be used in Tari.
Expand All @@ -26,7 +27,7 @@ use tari_utilities::ByteArray;
/// let k = RistrettoSecretKey::random(&mut rng);
/// let p = RistrettoPublicKey::from_secret_key(&k);
/// ```
pub trait SecretKey: ByteArray + Clone + PartialEq + Eq + Add<Output = Self> + Default {
pub trait SecretKey: ByteArray + Clone + PartialEq + Eq + Add<Output = Self> + Default + Zeroize {
/// The length of the key, in bytes
fn key_length() -> usize;
/// Generates a random secret key
Expand All @@ -40,7 +41,7 @@ pub trait SecretKey: ByteArray + Clone + PartialEq + Eq + Add<Output = Self> + D
///
/// See [SecretKey](trait.SecretKey.html) for an example.
pub trait PublicKey:
ByteArray + Add<Output = Self> + Clone + PartialOrd + Ord + Default + Serialize + DeserializeOwned
ByteArray + Add<Output = Self> + Clone + PartialOrd + Ord + Default + Serialize + DeserializeOwned + Zeroize
{
/// The output size len of Public Key
const KEY_LEN: usize;
Expand Down

0 comments on commit 086d164

Please sign in to comment.