Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Implement ProjectivePoint and improved docs. #56

Merged
merged 71 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
c86ecb7
Define `ProjectivePoint` struct and implement `Debug` for it
CPerezz Jul 4, 2019
08a23d9
Implement Default and Identity traits for `ProjectivePoint`
CPerezz Jul 4, 2019
a683322
Implement Into trait for ProjectivePoint
CPerezz Jul 4, 2019
f784267
Implement `From` trait for `ProjectivePoint`
CPerezz Jul 4, 2019
cec2468
Implement Point Doubling for `ProjectivePoint`
CPerezz Jul 4, 2019
d56cb5a
Doubling behaviour needs to be reviewd
CPerezz Jul 4, 2019
bdb0117
Add tests for conversions between Projective and Extended
CPerezz Jul 4, 2019
dc3f4ff
Merge branch 'master' into provective_edws
CPerezz Jul 5, 2019
0db4294
Implement `Neg` for `ProjectivePoint`
CPerezz Jul 5, 2019
33666d0
Implement `Add` for `ProjectivePoint`
CPerezz Jul 5, 2019
f9fcc41
Add Tests comment separator.
CPerezz Jul 5, 2019
809d7d1
Implement tests for `ProjectivePoint` negation
CPerezz Jul 5, 2019
3a4aabf
Define testing Point values in Projective Coordinates
CPerezz Jul 5, 2019
529fadd
[WIP] Implement tests for Projective Point addition
CPerezz Jul 5, 2019
ea7dc37
Implement `Add` for `FieldElement` by value
CPerezz Jul 5, 2019
ba752d3
Implement Traits for `FieldElement` without references
CPerezz Jul 5, 2019
121b903
Improve docs for `Mul` implementation
CPerezz Jul 5, 2019
adddfeb
Implement `Add` for `Scalar` for non-reference values
CPerezz Jul 5, 2019
1a131a1
Implement `Mul` for `Scalar`
CPerezz Jul 5, 2019
cd2eb27
Implement `Neg` for `Scalar` and `&Scalar`
CPerezz Jul 5, 2019
3aab967
Implement `Identity` for `Scalar`
CPerezz Jul 5, 2019
286dfa2
Implement `Square` for `&Scalar`
CPerezz Jul 5, 2019
bf8f861
Refactor `Mul` tests for `Scalar` implementation
CPerezz Jul 5, 2019
aaf23ae
Implement tests for squaring impl for `Scalar`
CPerezz Jul 5, 2019
fbf52c4
Improve `Identity` trait def and create `Square` trait.
CPerezz Jul 5, 2019
7c16654
Implement `square_internal()` for `FieldElement`
CPerezz Jul 5, 2019
f718b6e
Implement tests for `Square` impl for `&FieldElement`
CPerezz Jul 6, 2019
454471c
Fix doc-comments & Change internal fn visibility for Scalar
CPerezz Jul 6, 2019
789690a
Fix doc-comments & Change internal fn visibility for Scalar & FieldEl…
CPerezz Jul 6, 2019
3996fe6
Modify Benchmarks according to the new visibility.
CPerezz Jul 6, 2019
7fde279
Add `#[cfg(test)]` for `edwards.rs` tests module
CPerezz Jul 6, 2019
646dc10
Benchmark all `FieldElement` and `Scalar` basic ops.
CPerezz Jul 6, 2019
eb66db5
Implement Benchmarks for Twisted Edwards Extended Coordinates Point ops
CPerezz Jul 6, 2019
9aceeab
Implement Point Addition for `ProjectivePoint`.
CPerezz Jul 6, 2019
f2b3ac6
Add debug_assert macro on `half()` function for FieldElement
CPerezz Jul 6, 2019
a842a00
Implement `Sub` for `ProjectivePoint`
CPerezz Jul 6, 2019
3e6dfff
Declare DoubleAndAdd trait on edwards.rs
CPerezz Jul 6, 2019
b0d9bd6
Create `Double` op trait and impl for `EdwardsPoint`
CPerezz Jul 6, 2019
d258b66
Impl `double_and_add` for T
CPerezz Jul 7, 2019
282ea8a
Move traits and impl Double on traits.rs
CPerezz Jul 7, 2019
fe6e5b4
Define `Half` trait on `traits.rs`.
CPerezz Jul 7, 2019
f112b0b
Implement `Half` trait for `FieldElement`
CPerezz Jul 7, 2019
8144f5a
Refactor Half trait for self, not &self.
CPerezz Jul 7, 2019
1ad497a
Implement `Half` trait for `Scalar`.
CPerezz Jul 7, 2019
6fbbbb8
Impl `Div` trait for `&FieldElement`
CPerezz Jul 7, 2019
e7104d7
Implement `Div` for non reference FieldElement
CPerezz Jul 7, 2019
991b605
Create Skeleton for ProjevtivePoint benches.
CPerezz Jul 7, 2019
51b6eec
Merge branch 'master' into provective_edws
CPerezz Jul 8, 2019
d7caad3
Implement test for `Div` impl for `FieldElement`
CPerezz Jul 8, 2019
d700910
Add assert! check in `two_pow_k()`
CPerezz Jul 8, 2019
17b3cc4
Implement FieldElement security checks not only for debug builds.
CPerezz Jul 8, 2019
d10c467
Impl assert! instead of debug_assert! for Scalars
CPerezz Jul 8, 2019
999d46e
Fix doc err on empty line
CPerezz Jul 8, 2019
8450e4f
Merge branch 'master' into provective_edws
CPerezz Jul 8, 2019
38bf29a
Correct doc issues and impl of internal fn
CPerezz Jul 9, 2019
a20ed03
Remove Double impl for EdwardsPoint
CPerezz Jul 9, 2019
9ba89d0
Implement `two_pow_k()` for `&Scalar`
CPerezz Jul 9, 2019
a08f203
Impl `mul_by_pow_2()` for `&EdwardsPoint`
CPerezz Jul 9, 2019
dafd791
Impl `mul_by_cofactor()` for `&EdwardsPoint`
CPerezz Jul 9, 2019
afd5db0
Inline `two_pok_k()` function
CPerezz Jul 9, 2019
e407f46
Define generic Edwards functions on `edwards.rs`
CPerezz Jul 10, 2019
812f397
WIP repo.
CPerezz Jul 10, 2019
0e8f659
Implement Double trait for `&ProjectivePoint`.
CPerezz Jul 10, 2019
d698aaa
Implement `Mul` trait for `&ProjectiveEdwards`.
CPerezz Jul 10, 2019
463d85d
Merge branch 'docs' into provective_edws
CPerezz Jul 10, 2019
ca65356
Remove Upercase warnings from examples
CPerezz Jul 10, 2019
526b858
Add algorithm references
CPerezz Jul 10, 2019
c93ec53
Refactor field.rs example docs.
CPerezz Jul 10, 2019
45effe9
Impl assert! statement on `inverse`
CPerezz Jul 10, 2019
97aa478
Impl Benchmarks for `ProjectivePoint` vs `EdwardsPoint`
CPerezz Jul 10, 2019
6e3cb6b
Add latest compiled docs.
CPerezz Jul 10, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
width="100%"
src="https://camo.githubusercontent.com/db129d98b9686d0db27a9fd27c8e54086b14a6a7/68747470733a2f2f692e696d6775722e636f6d2f496a61645a50592e6a7067">

