Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade RustCrypto related crates #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ crate-type = ["lib"]
[dependencies]
cryptoxide = "0.1.2"
curve25519-dalek = "3"
digest = "0.9"
digest = "0.10"
generic-array = "0.14"
typenum = "1.13"
ff-zeroize = "0.6.3"
hex = { version = "0.4", features = ["serde"] }
hmac = "0.11"
hmac = "0.12"
thiserror = "1"
lazy_static = "1.4"
num-traits = "0.2"
Expand All @@ -33,8 +33,8 @@ rand_legacy = { package = "rand", version = "0.6" }
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
serde_derive = "1.0"
sha2 = "0.9"
sha3 = "0.9"
sha2 = "0.10"
sha3 = "0.10"
old_sha2 = { package = "sha2", version = "0.8" }
zeroize = "1"
merkle-cbt = "0.3"
Expand All @@ -47,7 +47,7 @@ version = "0.20"
features = ["serde", "rand-std", "global-context"]

[dependencies.p256]
version = "0.11.1"
version = "0.12"
features = ["ecdsa", "ecdsa-core"]

[dev-dependencies]
Expand All @@ -56,7 +56,6 @@ serde_json = "1"
paste = "1.0.2"
proptest = "0.10"
proptest-derive = "0.2"
blake2 = "0.9"

[features]
default = ["rust-gmp-kzen"]
Expand Down
2 changes: 1 addition & 1 deletion examples/diffie_hellman_key_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() {
Some("bls12_381_1") => ecdh::<Bls12_381_1>(),
Some("bls12_381_2") => ecdh::<Bls12_381_2>(),
Some("p256") => ecdh::<Secp256r1>(),
Some(unknown_curve) => eprintln!("Unknown curve: {}", unknown_curve),
Some(unknown_curve) => eprintln!("Unknown curve: {unknown_curve}"),
None => eprintln!("Missing curve name"),
}
}
5 changes: 2 additions & 3 deletions examples/pedersen_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ pub fn ped_com<E: Curve>(message: &BigInt) {
);

println!(
"\ncreated commitment with user defined randomness \n\n blinding_factor {} \n commitment: {:#?}",
blinding_factor, com
"\ncreated commitment with user defined randomness \n\n blinding_factor {blinding_factor} \n commitment: {com:#?}",
);
}

