Skip to content

Commit

Permalink
Merge branch 'master' into ao-kvdb-sled
Browse files Browse the repository at this point in the history
* master:
  [parity-crypto] Use upstream secp256k1 (#258)
  Bump parking_lot to 0.10 and minor versions (#332)
  • Loading branch information
ordian committed Feb 6, 2020
2 parents 5b94cbb + 2c5273c commit 44870c9
Show file tree
Hide file tree
Showing 23 changed files with 174 additions and 180 deletions.
3 changes: 3 additions & 0 deletions kvdb-memorydb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.4.0] - 2019-02-05
- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332)

## [0.3.1] - 2019-01-06
- Updated features and feature dependencies. [#307](https://github.com/paritytech/parity-common/pull/307)

Expand Down
10 changes: 5 additions & 5 deletions kvdb-memorydb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "kvdb-memorydb"
version = "0.3.1"
version = "0.4.0"
authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "A key-value in-memory database that implements the `KeyValueDB` trait"
license = "GPL-3.0"
edition = "2018"

[dependencies]
parity-util-mem = { path = "../parity-util-mem", version = "0.4", default-features = false, features = ["std"] }
parking_lot = "0.9.0"
kvdb = { version = "0.3", path = "../kvdb" }
parity-util-mem = { path = "../parity-util-mem", version = "0.5", default-features = false, features = ["std"] }
parking_lot = "0.10.0"
kvdb = { version = "0.4", path = "../kvdb" }

[dev-dependencies]
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.1" }
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.2" }
3 changes: 3 additions & 0 deletions kvdb-rocksdb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.5.0] - 2019-02-05
- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332

## [0.4.2] - 2019-02-04
### Fixes
- Fixed `iter_from_prefix` being slow. [#326](https://github.com/paritytech/parity-common/pull/326)
Expand Down
10 changes: 5 additions & 5 deletions kvdb-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kvdb-rocksdb"
version = "0.4.2"
version = "0.5.0"
authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "kvdb implementation backed by RocksDB"
Expand All @@ -15,19 +15,19 @@ harness = false
smallvec = "1.0.0"
fs-swap = "0.2.4"
interleaved-ordered = "0.1.1"
kvdb = { path = "../kvdb", version = "0.3" }
kvdb = { path = "../kvdb", version = "0.4" }
log = "0.4.8"
num_cpus = "1.10.1"
parking_lot = "0.9.0"
parking_lot = "0.10.0"
regex = "1.3.1"
rocksdb = { version = "0.13", features = ["snappy"], default-features = false }
owning_ref = "0.4.0"
parity-util-mem = { path = "../parity-util-mem", version = "0.4", default-features = false, features = ["std", "smallvec"] }
parity-util-mem = { path = "../parity-util-mem", version = "0.5", default-features = false, features = ["std", "smallvec"] }

[dev-dependencies]
alloc_counter = "0.0.4"
criterion = "0.3"
ethereum-types = { path = "../ethereum-types" }
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.1" }
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.2" }
rand = "0.7.2"
tempdir = "0.3.7"
4 changes: 2 additions & 2 deletions kvdb-shared-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "kvdb-shared-tests"
version = "0.1.0"
version = "0.2.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
description = "Shared tests for kvdb functionality, to be executed against actual implementations"
license = "GPL-3.0"

[dependencies]
kvdb = { path = "../kvdb", version = "0.3" }
kvdb = { path = "../kvdb", version = "0.4" }
3 changes: 3 additions & 0 deletions kvdb-web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.4.0] - 2019-02-05
- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332)

## [0.3.1] - 2019-01-06
- Updated features and feature dependencies. [#307](https://github.com/paritytech/parity-common/pull/307)

Expand Down
10 changes: 5 additions & 5 deletions kvdb-web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kvdb-web"
version = "0.3.1"
version = "0.4.0"
authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "A key-value database for use in browsers"
Expand All @@ -11,12 +11,12 @@ edition = "2018"
[dependencies]
wasm-bindgen = "0.2.54"
js-sys = "0.3.31"
kvdb = { version = "0.3", path = "../kvdb" }
kvdb-memorydb = { version = "0.3", path = "../kvdb-memorydb" }
kvdb = { version = "0.4", path = "../kvdb" }
kvdb-memorydb = { version = "0.4", path = "../kvdb-memorydb" }
futures = "0.3"
log = "0.4.8"
send_wrapper = "0.3.0"
parity-util-mem = { path = "../parity-util-mem", version = "0.4", default-features = false }
parity-util-mem = { path = "../parity-util-mem", version = "0.5", default-features = false }

[dependencies.web-sys]
version = "0.3.31"
Expand All @@ -39,6 +39,6 @@ features = [

[dev-dependencies]
console_log = "0.1.2"
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.1" }
kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.2" }
wasm-bindgen-test = "0.3.4"
wasm-bindgen-futures = "0.4.4"
3 changes: 3 additions & 0 deletions kvdb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.4.0] - 2019-01-06
- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332)