# WARNING: WIP Repo.

## Fast, efficient and bulletproof-friendly cryptographic operations.

Expand Down
200 changes: 170 additions & 30 deletions benchmarks/dusk_benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,198 @@ use criterion::{Criterion, Benchmark};

use zerocaf::backend::u64::{scalar, field};
use zerocaf::edwards::EdwardsPoint;

use zerocaf::traits::ops::*;

#[allow(unused_imports)]
use zerocaf::traits::Identity;



mod scalar_benches {
use super::*;
// B = 904625697166532776746648320197686575422163851717637391703244652875051672039
pub static B: scalar::Scalar = scalar::Scalar([2766226127823335, 4237835465749098, 4503599626623787, 4503599627370493, 2199023255551]);

// BA = B - A = 904625697166532776746648320014998870755800986942176787613709275418060104167
pub static BA: scalar::Scalar = scalar::Scalar([2766226127823335, 4237835465749098, 4503599626623787, 4503599627370491, 2199023255551]);

// Test if some implementation performs much better than the other even `inline`
// forces to replace the code too. So both implementations should perform similarly.
pub fn bench_mul_internal(c: &mut Criterion) {
c.bench(
"mul_internal",
Benchmark::new("Function", |b| b.iter(|| scalar::Scalar::mul_internal(&B, &BA)))
.with_function("Macro", |b| b.iter(|| scalar::Scalar::mul_internal_macros(&B, &BA))),
);
}
}

