Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
'binary_utils',
'block_producer',
'bls',
'bls/bls-arkworks',
'bls/bls-blst',
'bls/bls-core',
'bls/bls-zkcrypto',
Expand Down Expand Up @@ -284,6 +285,10 @@ aes = { version = '0.8', features = ['zeroize'] }
alloy-rlp = '0.3.9'
anyhow = { version = '1', features = ['backtrace'] }
arc-swap = '1'
ark-bls12-381 = { version = '0.5.0', default-features = false, features = [ "curve" ] }
ark-ec = '0.5.0'
ark-ff = '0.5.0'
ark-serialize = '0.5.0'
assert-json-diff = '2'
async-channel = '1'
async-trait = '0.1'
Expand All @@ -294,7 +299,7 @@ base64 = '0.22'
bincode = '1'
bit_field = '0.10'
bitvec = '1'
bls12_381 = { git = "https://github.com/zkcrypto/bls12_381.git" }
bls12_381 = { git = 'https://github.com/zkcrypto/bls12_381.git' }
blst = { version = '0.3', features = ['portable'] }
bstr = '1'
build-time = '0.1'
Expand Down Expand Up @@ -470,6 +475,7 @@ attestation_verifier = { path = 'attestation_verifier' }
binary_utils = { path = 'binary_utils' }
block_producer = { path = 'block_producer' }
bls = { path = 'bls' }
bls-arkworks = { path = 'bls/bls-arkworks' }
bls-blst = { path = 'bls/bls-blst' }
bls-core = { path = 'bls/bls-core' }
bls-zkcrypto = { path = 'bls/bls-zkcrypto' }
Expand Down
2 changes: 2 additions & 0 deletions bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ authors = ["Grandine <info@grandine.io>"]
workspace = true

[features]
arkworks = ["dep:bls-arkworks"]
blst = ["dep:bls-blst"]
zkcrypto = ["dep:bls-zkcrypto"]

[dependencies]
bls-core = { workspace = true }
bls-arkworks = { workspace = true, optional = true }
bls-blst = { workspace = true, optional = true }
bls-zkcrypto = { workspace = true, optional = true }
25 changes: 25 additions & 0 deletions bls/bls-arkworks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "bls-arkworks"
edition = { workspace = true }

[dependencies]
ark-bls12-381 = { workspace = true }
ark-ec = { workspace = true }
ark-ff = { workspace = true }
ark-serialize = { workspace = true }
bls-core = { workspace = true }
derivative = { workspace = true }
derive_more = { workspace = true }
hex = { workspace = true }
once_cell = { workspace = true }
fixed-hash = { workspace = true }
itertools = { workspace = true }
impl-serde = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
serde_utils = { workspace = true }
sha2 = { workspace = true }
ssz = { workspace = true }
static_assertions = { workspace = true }
typenum = { workspace = true }
zeroize = { workspace = true }
10 changes: 10 additions & 0 deletions bls/bls-arkworks/src/cached_public_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use bls_core::{impl_cached_public_key, CachedPublicKey as CachedPublicKeyTrait};

use super::{public_key::PublicKey, public_key_bytes::PublicKeyBytes};

impl_cached_public_key!(
CachedPublicKeyTrait,
CachedPublicKey,
PublicKeyBytes,
PublicKey
);
7 changes: 7 additions & 0 deletions bls/bls-arkworks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod cached_public_key;
pub mod public_key;
pub mod public_key_bytes;
pub mod secret_key;
pub mod secret_key_bytes;
pub mod signature;
pub mod signature_bytes;
53 changes: 53 additions & 0 deletions bls/bls-arkworks/src/public_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use ark_bls12_381::{G1Affine, G1Projective};
use ark_ec::AffineRepr;
use ark_serialize::CanonicalDeserialize;
use derive_more::From;

use bls_core::{error::Error, traits::PublicKey as PublicKeyTrait};

use super::public_key_bytes::PublicKeyBytes;

#[derive(Clone, Copy, PartialEq, Eq, Debug, From)]
pub struct PublicKey(G1Projective);

impl Default for PublicKey {
#[inline]
fn default() -> Self {
Self(G1Projective::default())
}
}

impl TryFrom<PublicKeyBytes> for PublicKey {
type Error = Error;

#[inline]
fn try_from(bytes: PublicKeyBytes) -> Result<Self, Self::Error> {
let point = G1Affine::deserialize_compressed::<&[u8]>(bytes.as_ref())
.map_err(|_| Error::DecompressionFailed)?;

if bool::from(point.is_zero()) {
return Err(Error::InvalidPublicKey);
}

if !bool::from(point.is_on_curve()) {
return Err(Error::DecompressionFailed);
}

Ok(Self(point.into()))
}
}

