Skip to content

Commit a6f22ef

Browse files
authored
elliptic-curve: add Reduce::from_*e_digest_reduced (#869)
Adds `digest` feature-gated methods to the `Reduce` trait for reducing the output of a hash function as either a big endian or little endian integer.
1 parent a42440f commit a6f22ef

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

.github/workflows/elliptic-curve.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features arithmetic
4141
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features bits
4242
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features dev
43+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features digest
4344
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features ecdh
4445
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features hazmat
4546
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features jwk
@@ -49,7 +50,7 @@ jobs:
4950
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features serde
5051
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features pkcs8,sec1
5152
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features pem,pkcs8,sec1
52-
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features alloc,ecdh,hazmat,jwk,pem,pkcs8,sec1,serde
53+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features alloc,digest,ecdh,hazmat,jwk,pem,pkcs8,sec1,serde
5354

5455
test:
5556
runs-on: ubuntu-latest

elliptic-curve/src/ops.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use subtle::CtOption;
88
#[cfg(feature = "arithmetic")]
99
use group::Group;
1010

11+
#[cfg(feature = "digest")]
12+
use digest::{BlockInput, Digest, FixedOutput, Reset, Update};
13+
1114
/// Perform an inversion on a field element (i.e. base field element or scalar)
1215
pub trait Invert {
1316
/// Field element type
@@ -46,24 +49,48 @@ pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
4649
/// Perform a modular reduction, returning a field element.
4750
fn from_uint_reduced(n: UInt) -> Self;
4851

49-
/// Interpret the given byte array as a big endian integer and perform a
50-
/// modular reduction.
52+
/// Interpret the given byte array as a big endian integer and perform
53+
/// a modular reduction.
5154
fn from_be_bytes_reduced(bytes: ByteArray<UInt>) -> Self {
5255
Self::from_uint_reduced(UInt::from_be_byte_array(bytes))
5356
}
5457

55-
/// Interpret the given byte array as a big endian integer and perform a
58+
/// Interpret the given byte array as a little endian integer and perform a
5659
/// modular reduction.
5760
fn from_le_bytes_reduced(bytes: ByteArray<UInt>) -> Self {
5861
Self::from_uint_reduced(UInt::from_le_byte_array(bytes))
5962
}
63+
64+
/// Interpret a digest as a big endian integer and perform a modular
65+
/// reduction.
66+
#[cfg(feature = "digest")]
67+
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
68+
fn from_be_digest_reduced<D>(digest: D) -> Self
69+
where
70+
D: FixedOutput<OutputSize = UInt::ByteSize> + BlockInput + Clone + Default + Reset + Update,
71+
{
72+
Self::from_be_bytes_reduced(digest.finalize())
73+
}
74+
75+
/// Interpret a digest as a little endian integer and perform a modular
76+
/// reduction.
77+
#[cfg(feature = "digest")]
78+
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
79+
fn from_le_digest_reduced<D>(digest: D) -> Self
80+
where
81+
D: FixedOutput<OutputSize = UInt::ByteSize> + BlockInput + Clone + Default + Reset + Update,
82+
{
83+
Self::from_le_bytes_reduced(digest.finalize())
84+
}
6085
}
6186

6287
/// Modular reduction to a non-zero output.
6388
///
64-
/// This trait is primarily intended for use by curve implementations.
89+
/// This trait is primarily intended for use by curve implementations such
90+
/// as the `k256` and `p256` crates.
6591
///
66-
/// End users can use the `Reduce` impl on `NonZeroScalar` instead.
92+
/// End users should use the [`Reduce`] impl on
93+
/// [`NonZeroScalar`][`crate::NonZeroScalar`] instead.
6794
pub trait ReduceNonZero<UInt: Integer + ArrayEncoding>: Sized {
6895
/// Perform a modular reduction, returning a field element.
6996
fn from_uint_reduced_nonzero(n: UInt) -> Self;

0 commit comments

Comments
 (0)