Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p256: move FieldElement inherent impl block to top of file #595

Merged
merged 1 commit into from Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
144 changes: 72 additions & 72 deletions p256/src/arithmetic/field.rs
Expand Up @@ -49,78 +49,6 @@ const R2: FieldElement = FieldElement([
#[derive(Clone, Copy, Debug)]
pub struct FieldElement(pub(crate) [u64; LIMBS]);

impl Field for FieldElement {
fn random(mut rng: impl RngCore) -> Self {
// We reduce a random 512-bit value into a 256-bit field, which results in a
// negligible bias from the uniform distribution.
let mut buf = [0; 64];
rng.fill_bytes(&mut buf);
FieldElement::from_bytes_wide(buf)
}

fn zero() -> Self {
Self::ZERO
}

fn one() -> Self {
Self::ONE
}

#[must_use]
fn square(&self) -> Self {
self.square()
}

#[must_use]
fn double(&self) -> Self {
self.double()
}

fn invert(&self) -> CtOption<Self> {
self.invert()
}

fn sqrt(&self) -> CtOption<Self> {
self.sqrt()
}
}

impl ConditionallySelectable for FieldElement {
fn conditional_select(a: &FieldElement, b: &FieldElement, choice: Choice) -> FieldElement {
FieldElement([
u64::conditional_select(&a.0[0], &b.0[0], choice),
u64::conditional_select(&a.0[1], &b.0[1], choice),
u64::conditional_select(&a.0[2], &b.0[2], choice),
u64::conditional_select(&a.0[3], &b.0[3], choice),
])
}
}

impl ConstantTimeEq for FieldElement {
fn ct_eq(&self, other: &Self) -> Choice {
self.0[0].ct_eq(&other.0[0])
& self.0[1].ct_eq(&other.0[1])
& self.0[2].ct_eq(&other.0[2])
& self.0[3].ct_eq(&other.0[3])
}
}

impl Default for FieldElement {
fn default() -> Self {
FieldElement::zero()
}
}

impl DefaultIsZeroes for FieldElement {}

impl Eq for FieldElement {}

impl PartialEq for FieldElement {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}

impl FieldElement {
/// Zero element.
pub const ZERO: Self = FieldElement([0, 0, 0, 0]);
Expand Down Expand Up @@ -488,6 +416,78 @@ impl FieldElement {
}
}

impl Field for FieldElement {
fn random(mut rng: impl RngCore) -> Self {
// We reduce a random 512-bit value into a 256-bit field, which results in a
// negligible bias from the uniform distribution.
let mut buf = [0; 64];
rng.fill_bytes(&mut buf);
FieldElement::from_bytes_wide(buf)
}

fn zero() -> Self {
Self::ZERO
}

fn one() -> Self {
Self::ONE
}

#[must_use]
fn square(&self) -> Self {
self.square()
}

#[must_use]
fn double(&self) -> Self {
self.double()
}

fn invert(&self) -> CtOption<Self> {
self.invert()
}

fn sqrt(&self) -> CtOption<Self> {
self.sqrt()
}
}

impl ConditionallySelectable for FieldElement {
fn conditional_select(a: &FieldElement, b: &FieldElement, choice: Choice) -> FieldElement {
FieldElement([
u64::conditional_select(&a.0[0], &b.0[0], choice),
u64::conditional_select(&a.0[1], &b.0[1], choice),
u64::conditional_select(&a.0[2], &b.0[2], choice),
u64::conditional_select(&a.0[3], &b.0[3], choice),
])
}
}

impl ConstantTimeEq for FieldElement {
fn ct_eq(&self, other: &Self) -> Choice {
self.0[0].ct_eq(&other.0[0])
& self.0[1].ct_eq(&other.0[1])
& self.0[2].ct_eq(&other.0[2])
& self.0[3].ct_eq(&other.0[3])
}
}

impl Default for FieldElement {
fn default() -> Self {
FieldElement::zero()
}
}

impl DefaultIsZeroes for FieldElement {}

impl Eq for FieldElement {}

impl PartialEq for FieldElement {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}

impl Add<FieldElement> for FieldElement {
type Output = FieldElement;

Expand Down