Skip to content

Make ProjectiveNielsPoint::identity() an associated constant #1330

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion ed448-goldilocks/src/curve/scalar_mul/window/wnaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl From<&ExtendedPoint> for LookupTable {
impl LookupTable {
/// Selects a projective niels point from a lookup table in constant time
pub fn select(&self, index: u32) -> ProjectiveNielsPoint {
let mut result = ProjectiveNielsPoint::identity();
let mut result = ProjectiveNielsPoint::IDENTITY;

for i in 1..9 {
let swap = index.ct_eq(&(i as u32));
Expand Down
32 changes: 26 additions & 6 deletions ed448-goldilocks/src/curve/twedwards/projective.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#![allow(non_snake_case)]

use crate::curve::twedwards::{extended::ExtendedPoint, extensible::ExtensiblePoint};
use crate::curve::twedwards::extended::ExtendedPoint;
use crate::field::FieldElement;
use subtle::{Choice, ConditionallyNegatable, ConditionallySelectable};

impl Default for ProjectiveNielsPoint {
fn default() -> ProjectiveNielsPoint {
ProjectiveNielsPoint::identity()
ProjectiveNielsPoint::IDENTITY
}
}

// Its a variant of Niels, where a Z coordinate is added for unmixed readdition
// ((y+x)/2, (y-x)/2, dxy, Z)
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub struct ProjectiveNielsPoint {
pub(crate) Y_plus_X: FieldElement,
pub(crate) Y_minus_X: FieldElement,
Expand Down Expand Up @@ -45,9 +45,12 @@ impl ConditionallyNegatable for ProjectiveNielsPoint {
}

impl ProjectiveNielsPoint {
pub fn identity() -> ProjectiveNielsPoint {
ExtensiblePoint::IDENTITY.to_projective_niels()
}
pub const IDENTITY: ProjectiveNielsPoint = ProjectiveNielsPoint {
Y_plus_X: FieldElement::ONE,
Y_minus_X: FieldElement::ONE,
Td: FieldElement::ZERO,
Z: FieldElement::TWO,
};

pub fn to_extended(self) -> ExtendedPoint {
let A = self.Y_plus_X - self.Y_minus_X;
Expand All @@ -63,6 +66,23 @@ impl ProjectiveNielsPoint {
#[cfg(test)]
mod tests {
use super::*;
use crate::curve::twedwards::extensible::ExtensiblePoint;

#[test]
fn identity() {
// Internally are compared by converting to `ExtendedPoint`.
// Here the right-side identity point is converted to Niel's
// and then both sides are converted to twisted-curve form.
assert_eq!(
ProjectiveNielsPoint::IDENTITY,
ExtensiblePoint::IDENTITY.to_projective_niels(),
);
// Here only the left-side identity point is converted.
assert_eq!(
ProjectiveNielsPoint::IDENTITY.to_extended(),
ExtendedPoint::IDENTITY,
);
}

#[test]
fn test_conditional_negate() {
Expand Down
1 change: 1 addition & 0 deletions ed448-goldilocks/src/field/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl FieldElement {
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000262a8",
)));
pub const ONE: Self = Self(ConstMontyType::new(&U448::ONE));
pub const TWO: Self = Self(ConstMontyType::new(&U448::from_u64(2)));
pub const TWISTED_D: Self = Self(ConstMontyType::new(&U448::from_be_hex(
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffff6755",
)));
Expand Down