impl PublicKeyTrait for PublicKey {
type PublicKeyBytes = PublicKeyBytes;

#[inline]
fn aggregate_in_place(&mut self, other: Self) {
self.0 = *self.as_raw() + other.as_raw();
}
}

impl PublicKey {
pub(crate) const fn as_raw(&self) -> &G1Projective {
&self.0
}
}
28 changes: 28 additions & 0 deletions bls/bls-arkworks/src/public_key_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use ark_serialize::CanonicalSerialize;
use bls_core::{impl_public_key_bytes, COMPRESSED_SIZE};
use derive_more::derive::AsRef;
use fixed_hash::construct_fixed_hash;
use impl_serde::impl_fixed_hash_serde;

use super::public_key::PublicKey;

construct_fixed_hash! {
#[derive(AsRef)]
pub struct PublicKeyBytes(COMPRESSED_SIZE);
}

impl_fixed_hash_serde!(PublicKeyBytes, COMPRESSED_SIZE);

impl_public_key_bytes!(PublicKeyBytes);

impl From<PublicKey> for PublicKeyBytes {
#[inline]
fn from(public_key: PublicKey) -> Self {
let mut bytes = [0u8; COMPRESSED_SIZE];
public_key
.as_raw()
.serialize_compressed(&mut bytes[..])
.unwrap();
Self(bytes)
}
}
84 changes: 84 additions & 0 deletions bls/bls-arkworks/src/secret_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use ark_bls12_381::{g2, Fr, G1Projective};
use ark_ec::{
hashing::{curve_maps::wb::WBMap, map_to_curve_hasher::MapToCurveBasedHasher, HashToCurve},
short_weierstrass::Projective,
PrimeGroup,
};
use ark_ff::{field_hashers::DefaultFieldHasher, BigInteger, PrimeField};
use ark_serialize::CanonicalSerialize;
use bls_core::{
consts::DOMAIN_SEPARATION_TAG, error::Error, impl_secret_key,
traits::SecretKey as SecretKeyTrait,
};
use sha2::Sha256;

use super::{
public_key::PublicKey,
secret_key_bytes::{SecretKeyBytes, SIZE},
signature::Signature,
};

impl_secret_key!(
SecretKeyTrait<SIZE>,
SecretKey,
Fr,
SecretKeyBytes,
PublicKey,
Signature,
|scalar: &Fr| scalar.into_bigint().to_bytes_le()
);

impl TryFrom<SecretKeyBytes> for SecretKey {
type Error = Error;

#[inline]
fn try_from(secret_key_bytes: SecretKeyBytes) -> Result<Self, Self::Error> {
if secret_key_bytes.bytes.iter().all(|&b| b == 0) {
return Err(Error::InvalidSecretKey);
}

let scalar = match Option::from(Fr::from_be_bytes_mod_order(&secret_key_bytes.bytes)) {
Some(scalar) => scalar,
None => {
return Err(Error::InvalidSecretKey);
}
};
Ok(Self(scalar))
}
}

impl SecretKeyTrait<SIZE> for SecretKey {
type SecretKeyBytes = SecretKeyBytes;
type PublicKey = PublicKey;
type Signature = Signature;

#[inline]
#[must_use]
fn to_bytes(&self) -> SecretKeyBytes {
let mut bytes = [0u8; SIZE];
self.as_raw().serialize_compressed(&mut bytes[..]).unwrap();
SecretKeyBytes { bytes }
}

#[inline]
#[must_use]
fn to_public_key(&self) -> PublicKey {
let point = G1Projective::generator() * self.as_raw();
PublicKey::from(point)
}

#[inline]
#[must_use]
fn sign(&self, message: impl AsRef<[u8]>) -> Signature {
let hasher = MapToCurveBasedHasher::<
Projective<g2::Config>,
DefaultFieldHasher<Sha256, 128>,
WBMap<g2::Config>,
>::new(DOMAIN_SEPARATION_TAG)
.unwrap();
let hash = hasher.hash(message.as_ref()).unwrap();
let signature = hash * self.as_raw();

Signature::from(signature)
}
}
7 changes: 7 additions & 0 deletions bls/bls-arkworks/src/secret_key_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use bls_core::impl_secret_key_bytes;

use crate::secret_key::SecretKey;

pub const SIZE: usize = size_of::<SecretKey>();

impl_secret_key_bytes!(SecretKeyBytes, SIZE);
Loading