Skip to content

Commit 85e4e70

Browse files
committed
Add null-check for hash_to_scalar
1 parent c2ebc48 commit 85e4e70

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/group/p256.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ use num_traits::{One, ToPrimitive, Zero};
2727
use once_cell::unsync::Lazy;
2828
use p256_::elliptic_curve::bigint::{Encoding, U384};
2929
use p256_::elliptic_curve::group::prime::PrimeCurveAffine;
30-
use p256_::elliptic_curve::ops::Reduce;
3130
use p256_::elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint};
3231
#[cfg(test)]
3332
use p256_::elliptic_curve::Field;
33+
use p256_::elliptic_curve::PrimeField;
3434
use p256_::{AffinePoint, EncodedPoint, NistP256, ProjectivePoint, PublicKey, Scalar, SecretKey};
3535
use rand_core::{CryptoRng, RngCore};
3636
use subtle::{Choice, ConditionallySelectable};
@@ -133,9 +133,11 @@ impl Group for NistP256 {
133133
.unwrap()
134134
.to_be_bytes();
135135

136-
Ok(Scalar::from_be_bytes_reduced(
137-
GenericArray::clone_from_slice(&bytes[16..]),
138-
))
136+
if bytes == [0; 48] {
137+
Err(Error::PointError)
138+
} else {
139+
Ok(Scalar::from_repr_vartime(GenericArray::clone_from_slice(&bytes[16..])).unwrap())
140+
}
139141
}
140142

141143
fn base_elem() -> Self::Elem {

src/group/ristretto.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
66
// of this source tree.
77

8-
use core::convert::TryInto;
9-
108
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
119
use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
1210
use curve25519_dalek::scalar::Scalar;
@@ -62,13 +60,13 @@ impl Group for Ristretto255 {
6260
GenericArray::from(STR_HASH_TO_SCALAR).concat(voprf::get_context_string::<Self>(mode));
6361

6462
let uniform_bytes = expand::expand_message_xmd::<H, U64>(input, &dst)?;
63+
let scalar = Scalar::from_bytes_mod_order_wide(&uniform_bytes.into());
6564

66-
Ok(Scalar::from_bytes_mod_order_wide(
67-
uniform_bytes
68-
.as_slice()
69-
.try_into()
70-
.map_err(|_| Error::HashToCurveError)?,
71-
))
65+
if scalar == Scalar::zero() {
66+
Err(Error::PointError)
67+
} else {
68+
Ok(scalar)
69+
}
7270
}
7371

7472
fn base_elem() -> Self::Elem {

0 commit comments

Comments
 (0)