Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Implement tests for Point Addition and Doubling.
Browse files Browse the repository at this point in the history
This closes #31 since the point values obtained
were proven correct.

- Refactored Point addition and subtraction functions.
- Tested Point Addition successfully.
- Tested Point doubling with double addition successfully.
  • Loading branch information
CPerezz committed Jul 3, 2019
1 parent cf6f37a commit f90c86d
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,8 @@ impl<'a, 'b> Add<&'b EdwardsPoint> for &'a EdwardsPoint {
/// This implementation is specific for curves with `a = -1` as Doppio is.
/// [Source: 2008 Hisil–Wong–Carter–Dawson],
/// (http://eprint.iacr.org/2008/522), Section 3.1.
///
/// Trick 2k' is used to reduce 1D and 1M. `2d' = k = 2*(-a/EDWARDS_D)`.
#[inline]
fn add(self, other: &'b EdwardsPoint) -> EdwardsPoint {
let k = constants::EDWARDS_2_D_PRIME;
let two = FieldElement::from(&2u8);

let A = &(&self.Y - &self.X) * &(&other.Y - &other.X);
let B = &(&self.Y + &self.X) * &(&other.Y + &other.X);
let C = &k * &(&self.T * &other.T);
let D = &two * &(&self.Z * &other.Z);


EdwardsPoint {
X: &(&B - &A) * &(&D - &C),
Y: &(&D + &C) * &(&B + &A),
Z: &(&D - &C) * &(&D + &C),
T: &(&B - &A) * &(&B + &A),
}
/*
let A = &self.X * &other.X;
let B = &self.Y * &other.Y;
let C = &constants::EDWARDS_D * &(&self.T * &other.T);
Expand All @@ -229,7 +211,7 @@ impl<'a, 'b> Add<&'b EdwardsPoint> for &'a EdwardsPoint {
Z: &F * &G,
T: &E * &H
}
*/

}
}

Expand All @@ -240,25 +222,25 @@ impl<'a, 'b> Sub<&'b EdwardsPoint> for &'a EdwardsPoint {
/// Source: 2008 Hisil–Wong–Carter–Dawson,
/// http://eprint.iacr.org/2008/522, Section 3.1.
///
/// Trick 2k' is used to reduce 1D and 1M. `2d' = k = 2*(-a/EDWARDS_D)`.
///
/// The only thing we do is to negate the second `EdwardsPoint`
/// and add it following the same addition algorithm.
fn sub(self, other: &'b EdwardsPoint) -> EdwardsPoint {
let other_neg = -other;
let k = constants::EDWARDS_2_D_PRIME;
let two = FieldElement::from(&2u8);

let A = &(&self.Y - &self.X) * &(&other_neg.Y - &other_neg.X);
let B = &(&self.Y + &self.X) * &(&other_neg.Y + &other_neg.X);
let C = &k * &(&self.T * &other_neg.T);
let D = &two * &(&self.Z * &other_neg.Z);

let A = &self.X * &other_neg.X;
let B = &self.Y * &other_neg.Y;
let C = &constants::EDWARDS_D * &(&self.T * &other_neg.T);
let D = &self.Z * &other_neg.Z;
let E = &(&(&(&self.X + &self.Y) * &(&other_neg.X + &other_neg.Y)) - &A) - &B;
let F = &D - &C;
let G = &D + &C;
let H = &B - &(&constants::EDWARDS_A * &A);

EdwardsPoint {
X: &(&B - &A) * &(&D - &C),
Y: &(&D + &C) * &(&B + &A),
Z: &(&D - &C) * &(&D + &C),
T: &(&B - &A) * &(&B + &A),
X: &E * &F,
Y: &G * &H,
Z: &F * &G,
T: &E * &H
}
}
}
Expand Down Expand Up @@ -371,25 +353,33 @@ pub mod tests {
use constants::*;

pub static P1: EdwardsPoint = EdwardsPoint {
X: FieldElement([16, 0, 0, 0, 0]),
Y: FieldElement([4112882858868125, 171583307561880, 463872010149030, 512880349810581, 19710686939275]),
X: FieldElement([23, 0, 0, 0, 0]),
Y: FieldElement([1664892896009688, 132583819244870, 812547420185263, 637811013879057, 13284180325998]),
Z: FieldElement([1, 0, 0, 0, 0]),
T: FieldElement([275573095870305, 2976693068521307, 3444354992660745, 820608559696929, 31537099102840])
T: FieldElement([4351986304670635, 4020128726404030, 674192131526433, 1158854437106827, 6468984742885])
};

pub static P2: EdwardsPoint = EdwardsPoint {
X: FieldElement([14, 0, 0, 0, 0]),
Y: FieldElement([2541731663760519, 1411166583131170, 672412408502378, 2234221051654575, 162486610602019]),
X: FieldElement([68, 0, 0, 0, 0]),
Y: FieldElement([1799957170131195, 4493955741554471, 4409493758224495, 3389415867291423, 16342693473584]),
Z: FieldElement([1, 0, 0, 0, 0]),
T: FieldElement([2657704403790627, 1074913290909539, 3643537148325627, 1326469621368206, 227481254842827])
T: FieldElement([3505259403500377, 292342788271022, 2608000066641474, 796697979921534, 2995435405555])
};

/// `P4 = P1 + P2` over Twisted Edwards Extended Coordinates.
pub static P4: EdwardsPoint = EdwardsPoint {
X: FieldElement([1106116171721398, 2254842443722131, 1674823643031469, 888258527705032, 9168478096854]),
Y: FieldElement([3238218424552481, 1479431889513510, 4104779723211529, 1876200851780615, 4567251644102]),
Z: FieldElement([4438549926743592, 904964170113195, 1175658886298356, 1566705569164566, 7405911658994]),
T: FieldElement([4359306299415257, 462203368072626, 4155104864536782, 1553443116496070, 6310755135977])
X: FieldElement([1933054591726350, 4403792816774408, 3029093546253310, 1134491999944368, 8146384875494]),
Y: FieldElement([3369514960042642, 4355698098047571, 1547650124195635, 1314697306673062, 12051634278308]),
Z: FieldElement([576719056868307, 1763329757922533, 3184642959683715, 2550235128581121, 11094626825862]),
T: FieldElement([4345938036071968, 1280347559053553, 3286762790776823, 3577757860131876, 6505793015434])
};

/// `P3 = 2P1` over Twisted Edwards Extended Coordinates.
pub static P3: EdwardsPoint = EdwardsPoint {
X: FieldElement([3851124475403222, 3539758816612178, 1146717153316815, 2152796892260637, 5956037993247]),
Y: FieldElement([980361497621373, 1671502808757874, 2143986549518967, 1109176323830729, 9039277193734]),
Z: FieldElement([2942534902618579, 3556685217095302, 1974617438797742, 1657071371119364, 16635295697052]),
T: FieldElement([2487305805734419, 681684275336734, 499518740758148, 156812857292600, 3978688323434])
};


Expand Down Expand Up @@ -424,7 +414,14 @@ pub mod tests {
#[test]
fn point_addition_extended_coords() {
let res = &P1 + &P2;
println!("{:?}", res);

assert!(res == P4);
}

#[test]
fn point_doubling_double_adding_extended_coords() {
let res: EdwardsPoint = &P1 + &P1;

assert!(res == P3);
}
}

0 comments on commit f90c86d

Please sign in to comment.