Enable radix_51 on nightly

This commit is contained in:
Henry de Valence 2017-03-13 17:04:37 -07:00
parent f80754a954
commit cf9f9aa402
3 changed files with 38 additions and 40 deletions

View file

@ -33,16 +33,14 @@ version = "^0.6"
version = "0.4"
[features]
nightly = ["basepoint_table_creation"]
default = ["std", "radix_25_5"]
nightly = ["basepoint_table_creation", "radix_51"]
default = ["std"]
std = ["rand"]
yolocrypto = []
# Needs nightly for placement new
basepoint_table_creation = []
# Radix-51 arithmetic using u128
radix_51 = []
# Radix-25.5 arithmetic using i64
radix_25_5 = []
# The development profile, used for `cargo build`.
[profile.dev]

View file

@ -29,25 +29,25 @@ use scalar::Scalar;
#[cfg(feature="radix_51")]
pub const p: FieldElement = FieldElement([2251799813685229, 2251799813685247, 2251799813685247, 2251799813685247, 2251799813685247]);
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const d: FieldElement = FieldElement([
-10913610, 13857413, -15372611, 6949391, 114729,
-8787816, -6275908, -3247719, -18696448, -12055116, ]);
#[cfg(feature="radix_51")]
pub const d: FieldElement = FieldElement([929955233495203, 466365720129213, 1662059464998953, 2033849074728123, 1442794654840575]);
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const d2: FieldElement = FieldElement([
-21827239, -5839606, -30745221, 13898782, 229458,
15978800, -12551817, -6495438, 29715968, 9444199, ]);
#[cfg(feature="radix_51")]
pub const d2: FieldElement = FieldElement([1859910466990425, 932731440258426, 1072319116312658, 1815898335770999, 633789495995903]);
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const d4: FieldElement = FieldElement([
23454405, -11679213, 5618422, -5756869, 458917,
-1596832, -25103633, -12990876, -7676928, -14666033 ]);
#[cfg(feature="radix_51")]
pub const d4: FieldElement = FieldElement([1468021120295602, 1865462880516853, 2144638232625316, 1379996857856750, 1267578991991807]);
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const a_minus_d: FieldElement = FieldElement([
10913609, -13857413, 15372611, -6949391, -114729,
8787816, 6275908, 3247719, 18696448, 12055116, ]);
@ -62,7 +62,7 @@ pub const HALF_P_MINUS_1_BYTES: [u8; 32] =
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f];
/// Precomputed value of one of the square roots of -1 (mod p)
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const SQRT_M1: FieldElement = FieldElement([
-32595792, -7943725, 9377950, 3500415, 12389472,
-272473, -25146209, -2005654, 326686, 11406482, ]);
@ -71,7 +71,7 @@ pub const SQRT_M1: FieldElement = FieldElement([1718705420411056, 23490888355650
/// Precomputed value of the other square root of -1 (mod p),
/// i.e., MSQRT_M1 = -SQRT_M1.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const MSQRT_M1: FieldElement = FieldElement([
32595792, 7943725, -9377950, -3500415, -12389472,
272473, 25146209, 2005654, -326686, -11406482, ]);
@ -79,14 +79,14 @@ pub const MSQRT_M1: FieldElement = FieldElement([
pub const MSQRT_M1: FieldElement = FieldElement([533094393274173, 2016890930128738, 18285341111199, 134597186663265, 1486323764102114]);
/// Precomputed value of 1/2 (mod p).
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const HALF: FieldElement = FieldElement([
10, 0, 0, 0, 0, 0, 0, 0, 0, -16777216, ]);
#[cfg(feature="radix_51")]
pub const HALF: FieldElement = FieldElement([2251799813685239, 2251799813685247, 2251799813685247, 2251799813685247, 1125899906842623]);
/// In Montgomery form y² = x³+Ax²+x, Curve25519 has A=486662.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const A: FieldElement = FieldElement([
486662, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]);
#[cfg(feature="radix_51")]
@ -96,7 +96,7 @@ pub const A: FieldElement = FieldElement([486662, 0, 0, 0, 0]);
// XXX I think that this was used in Adam's code for his elligator
// implementation, but that should maybe be using sqrt(-486664)
// instead...? - hdevalence
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const SQRT_MINUS_A: FieldElement = FieldElement([ // sqrtMinusA
12222970, 8312128, 11511410, -9067497, 15300785,
241793, -25456130, -14121551, 12187136, -3972024, ]);
@ -104,7 +104,7 @@ pub const SQRT_MINUS_A: FieldElement = FieldElement([ // sqrtMinusA
pub const SQRT_MINUS_A: FieldElement = FieldElement([557817479725543, 1643290402203250, 16226468853936, 1304118542701054, 1985241807451647]);
/// SQRT_MINUS_APLUS2 is sqrt(-486664)
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const SQRT_MINUS_APLUS2: FieldElement = FieldElement([
-12222970, -8312128, -11511410, 9067497, -15300785,
-241793, 25456130, 14121551, -12187136, 3972024]);
@ -112,7 +112,7 @@ pub const SQRT_MINUS_APLUS2: FieldElement = FieldElement([
pub const SQRT_MINUS_APLUS2: FieldElement = FieldElement([1693982333959686, 608509411481997, 2235573344831311, 947681270984193, 266558006233600]);
/// SQRT_MINUS_HALF is sqrt(-1/2)
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const SQRT_MINUS_HALF: FieldElement = FieldElement([ // sqrtMinusHalf
-17256545, 3971863, 28865457, -1750208, 27359696,
-16640980, 12573105, 1002827, -163343, 11073975, ]);
@ -137,7 +137,7 @@ pub const BASE_CMPRSSD: CompressedEdwardsY =
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66]);
/// Basepoint has y = 4/5.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const ED25519_BASEPOINT: ExtendedPoint = ExtendedPoint{
X: FieldElement([-14297830, -7645148, 16144683, -16471763, 27570974, -2696100, -26142465, 8378389, 20764389, 8758491]),
Y: FieldElement([-26843541, -6710886, 13421773, -13421773, 26843546, 6710886, -13421773, 13421773, -26843546, -6710886]),
@ -172,7 +172,7 @@ pub const lminus1: Scalar = Scalar([ 0xec, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0
///
/// Thus Ɛ[4] is the points indexed by 0,2,4,6 and Ɛ[2] is the points
/// indexed by 0,4.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const EIGHT_TORSION: [ExtendedPoint; 8] = [
ExtendedPoint{
X: FieldElement([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
@ -282,7 +282,7 @@ pub const EIGHT_TORSION: [ExtendedPoint; 8] = [
}
];
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const bi: [AffineNielsPoint; 8] = [
AffineNielsPoint{
y_plus_x: FieldElement([25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605]),
@ -380,7 +380,7 @@ pub const bi: [AffineNielsPoint; 8] = [
///
/// The table is defined so `constants::base[i][j-1] = j*(16^2i)*B`,
/// for `0 ≤ i < 32`, `1 ≤ j < 9`.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub const ED25519_BASEPOINT_TABLE: EdwardsBasepointTable = EdwardsBasepointTable([
[
AffineNielsPoint{
@ -3160,7 +3160,7 @@ mod test {
/// Test that the constant for sqrt(-486664) really is a square
/// root of -486664.
#[test]
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
fn sqrt_minus_aplus2() {
let minus_aplus2 = FieldElement([-486664,0,0,0,0,0,0,0,0,0]);
let sqrt = constants::SQRT_MINUS_APLUS2;
@ -3193,7 +3193,7 @@ mod test {
}
/// Test that d = -121665/121666
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
#[test]
fn test_d_vs_ratio() {
let a = FieldElement([-121665,0,0,0,0,0,0,0,0,0]);

View file

@ -50,14 +50,14 @@ pub struct FieldElement(pub [u64; 5]);
/// 25.5, that is, each Limb of a FieldElement alternates between being
/// represented as a factor of 2^25 or 2^26 more than the last corresponding
/// integer.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub type Limb = i32;
/// FieldElement represents an element of the field GF(2^255 - 19). An element
/// t, entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
/// t[3]+2^102 t[4]+...+2^230 t[9]. Bounds on each t[i] vary depending on
/// context.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
#[derive(Copy, Clone)]
pub struct FieldElement(pub [i32; 10]);
@ -136,7 +136,7 @@ impl<'a, 'b> Add<&'b FieldElement> for &'a FieldElement {
}
impl<'b> SubAssign<&'b FieldElement> for FieldElement {
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
fn sub_assign(&mut self, _rhs: &'b FieldElement) { // fdifference()
for i in 0..10 {
self[i] -= _rhs[i];
@ -153,7 +153,7 @@ impl<'b> SubAssign<&'b FieldElement> for FieldElement {
impl<'a, 'b> Sub<&'b FieldElement> for &'a FieldElement {
type Output = FieldElement;
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
fn sub(self, _rhs: &'b FieldElement) -> FieldElement {
let mut output = self.clone();
output -= _rhs;
@ -235,7 +235,7 @@ impl<'a, 'b> Mul<&'b FieldElement> for &'a FieldElement {
FieldElement::reduce([c0,c1,c2,c3,c4])
}
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
fn mul(self, _rhs: &'b FieldElement) -> FieldElement {
// Notes preserved from ed25519.go (presumably originally from ref10):
//
@ -362,7 +362,7 @@ impl CTAssignable for FieldElement {
/// # Preconditions
///
/// * `choice` in {0,1}
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
fn conditional_assign(&mut self, f: &FieldElement, choice: u8) {
let mask = -(choice as Limb);
for i in 0..10 {
@ -380,7 +380,7 @@ impl CTAssignable for FieldElement {
impl FieldElement {
/// Invert the sign of this field element
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn negate(&mut self) {
for i in 0..10 {
self[i] = -self[i];
@ -401,7 +401,7 @@ impl FieldElement {
}
/// Construct zero.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn zero() -> FieldElement {
FieldElement([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
}
@ -412,7 +412,7 @@ impl FieldElement {
}
/// Construct one.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn one() -> FieldElement {
FieldElement([ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
}
@ -423,7 +423,7 @@ impl FieldElement {
}
/// Construct -1.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn minus_one() -> FieldElement {
FieldElement([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
}
@ -451,7 +451,7 @@ impl FieldElement {
FieldElement(limbs)
}
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
fn reduce(input: &[i64;10]) -> FieldElement { //FeCombine
let mut c = [0i64;10];
let mut h = input.clone();
@ -564,7 +564,7 @@ impl FieldElement {
/// # Return
///
/// Returns a new FieldElement.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn from_bytes(data: &[u8; 32]) -> FieldElement { //FeFromBytes
let mut h = [0i64;10];
h[0] = load4(&data[ 0..]);
@ -617,7 +617,7 @@ impl FieldElement {
/// let bytes: [u8; 32] = fe.to_bytes();
/// assert!(data == bytes);
/// ```
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn to_bytes(&self) -> [u8;32] { //FeToBytes
// Comment preserved from ed25519.go (presumably originally from ref10):
//
@ -921,7 +921,7 @@ impl FieldElement {
return byte_is_nonzero(x);
}
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
fn square_inner(&self) -> [i64;10] {
let f0 = self[0] as i64;
let f1 = self[1] as i64;
@ -1023,7 +1023,7 @@ impl FieldElement {
/// # Postconditions
///
/// * |h[i]| bounded by 1.1*2^25, 1.1*2^24, 1.1*2^25, 1.1*2^24, etc.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn square(&self) -> FieldElement {
FieldElement::reduce(&self.square_inner())
}
@ -1049,7 +1049,7 @@ impl FieldElement {
///
/// See fe_mul.c in ref10 implementation for discussion of implementation
/// strategy.
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
pub fn square2(&self) -> FieldElement {
let mut coeffs = self.square_inner();
for i in 0..self.0.len() {
@ -1381,12 +1381,12 @@ mod test {
assert_eq!(without_highbit_set, with_highbit_set);
}
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
static B_LIMBS_RADIX_25_5: FieldElement = FieldElement(
[-5652623, 8034020, 8266223, -13556020, -5672552,
-5582839, -12603138, 15161929, -16418207, 13296296]);
#[cfg(feature="radix_25_5")]
#[cfg(not(feature="radix_51"))]
#[test]
fn from_bytes_vs_radix_25_5_limb_constants() {
let test_elt = FieldElement::from_bytes(&B_BYTES);
@ -1395,7 +1395,7 @@ mod test {
}
}
#[cfg(feature="radix_25_5")]
#[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();