Skip to content
Open
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
39 changes: 38 additions & 1 deletion k256/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ use core::{
use elliptic_curve::{
Generate,
bigint::{Odd, U256, modular::Retrieve},
ff::{self, Field, PrimeField},
ff::{self, Field, FromUniformBytes, PrimeField},
ops::Invert,
rand_core::{TryCryptoRng, TryRng},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
zeroize::DefaultIsZeroes,
};

#[cfg(feature = "bits")]
use {crate::ScalarBits, elliptic_curve::group::ff::PrimeFieldBits};

#[cfg(test)]
use num_bigint::{BigUint, ToBigUint};

Expand Down Expand Up @@ -324,6 +327,33 @@ impl PrimeField for FieldElement {
}
}

impl FromUniformBytes<64> for FieldElement {
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self {
let hi = U256::from_be_slice(&bytes[..32]);
let lo = U256::from_be_slice(&bytes[32..]);
let modulus = const { *Odd::from_be_hex(MODULUS_HEX).as_nz_ref() };
Self(FieldElementImpl::from_u256_unchecked(
U256::rem_wide_vartime((lo, hi), &modulus),
))
}
}

#[cfg(feature = "bits")]
impl PrimeFieldBits for FieldElement {
cpubits! {
32 => { type ReprBits = [u32; 8]; }
64 => { type ReprBits = [u64; 4]; }
}

fn to_le_bits(&self) -> ScalarBits {
self.into()
}

fn char_le_bits() -> ScalarBits {
U256::from_be_hex(MODULUS_HEX).to_words().into()
}
}

impl Retrieve for FieldElement {
type Output = U256;

Expand Down Expand Up @@ -509,6 +539,13 @@ impl<'a> Product<&'a FieldElement> for FieldElement {
}
}

#[cfg(feature = "bits")]
impl From<&FieldElement> for ScalarBits {
fn from(scalar: &FieldElement) -> ScalarBits {
scalar.0.to_u256().to_words().into()
}
}

#[cfg(test)]
mod tests {
use elliptic_curve::Generate;
Expand Down
Loading