Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secp256k1 in parity crypto and alternative wasm32 implementation #80

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
6cf6ec6
Switch rust-crypto crate to RustCrypto.
cheme Aug 6, 2018
07b3afc
Merge branch 'master' of https://github.com/paritytech/parity-common …
cheme Aug 6, 2018
e6a4d8a
Merge branch 'master' into RustCrypto
cheme Oct 11, 2018
0cc2661
Build to wasm32 (hmac and pbkdf2 will probably need to be implemented
cheme Oct 11, 2018
c20f544
temp use of previous ring version
cheme Oct 11, 2018
c19caa5
Alternate hmac impl
cheme Oct 15, 2018
2da14e4
hmac verify in test and is_equal method
cheme Oct 16, 2018
41d4c78
In progress of moving secp256k1 in crypto
cheme Oct 16, 2018
0083acf
Add basic (type and proto are currently bad and should probably move to
cheme Oct 18, 2018
a502a11
Some last munute change for wasm secp, usage of a trait is definitely
cheme Oct 18, 2018
ccb7f0a
Added some compat wasm crate.
cheme Oct 25, 2018
f606daa
Single threadpool compat.
cheme Oct 26, 2018
5b2aef6
Add single direction single thread mpsc (can be use under certain
cheme Oct 29, 2018
c57fb09
ring to 0.13
cheme Oct 29, 2018
0ec49ac
Merge branch 'master' into wasm-test
cheme Oct 29, 2018
85d2f80
home_dir to '/home' for browser.
cheme Oct 29, 2018
58c9e72
dummy memmap
cheme Oct 29, 2018
41481dd
Allow heapsize on wasm32.
cheme Oct 30, 2018
4b72742
Pbkdf2 RustCrypto alternative.
cheme Oct 30, 2018
1c5b4b1
tabs
cheme Oct 30, 2018
3ff4e30
Missing file for snappy compat.
cheme Oct 30, 2018
37a38dc
Some wasmp compat.
cheme Nov 1, 2018
85707f9
Keep only crypto changes.
cheme Nov 7, 2018
4aed419
Merge branch 'crypto-tmp' into crypto-compat
cheme Nov 7, 2018
6ab9fb9
moving mem from parity-ethereum to parity-common and generalizing
cheme Nov 8, 2018
a976b47
Check point commit.
cheme Nov 9, 2018
e41feab
Switching to more aligned to parity-eth code base refacto (make key `…
cheme Nov 10, 2018
a24dbd0
Switch arithmetic trait to simple mut ref (better for using with type
cheme Nov 13, 2018
9445702
Test async trait and bench it too.
cheme Nov 14, 2018
6b5e6fa
No as_ref buffer usage.
cheme Nov 14, 2018
30d9198
Do not allow invalid public keys. (remove default trait and test
cheme Nov 15, 2018
c91370b
Avoid panicking on incorrect message/key length for secp256k1_alt
cheme Nov 20, 2018
9968c73
No need for deprecated trait function.
cheme Nov 20, 2018
0117a5f
Switch libsecp version.
cheme Nov 20, 2018
ac0eb93
Remove last maths from secp256k1_alt
cheme Nov 20, 2018
aa0bdca
Use rust-secp256k1 fork aligned to current master.
cheme Nov 23, 2018
ed370cc
Fix import, remove inlines.
cheme Nov 23, 2018
6a6a111
Switch to RustCrypto.
cheme Nov 30, 2018
7a3d75d
Test values directly define in code.
cheme Nov 30, 2018
abb0134
Merge branch 'RustCrypto2' into crypto-compat
cheme Nov 30, 2018
862533f
Missing from merge
cheme Nov 30, 2018
351b964
Move tests in their own file and remove macros
cheme Dec 3, 2018
82b04ac
Merge branch 'RustCrypto2' into crypto-compat
cheme Dec 3, 2018
3c36377
remove part of macro (TODO remove all and move alt to module)
cheme Dec 3, 2018
bb81eb3
Merge branch 'master' into crypto-compat
cheme May 17, 2019
ec212b0
rename of package is now stable
cheme May 17, 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ members = [
"trace-time",
"trie-standardmap",
"triehash",
"uint"
"uint",
]
14 changes: 14 additions & 0 deletions mem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "parity-util-mem"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need description, license and repository info here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The crate was copied from https://github.com/paritytech/parity-ethereum/tree/master/util/mem so staying with gpl 3 license.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Just make sure you add the required fields to the [package] section here (or else it can't be published).

repository = "https://github.com/paritytech/parity-common"
description = "Collection of memory related utilities"
license = "GPL-3.0"

[dependencies]
clear_on_drop = "0.2"

[features]
# when activated mem is removed through volatile primitive instead of clear_on_drop crate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably go into the README/module docs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about 'README/module', just added a README file to mem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, I just meant "make sure this info is in either the README or in the module-level docs (or both)".

volatile-erase = []
7 changes: 7 additions & 0 deletions mem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# parity-util-mem

Collection of memory related utilities.

## Features

- volatile-erase : Not set by default, `Memzero` erase memory with `write_volatile`.
76 changes: 76 additions & 0 deletions mem/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

cheme marked this conversation as resolved.
Show resolved Hide resolved
//! Memory related utilities.

extern crate clear_on_drop as cod;

use std::ops::{Deref, DerefMut};

#[cfg(feature = "volatile-erase")]
use std::ptr;

#[cfg(not(feature = "volatile-erase"))]
pub use cod::clear::Clear;

/// reexport clear_on_drop crate
pub mod clear_on_drop {
pub use cod::*;
}

/// Wrapper to zero out memory when dropped.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Memzero<T: AsMut<[u8]>> {
mem: T,
}

impl<T: AsMut<[u8]>> From<T> for Memzero<T> {
fn from(mem: T) -> Memzero<T> {
Memzero { mem }
}
}

#[cfg(feature = "volatile-erase")]
impl<T: AsMut<[u8]>> Drop for Memzero<T> {
fn drop(&mut self) {
unsafe {
for byte_ref in self.mem.as_mut() {
ptr::write_volatile(byte_ref, 0)
}
}
}
}

#[cfg(not(feature = "volatile-erase"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When should volatile-erase not be used and why offering clear_on_drop and write_volatile?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did use 'clear_on_drop' in my implementation (it is a bit more flexible api-wise) in other contexts and feel like using a single erasure mechanism was simpler.
The crate in itself got its own advantages (cesarb/clear_on_drop#2), yet for me it was just a matter of giving the choice (under current configuration it is imported with 'volatile' feature activated in parity-crypto).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case "less is more". I think we should have a single way of doing memory zeroing.

impl<T: AsMut<[u8]>> Drop for Memzero<T> {
fn drop(&mut self) {
self.as_mut().clear();
}
}

impl<T: AsMut<[u8]>> Deref for Memzero<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.mem
}
}

impl<T: AsMut<[u8]>> DerefMut for Memzero<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.mem
}
}
46 changes: 44 additions & 2 deletions parity-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,51 @@ authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "Crypto utils used by ethstore and network."
license = "GPL-3.0"
autobenches = false

[[bench]]
name = "bench"
harness = false


[dependencies]
quick-error = "1.2.2"
ring = "0.13"
rust-crypto = "0.2.36"
tiny-keccak = "1.4"
scrypt = { version = "0.1.1", default-features = false }
ripemd160 = "0.8.0"
sha2 = "0.8.0"
digest = "0.8"
aes = "0.3.2"
aes-ctr = "0.3.0"
block-modes = "0.2.0"
lazy_static = "1.0" # for secp
parity-util-mem = { version = "0.1", path = "../mem" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = "0.13"
#secp256k1 = "0.11"
secp256k1 = { git = "https://github.com/cheme/rust-secp256k1", branch = "parity-eth" }
hmac = { version = "0.7", optional = true }
libsecp256k1 = { path = "./libsecp256k1", optional = true }
pbkdf2 = { version = "0.3", default-features = false, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
hmac = "0.7"
subtle = { version = "1.0" }
# libsecp256k1 = { version = "0.1", package = "libsecp256k1" } // requires 'rename-dependency' in stable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remind me what this is about?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It allows changing lib name but before import, thus allowing to import two crate with the same declared name (in the commented line I rename sorpaas crate from 'secp256k1' to 'libsecp256k1' but it is not really explicit).

It is a really nice feature, to manage crates with same name, and with rust 2018 I think it could be use to avoid using 'extern crate xxxx as yyy;' (no need for 'extern crate' in 2018).

libsecp256k1 = { path = "./libsecp256k1" }
pbkdf2 = { version = "0.3", default-features = false }

[dev-dependencies]
hmac = "0.7"
# libsecp256k1 = { version = "0.1", package = "libsecp256k1" } // requires 'rename-dependency' in stable
libsecp256k1 = { path = "./libsecp256k1" }
pbkdf2 = { version = "0.3", default-features = false }
criterion = "0.2"
rand = "0.4"

[features]
# expose alternative wasm32 implementation for testing and benchmarks
alt = ["hmac", "libsecp256k1", "pbkdf2"]
nightly = ["subtle/nightly"]
volatile-erase = ["parity-util-mem/volatile-erase"]
63 changes: 63 additions & 0 deletions parity-crypto/benches/asym.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few lines of introduction here would be nice.


//! Benches related to asym crypto, mainly signing and veryfing.

use criterion::{Criterion, Bencher};

use parity_crypto::traits::asym::*;

pub fn secp256k1(c: &mut Criterion) {
use parity_crypto::secp256k1::Secp256k1;
asym_bench::<Secp256k1>(c, "secp256k1".to_owned())
}

#[cfg(feature="alt")]
pub fn secp256k1_alt(c: &mut Criterion) {
use parity_crypto::secp256k1_alt::Secp256k1;
asym_bench::<Secp256k1>(c, "secp256k1_alt".to_owned())
}



fn asym_bench<A: Asym>(c: &mut Criterion, name: String) {

c.bench_function(&(name.clone() + "_sign_verify"),
|b: &mut Bencher| {
let mut sec_buf = vec![7; A::SECRET_SIZE];
let message = vec![0;32];
b.iter(|| {
let (secret, public) = A::keypair_from_slice(&mut sec_buf).unwrap();
let signature = secret.sign(&message).unwrap();
assert!(public.verify(&signature, &message).unwrap());
});
}
);

c.bench_function(&(name.clone() + "_sign_recover"),
|b: &mut Bencher| {
let mut sec_buf = vec![3; A::SECRET_SIZE];
let message = vec![0;32];
b.iter(|| {
let (secret, public) = A::keypair_from_slice(&mut sec_buf).unwrap();
let signature = secret.sign(&message).unwrap();
assert!(public == A::recover(&signature, &message).unwrap());
});
}
);

}
62 changes: 62 additions & 0 deletions parity-crypto/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.


extern crate parity_crypto;

#[macro_use]
extern crate criterion;

mod asym;

use criterion::{Criterion, Bencher};

#[cfg(not(feature="alt"))]
criterion_group!(benches, input_len, asym::secp256k1);

#[cfg(feature="alt")]
criterion_group!(benches, input_len, asym::secp256k1, asym::secp256k1_alt);

criterion_main!(benches);

/// general benches for multiple input size
fn input_len(c: &mut Criterion) {

c.bench_function_over_inputs("ripemd",
|b: &mut Bencher, size: &usize| {
let data = vec![0u8; *size];
b.iter(|| parity_crypto::digest::ripemd160(&data[..]));
},
vec![100, 500, 1_000, 10_000, 100_000]
);

c.bench_function_over_inputs("aes_ctr",
|b: &mut Bencher, size: &usize| {
let data = vec![0u8; *size];
let mut dest = vec![0; *size];
let k = [0; 16];
let iv = [0; 16];

b.iter(||{
parity_crypto::aes::encrypt_128_ctr(&k[..], &iv[..], &data[..], &mut dest[..]).unwrap();
// same as encrypt but add it just in case
parity_crypto::aes::decrypt_128_ctr(&k[..], &iv[..], &data[..], &mut dest[..]).unwrap();
});
},
vec![100, 500, 1_000, 10_000, 100_000]
);

}
10 changes: 10 additions & 0 deletions parity-crypto/libsecp256k1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "libsecp256k1"
version = "0.0.1"
authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "Renaming crate for libsecp256k1."
license = "GPL-3.0"

[dependencies]
libsecp256k1 = { version = "0.2" }
23 changes: 23 additions & 0 deletions parity-crypto/libsecp256k1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Renaming crate for libsecp256k1, this crate should be drop as soon as
//! 'renaming-crates' functionality makes it to stable.


extern crate secp256k1;

pub use secp256k1::*;
Loading