Skip to content

Commit

Permalink
Migrate to digest 0.9 (#49)
Browse files Browse the repository at this point in the history
* Migrate to digest 0.9

This PR moves all dependencies to use the digest 0.9 traits and APIs.
This is a breaking change, so the minor version is incremented.

Clients of this generally only need to update the `result` method to
`finalize`; and obviously make use of the v0.9 `digest::Digest` trait
where necessary.

As a result, the deprecated k12, sha3 and Blake3 objects can be removed.
Methods and functins that need a hasher are all generic over `Digest`.

We retain the convenience wrapper over `VarBlake2B` to produce 256 bit
hashes and implement the necessary sub-traits to support
`digest::Digest`.

This update also fixes
#35

* Update src/ristretto/ristretto_keys.rs

Co-authored-by: Stan Bondi <[email protected]>

Co-authored-by: Stan Bondi <[email protected]>
  • Loading branch information
CjS77 and sdbondi authored Jul 5, 2021
1 parent fa20692 commit f9428bc
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 417 deletions.
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ categories = ["cryptography"]
homepage = "https://tari.com"
readme = "README.md"
license = "BSD-3-Clause"
version = "0.10.0"
version = "0.11.0"
edition = "2018"

[dependencies]
tari_utilities = "^0.3"
base64 = "0.10.1"
digest = "0.8.0"
digest = "0.9.0"
rand = { version = "0.8", default-features = false }
clear_on_drop = "=0.2.4"
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4", default-features = false, features = ["u64_backend", "serde", "alloc"] }
bulletproofs = {version = "4.0.0", package="tari_bulletproofs"}
merlin = { version = "3", default-features = false }
sha2 = "0.8.0"
sha2 = "0.9.5"
sha3 = "0.9"
thiserror = "1.0.20"
blake2 = "0.8.1"
blake3 = "0.3"
k12 = "0.1"
blake2 = "0.9.1"
rmp-serde = "0.13.7"
serde = "1.0.89"
serde_json = "1.0"
Expand All @@ -35,6 +33,7 @@ wasm-bindgen = { version = "^0.2", features = ["serde-serialize"], optional = tr
[dev-dependencies]
criterion = "0.3.4"
bincode = "1.1.4"
blake3 = "0.3"

[build-dependencies]
cbindgen = "0.17.0"
Expand Down
52 changes: 18 additions & 34 deletions src/hash/blake2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,29 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use blake2::VarBlake2b;
use digest::{
generic_array::{typenum::U32, GenericArray},
FixedOutput,
Input,
Reset,
VariableOutput,
};
use blake2::{digest::VariableOutput, VarBlake2b};
use digest::{consts::U32, generic_array::GenericArray, FixedOutput, Reset, Update};

/// A convenience wrapper produce 256 bit hashes from Blake2b
#[derive(Clone, Debug)]
pub struct Blake256(VarBlake2b);

impl Blake256 {
pub fn new() -> Self {
let h = VarBlake2b::new(32).unwrap();
Blake256(h)
}

pub fn result(self) -> GenericArray<u8, U32> {
self.fixed_result()
}
}

impl Default for Blake256 {
fn default() -> Self {
let h = VarBlake2b::new(32).unwrap();
let h = VariableOutput::new(32).unwrap();
Blake256(h)
}
}

impl Input for Blake256 {
fn input<B: AsRef<[u8]>>(&mut self, data: B) {
(self.0).input(data);
}
}

impl FixedOutput for Blake256 {
type OutputSize = U32;

fn fixed_result(self) -> GenericArray<u8, U32> {
let mut arr = GenericArray::default();
// ..32 range index is always safe because VarBlake2b is initialized with 32 elements
self.0.variable_result(|res| arr.copy_from_slice(&res[..32]));
arr
fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>) {
self.0.finalize_variable(|res| out.copy_from_slice(res));
}

fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) {
self.0.finalize_variable_reset(|res| out.copy_from_slice(res));
}
}

Expand All @@ -74,15 +52,21 @@ impl Reset for Blake256 {
}
}

impl Update for Blake256 {
fn update(&mut self, data: impl AsRef<[u8]>) {
self.0.update(data);
}
}

#[cfg(test)]
mod test {
use crate::common::Blake256;
use digest::{Input, Reset};
use digest::Digest;
use tari_utilities::hex;

#[test]
fn blake256() {
let e = Blake256::new().chain(b"one").chain(b"two").result().to_vec();
let e = Blake256::new().chain(b"one").chain(b"two").finalize().to_vec();
let h = hex::to_hex(&e);
assert_eq!(
h,
Expand All @@ -94,7 +78,7 @@ mod test {
fn reset() {
let mut e = Blake256::default().chain(b"foobar");
e.reset();
let e = e.chain(b"onetwo").result().to_vec();
let e = e.chain(b"onetwo").finalize().to_vec();
let h = hex::to_hex(&e);
assert_eq!(
h,
Expand Down
120 changes: 0 additions & 120 deletions src/hash/blake3.rs

This file was deleted.

115 changes: 0 additions & 115 deletions src/hash/k12.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pub mod blake2;
pub mod blake3;
pub mod k12;
pub mod sha3;
Loading

0 comments on commit f9428bc

Please sign in to comment.