Skip to content

Commit

Permalink
fix: tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Capybara committed Dec 13, 2023
1 parent 3257821 commit a96b58b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/curve/extended_edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub struct ExtendedPoint {
}

impl ExtendedPoint {
/// Performs variable-base scalar multiplication on an elliptic curve point.
/// Performs fixed-base scalar multiplication on an elliptic curve point.
///
/// This function multiplies an elliptic curve point (`point`) with a scalar (`s`) and returns
/// the resulting point. It is optimized for variable-base multiplication, which is a common
/// the resulting point. It is optimized for fixed-base multiplication, which is a common
/// operation in elliptic curve cryptography, particularly in contexts like key exchange or
/// digital signature generation.
///
Expand Down Expand Up @@ -59,7 +59,7 @@ impl ExtendedPoint {
/// # Returns
///
/// An `ExtendedPoint` that is the result of the scalar multiplication of `point` by `s`.
pub fn variable_base(point: &ExtendedPoint, s: &Scalar) -> ExtendedPoint {
pub fn fixed_base(point: &ExtendedPoint, s: &Scalar) -> ExtendedPoint {
// We make use of the faster doubling for TwistedPoint
let mut result = TwistedPoint::identity();

Expand Down Expand Up @@ -206,7 +206,7 @@ impl ConditionallySelectable for ExtendedPoint {
impl Mul<Scalar> for ExtendedPoint {
type Output = ExtendedPoint;
fn mul(self, scalar: Scalar) -> ExtendedPoint {
ExtendedPoint::variable_base(&self, &scalar)
ExtendedPoint::fixed_base(&self, &scalar)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/curve/field/lookup_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl From<&ExtendedPoint> for LookupTable {
impl LookupTable {
/// Selects a projective niels point from a lookup table in fixed-time
pub fn select(&self, index: u32) -> ProjectiveNielsPoint {
let mut result = ProjectiveNielsPoint::id_point();
let mut result = ProjectiveNielsPoint::identity();
for i in 1..9 {
let swap = index.ct_eq(&(i as u32));
result.conditional_assign(&self.0[i - 1], swap);
Expand Down
25 changes: 23 additions & 2 deletions src/curve/projective_niels.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(non_snake_case)]
use super::{field::field_element::FieldElement, twisted_edwards::TwistedPoint};
use super::{
extended_edwards::ExtendedPoint, field::field_element::FieldElement,
twisted_edwards::TwistedPoint,
};
use crypto_bigint::subtle::{Choice, ConditionallyNegatable, ConditionallySelectable};

// Variant of Niels, where a Z coordinate is added for unmixed readdition
Expand All @@ -13,9 +16,27 @@ pub struct ProjectiveNielsPoint {
}

impl ProjectiveNielsPoint {
pub fn id_point() -> ProjectiveNielsPoint {
pub fn identity() -> ProjectiveNielsPoint {
TwistedPoint::identity().to_projective_niels()
}

pub fn double(&self) -> ProjectiveNielsPoint {
self.to_extended()
.to_extensible()
.double()
.to_projective_niels()
}

pub fn to_extended(&self) -> ExtendedPoint {
let A = self.Y_plus_X - self.Y_minus_X;
let B = self.Y_plus_X + self.Y_minus_X;
ExtendedPoint {
X: self.Z * A,
Y: self.Z * B,
Z: self.Z.square(),
T: B * A,
}
}
}

impl ConditionallySelectable for ProjectiveNielsPoint {
Expand Down
22 changes: 11 additions & 11 deletions tests/e448_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tiny_ed448_goldilocks::curve::{

#[test]
// 0 * G = 𝒪
pub fn test_g_times_zero_id() {
pub fn zerog_id() {
let p = ExtendedPoint::tw_generator();
let zero = Scalar::from(0_u64);
let res = p * zero;
Expand All @@ -21,7 +21,7 @@ pub fn test_g_times_zero_id() {

#[test]
// G * 1 = G
pub fn test_g_times_one_g() {
pub fn oneg_g() {
let p = ExtendedPoint::tw_generator();
let one = Scalar::from(1_u64);
let res = p * one;
Expand All @@ -32,7 +32,7 @@ pub fn test_g_times_one_g() {

// G + (-G) = 𝒪
#[test]
fn test_g_plus_neg_g() {
fn gminusg_id() {
let g = ExtendedPoint::tw_generator();
let neg_g = ExtendedPoint::tw_generator().negate();
let id = g.add(&neg_g);
Expand All @@ -42,7 +42,7 @@ fn test_g_plus_neg_g() {

#[test]
// 2 * G = G + G
pub fn test_g_times_two_g_plus_g() {
pub fn twog_gplusg() {
let g: ExtendedPoint = ExtendedPoint::tw_generator();
let two = Scalar::from(2_u64);
let res = g * two;
Expand All @@ -53,7 +53,7 @@ pub fn test_g_times_two_g_plus_g() {

#[test]
// 4 * G = 2 * (2 * G)
fn test_four_g() {
fn fourg_twotwoG() {
let four_g = ExtendedPoint::tw_generator() * Scalar::from(4_u64);
let two_times_two_g = (ExtendedPoint::tw_generator().double()).double();

Expand All @@ -62,7 +62,7 @@ fn test_four_g() {

#[test]
//4 * G != 𝒪
fn test_four_g_not_id() {
fn fourg_not_id() {
let four_g = ExtendedPoint::tw_generator() * Scalar::from(4_u64);
let tw_four_g = ExtendedPoint::tw_generator() * Scalar::from(4_u64);
let id = ExtendedPoint::id_point();
Expand All @@ -73,7 +73,7 @@ fn test_four_g_not_id() {

#[test]
//r*G = 𝒪
fn r_times_g_id() {
fn rg_id() {
let mut g = ExtendedPoint::tw_generator();
g = g * Scalar::from(U448::from_be_hex(R_448));
let id = ExtendedPoint::id_point();
Expand All @@ -83,7 +83,7 @@ fn r_times_g_id() {

#[test]
// k * G = (k mod r) * G
fn k_g_equals_k_mod_r_times_g() {
fn kg_kmodrg() {
use rand::Rng;
let mut rng = rand::thread_rng();
let random_number: u64 = rng.gen();
Expand All @@ -104,7 +104,7 @@ fn k_g_equals_k_mod_r_times_g() {

#[test]
// (k + 1)*G = (k*G) + G
fn k_plus_one_g() {
fn k_plus_g() {
let mut rng = rand::thread_rng();
let k = rand::Rng::gen::<u64>(&mut rng);

Expand All @@ -117,7 +117,7 @@ fn k_plus_one_g() {

#[test]
//(k + t)*G = (k*G) + (t*G)
fn k_t() {
fn ktG_kgplustg() {
let mut rng = rand::thread_rng();
let k: u32 = rand::Rng::gen::<u32>(&mut rng);
let t: u32 = rand::Rng::gen::<u32>(&mut rng);
Expand All @@ -134,7 +134,7 @@ fn k_t() {

#[test]
//k*(t*G) = t*(k*G) = (k*t mod r)*G
fn test_ktg() {
fn ktG_tkG_ktmodrG() {
let mut rng = rand::thread_rng();
let k: u32 = rand::Rng::gen::<u32>(&mut rng);
let t: u32 = rand::Rng::gen::<u32>(&mut rng);
Expand Down

0 comments on commit a96b58b

Please sign in to comment.