## [0.3.1] - 2019-01-06
- Updated features and feature dependencies. [#307](https://github.com/paritytech/parity-common/pull/307)

Expand Down
4 changes: 2 additions & 2 deletions kvdb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kvdb"
version = "0.3.1"
version = "0.4.0"
authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "Generic key-value trait"
Expand All @@ -10,4 +10,4 @@ edition = "2018"
[dependencies]
smallvec = "1.0.0"
bytes = { package = "parity-bytes", version = "0.1", path = "../parity-bytes" }
parity-util-mem = { path = "../parity-util-mem", version = "0.4", default-features = false }
parity-util-mem = { path = "../parity-util-mem", version = "0.5", default-features = false }
6 changes: 6 additions & 0 deletions parity-crypto/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ The format is based on [Keep a Changelog].
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
- Remove `inv()` from `SecretKey` (breaking)
- `Generate::generate()` does not return error
- `Secp256k1` is no longer exported
- Remove `public_is_valid()` as it is now impossible to create invalid public keys
- 0-valued `Secp::Message`s are disallowed (signatures on them are forgeable for all keys)
- updates to upstream `rust-secp256k1` at v0.17.2
8 changes: 4 additions & 4 deletions parity-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ required-features = ["publickey"]
[dependencies]
tiny-keccak = { version = "2.0", features = ["keccak"] }
scrypt = { version = "0.2.0", default-features = false }
parity-secp256k1 = { version = "0.7.0", optional = true }
secp256k1 = { version = "0.17.2", optional = true, features = ["recovery", "rand-std"] }
ethereum-types = { version = "0.8.0", optional = true }
lazy_static = { version = "1.0", optional = true }
ripemd160 = "0.8.0"
sha2 = "0.8.0"
digest = "0.8.1"
hmac = "0.7.1"
digest = "0.8"
hmac = "0.7"
aes = "0.3.2"
aes-ctr = "0.3.0"
block-modes = "0.3.3"
Expand All @@ -40,4 +40,4 @@ hex-literal = "0.2.1"
default = []
# public key crypto utils
# moved from ethkey module in parity ethereum repository
publickey = ["parity-secp256k1", "lazy_static", "ethereum-types"]
publickey = ["secp256k1", "lazy_static", "ethereum-types"]
42 changes: 15 additions & 27 deletions parity-crypto/src/publickey/ec_math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,46 @@ lazy_static! {
pub static ref CURVE_ORDER: U256 = H256::from_slice(&SECP256K1_CURVE_ORDER).into_uint();
}

/// Whether the public key is valid.
pub fn public_is_valid(public: &Public) -> bool {
to_secp256k1_public(public).ok().map_or(false, |p| p.is_valid())
}

/// In-place multiply public key by secret key (EC point * scalar)
pub fn public_mul_secret(public: &mut Public, secret: &Secret) -> Result<(), Error> {
let key_secret = secret.to_secp256k1_secret()?;
let mut key_public = to_secp256k1_public(public)?;
key_public.mul_assign(&SECP256K1, &key_secret)?;
key_public.mul_assign(&SECP256K1, &key_secret[..])?;
set_public(public, &key_public);
Ok(())
}

/// In-place add one public key to another (EC point + EC point)
pub fn public_add(public: &mut Public, other: &Public) -> Result<(), Error> {
let mut key_public = to_secp256k1_public(public)?;
let key_public = to_secp256k1_public(public)?;
let other_public = to_secp256k1_public(other)?;
key_public.add_assign(&SECP256K1, &other_public)?;
let key_public = key_public.combine(&other_public)?;
set_public(public, &key_public);
Ok(())
}

/// In-place sub one public key from another (EC point - EC point)
pub fn public_sub(public: &mut Public, other: &Public) -> Result<(), Error> {
let mut key_neg_other = to_secp256k1_public(other)?;
key_neg_other.mul_assign(&SECP256K1, &key::MINUS_ONE_KEY)?;
key_neg_other.mul_assign(&SECP256K1, super::MINUS_ONE_KEY)?;

let mut key_public = to_secp256k1_public(public)?;
key_public.add_assign(&SECP256K1, &key_neg_other)?;
key_public = key_public.combine(&key_neg_other)?;
set_public(public, &key_public);
Ok(())
}

/// Replace a public key with its additive inverse (EC point = - EC point)
pub fn public_negate(public: &mut Public) -> Result<(), Error> {
let mut key_public = to_secp256k1_public(public)?;
key_public.mul_assign(&SECP256K1, &key::MINUS_ONE_KEY)?;
key_public.mul_assign(&SECP256K1, super::MINUS_ONE_KEY)?;
set_public(public, &key_public);
Ok(())
}

/// Return the generation point (aka base point) of secp256k1
pub fn generation_point() -> Public {
let public_key =
key::PublicKey::from_slice(&SECP256K1, &BASE_POINT_BYTES).expect("constructed using constants; qed");
let public_key = key::PublicKey::from_slice(&BASE_POINT_BYTES).expect("constructed using constants; qed");
let mut public = Public::default();
set_public(&mut public, &public_key);
public
Expand All @@ -95,24 +89,24 @@ fn to_secp256k1_public(public: &Public) -> Result<key::PublicKey, Error> {
temp
};

Ok(key::PublicKey::from_slice(&SECP256K1, &public_data)?)
Ok(key::PublicKey::from_slice(&public_data)?)
}

fn set_public(public: &mut Public, key_public: &key::PublicKey) {
let key_public_serialized = key_public.serialize_vec(&SECP256K1, false);
let key_public_serialized = key_public.serialize_uncompressed();
public.as_bytes_mut().copy_from_slice(&key_public_serialized[1..65]);
}

#[cfg(test)]
mod tests {
use super::super::{Generator, Random, Secret};
use super::{generation_point, public_add, public_is_valid, public_mul_secret, public_negate, public_sub};
use super::{generation_point, public_add, public_mul_secret, public_negate, public_sub};
use std::str::FromStr;

#[test]
fn public_addition_is_commutative() {
let public1 = Random.generate().unwrap().public().clone();
let public2 = Random.generate().unwrap().public().clone();
let public1 = Random.generate().public().clone();
let public2 = Random.generate().public().clone();

let mut left = public1.clone();
public_add(&mut left, &public2).unwrap();
Expand All @@ -125,8 +119,8 @@ mod tests {

#[test]
fn public_addition_is_reversible_with_subtraction() {
let public1 = Random.generate().unwrap().public().clone();
let public2 = Random.generate().unwrap().public().clone();
let public1 = Random.generate().public().clone();
let public2 = Random.generate().public().clone();

let mut sum = public1.clone();
public_add(&mut sum, &public2).unwrap();
Expand All @@ -137,20 +131,14 @@ mod tests {

#[test]
fn public_negation_is_involutory() {
let public = Random.generate().unwrap().public().clone();
let public = Random.generate().public().clone();
let mut negation = public.clone();
public_negate(&mut negation).unwrap();
public_negate(&mut negation).unwrap();

assert_eq!(negation, public);
}

#[test]
fn known_public_is_valid() {
let public = Random.generate().unwrap().public().clone();
assert!(public_is_valid(&public));
}

#[test]
fn generation_point_expected() {
let point = generation_point();
Expand Down
26 changes: 21 additions & 5 deletions parity-crypto/src/publickey/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,37 @@

//! ECDH key agreement scheme implemented as a free function.
use super::{Error, Public, Secret, SECP256K1};
use super::{Error, Public, Secret};
use secp256k1::{self, ecdh, key};

/// Agree on a shared secret
pub fn agree(secret: &Secret, public: &Public) -> Result<Secret, Error> {
let context = &SECP256K1;
let pdata = {
let mut temp = [4u8; 65];
(&mut temp[1..65]).copy_from_slice(&public[0..64]);
temp
};

let publ = key::PublicKey::from_slice(context, &pdata)?;
let sec = key::SecretKey::from_slice(context, secret.as_bytes())?;
let shared = ecdh::SharedSecret::new_raw(context, &publ, &sec);
let publ = key::PublicKey::from_slice(&pdata)?;
let sec = key::SecretKey::from_slice(secret.as_bytes())?;
let shared = ecdh::SharedSecret::new_with_hash(&publ, &sec, |x, _| x.into())?;

Secret::import_key(&shared[0..32]).map_err(|_| Error::Secp(secp256k1::Error::InvalidSecretKey))
}

#[cfg(test)]
mod tests {
use super::{agree, Public, Secret};
use std::str::FromStr;

#[test]
fn test_agree() {
// Just some random values for secret/public to check we agree with previous implementation.
let secret = Secret::from_str("01a400760945613ff6a46383b250bf27493bfe679f05274916182776f09b28f1").unwrap();
let public= Public::from_str("e37f3cbb0d0601dc930b8d8aa56910dd5629f2a0979cc742418960573efc5c0ff96bc87f104337d8c6ab37e597d4f9ffbd57302bc98a825519f691b378ce13f5").unwrap();
let shared = agree(&secret, &public);

assert!(shared.is_ok());
assert_eq!(shared.unwrap().to_hex(), "28ab6fad6afd854ff27162e0006c3f6bd2daafc0816c85b5dfb05dbb865fa6ac",);
}
}
Loading

0 comments on commit 44870c9

Please sign in to comment.