First attempt at generating basepoint tables at compile time

This commit is contained in:
Henry de Valence 2017-10-31 14:07:27 -07:00 committed by Henry de Valence
parent b966f942a8
commit 108a690941
6 changed files with 115 additions and 2730 deletions

View file

@ -14,6 +14,7 @@ description = "A low-level cryptographic library for point, group, field, and sc
exclude = [
".gitignore"
]
build = "build.rs"
[package.metadata.docs.rs]
rustdoc-args = ["--html-in-header", "rustdoc-include-katex-header.html"]
@ -49,6 +50,17 @@ version = "0.6"
[dev-dependencies.serde_cbor]
version = "0.6"
[build-dependencies]
subtle = "^0.3"
rand = "0.3"
generic-array = "^0.8"
digest = "0.6"
arrayref = "0.3.4"
[build-dependencies.serde]
version = "1.0"
optional = true
[features]
nightly = ["radix_51", "subtle/nightly"]
default = ["std"]
@ -59,3 +71,6 @@ yolocrypto = []
bench = []
# Radix-51 arithmetic using u128
radix_51 = []
# Include precomputed basepoint tables. This is off by default so that build.rs can generate the tables, and then re-enabled by build.rs in the main-stage compilation.
precomputed_tables = []

89
build.rs Normal file
View file

@ -0,0 +1,89 @@
#![cfg_attr(feature = "nightly", feature(i128_type))]
extern crate core;
extern crate subtle;
extern crate rand;
extern crate digest;
extern crate generic_array;
#[macro_use]
extern crate arrayref;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
// Replicate lib.rs in the build.rs, since we're effectively building the whole crate twice.
//
// This should be fixed up by refactoring our code to seperate the "minimal" parts from the rest.
//
// For instance, this shouldn't exist here at all, but it does.
#[cfg(feature = "serde")]
extern crate serde;
#[path="src/field.rs"]
mod field;
#[cfg(not(feature="radix_51"))]
#[path="src/field_32bit.rs"]
mod field_32bit;
#[cfg(feature="radix_51")]
#[path="src/field_64bit.rs"]
mod field_64bit;
#[path="src/scalar.rs"]
mod scalar;
#[cfg(not(feature="radix_51"))]
#[path="src/scalar_32bit.rs"]
mod scalar_32bit;
#[cfg(feature="radix_51")]
#[path="src/scalar_64bit.rs"]
mod scalar_64bit;
#[path="src/montgomery.rs"]
mod montgomery;
#[path="src/edwards.rs"]
mod edwards;
#[path="src/ristretto.rs"]
mod ristretto;
#[path="src/utils.rs"]
mod utils;
#[path="src/constants.rs"]
mod constants;
#[cfg(not(feature="radix_51"))]
#[path="src/constants_32bit.rs"]
mod constants_32bit;
#[cfg(feature="radix_51")]
#[path="src/constants_64bit.rs"]
mod constants_64bit;
use edwards::EdwardsBasepointTable;
fn main() {
// Enable the "precomputed_tables" feature in the main build stage
println!("cargo:rustc-cfg=feature=\"precomputed_tables\"\n");
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("basepoint_table.rs");
let mut f = File::create(&dest_path).unwrap();
// Generate a table of precomputed multiples of the basepoint
let table = EdwardsBasepointTable::create(&constants::ED25519_BASEPOINT_POINT);
f.write_all(format!("\n
#[cfg(feature=\"radix_51\")]
use field_64bit::FieldElement64;
#[cfg(not(feature=\"radix_51\"))]
use field_32bit::FieldElement32;
use edwards::AffineNielsPoint;
use edwards::EdwardsBasepointTable;
/// Table containing precomputed multiples of the basepoint `B = (x,4/5)`.
///
/// The table is defined so `constants::base[i][j-1] = j*(16^2i)*B`,
/// for `0 ≤ i < 32`, `1 ≤ j < 9`.
pub const ED25519_BASEPOINT_TABLE: EdwardsBasepointTable = {:?};
\n\n", &table).as_bytes()).unwrap();
}

View file

@ -86,7 +86,17 @@ pub const BASEPOINT_ORDER_MINUS_2: Scalar = Scalar([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
]);
// Precomputed basepoint table is generated into a file by build.rs
/*
#[cfg(feature="precomputed_tables")]
pub use generated::ED25519_BASEPOINT_TABLE;
*/
#[cfg(feature="precomputed_tables")]
include!(concat!(env!("OUT_DIR"), "/basepoint_table.rs"));
/// The Ed25519 basepoint, as a RistrettoPoint
#[cfg(feature="precomputed_tables")]
pub const RISTRETTO_BASEPOINT_TABLE: RistrettoBasepointTable
= RistrettoBasepointTable(ED25519_BASEPOINT_TABLE);

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -91,3 +91,4 @@ pub mod constants;
mod constants_32bit;
#[cfg(feature="radix_51")]
mod constants_64bit;