From edd728d13bb5917554eb194b0af0d430078c2cc7 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Mon, 27 Dec 2021 16:00:08 +0200 Subject: [PATCH] Remove unused files (old blake/sha implementations) --- .../hashing/blake2b512.rs | 153 ------------------ .../hashing/hash_sha256.rs | 127 --------------- .../hashing/hash_sha512.rs | 115 ------------- .../hashing/hmac_sha512.rs | 79 --------- 4 files changed, 474 deletions(-) delete mode 100644 src/cryptographic_primitives/hashing/blake2b512.rs delete mode 100644 src/cryptographic_primitives/hashing/hash_sha256.rs delete mode 100644 src/cryptographic_primitives/hashing/hash_sha512.rs delete mode 100644 src/cryptographic_primitives/hashing/hmac_sha512.rs diff --git a/src/cryptographic_primitives/hashing/blake2b512.rs b/src/cryptographic_primitives/hashing/blake2b512.rs deleted file mode 100644 index 2fb2ad07..00000000 --- a/src/cryptographic_primitives/hashing/blake2b512.rs +++ /dev/null @@ -1,153 +0,0 @@ -/* - This file is part of Curv library - Copyright 2018 by Kzen Networks - (https://github.com/KZen-networks/curv) - License MIT: https://github.com/KZen-networks/curv/blob/master/LICENSE -*/ - -use blake2b_simd::{Params, State}; -use typenum::Unsigned; - -use crate::arithmetic::traits::*; -use crate::elliptic::curves::{Curve, ECScalar, Point, Scalar}; -use crate::BigInt; - -/// Wrapper over [blake2b_simd](blake2b_simd::State) exposing facilities to hash bigints, elliptic points, -/// and scalars -pub struct Blake { - state: State, -} - -impl Blake { - const HASH_LENGTH: usize = 64; - pub fn with_personal(persona: &[u8]) -> Self { - Self { - state: Params::new() - .hash_length(Self::HASH_LENGTH) - .personal(persona) - .to_state(), - } - } - - pub fn chain_bigint(&mut self, n: &BigInt) -> &mut Self { - self.state.update(&n.to_bytes()); - self - } - - pub fn chain_point(&mut self, point: &Point) -> &mut Self { - self.state.update(&point.to_bytes(false)); - self - } - - pub fn result_bigint(&self) -> BigInt { - BigInt::from_bytes(self.state.finalize().as_ref()) - } - - pub fn result_scalar(&self) -> Scalar { - let scalar_len = <::ScalarLength as Unsigned>::to_usize(); - assert!( - Self::HASH_LENGTH >= scalar_len, - "Output size of the hash({}) is smaller than the scalar length({})", - Self::HASH_LENGTH, - scalar_len - ); - // Try and increment. - for i in 0u32.. { - let mut starting_state = self.state.clone(); - let hash = starting_state.update(&i.to_be_bytes()).finalize(); - if let Ok(scalar) = Scalar::from_bytes(&hash.as_bytes()[..scalar_len]) { - return scalar; - } - } - unreachable!("The probably of this reaching is extremely small ((2^n-q)/(2^n))^(2^32)") - } - - #[deprecated( - since = "0.8.0", - note = "Blake API has been changed, this method is outdated" - )] - pub fn create_hash(big_ints: &[&BigInt], persona: &[u8]) -> BigInt { - let mut digest = Params::new().hash_length(64).personal(persona).to_state(); - for value in big_ints { - digest.update(&BigInt::to_bytes(value)); - } - - BigInt::from_bytes(digest.finalize().as_ref()) - } - - #[deprecated( - since = "0.8.0", - note = "Blake API has been changed, this method is outdated" - )] - pub fn create_hash_from_ge(ge_vec: &[&Point], persona: &[u8]) -> Scalar { - let mut digest = Params::new().hash_length(64).personal(persona).to_state(); - // let mut digest = Blake2b::with_params(64, &[], &[], persona); - - for value in ge_vec { - digest.update(&value.to_bytes(false)); - } - - let result = BigInt::from_bytes(digest.finalize().as_ref()); - Scalar::from(&result) - } -} - -#[cfg(test)] -mod tests { - use super::Blake; - use crate::arithmetic::traits::*; - use crate::elliptic::curves::{Curve, Point}; - use crate::BigInt; - - #[test] - // Very basic test here, TODO: suggest better testing - fn create_hash_test_legacy() { - #![allow(deprecated)] - let result = Blake::create_hash(&[&BigInt::one(), &BigInt::zero()], b"Zcash_RedJubjubH"); - assert!(result > BigInt::zero()); - } - #[test] - // Very basic test here, TODO: suggest better testing - fn create_hash_test() { - let result = Blake::with_personal(b"Zcash_RedJubjubH") - .chain_bigint(&BigInt::one()) - .chain_bigint(&BigInt::zero()) - .result_bigint(); - assert!(result > BigInt::zero()); - } - - crate::test_for_all_curves!(create_hash_from_ge_test_legacy); - fn create_hash_from_ge_test_legacy() { - #![allow(deprecated)] - let base_point2 = Point::::base_point2(); - let generator = Point::::generator(); - let result1 = - Blake::create_hash_from_ge::(&[base_point2, &generator], b"Zcash_RedJubjubH"); - assert!(result1.to_bigint().bit_length() > 240); - let result2 = Blake::create_hash_from_ge(&[&generator, base_point2], b"Zcash_RedJubjubH"); - assert_ne!(result1, result2); - let result3 = Blake::create_hash_from_ge(&[&generator, base_point2], b"Zcash_RedJubjubH"); - assert_eq!(result2, result3); - } - - crate::test_for_all_curves!(create_hash_from_ge_test); - fn create_hash_from_ge_test() { - let base_point2 = Point::::base_point2(); - let generator = Point::::generator(); - let result1 = Blake::with_personal(b"Zcash_RedJubjubH") - .chain_point(base_point2) - .chain_point(&generator) - .result_scalar::(); - assert!(result1.to_bigint().bit_length() > 240); - let result2 = Blake::with_personal(b"Zcash_RedJubjubH") - .chain_point(&generator) - .chain_point(base_point2) - .result_scalar::(); - assert_ne!(result1, result2); - let result3 = Blake::with_personal(b"Zcash_RedJubjubH") - .chain_point(&generator) - .chain_point(base_point2) - .result_scalar::(); - assert_eq!(result2, result3); - } -} diff --git a/src/cryptographic_primitives/hashing/hash_sha256.rs b/src/cryptographic_primitives/hashing/hash_sha256.rs deleted file mode 100644 index a09cd549..00000000 --- a/src/cryptographic_primitives/hashing/hash_sha256.rs +++ /dev/null @@ -1,127 +0,0 @@ -/* - This file is part of Curv library - Copyright 2018 by Kzen Networks - (https://github.com/KZen-networks/curv) - License MIT: https://github.com/KZen-networks/curv/blob/master/LICENSE -*/ - -#![allow(deprecated)] - -use super::traits::Hash; -use crate::arithmetic::traits::*; -use crate::elliptic::curves::{Curve, Point, Scalar}; - -use digest::Digest; -use sha2::Sha256; - -use crate::BigInt; - -#[deprecated(since = "0.8.0", note = "use DigestExt instead")] -pub struct HSha256; - -impl Hash for HSha256 { - fn create_hash(big_ints: &[&BigInt]) -> BigInt { - let mut hasher = Sha256::new(); - - for value in big_ints { - hasher.update(&BigInt::to_bytes(value)); - } - - let result_hex = hasher.finalize(); - BigInt::from_bytes(&result_hex[..]) - } - - fn create_hash_from_ge(ge_vec: &[&Point]) -> Scalar { - let mut hasher = Sha256::new(); - for value in ge_vec { - hasher.update(&value.to_bytes(false)[..]); - } - - let result_hex = hasher.finalize(); - let result = BigInt::from_bytes(&result_hex[..]); - Scalar::from(&result) - } - - fn create_hash_from_slice(byte_slice: &[u8]) -> BigInt { - let mut hasher = Sha256::new(); - hasher.update(byte_slice); - let result_hex = hasher.finalize(); - BigInt::from_bytes(&result_hex[..]) - } -} - -#[cfg(test)] -mod tests { - use super::HSha256; - use super::Hash; - use crate::arithmetic::traits::*; - use crate::elliptic::curves::{Curve, Point}; - use crate::BigInt; - use sha2::Digest; - use sha2::Sha256; - - #[test] - fn test_byte_vec() { - let message: Vec = vec![0, 1]; - let big_int0 = BigInt::from(message[0] as i32); - let big_int1 = BigInt::from(message[1] as i32); - - let result = HSha256::create_hash(&[&big_int0, &big_int1]).to_hex(); - let mut hasher = Sha256::new(); - hasher.update(&message); - let result2 = hex::encode(hasher.finalize()); - assert_eq!(result, result2); - } - - #[test] - // Test Vectors taken from: - // https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing#shavs - fn vector_sha256_test() { - // Empty Message - let result: BigInt = HSha256::create_hash(&[]); - assert_eq!( - result.to_hex(), - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ); - - // 256 bit message - let result: BigInt = HSha256::create_hash(&[&BigInt::from_hex( - "09fc1accc230a205e4a208e64a8f204291f581a12756392da4b8c0cf5ef02b95", - ) - .unwrap()]); - assert_eq!( - result.to_hex(), - "4f44c1c7fbebb6f9601829f3897bfd650c56fa07844be76489076356ac1886a4" - ); - - // 2x128 bit messages - let result: BigInt = HSha256::create_hash(&[ - &BigInt::from_hex("09fc1accc230a205e4a208e64a8f2042").unwrap(), - &BigInt::from_hex("91f581a12756392da4b8c0cf5ef02b95").unwrap(), - ]); - assert_eq!( - result.to_hex(), - "4f44c1c7fbebb6f9601829f3897bfd650c56fa07844be76489076356ac1886a4" - ); - - // 512 bit message - let result: BigInt = HSha256::create_hash(&[&BigInt::from_hex("5a86b737eaea8ee976a0a24da63e7ed7eefad18a101c1211e2b3650c5187c2a8a650547208251f6d4237e661c7bf4c77f335390394c37fa1a9f9be836ac28509").unwrap()]); - assert_eq!( - result.to_hex(), - "42e61e174fbb3897d6dd6cef3dd2802fe67b331953b06114a65c772859dfc1aa" - ); - } - - crate::test_for_all_curves!(create_sha256_from_ge_test); - - fn create_sha256_from_ge_test() { - let generator = Point::::generator(); - let base_point2 = Point::::base_point2(); - let result1 = HSha256::create_hash_from_ge::(&[base_point2, &generator]); - assert!(result1.to_bigint().bit_length() > 240); - let result2 = HSha256::create_hash_from_ge(&[&generator, base_point2]); - assert_ne!(result1, result2); - let result3 = HSha256::create_hash_from_ge(&[&generator, base_point2]); - assert_eq!(result2, result3); - } -} diff --git a/src/cryptographic_primitives/hashing/hash_sha512.rs b/src/cryptographic_primitives/hashing/hash_sha512.rs deleted file mode 100644 index 600797bd..00000000 --- a/src/cryptographic_primitives/hashing/hash_sha512.rs +++ /dev/null @@ -1,115 +0,0 @@ -/* - This file is part of Curv library - Copyright 2018 by Kzen Networks - (https://github.com/KZen-networks/curv) - License MIT: https://github.com/KZen-networks/curv/blob/master/LICENSE -*/ - -#![allow(deprecated)] - -use super::traits::Hash; -use crate::arithmetic::traits::*; -use crate::elliptic::curves::{Curve, Point, Scalar}; - -use digest::Digest; -use sha2::Sha512; - -use crate::BigInt; - -#[deprecated(since = "0.8.0", note = "use DigestExt instead")] -pub struct HSha512; - -impl Hash for HSha512 { - fn create_hash(big_ints: &[&BigInt]) -> BigInt { - let mut hasher = Sha512::new(); - - for value in big_ints { - hasher.update(&BigInt::to_bytes(value)); - } - - let result_hex = hasher.finalize(); - BigInt::from_bytes(&result_hex[..]) - } - - fn create_hash_from_ge(ge_vec: &[&Point]) -> Scalar { - let mut hasher = Sha512::new(); - for value in ge_vec { - hasher.update(&value.to_bytes(false)[..]); - } - - let result_hex = hasher.finalize(); - let result = BigInt::from_bytes(&result_hex[..]); - Scalar::from(&result) - } - - fn create_hash_from_slice(byte_slice: &[u8]) -> BigInt { - let mut hasher = Sha512::new(); - hasher.update(byte_slice); - let result_hex = hasher.finalize(); - BigInt::from_bytes(&result_hex[..]) - } -} - -#[cfg(test)] -mod tests { - use crate::arithmetic::*; - use crate::elliptic::curves::{Curve, Point}; - - use super::HSha512; - use super::Hash; - - #[test] - // Test Vectors taken from: - // https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing#shavs - fn vector_sha512_test() { - // Empty message - let result: BigInt = HSha512::create_hash(&[]); - assert_eq!( - result.to_hex(), - "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" - ); - - // 2x256 bit message - let result: BigInt = HSha512::create_hash(&[ - &BigInt::from_hex("c1ca70ae1279ba0b918157558b4920d6b7fba8a06be515170f202fafd36fb7f7") - .unwrap(), - &BigInt::from_hex("9d69fad745dba6150568db1e2b728504113eeac34f527fc82f2200b462ecbf5d") - .unwrap(), - ]); - assert_eq!( - result.to_hex(), - "46e46623912b3932b8d662ab42583423843206301b58bf20ab6d76fd47f1cbbcf421df536ecd7e56db5354e7e0f98822d2129c197f6f0f222b8ec5231f3967d" - ); - - // 512 bit message - let result: BigInt = HSha512::create_hash(&[&BigInt::from_hex( - "c1ca70ae1279ba0b918157558b4920d6b7fba8a06be515170f202fafd36fb7f79d69fad745dba6150568db1e2b728504113eeac34f527fc82f2200b462ecbf5d", - - ) - .unwrap()]); - assert_eq!( - result.to_hex(), - "46e46623912b3932b8d662ab42583423843206301b58bf20ab6d76fd47f1cbbcf421df536ecd7e56db5354e7e0f98822d2129c197f6f0f222b8ec5231f3967d" - ); - - // 1024 bit message - let result: BigInt = HSha512::create_hash(&[&BigInt::from_hex("fd2203e467574e834ab07c9097ae164532f24be1eb5d88f1af7748ceff0d2c67a21f4e4097f9d3bb4e9fbf97186e0db6db0100230a52b453d421f8ab9c9a6043aa3295ea20d2f06a2f37470d8a99075f1b8a8336f6228cf08b5942fc1fb4299c7d2480e8e82bce175540bdfad7752bc95b577f229515394f3ae5cec870a4b2f8").unwrap()]); - assert_eq!( - result.to_hex(), - "a21b1077d52b27ac545af63b32746c6e3c51cb0cb9f281eb9f3580a6d4996d5c9917d2a6e484627a9d5a06fa1b25327a9d710e027387fc3e07d7c4d14c6086cc" - ); - } - - crate::test_for_all_curves!(create_sha512_from_ge_test); - - fn create_sha512_from_ge_test() { - let generator = Point::::generator(); - let base_point2 = Point::::base_point2(); - let result1 = HSha512::create_hash_from_ge::(&[base_point2, &generator]); - assert!(result1.to_bigint().bit_length() > 240); - let result2 = HSha512::create_hash_from_ge(&[&generator, base_point2]); - assert_ne!(result1, result2); - let result3 = HSha512::create_hash_from_ge(&[&generator, base_point2]); - assert_eq!(result2, result3); - } -} diff --git a/src/cryptographic_primitives/hashing/hmac_sha512.rs b/src/cryptographic_primitives/hashing/hmac_sha512.rs deleted file mode 100644 index 4d142a1d..00000000 --- a/src/cryptographic_primitives/hashing/hmac_sha512.rs +++ /dev/null @@ -1,79 +0,0 @@ -/* - This file is part of Curv library - Copyright 2018 by Kzen Networks - (https://github.com/KZen-networks/curv) - License MIT: https://github.com/KZen-networks/curv/blob/master/LICENSE -*/ - -#![allow(deprecated)] - -use crate::BigInt; - -use super::traits::KeyedHash; -use crate::arithmetic::traits::*; - -use hmac::{Hmac, Mac, NewMac}; -use sha2::Sha512; -use zeroize::Zeroize; -type HmacSha256type = Hmac; - -#[deprecated(since = "0.8.0", note = "use HmacExt instead")] -pub struct HMacSha512; - -impl KeyedHash for HMacSha512 { - fn create_hmac(key: &BigInt, data: &[&BigInt]) -> BigInt { - let mut key_bytes = key.to_bytes(); - - let mut hmac = HmacSha256type::new_from_slice(&key_bytes).expect(""); - - for value in data { - hmac.update(&BigInt::to_bytes(value)); - } - key_bytes.zeroize(); - let result = hmac.finalize(); - let code = result.into_bytes(); - - BigInt::from_bytes(code.as_slice()) - } - fn verify(key: &BigInt, data: &[&BigInt], code_bytes: [u8; 64]) -> Result<(), ()> { - let key_bytes = key.to_bytes(); - - let mut hmac = HmacSha256type::new_from_slice(&key_bytes).expect(""); - - for value in data { - hmac.update(&BigInt::to_bytes(value)); - } - match hmac.verify(&code_bytes) { - Ok(_) => Ok(()), - Err(_) => Err(()), - } - } -} - -#[cfg(test)] -mod tests { - use super::HMacSha512; - use crate::arithmetic::traits::*; - use crate::cryptographic_primitives::hashing::traits::KeyedHash; - use crate::BigInt; - - #[test] - fn create_hmac_test() { - let key = BigInt::sample(512); - let result1 = HMacSha512::create_hmac(&key, &[&BigInt::from(10)]); - let result1_bytes = &BigInt::to_bytes(&result1)[..]; - let mut array_result: [u8; 64] = [0u8; 64]; - array_result.copy_from_slice(result1_bytes); - assert!(HMacSha512::verify(&key, &[&BigInt::from(10)], array_result).is_ok()); - let key2 = BigInt::sample(512); - // same data , different key - let result2 = HMacSha512::create_hmac(&key2, &[&BigInt::from(10)]); - assert_ne!(result1, result2); - // same key , different data - let result3 = HMacSha512::create_hmac(&key, &[&BigInt::from(10), &BigInt::from(11)]); - assert_ne!(result1, result3); - // same key, same data - let result4 = HMacSha512::create_hmac(&key, &[&BigInt::from(10)]); - assert_eq!(result1, result4) - } -}