mod field_benches {

use super::*;
use zerocaf::field::FieldElement;
use zerocaf::scalar::Scalar;

/// `B = 904625697166532776746648320197686575422163851717637391703244652875051672039`
pub static B: field::FieldElement = field::FieldElement([2766226127823335, 4237835465749098, 4503599626623787, 4503599627370493, 2199023255551]);

/// `A = 182687704666362864775460604089535377456991567872`
pub static A: FieldElement = FieldElement([0, 0, 0, 2, 0]);


pub fn bench_field_element_ops(c: &mut Criterion) {
c.bench(
"Field Element",
Benchmark::new("Addition", |b| b.iter(|| &B + &A))
);

c.bench(
"Field Element",
Benchmark::new("Subtraction", |b| b.iter(|| &B - &A))
);

c.bench(
"Field Element",
Benchmark::new("Mul", |b| b.iter(|| &B * &A))
);

c.bench(
"Field Element",
Benchmark::new("Squaring", |b| b.iter(|| B.square()))
);

c.bench(
"Field Element",
Benchmark::new("Half", |b| b.iter(|| A.half()))
);

c.bench(
"Field Element",
Benchmark::new("Two Pow k (2^k)", |b| b.iter(|| FieldElement::two_pow_k(&213u64)))
);
}

pub fn bench_modular_inverse(c: &mut Criterion) {
c.bench(
"Modular Inverse",
Benchmark::new("Kalinski inverse", |b| b.iter(|| field::FieldElement::kalinski_inverse(&B))).
with_function("Savas & Koç inverse", |b| b.iter(|| field::FieldElement::savas_koc_inverse(&B))),
);
c.bench(
"Modular Inverse",
Benchmark::new("Savas & Koç Modular Inverse algorithm", |b| b.iter(|| field::FieldElement::inverse(&B)))
);
}
}

mod scalar_benches {
use super::*;
use zerocaf::field::FieldElement;
use zerocaf::scalar::Scalar;


/// `C = 182687704666362864775460604089535377456991567872`.
pub static C: Scalar = Scalar([0, 0, 0, 2, 0]);

/// `D = 904625697166532776746648320197686575422163851717637391703244652875051672039`
pub static D: Scalar = Scalar([2766226127823335, 4237835465749098, 4503599626623787, 4503599627370493, 2199023255551]);

pub fn bench_scalar_element_ops(c: &mut Criterion) {
c.bench(
"Scalar",
Benchmark::new("Addition", |b| b.iter(|| &C + &D))
);

c.bench(
"Scalar",
Benchmark::new("Subtraction", |b| b.iter(|| &C - &D))
);

c.bench(
"Scalar",
Benchmark::new("Mul", |b| b.iter(|| &C * &D))
);

c.bench(
"Scalar",
Benchmark::new("Squaring", |b| b.iter(|| C.square()))
);

c.bench(
"Scalar",
Benchmark::new("Half", |b| b.iter(|| C.half()))
);
}
}

