Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -380,6 +380,12 @@ impl From<u128> for Scalar {
}
}

impl FromUniformBytes<64> for Scalar {
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self {
WideScalar::from_bytes(bytes).reduce()
}
}

impl From<NonZeroScalar> for Scalar {
fn from(scalar: NonZeroScalar) -> Self {
*scalar.as_ref()
Expand Down
11 changes: 10 additions & 1 deletion p256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down