Add utility for extracting constants in the new repr

This commit is contained in:
Henry de Valence 2017-03-12 23:17:59 -07:00
parent 4db735ca44
commit 87001f9d6e

View file

@ -1109,6 +1109,76 @@ mod test {
use field::*;
use subtle::CTNegatable;
#[test]
fn print_constants() {
use curve::*;
println!("");
fn repr_fe(s: &str, fe: &FieldElement, t: &'static str) {
let f = from_bytes_64(&fe.to_bytes());
println!("{}FieldElement([{}, {}, {}, {}, {}]){}", s, f[0], f[1], f[2], f[3], f[4], t);
}
fn repr_ext(s: &'static str, P: &ExtendedPoint) {
println!("{}ExtendedPoint {{", s);
repr_fe(" X: ", &P.X, ",");
repr_fe(" Y: ", &P.Y, ",");
repr_fe(" Z: ", &P.Z, ",");
repr_fe(" T: ", &P.T, ",");
println!("}};");
}
fn repr_aff(s: &'static str, P: &AffineNielsPoint) {
println!("{}AffineNielsPoint {{", s);
repr_fe(" y_plus_x: ", &P.y_plus_x, ",");
repr_fe(" y_minus_x: ", &P.y_minus_x, ",");
repr_fe(" xy2d: ", &P.xy2d, ",");
println!("}};");
}
fn print(name: &'static str, f: &FieldElement) {
repr_fe(format!("pub const {}: FieldElement = ", name).as_str(), f, ";");
}
print("d", &constants::d);
print("d2", &constants::d2);
print("d4", &constants::d4);
print("a_minus_d", &constants::a_minus_d);
print("SQRT_M1", &constants::SQRT_M1);
print("MSQRT_M1", &constants::MSQRT_M1);
print("HALF", &constants::HALF);
print("A", &constants::A);
print("SQRT_MINUS_A", &constants::SQRT_MINUS_A);
print("SQRT_MINUS_APLUS2", &constants::SQRT_MINUS_APLUS2);
print("SQRT_MINUS_HALF", &constants::SQRT_MINUS_HALF);
repr_ext("pub const ED25519_BASEPOINT: ExtendedPoint = ", &constants::ED25519_BASEPOINT);
println!("pub const EIGHT_TORSION: [ExtendedPoint; 8] =");
for i in 0..8 {
repr_ext("", &constants::EIGHT_TORSION[i]);
println!(",");
}
println!("pub const bi: [AffineNielsPoint; 8] =");
for i in 0..8 {
repr_aff("", &constants::bi[i]);
println!(",");
}
println!("pub const ED25519_BASEPOINT_TABLE: EdwardsBasepointTable = EdwardsBasepointTable([");
let table = EdwardsBasepointTable::create(&constants::ED25519_BASEPOINT);
for i in 0..32 {
println!("\n\n");
for j in 0..8 {
repr_aff("", &table.0[i][j]);
}
}
panic!();
}
/// Random element a of GF(2^255-19), from Sage
/// a = 1070314506888354081329385823235218444233221\
/// 2228051251926706380353716438957572