Skip to content

Commit

Permalink
[refactor]: Replace amcl_wrapper with w3f-bls
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Polyakov <arjentix@gmail.com>
  • Loading branch information
Arjentix committed Jan 22, 2024
1 parent fa82434 commit 0c7bea0
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 495 deletions.
466 changes: 258 additions & 208 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ strum = { version = "0.25.0", default-features = false }
getset = "0.1.2"
hex-literal = "0.4.1"

rand = "0.8.5"
rand = { version = "0.8.5", default-features = false, features = ["getrandom", "alloc"] }
warp = { version = "0.3.6", default-features = false }
wasmtime = "15.0.0"

Expand Down
Binary file modified configs/peer/executor.wasm
Binary file not shown.
5 changes: 3 additions & 2 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ std = [
"digest/std",
"sha2/std",
"hkdf/std",
"w3f-bls/std",
"signature/std",
"ed25519-dalek/std",
"rand/std",
Expand Down Expand Up @@ -58,8 +59,7 @@ digest = { version = "0.10.7", default-features = false, features = ["alloc"]}
blake2 = { version = "0.10.6", default-features = false }
sha2 = { version = "0.10.8", default-features = false }
hkdf = { version = "0.12.3", default-features = false }
amcl = { version = "0.2.0", default-features = false, features = ["secp256k1"] }
amcl_wrapper = { version = "0.4.0" }
w3f-bls = { version = "0.1.3", default-features = false }

signature = { version = "2.1.0", default-features = false }
ed25519-dalek = { version = "2.0.0", default-features = false, features = ["rand_core"] }
Expand All @@ -85,6 +85,7 @@ serde_json = { workspace = true }

# these crypto libraries are not used to implement actual crypto algorithms
# but to test some of the primitives against them
amcl = { version = "0.2.0", default-features = false, features = ["secp256k1"] }
secp256k1 = { version = "0.28.0", features = ["rand", "serde"] }
libsodium-sys-stable = "1.20.3"
openssl = { version = "0.10.59", features = ["vendored"] }
37 changes: 28 additions & 9 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub use merkle::MerkleTree;
use parity_scale_codec::{Decode, Encode};
use serde::{ser::SerializeStruct, Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use w3f_bls::SerializableToBytes;

pub use self::signature::*;

Expand Down Expand Up @@ -248,17 +249,19 @@ impl From<(secp256k1::PublicKey, secp256k1::PrivateKey)> for KeyPair {
}
}

impl From<(bls::BlsNormalPublicKey, bls::PrivateKey)> for KeyPair {
fn from((public_key, private_key): (bls::BlsNormalPublicKey, bls::PrivateKey)) -> Self {
impl From<(bls::BlsNormalPublicKey, bls::BlsNormalPrivateKey)> for KeyPair {
fn from(
(public_key, private_key): (bls::BlsNormalPublicKey, bls::BlsNormalPrivateKey),
) -> Self {
Self {
public_key: PublicKey::BlsNormal(public_key),
private_key: PrivateKey::BlsNormal(private_key),
}
}
}

impl From<(bls::BlsSmallPublicKey, bls::PrivateKey)> for KeyPair {
fn from((public_key, private_key): (bls::BlsSmallPublicKey, bls::PrivateKey)) -> Self {
impl From<(bls::BlsSmallPublicKey, bls::BlsSmallPrivateKey)> for KeyPair {
fn from((public_key, private_key): (bls::BlsSmallPublicKey, bls::BlsSmallPrivateKey)) -> Self {
Self {
public_key: PublicKey::BlsSmall(Box::new(public_key)),
private_key: PrivateKey::BlsSmall(private_key),
Expand Down Expand Up @@ -336,6 +339,8 @@ impl PublicKey {

/// Key payload
fn payload(&self) -> Vec<u8> {
use w3f_bls::SerializableToBytes as _;

match self {
PublicKey::Ed25519(key) => key.as_bytes().to_vec(),
PublicKey::Secp256k1(key) => key.to_sec1_bytes().to_vec(),
Expand Down Expand Up @@ -509,17 +514,31 @@ impl From<PrivateKey> for PublicKey {

ffi::ffi_item! {
/// Private Key used in signatures.
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone)]
#[cfg_attr(all(feature = "ffi_export", not(feature = "ffi_import")), ffi_type(opaque))]
#[allow(missing_docs)]
pub enum PrivateKey {
Ed25519(Box<ed25519::PrivateKey>),
Secp256k1(secp256k1::PrivateKey),
BlsNormal(bls::PrivateKey),
BlsSmall(bls::PrivateKey),
BlsNormal(bls::BlsNormalPrivateKey),
BlsSmall(bls::BlsSmallPrivateKey),
}
}

impl PartialEq for PrivateKey {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Ed25519(l), Self::Ed25519(r)) => l == r,
(Self::Secp256k1(l), Self::Secp256k1(r)) => l == r,
(Self::BlsNormal(l), Self::BlsNormal(r)) => l.to_bytes() == r.to_bytes(),
(Self::BlsSmall(l), Self::BlsSmall(r)) => l.to_bytes() == r.to_bytes(),
_ => false,
}
}
}

impl Eq for PrivateKey {}

impl PrivateKey {
/// Creates a new public key from raw bytes received from elsewhere
///
Expand Down Expand Up @@ -567,7 +586,8 @@ impl PrivateKey {
match self {
Self::Ed25519(key) => key.to_keypair_bytes().to_vec(),
Self::Secp256k1(key) => key.to_bytes().to_vec(),
Self::BlsNormal(key) | Self::BlsSmall(key) => key.to_bytes(),
Self::BlsNormal(key) => key.to_bytes(),
Self::BlsSmall(key) => key.to_bytes(),
}
}
}
Expand Down Expand Up @@ -619,7 +639,6 @@ impl<'de> Deserialize<'de> for PrivateKey {
}

/// A session key derived from a key exchange. Will usually be used for a symmetric encryption afterwards
#[allow(unused_tuple_struct_fields)]
pub struct SessionKey(ConstVec<u8>);

impl SessionKey {
Expand Down
Loading

0 comments on commit 0c7bea0

Please sign in to comment.