diff --git a/k256/src/arithmetic/scalar.rs b/k256/src/arithmetic/scalar.rs index 51a5b0bcb..8e84991cc 100644 --- a/k256/src/arithmetic/scalar.rs +++ b/k256/src/arithmetic/scalar.rs @@ -17,7 +17,7 @@ use core::{ use elliptic_curve::{ Curve, Error, ScalarPrimitive, bigint::{Limb, U256, U512, Word, prelude::*}, - ff::{self, Field, PrimeField}, + ff::{self, Field, FromUniformBytes, PrimeField}, ops::{Invert, Reduce, ReduceNonZero}, rand_core::{CryptoRng, TryCryptoRng, TryRngCore}, scalar::{FromUintUnchecked, IsHigh}, @@ -380,6 +380,12 @@ impl From for Scalar { } } +impl FromUniformBytes<64> for Scalar { + fn from_uniform_bytes(bytes: &[u8; 64]) -> Self { + WideScalar::from_bytes(bytes).reduce() + } +} + impl From for Scalar { fn from(scalar: NonZeroScalar) -> Self { *scalar.as_ref() diff --git a/p256/src/arithmetic/scalar.rs b/p256/src/arithmetic/scalar.rs index 805d315f7..1471e7a65 100644 --- a/p256/src/arithmetic/scalar.rs +++ b/p256/src/arithmetic/scalar.rs @@ -14,7 +14,7 @@ use core::{ use elliptic_curve::{ Curve, bigint::{Limb, NonZero, U256, prelude::*}, - group::ff::{self, Field, PrimeField}, + group::ff::{self, Field, FromUniformBytes, PrimeField}, ops::{Invert, Reduce, ReduceNonZero}, rand_core::TryRngCore, scalar::{FromUintUnchecked, IsHigh}, @@ -489,6 +489,15 @@ impl From<&Scalar> for U256 { } } +impl FromUniformBytes<64> for Scalar { + fn from_uniform_bytes(bytes: &[u8; 64]) -> Self { + Self(barrett_reduce( + U256::from_be_slice(&bytes[32..]), + U256::from_be_slice(&bytes[..32]), + )) + } +} + #[cfg(feature = "bits")] impl From<&Scalar> for ScalarBits { fn from(scalar: &Scalar) -> ScalarBits {