From 207af566b92dcb85e92be1ff58f962208c512793 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Thu, 16 Nov 2017 11:14:58 -0800 Subject: [PATCH] Replace signed constants with unsigned constants --- src/constants.rs | 10 +++--- src/constants_32bit.rs | 72 +++++++++++++++++++++++------------------- src/field.rs | 23 -------------- 3 files changed, 45 insertions(+), 60 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 4c159cd..6434f89 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -37,8 +37,8 @@ use scalar::Scalar; #[cfg(feature="radix_51")] pub use constants_64bit::*; -//#[cfg(not(feature="radix_51"))] -//pub use constants_32bit::*; +#[cfg(not(feature="radix_51"))] +pub use constants_32bit::*; /// Basepoint has y = 4/5. /// @@ -151,7 +151,7 @@ mod test { #[cfg(not(feature="radix_51"))] fn sqrt_minus_aplus2() { use field_32bit::FieldElement32; - let minus_aplus2 = FieldElement32([-486664,0,0,0,0,0,0,0,0,0]); + let minus_aplus2 = -&FieldElement32([486664,0,0,0,0,0,0,0,0,0]); let sqrt = constants::SQRT_MINUS_APLUS2; let sq = &sqrt * &sqrt; assert_eq!(sq, minus_aplus2); @@ -181,8 +181,8 @@ mod test { #[test] fn test_d_vs_ratio() { use field_32bit::FieldElement32; - let a = FieldElement32([-121665,0,0,0,0,0,0,0,0,0]); - let b = FieldElement32([ 121666,0,0,0,0,0,0,0,0,0]); + let a = -&FieldElement32([121665,0,0,0,0,0,0,0,0,0]); + let b = FieldElement32([121666,0,0,0,0,0,0,0,0,0]); let d = &a * &b.invert(); let d2 = &d + &d; assert_eq!(d, constants::EDWARDS_D); diff --git a/src/constants_32bit.rs b/src/constants_32bit.rs index 9fbf670..e5fea09 100644 --- a/src/constants_32bit.rs +++ b/src/constants_32bit.rs @@ -18,41 +18,49 @@ use edwards::ExtendedPoint; /// Edwards `d` value, equal to `-121665/121666 mod p`. pub(crate) const EDWARDS_D: FieldElement32 = FieldElement32([ - -10913610, 13857413, -15372611, 6949391, 114729, - -8787816, -6275908, -3247719, -18696448, -12055116, ]); + 56195235, 13857412, 51736253, 6949390, 114729, + 24766616, 60832955, 30306712, 48412415, 21499315, +]); /// Edwards `2*d` value, equal to `2*(-121665/121666) mod p`. pub(crate) const EDWARDS_D2: FieldElement32 = FieldElement32([ - -21827239, -5839606, -30745221, 13898782, 229458, - 15978800, -12551817, -6495438, 29715968, 9444199, ]); + 45281625, 27714825, 36363642, 13898781, 229458, + 15978800, 54557047, 27058993, 29715967, 9444199, +]); /// `= sqrt(a*d - 1)`, where `a = -1 (mod p)`, `d` are the Edwards curve parameters. pub(crate) const SQRT_AD_MINUS_ONE: FieldElement32 = FieldElement32([ - 24849947, -153582, -23613485, 6347715, -21072328, -667138, -25271143, -15367704, -870347, 14525639 + 24849947, 33400850, 43495378, 6347714, 46036536, + 32887293, 41837720, 18186727, 66238516, 14525638, ]); /// `= 1/sqrt(a-d)`, where `a = -1 (mod p)`, `d` are the Edwards curve parameters. pub(crate) const INVSQRT_A_MINUS_D: FieldElement32 = FieldElement32([ - 6111485, 4156064, -27798727, 12243468, -25904040, - 120897, 20826367, -7060776, 6093568, -1986012 + 6111466, 4156064, 39310137, 12243467, 41204824, + 120896, 20826367, 26493656, 6093567, 31568420, ]); /// Precomputed value of one of the square roots of -1 (mod p) pub(crate) const SQRT_M1: FieldElement32 = FieldElement32([ - -32595792, -7943725, 9377950, 3500415, 12389472, - -272473, -25146209, -2005654, 326686, 11406482, ]); + 34513072, 25610706, 9377949, 3500415, 12389472, + 33281959, 41962654, 31548777, 326685, 11406482, +]); /// In Montgomery form y² = x³+Ax²+x, Curve25519 has A=486662. -pub(crate) const MONTGOMERY_A: FieldElement32 = FieldElement32([ - 486662, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]); +pub(crate) const MONTGOMERY_A: FieldElement32 = FieldElement32([ + 486662, 0, 0, 0, 0, 0, 0, 0, 0, 0, +]); /// `APLUS2_OVER_FOUR` is (A+2)/4. (This is used internally within the Montgomery ladder.) -pub(crate) const APLUS2_OVER_FOUR: FieldElement32 = FieldElement32([121666, 0, 0, 0, 0, 0, 0, 0, 0, 0]); +pub(crate) const APLUS2_OVER_FOUR: FieldElement32 = FieldElement32([ + 121666, 0, 0, 0, 0, 0, 0, 0, 0, 0 +]); /// `SQRT_MINUS_APLUS2` is sqrt(-486664) pub(crate) const SQRT_MINUS_APLUS2: FieldElement32 = FieldElement32([ - -12222970, -8312128, -11511410, 9067497, -15300785, - -241793, 25456130, 14121551, -12187136, 3972024]); + 54885894, 25242303, 55597453, 9067496, 51808079, + 33312638, 25456129, 14121551, 54921728, 3972023, +]); /// `SQRT_MINUS_HALF` is sqrt(-1/2) pub const SQRT_MINUS_HALF: FieldElement32 = FieldElement32([ // sqrtMinusHalf @@ -82,10 +90,10 @@ pub(crate) const RR: Scalar32 = Scalar32([ 0x0b5f9d12, 0x1e141b17, 0x158d7f3d, 0 /// distinguish it from `_TABLE`, which should be used for scalar /// multiplication (it's much faster). pub const ED25519_BASEPOINT_POINT: ExtendedPoint = ExtendedPoint{ - X: FieldElement32([-14297830, -7645148, 16144683, -16471763, 27570974, -2696100, -26142465, 8378389, 20764389, 8758491]), - Y: FieldElement32([-26843541, -6710886, 13421773, -13421773, 26843546, 6710886, -13421773, 13421773, -26843546, -6710886]), + X: FieldElement32([52811034, 25909283, 16144682, 17082669, 27570973, 30858332, 40966398, 8378388, 20764389, 8758491]), + Y: FieldElement32([40265304, 26843545, 13421772, 20132659, 26843545, 6710886, 53687091, 13421772, 40265318, 26843545]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - T: FieldElement32([28827062, -6116119, -27349572, 244363, 8635006, 11264893, 19351346, 13413597, 16611511, -6414980]), + T: FieldElement32([28827043, 27438313, 39759291, 244362, 8635006, 11264893, 19351346, 13413597, 16611511, 27139452]), }; /// The 8-torsion subgroup Ɛ[8]. @@ -103,45 +111,45 @@ pub const EIGHT_TORSION: [ExtendedPoint; 8] = [ T: FieldElement32([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }, ExtendedPoint{ - X: FieldElement32([21352778, 5345713, 4660180, -8347857, 24143090, 14568123, 30185756, -12247770, -33528939, 8345319]), - Y: FieldElement32([6952922, 1265500, -6862341, 7057498, 4037696, 5447722, -31680899, 15325402, 19365852, -1569102]), + X: FieldElement32([21352778, 5345713, 4660180, 25206575, 24143089, 14568123, 30185756, 21306662, 33579924, 8345318]), + Y: FieldElement32([6952903, 1265500, 60246523, 7057497, 4037696, 5447722, 35427965, 15325401, 19365852, 31985330]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - T: FieldElement32([-25262188, -11972680, 11716002, -5869612, -18193162, 16297739, 20670665, -8559098, 3541543, -5011181]) + T: FieldElement32([41846657, 21581751, 11716001, 27684820, 48915701, 16297738, 20670665, 24995334, 3541542, 28543251]) }, ExtendedPoint{ - X: FieldElement32([32595792, 7943725, -9377950, -3500415, -12389472, 272473, 25146209, 2005654, -326686, -11406482]), + X: FieldElement32([32595773, 7943725, 57730914, 30054016, 54719391, 272472, 25146209, 2005654, 66782178, 22147949]), Y: FieldElement32([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), T: FieldElement32([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }, ExtendedPoint{ - X: FieldElement32([21352778, 5345713, 4660180, -8347857, 24143090, 14568123, 30185756, -12247770, -33528939, 8345319]), - Y: FieldElement32([-6952922, -1265500, 6862341, -7057498, -4037696, -5447722, 31680899, -15325402, -19365852, 1569102]), + X: FieldElement32([21352778, 5345713, 4660180, 25206575, 24143089, 14568123, 30185756, 21306662, 33579924, 8345318]), + Y: FieldElement32([60155942, 32288931, 6862340, 26496934, 63071167, 28106709, 31680898, 18229030, 47743011, 1569101]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - T: FieldElement32([25262188, 11972680, -11716002, 5869612, 18193162, -16297739, -20670665, 8559098, -3541543, 5011181]) + T: FieldElement32([25262188, 11972680, 55392862, 5869611, 18193162, 17256693, 46438198, 8559097, 63567321, 5011180]) }, ExtendedPoint{ X: FieldElement32([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - Y: FieldElement32([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), + Y: FieldElement32([67108844, 33554431, 67108863, 33554431, 67108863, 33554431, 67108863, 33554431, 67108863, 33554431]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), T: FieldElement32([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }, ExtendedPoint{ - X: FieldElement32([-21352778, -5345713, -4660180, 8347857, -24143090, -14568123, -30185756, 12247770, 33528939, -8345319]), - Y: FieldElement32([-6952922, -1265500, 6862341, -7057498, -4037696, -5447722, 31680899, -15325402, -19365852, 1569102]), + X: FieldElement32([45756067, 28208718, 62448683, 8347856, 42965774, 18986308, 36923107, 12247769, 33528939, 25209113]), + Y: FieldElement32([60155942, 32288931, 6862340, 26496934, 63071167, 28106709, 31680898, 18229030, 47743011, 1569101]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - T: FieldElement32([-25262188, -11972680, 11716002, -5869612, -18193162, 16297739, 20670665, -8559098, 3541543, -5011181]) + T: FieldElement32([41846657, 21581751, 11716001, 27684820, 48915701, 16297738, 20670665, 24995334, 3541542, 28543251]) }, ExtendedPoint{ - X: FieldElement32([-32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482]), + X: FieldElement32([34513072, 25610706, 9377949, 3500415, 12389472, 33281959, 41962654, 31548777, 326685, 11406482]), Y: FieldElement32([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), T: FieldElement32([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }, ExtendedPoint{ - X: FieldElement32([-21352778, -5345713, -4660180, 8347857, -24143090, -14568123, -30185756, 12247770, 33528939, -8345319]), - Y: FieldElement32([6952922, 1265500, -6862341, 7057498, 4037696, 5447722, -31680899, 15325402, 19365852, -1569102]), + X: FieldElement32([45756067, 28208718, 62448683, 8347856, 42965774, 18986308, 36923107, 12247769, 33528939, 25209113]), + Y: FieldElement32([6952903, 1265500, 60246523, 7057497, 4037696, 5447722, 35427965, 15325401, 19365852, 31985330]), Z: FieldElement32([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - T: FieldElement32([25262188, 11972680, -11716002, 5869612, 18193162, -16297739, -20670665, 8559098, -3541543, 5011181]) + T: FieldElement32([25262188, 11972680, 55392862, 5869611, 18193162, 17256693, 46438198, 8559097, 63567321, 5011180]) }, ]; diff --git a/src/field.rs b/src/field.rs index 840355d..9464fa3 100644 --- a/src/field.rs +++ b/src/field.rs @@ -404,29 +404,6 @@ mod test { assert_eq!(without_highbit_set, with_highbit_set); } - #[cfg(not(feature="radix_51"))] - static B_LIMBS_RADIX_25_5: FieldElement32 = FieldElement32( - [-5652623, 8034020, 8266223, -13556020, -5672552, - -5582839, -12603138, 15161929, -16418207, 13296296]); - - #[cfg(not(feature="radix_51"))] - #[test] - fn from_bytes_vs_radix_25_5_limb_constants() { - let test_elt = FieldElement::from_bytes(&B_BYTES); - assert_eq!(test_elt.0, B_LIMBS_RADIX_25_5.0); - } - - #[cfg(not(feature="radix_51"))] - #[test] - fn radix_25_5_limb_constants_to_bytes_vs_byte_constants() { - let test_bytes = B_LIMBS_RADIX_25_5.to_bytes(); - for i in 0..31 { - assert!(test_bytes[i] == B_BYTES[i]); - } - // Check that high bit is set to zero in to_bytes - assert!(test_bytes[31] == (B_BYTES[31] & 127u8)); - } - #[test] fn conditional_negate() { let one = FieldElement::one();