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

Update to digest 0.10 #883

Merged
merged 2 commits into from
Jan 17, 2022
Merged
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
44 changes: 19 additions & 25 deletions elliptic-curve/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ zeroize = { version = "1.5", default-features = false }

# optional dependencies
base64ct = { version = "1", optional = true, default-features = false }
digest = { version = "0.9", optional = true, default-features = false }
digest = { version = "0.10", optional = true }
ff = { version = "0.11", optional = true, default-features = false }
group = { version = "0.11", optional = true, default-features = false }
hex-literal = { version = "0.3", optional = true }
Expand All @@ -41,8 +41,8 @@ serde_json = { version = "1", optional = true, default-features = false, feature

[dev-dependencies]
hex-literal = "0.3"
sha2 = "0.9"
sha3 = "0.9"
sha2 = "0.10"
sha3 = "0.10"

[features]
default = ["arithmetic"]
Expand Down
7 changes: 6 additions & 1 deletion elliptic-curve/src/hash2field/expand_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ where
X: Digest<OutputSize = L>,
{
if dst.len() > MAX_DST_LEN {
Self::Hashed(X::new().chain(OVERSIZE_DST_SALT).chain(dst).finalize())
Self::Hashed({
let mut hash = X::new();
hash.update(OVERSIZE_DST_SALT);
hash.update(dst);
hash.finalize()
})
} else {
Self::Array(dst)
}
Expand Down
57 changes: 29 additions & 28 deletions elliptic-curve/src/hash2field/expand_msg/xmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use core::marker::PhantomData;
use super::{Domain, ExpandMsg, Expander};
use crate::{Error, Result};
use digest::{
core_api::BlockSizeUser,
generic_array::{
typenum::{IsLess, IsLessOrEqual, Unsigned, U256},
GenericArray,
},
BlockInput, Digest,
Digest,
};

/// Placeholder type for implementing `expand_message_xmd` based on a hash function
Expand All @@ -20,14 +21,14 @@ use digest::{
/// - `len_in_bytes > 255 * HashT::OutputSize`
pub struct ExpandMsgXmd<HashT>(PhantomData<HashT>)
where
HashT: Digest + BlockInput,
HashT: Digest + BlockSizeUser,
HashT::OutputSize: IsLess<U256>,
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>;

/// ExpandMsgXmd implements expand_message_xmd for the ExpandMsg trait
impl<'a, HashT> ExpandMsg<'a> for ExpandMsgXmd<HashT>
where
HashT: Digest + BlockInput,
HashT: Digest + BlockSizeUser,
// If `len_in_bytes` is bigger then 256, length of the `DST` will depend on
// the output size of the hash, which is still not allowed to be bigger then 256:
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-6
Expand All @@ -53,25 +54,25 @@ where
let ell = u8::try_from((len_in_bytes + b_in_bytes - 1) / b_in_bytes).map_err(|_| Error)?;

let domain = Domain::xmd::<HashT>(dst);
let mut b_0 = HashT::new().chain(GenericArray::<u8, HashT::BlockSize>::default());
let mut b_0 = HashT::new();
b_0.update(GenericArray::<u8, HashT::BlockSize>::default());

for msg in msgs {
b_0 = b_0.chain(msg);
b_0.update(msg);
}

let b_0 = b_0
.chain(len_in_bytes_u16.to_be_bytes())
.chain([0])
.chain(domain.data())
.chain([domain.len()])
.finalize();
b_0.update(len_in_bytes_u16.to_be_bytes());
b_0.update([0]);
b_0.update(domain.data());
b_0.update([domain.len()]);
let b_0 = b_0.finalize();

let b_vals = HashT::new()
.chain(&b_0[..])
.chain([1u8])
.chain(domain.data())
.chain([domain.len()])
.finalize();
let mut b_vals = HashT::new();
b_vals.update(&b_0[..]);
b_vals.update([1u8]);
b_vals.update(domain.data());
b_vals.update([domain.len()]);
let b_vals = b_vals.finalize();

Ok(ExpanderXmd {
b_0,
Expand All @@ -87,7 +88,7 @@ where
/// [`Expander`] type for [`ExpandMsgXmd`].
pub struct ExpanderXmd<'a, HashT>
where
HashT: Digest + BlockInput,
HashT: Digest + BlockSizeUser,
HashT::OutputSize: IsLess<U256>,
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
{
Expand All @@ -101,7 +102,7 @@ where

impl<'a, HashT> ExpanderXmd<'a, HashT>
where
HashT: Digest + BlockInput,
HashT: Digest + BlockSizeUser,
HashT::OutputSize: IsLess<U256>,
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
{
Expand All @@ -116,12 +117,12 @@ where
.zip(&self.b_vals[..])
.enumerate()
.for_each(|(j, (b0val, bi1val))| tmp[j] = b0val ^ bi1val);
self.b_vals = HashT::new()
.chain(tmp)
.chain([self.index])
.chain(self.domain.data())
.chain([self.domain.len()])
.finalize();
let mut b_vals = HashT::new();
b_vals.update(tmp);
b_vals.update([self.index]);
b_vals.update(self.domain.data());
b_vals.update([self.domain.len()]);
self.b_vals = b_vals.finalize();
true
} else {
false
Expand All @@ -131,7 +132,7 @@ where

impl<'a, HashT> Expander for ExpanderXmd<'a, HashT>
where
HashT: Digest + BlockInput,
HashT: Digest + BlockSizeUser,
HashT::OutputSize: IsLess<U256>,
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
{
Expand Down Expand Up @@ -163,7 +164,7 @@ mod test {
len_in_bytes: u16,
bytes: &[u8],
) where
HashT: Digest + BlockInput,
HashT: Digest + BlockSizeUser,
HashT::OutputSize: IsLess<U256>,
{
let block = HashT::BlockSize::to_usize();
Expand Down Expand Up @@ -203,7 +204,7 @@ mod test {
domain: &Domain<'_, HashT::OutputSize>,
) -> Result<()>
where
HashT: Digest + BlockInput,
HashT: Digest + BlockSizeUser,
HashT::OutputSize: IsLess<U256> + IsLessOrEqual<HashT::BlockSize>,
{
assert_message::<HashT>(self.msg, domain, L::to_u16(), self.msg_prime);
Expand Down
6 changes: 3 additions & 3 deletions elliptic-curve/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use subtle::CtOption;
use group::Group;

#[cfg(feature = "digest")]
use digest::{BlockInput, Digest, FixedOutput, Reset, Update};
use digest::{core_api::BlockSizeUser, Digest, FixedOutput, Reset};

/// Perform an inversion on a field element (i.e. base field element or scalar)
pub trait Invert {
Expand Down Expand Up @@ -67,7 +67,7 @@ pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
fn from_be_digest_reduced<D>(digest: D) -> Self
where
D: FixedOutput<OutputSize = UInt::ByteSize> + BlockInput + Clone + Default + Reset + Update,
D: FixedOutput<OutputSize = UInt::ByteSize> + BlockSizeUser + Clone + Digest + Reset,
{
Self::from_be_bytes_reduced(digest.finalize())
}
Expand All @@ -78,7 +78,7 @@ pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
fn from_le_digest_reduced<D>(digest: D) -> Self
where
D: FixedOutput<OutputSize = UInt::ByteSize> + BlockInput + Clone + Default + Reset + Update,
D: FixedOutput<OutputSize = UInt::ByteSize> + BlockSizeUser + Clone + Digest + Reset,
{
Self::from_le_bytes_reduced(digest.finalize())
}
Expand Down