mod edwards_benches {

use super::*;
use zerocaf::edwards::{EdwardsPoint, ProjectivePoint};
use zerocaf::scalar::Scalar;
use zerocaf::field::FieldElement;

pub static P1_EXTENDED: EdwardsPoint = EdwardsPoint {
X: FieldElement([23, 0, 0, 0, 0]),
Y: FieldElement([1664892896009688, 132583819244870, 812547420185263, 637811013879057, 13284180325998]),
Z: FieldElement([1, 0, 0, 0, 0]),
T: FieldElement([4351986304670635, 4020128726404030, 674192131526433, 1158854437106827, 6468984742885])
};

pub static P2_EXTENDED: EdwardsPoint = EdwardsPoint {
X: FieldElement([68, 0, 0, 0, 0]),
Y: FieldElement([1799957170131195, 4493955741554471, 4409493758224495, 3389415867291423, 16342693473584]),
Z: FieldElement([1, 0, 0, 0, 0]),
T: FieldElement([3505259403500377, 292342788271022, 2608000066641474, 796697979921534, 2995435405555])
};

pub static P1_PROJECTIVE: ProjectivePoint = ProjectivePoint {
X: FieldElement([23, 0, 0, 0, 0]),
Y: FieldElement([1664892896009688, 132583819244870, 812547420185263, 637811013879057, 13284180325998]),
Z: FieldElement([1, 0, 0, 0, 0])
};

pub static P2_PROJECTIVE: ProjectivePoint = ProjectivePoint {
X: FieldElement([68, 0, 0, 0, 0]),
Y: FieldElement([1799957170131195, 4493955741554471, 4409493758224495, 3389415867291423, 16342693473584]),
Z: FieldElement([1, 0, 0, 0, 0])
};



/// `A = 182687704666362864775460604089535377456991567872`.
pub static A: Scalar = Scalar([0, 0, 0, 2, 0]);

pub fn bench_extended_point_ops(c: &mut Criterion) {
c.bench(
"Extended Coordinates Point Addition",
Benchmark::new("2008 Hisil–Wong–Carter–Dawson, Section 3.1.", |b| b.iter(|| &P1_EXTENDED + &P2_EXTENDED))
);

c.bench(
"Extended Coordinates Point Subtraction",
Benchmark::new("2008 Hisil–Wong–Carter–Dawson, Section 3.1.", |b| b.iter(|| &P1_EXTENDED - &P2_EXTENDED))
);

c.bench(
"Extended Coordinates Point Doubling",
Benchmark::new("2008 Hisil–Wong–Carter–Dawson, Section 3.1.", |b| b.iter(|| P1_EXTENDED.double()))
);

c.bench(
"Extended Coordinates Scalar Mul",
Benchmark::new("Hankerson, Darrel; Vanstone, Scott; Menezes, Alfred (2004) - Guide to Elliptic Curve Cryptography. ",
|b| b.iter(|| &P1_EXTENDED * &A))
);
}

pub fn bench_point_addition(c: &mut Criterion) {
c.bench(
"Extended Coordinates Point Addition",
Benchmark::new("2008 Hisil–Wong–Carter–Dawson, Section 3.1.", |b| b.iter(|| &EdwardsPoint::identity() + &EdwardsPoint::identity()))
);
pub fn bench_projective_point_ops(c: &mut Criterion) {

c.bench(
"Projective Coordinates Point Addition",
Benchmark::new("D. J. Bernstein, P. Birkner, M. Joye, T. Lange, C. Peters. AFRICACRYPT 2008 - Section 6.", |b| b.iter(|| &P1_PROJECTIVE + &P2_PROJECTIVE))
);

c.bench(
"Projective Coordinates Point Subtraction",
Benchmark::new("D. J. Bernstein, P. Birkner, M. Joye, T. Lange, C. Peters. AFRICACRYPT 2008 - Section 6.", |b| b.iter(|| &P1_PROJECTIVE - &P2_PROJECTIVE))
);

c.bench(
"Projective Coordinates Point Doubling",
Benchmark::new("D. J. Bernstein, P. Birkner, M. Joye, T. Lange, C. Peters. AFRICACRYPT 2008 - Section 6.", |b| b.iter(|| P1_PROJECTIVE.double()))
);

c.bench(
"Projective Coordinates Scalar Mul",
Benchmark::new("Hankerson, Darrel; Vanstone, Scott; Menezes, Alfred (2004) - Guide to Elliptic Curve Cryptography. ",
|b| b.iter(|| &P1_PROJECTIVE * &A))
);
}
}

criterion_group!(benchmarks, scalar_benches::bench_mul_internal, field_benches::bench_modular_inverse, edwards_benches::bench_point_addition);
criterion_group!(benchmarks,
field_benches::bench_field_element_ops,
field_benches::bench_modular_inverse,
scalar_benches::bench_scalar_element_ops,
edwards_benches::bench_extended_point_ops,
edwards_benches::bench_projective_point_ops);
//criterion_group!(benchmarks, field_benches::bench_modular_inverse);
criterion_main!(benchmarks);
1 change: 1 addition & 0 deletions examples/basic_ops.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(non_snake_case)]
/// The purpose of this implementation is to provide support for one of the most
/// commonly used operations over EC which is Random Scalar Mul.
///
Expand Down
Loading