Expand All @@ -40,7 +39,7 @@ fn main() {
Some("bls12_381_1") => ped_com::<Bls12_381_1>(&message_bn),
Some("bls12_381_2") => ped_com::<Bls12_381_2>(&message_bn),
Some("p256") => ped_com::<Secp256r1>(&message_bn),
Some(unknown_curve) => eprintln!("Unknown curve: {}", unknown_curve),
Some(unknown_curve) => eprintln!("Unknown curve: {unknown_curve}"),
None => eprintln!("Missing curve name"),
}
}
2 changes: 1 addition & 1 deletion examples/proof_of_knowledge_of_dlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
Some("bls12_381_1") => dlog_proof::<Bls12_381_1>(),
Some("bls12_381_2") => dlog_proof::<Bls12_381_2>(),
Some("p256") => dlog_proof::<Secp256r1>(),
Some(unknown_curve) => eprintln!("Unknown curve: {}", unknown_curve),
Some(unknown_curve) => eprintln!("Unknown curve: {unknown_curve}"),
None => eprintln!("Missing curve name"),
}
}
2 changes: 1 addition & 1 deletion examples/verifiable_secret_sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() {
Some("bls12_381_1") => secret_sharing_3_out_of_5::<Bls12_381_1>(),
Some("bls12_381_2") => secret_sharing_3_out_of_5::<Bls12_381_2>(),
Some("p256") => secret_sharing_3_out_of_5::<Secp256r1>(),
Some(unknown_curve) => eprintln!("Unknown curve: {}", unknown_curve),
Some(unknown_curve) => eprintln!("Unknown curve: {unknown_curve}"),
None => eprintln!("Missing curve name"),
}
}
2 changes: 1 addition & 1 deletion src/arithmetic/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl fmt::Display for ParseBigIntError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.reason {
#[cfg(feature = "rust-gmp-kzen")]
ParseErrorReason::Gmp(reason) => write!(f, "{}", reason),
ParseErrorReason::Gmp(reason) => write!(f, "{reason}"),
#[cfg(feature = "num-bigint")]
ParseErrorReason::NumBigint => {
write!(f, "invalid {}-based number representation", self.radix)
Expand Down
4 changes: 2 additions & 2 deletions src/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod test {

for (i, expect) in expectations.into_iter().enumerate() {
let i = 7 - i - 1;
assert_eq!(n.test_bit(i), expect, "testing {} bit", i)
assert_eq!(n.test_bit(i), expect, "testing {i} bit")
}
}

Expand Down Expand Up @@ -285,7 +285,7 @@ mod test {
expected = expected.wrapping_mul(n);
}
}
assert_eq!(actual, BigInt::from(expected), "{} [{:?}] {}", was, op, n)
assert_eq!(actual, BigInt::from(expected), "{was} [{op:?}] {n}")
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/cryptographic_primitives/commitments/hash_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl<H: Digest + Clone> Commitment<BigInt> for HashCommitment<H> {
blinding_factor: &BigInt,
) -> BigInt {
let digest_result = H::new()
.chain(message.to_bytes())
.chain(blinding_factor.to_bytes())
.chain_update(message.to_bytes())
.chain_update(blinding_factor.to_bytes())
.finalize();
BigInt::from_bytes(digest_result.as_ref())
}
Expand All @@ -47,11 +47,11 @@ mod tests {
use super::SECURITY_BITS;
use crate::arithmetic::traits::*;
use crate::{test_for_all_hashes, BigInt};
use digest::Digest;
use digest::{Digest, OutputSizeUser};

test_for_all_hashes!(test_bit_length_create_commitment);
fn test_bit_length_create_commitment<H: Digest + Clone>() {
let hex_len = H::output_size() * 8;
let hex_len = <H as OutputSizeUser>::output_size() * 8;
let mut ctr_commit_len = 0;
let mut ctr_blind_len = 0;
let sample_size = 10_000;
Expand All @@ -77,7 +77,7 @@ mod tests {

test_for_all_hashes!(test_bit_length_create_commitment_with_user_defined_randomness);
fn test_bit_length_create_commitment_with_user_defined_randomness<H: Digest + Clone>() {
let sec_bits = H::output_size() * 8;
let sec_bits = <H as OutputSizeUser>::output_size() * 8;
let message = BigInt::sample(sec_bits);
let (_commitment, blind_factor) = HashCommitment::<H>::create_commitment(&message);
let commitment2 = HashCommitment::<H>::create_commitment_with_user_defined_randomness(
Expand Down Expand Up @@ -109,9 +109,9 @@ mod tests {
&BigInt::zero(),
);
let message2 = message.to_bytes();
digest.update(&message2);
digest.update(message2);
let bytes_blinding_factor = &BigInt::zero().to_bytes();
digest.update(&bytes_blinding_factor);
digest.update(bytes_blinding_factor);
let hash_result = BigInt::from_bytes(digest.finalize().as_ref());
assert_eq!(&commitment, &hash_result);
}
Expand Down
47 changes: 30 additions & 17 deletions src/cryptographic_primitives/hashing/ext.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use digest::Digest;
use digest::block_buffer::Eager;
use digest::core_api::{BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore, UpdateCore};
use digest::{Digest, HashMarker, MacError, OutputSizeUser};
use generic_array::GenericArray;
use hmac::crypto_mac::MacError;
use hmac::{Hmac, Mac, NewMac};
use typenum::Unsigned;
use hmac::{Hmac, Mac};
use typenum::{IsLess, Le, NonZero, Unsigned, U256};

use crate::arithmetic::*;
use crate::elliptic::curves::{Curve, ECScalar, Point, Scalar};
Expand Down Expand Up @@ -87,15 +88,15 @@ where
D: Digest + Clone,
{
fn input_bigint(&mut self, n: &BigInt) {
self.update(&n.to_bytes())
self.update(n.to_bytes())
}

fn input_point<E: Curve>(&mut self, point: &Point<E>) {
self.update(&point.to_bytes(false)[..])
}

fn input_scalar<E: Curve>(&mut self, scalar: &Scalar<E>) {
self.update(&scalar.to_bigint().to_bytes())
self.update(scalar.to_bigint().to_bytes())
}

fn result_bigint(self) -> BigInt {
Expand All @@ -106,15 +107,15 @@ where
fn result_scalar<E: Curve>(self) -> Scalar<E> {
let scalar_len = <<E::Scalar as ECScalar>::ScalarLength as Unsigned>::to_usize();
assert!(
Self::output_size() >= scalar_len,
<Self as OutputSizeUser>::output_size() >= scalar_len,
"Output size of the hash({}) is smaller than the scalar length({})",
Self::output_size(),
<Self as OutputSizeUser>::output_size(),
scalar_len
);
// Try and increment.
for i in 0u32.. {
let starting_state = self.clone();
let hash = starting_state.chain(i.to_be_bytes()).finalize();
let hash = starting_state.chain_update(i.to_be_bytes()).finalize();
if let Ok(scalar) = Scalar::from_bytes(&hash[..scalar_len]) {
return scalar;
}
Expand All @@ -123,7 +124,7 @@ where
}

fn digest_bigint(bytes: &[u8]) -> BigInt {
Self::new().chain(bytes).result_bigint()
Self::new().chain_update(bytes).result_bigint()
}
}

Expand All @@ -147,7 +148,15 @@ pub trait HmacExt: Sized {

impl<D> HmacExt for Hmac<D>
where
D: digest::Update + digest::BlockInput + digest::FixedOutput + digest::Reset + Default + Clone,
D: CoreProxy,
D::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
fn new_bigint(key: &BigInt) -> Self {
let bytes = key.to_bytes();
Expand All @@ -163,7 +172,7 @@ where
}

fn verify_bigint(self, code: &BigInt) -> Result<(), MacError> {
let mut code_array = GenericArray::<u8, <D as digest::FixedOutput>::OutputSize>::default();
let mut code_array = GenericArray::<u8, <D::Core as OutputSizeUser>::OutputSize>::default();
let code_length = code_array.len();
let bytes = code.to_bytes();
if bytes.len() > code_length {
Expand All @@ -176,8 +185,6 @@ where

#[cfg(test)]
mod test {
use digest::generic_array::ArrayLength;
use digest::{BlockInput, FixedOutput, Reset, Update};
use hmac::Hmac;
use sha2::{Sha256, Sha512};

Expand Down Expand Up @@ -303,9 +310,15 @@ mod test {
crate::test_for_all_hashes!(create_hmac_test);
fn create_hmac_test<H>()
where
H: Update + BlockInput + FixedOutput + Reset + Default + Clone,
H::BlockSize: ArrayLength<u8>,
H::OutputSize: ArrayLength<u8>,
H: CoreProxy,
H::Core: HashMarker
+ UpdateCore
+ FixedOutputCore
+ BufferKindUser<BufferKind = Eager>
+ Default
+ Clone,
<H::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<H::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
let key = BigInt::sample(512);
let result1 = Hmac::<H>::new_bigint(&key)
Expand Down
2 changes: 1 addition & 1 deletion src/cryptographic_primitives/hashing/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ where
type Item = Output<D>;

fn merge(left: &Self::Item, right: &Self::Item) -> Self::Item {
D::new().chain(left).chain(right).finalize()
D::new().chain_update(left).chain_update(right).finalize()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ mod tests {
E,
};
let proof = HomoELGamalProof::<E, H>::prove(&witness, &delta);
assert!(!proof.verify(&delta).is_ok());
assert!(proof.verify(&delta).is_err());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ mod tests {
E,
};
let proof = HomoELGamalDlogProof::<E, H>::prove(&witness, &delta);
assert!(!proof.verify(&delta).is_ok());
assert!(proof.verify(&delta).is_err());
}
}
2 changes: 1 addition & 1 deletion src/cryptographic_primitives/proofs/sigma_ec_ddh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ mod tests {
};
let w = ECDDHWitness { x };
let proof = ECDDHProof::<E, H>::prove(&w, &delta);
assert!(!proof.verify(&delta).is_ok());
assert!(proof.verify(&delta).is_err());
}
}
2 changes: 1 addition & 1 deletion src/elliptic/curves/bls12_381/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ mod tests {
// Print in uncompressed form
use pairing_plus::EncodedPoint;
let point_uncompressed = G1Uncompressed::from_affine(point);
println!("Uncompressed base_point2: {:?}", point_uncompressed);
println!("Uncompressed base_point2: {point_uncompressed:?}");

// Check that ECPoint::base_point2() returns generated point
let base_point2: &GE1 = ECPoint::base_point2();
Expand Down
2 changes: 1 addition & 1 deletion src/elliptic/curves/bls12_381/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ mod tests {

// Print in uncompressed form
let point_uncompressed = G2Uncompressed::from_affine(point);
println!("Uncompressed base_point2: {:?}", point_uncompressed);
println!("Uncompressed base_point2: {point_uncompressed:?}");

// Check that ECPoint::base_point2() returns generated point
let base_point2: &G2Point = ECPoint::base_point2();
Expand Down
2 changes: 1 addition & 1 deletion src/elliptic/curves/curve_ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ lazy_static::lazy_static! {

static ref BASE_POINT2: RistrettoPoint = {
let g = RistrettoPoint::generator();
let hash = Sha256::digest(g.serialize_compressed().as_ref());
let hash = Sha256::digest(g.serialize_compressed());
RistrettoPoint {
purpose: "base_point2",
ge: RistrettoPoint::deserialize(&hash).unwrap().ge,
Expand Down
4 changes: 2 additions & 2 deletions src/elliptic/curves/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ lazy_static::lazy_static! {

static ref BASE_POINT2: Ed25519Point = {
let bytes = GENERATOR.serialize_compressed();
let hashed = sha2::Sha256::digest(bytes.as_ref());
let hashed_twice = sha2::Sha256::digest(&hashed);
let hashed = sha2::Sha256::digest(bytes);
let hashed_twice = sha2::Sha256::digest(hashed);
let p = Ed25519Point::deserialize(&hashed_twice).unwrap();
let eight = Ed25519Scalar::from_bigint(&BigInt::from(8));
Ed25519Point {
Expand Down
4 changes: 2 additions & 2 deletions src/elliptic/curves/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ mod tests {
let base_point2 = GE::base_point2();

let g = GE::generator();
let hash = Sha256::digest(g.serialize_compressed().as_ref());
let hash = Sha256::digest(&hash);
let hash = Sha256::digest(g.serialize_compressed());
let hash = Sha256::digest(hash);

assert_eq!(BigInt::from_bytes(&hash), base_point2.x_coord().unwrap());

Expand Down
6 changes: 3 additions & 3 deletions src/elliptic/curves/secp256_k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ mod test {
let base_point2 = GE::base_point2();

let g = GE::generator();
let hash = Sha256::digest(&g.serialize_compressed());
let hash = Sha256::digest(&hash);
let hash = Sha256::digest(&hash);
let hash = Sha256::digest(g.serialize_compressed());
let hash = Sha256::digest(hash);
let hash = Sha256::digest(hash);

assert_eq!(BigInt::from_bytes(&hash), base_point2.x_coord().unwrap());

Expand Down
Loading