Skip to content

Commit

Permalink
Merge branch 'master' into remco/feature/lehmer
Browse files Browse the repository at this point in the history
  • Loading branch information
Remco Bloemen committed Jun 17, 2019
2 parents 0fed26d + 2b1ef50 commit f276a42
Show file tree
Hide file tree
Showing 15 changed files with 679 additions and 57 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ Cargo.lock

wasm/bin
wasm/pkg
wasm-pack.log
wasm-pack.log

# Vim swap files
*.swp
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ harness = false
[profile.release]
opt-level = 3
lto = true
debug = true # Debug symbols are required for profiling. Remove for produciont!
debug = true # Debug symbols are required for profiling. Remove for production!

# TODO: Make sure it doesn't hurt wasm
[build]
Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ Note: The modulus is always assumed to be 256bit or less.
provided.
- Compiler time known fields.
The compiler can compute constants, for example for Montgomery
rerpesentation. The field parameters should be inlined.
representation. The field parameters should be inlined.
- Statically runtime known fields.
Modulus is not known during compilation (but it's size is). Element
membership of a particular field is known at compile time. The field
parameters should statically allocated and the pointers inlined.
- Dynamically runtime known fields.
Modulus is not known during compilation (but it's size is). Element
Modulus is not known during compilation (but its size is). Element
membership of a particular field is not known at compile time. The field
element should carry a pointer to the field parameters.

Expand Down
71 changes: 70 additions & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use hex_literal::*;
use starkcrypto::curve::Affine;
use starkcrypto::ecdsa::{private_to_public, sign, verify};
use starkcrypto::fft::fft_cofactor;
use starkcrypto::field::FieldElement;
use starkcrypto::gcd::gcd;
use starkcrypto::jacobian::Jacobian;
use starkcrypto::merkle::*;
use starkcrypto::pedersen::hash;
use starkcrypto::square_root::square_root;
use starkcrypto::u256::U256;
use starkcrypto::u256h;
use starkcrypto::wnaf;
Expand Down Expand Up @@ -137,6 +140,18 @@ fn field_mul(crit: &mut Criterion) {
});
}

fn field_sqrt(crit: &mut Criterion) {
let a = FieldElement::new(&[
0x0f3855f5, 0x37862eb2, 0x275b919f, 0x325329cb, 0xe968e6a2, 0xa2ceee5c, 0xd5f1d547,
0x07211989,
]);
crit.bench_function("Field square root", move |bench| {
bench.iter(|| {
black_box(square_root(black_box(&a)));
})
});
}

fn field_inv(crit: &mut Criterion) {
let a = FieldElement::new(&[
0x0f3855f5, 0x37862eb2, 0x275b919f, 0x325329cb, 0xe968e6a2, 0xa2ceee5c, 0xd5f1d547,
Expand Down Expand Up @@ -225,7 +240,7 @@ fn jacobian_to_affine(crit: &mut Criterion) {
0x011cf020,
]),
});
crit.bench_function("Jacobian add", move |bench| {
crit.bench_function("Jacobian to Affine", move |bench| {
bench.iter(|| {
black_box(Affine::from(black_box(&a)));
})
Expand Down Expand Up @@ -391,6 +406,56 @@ fn ecdsa_verify(crit: &mut Criterion) {
});
}

fn merkle_proof_make(crit: &mut Criterion) {
let depth = 6;
let mut leaves = Vec::new();

for i in 0..2_u64.pow(depth) {
leaves.push(U256::from((i + 10).pow(3)));
}
crit.bench_function("Making depth 6 Merkle Tree", move |bench| {
bench.iter(|| black_box(make_tree(leaves.clone())))
});
}

fn fft_timing(crit: &mut Criterion) {
let root = FieldElement::from(u256h!(
"063365fe0de874d9c90adb1e2f9c676e98c62155e4412e873ada5e1dee6feebb"
));
let cofactor = FieldElement::from(u256h!(
"07696b8ff70e8e9285c76bef95d3ad76cdb29e213e4b5d9a9cd0afbd7cb29b5c"
));
let vector = vec![
FieldElement::from(u256h!(
"008ee28fdbe9f1a7983bc1b600dfb9177c2d82d825023022ab4965d999bd3faf"
)),
FieldElement::from(u256h!(
"037fa3db272cc54444894042223dcf260e1d1ec73fa9baea0e4572817fdf5751"
)),
FieldElement::from(u256h!(
"054483fc9bcc150b421fae26530f8d3d2e97cf1918f534e67ef593038f683241"
)),
FieldElement::from(u256h!(
"005b695b9001e5e62549557c48a23fd7f1706c1acdae093909d81451cd455b43"
)),
FieldElement::from(u256h!(
"025079cb6cb547b63b67614dd2c78474c8a7b17b3bc53f7f7276984b6b67b18a"
)),
FieldElement::from(u256h!(
"044729b25360c0025d244d31a5f144917e59f728a3d03dd4685c634d2b0e7cda"
)),
FieldElement::from(u256h!(
"079b0e14d0bae81ff4fe55328fb09c4117bcd961cb60581eb6f2a770a42240ed"
)),
FieldElement::from(u256h!(
"06c0926a786abb30b8f6e0eb9ef2278b910862717ed4beb35121d4741717e0e0"
)),
];
crit.bench_function("Performing FFT", move |bench| {
bench.iter(|| black_box(fft_cofactor(root.clone(), &vector, cofactor.clone())))
});
}

fn criterion_benchmark(c: &mut Criterion) {
u256_add(c);
u256_mul(c);
Expand All @@ -401,6 +466,7 @@ fn criterion_benchmark(c: &mut Criterion) {
field_add(c);
field_mul(c);
field_inv(c);
field_sqrt(c);
curve_add(c);
curve_dbl(c);
curve_mul(c);
Expand All @@ -409,10 +475,13 @@ fn criterion_benchmark(c: &mut Criterion) {
jacobian_dbl(c);
jacobian_mul(c);
jacobian_mul_affine(c);
jacobian_to_affine(c);
wnaf_mul_affine(c);
pedersen_hash(c);
ecdsa_sign(c);
ecdsa_verify(c);
merkle_proof_make(c);
fft_timing(c);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
2 changes: 0 additions & 2 deletions src/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,11 @@ mod tests {

#[allow(clippy::eq_op)]
#[quickcheck]
#[test]
fn add_commutative(a: Affine, b: Affine) -> bool {
&a + &b == &b + &a
}

#[quickcheck]
#[test]
fn distributivity(p: Affine, mut a: U256, mut b: U256) -> bool {
a %= ℴ
b %= ℴ
Expand Down
1 change: 0 additions & 1 deletion src/division.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ mod tests {
}

#[quickcheck]
#[test]
fn div_3by2_correct(q: u64, d0: u64, d1: u64) -> bool {
let d1 = d1 | (1 << 63);
let n = U256::new(d0, d1, 0, 0) * &U256::from(q);
Expand Down
1 change: 0 additions & 1 deletion src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ mod tests {
}

#[quickcheck]
#[test]
fn test_ecdsa(mut message_hash: U256, private_key: U256) -> bool {
message_hash >>= 5; // Need message_hash <= 2**251
let public_key = private_to_public(&private_key);
Expand Down
Loading

0 comments on commit f276a42

Please sign in to comment.