@@ -25,6 +25,7 @@ use num_bigint::{BigInt, Sign};
25
25
use num_integer:: Integer ;
26
26
use num_traits:: { One , ToPrimitive , Zero } ;
27
27
use once_cell:: unsync:: Lazy ;
28
+ use p256_:: elliptic_curve:: bigint:: { Encoding , U384 } ;
28
29
use p256_:: elliptic_curve:: group:: prime:: PrimeCurveAffine ;
29
30
use p256_:: elliptic_curve:: ops:: Reduce ;
30
31
use p256_:: elliptic_curve:: sec1:: { FromEncodedPoint , ToEncodedPoint } ;
@@ -122,24 +123,19 @@ impl Group for NistP256 {
122
123
// P-256 `n` is defined as
123
124
// `115792089210356248762697446949407573529996955224135760342
124
125
// 422259061068512044369`
125
- const N : Lazy < BigInt > = Lazy :: new ( || {
126
- BigInt :: from_str (
127
- "115792089210356248762697446949407573529996955224135760342422259061068512044369" ,
128
- )
129
- . unwrap ( )
130
- } ) ;
126
+ const N : U384 =
127
+ U384 :: from_be_hex ( "00000000000000000000000000000000FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551" ) ;
131
128
132
129
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.3
133
130
// `HashToScalar` is `hash_to_field`
134
131
let uniform_bytes = super :: expand:: expand_message_xmd :: < H , L > ( input, & dst) ?;
135
- let bytes = BigInt :: from_bytes_be ( Sign :: Plus , & uniform_bytes)
136
- . mod_floor ( & N )
137
- . to_bytes_be ( )
138
- . 1 ;
139
- let mut result = GenericArray :: default ( ) ;
140
- result[ ..bytes. len ( ) ] . copy_from_slice ( & bytes) ;
132
+ let bytes = Option :: < U384 > :: from ( U384 :: from_be_slice ( & uniform_bytes) . reduce ( & N ) )
133
+ . unwrap ( )
134
+ . to_be_bytes ( ) ;
141
135
142
- Ok ( Scalar :: from_be_bytes_reduced ( result) )
136
+ Ok ( Scalar :: from_be_bytes_reduced (
137
+ GenericArray :: clone_from_slice ( & bytes[ 16 ..] ) ,
138
+ ) )
143
139
}
144
140
145
141
fn base_elem ( ) -> Self :: Elem {
0 commit comments