Skip to content

Commit b595134

Browse files
authored
Add FromUniformBytes<64> to {k,p}256::Scalar (#1379)
1 parent 6ff0824 commit b595134

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

k256/src/arithmetic/scalar.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use core::{
1717
use elliptic_curve::{
1818
Curve, Error, ScalarPrimitive,
1919
bigint::{Limb, U256, U512, Word, prelude::*},
20-
ff::{self, Field, PrimeField},
20+
ff::{self, Field, FromUniformBytes, PrimeField},
2121
ops::{Invert, Reduce, ReduceNonZero},
2222
rand_core::{CryptoRng, TryCryptoRng, TryRngCore},
2323
scalar::{FromUintUnchecked, IsHigh},
@@ -380,6 +380,12 @@ impl From<u128> for Scalar {
380380
}
381381
}
382382

383+
impl FromUniformBytes<64> for Scalar {
384+
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self {
385+
WideScalar::from_bytes(bytes).reduce()
386+
}
387+
}
388+
383389
impl From<NonZeroScalar> for Scalar {
384390
fn from(scalar: NonZeroScalar) -> Self {
385391
*scalar.as_ref()

p256/src/arithmetic/scalar.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::{
1414
use elliptic_curve::{
1515
Curve,
1616
bigint::{Limb, NonZero, U256, prelude::*},
17-
group::ff::{self, Field, PrimeField},
17+
group::ff::{self, Field, FromUniformBytes, PrimeField},
1818
ops::{Invert, Reduce, ReduceNonZero},
1919
rand_core::TryRngCore,
2020
scalar::{FromUintUnchecked, IsHigh},
@@ -489,6 +489,15 @@ impl From<&Scalar> for U256 {
489489
}
490490
}
491491

492+
impl FromUniformBytes<64> for Scalar {
493+
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self {
494+
Self(barrett_reduce(
495+
U256::from_be_slice(&bytes[32..]),
496+
U256::from_be_slice(&bytes[..32]),
497+
))
498+
}
499+
}
500+
492501
#[cfg(feature = "bits")]
493502
impl From<&Scalar> for ScalarBits {
494503
fn from(scalar: &Scalar) -> ScalarBits {

0 commit comments

Comments
 (0)