@@ -8,6 +8,9 @@ use subtle::CtOption;
88#[ cfg( feature = "arithmetic" ) ]
99use 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)
1215pub 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.
6794pub 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