Skip to content

Commit 3e70dd3

Browse files
shreyas-londhepovi
authored andcommitted
feat: modularised bls crate to have mulitple backend impl
chore: make bls/blst default backend for bls crate
1 parent bdb716d commit 3e70dd3

File tree

82 files changed

+1314
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1314
-537
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
run: cargo test --release --no-fail-fast -- --skip behaviour --skip common
3838
- name: Run tests
3939
if: runner.os != 'Macos'
40-
run: cargo test --release --no-fail-fast
40+
run: cargo test --release --no-fail-fast --no-default-features --features bls/zkcrypto
4141
- name: Check consensus-spec-tests coverage (Linux)
4242
if: runner.os == 'Linux'
4343
run: scripts/ci/consensus-spec-tests-coverage.rb

Cargo.lock

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ members = [
88
'binary_utils',
99
'block_producer',
1010
'bls',
11+
'bls/bls-blst',
12+
'bls/bls-core',
13+
'bls/bls-zkcrypto',
1114
'builder_api',
1215
'clock',
1316
'database',
@@ -291,6 +294,7 @@ base64 = '0.22'
291294
bincode = '1'
292295
bit_field = '0.10'
293296
bitvec = '1'
297+
bls12_381 = { git = "https://github.com/zkcrypto/bls12_381.git" }
294298
blst = { version = '0.3', features = ['portable'] }
295299
bstr = '1'
296300
build-time = '0.1'
@@ -324,6 +328,7 @@ enum-map = '2'
324328
enumset = '1'
325329
env_logger = '0.11'
326330
ethereum-types = '0.14'
331+
ff = "0.13.0"
327332
fixed-hash = '0.8.0'
328333
fnv = '1'
329334
fs-err = { version = '2', features = ['tokio'] }
@@ -457,6 +462,9 @@ attestation_verifier = { path = 'attestation_verifier' }
457462
binary_utils = { path = 'binary_utils' }
458463
block_producer = { path = 'block_producer' }
459464
bls = { path = 'bls' }
465+
bls-blst = { path = 'bls/bls-blst' }
466+
bls-core = { path = 'bls/bls-core' }
467+
bls-zkcrypto = { path = 'bls/bls-zkcrypto' }
460468
builder_api = { path = 'builder_api' }
461469
clock = { path = 'clock' }
462470
database = { path = 'database' }
@@ -566,4 +574,4 @@ codegen-units = 1
566574
[patch.crates-io]
567575
# `geth` responds to invalid payloads with objects containing `method` and `params`.
568576
# We had to fork `jsonrpc` because it does not allow nonstandard members.
569-
jsonrpc-core = { git = 'https://github.com/grandinetech/jsonrpc.git' }
577+
jsonrpc-core = { git = 'https://github.com/grandinetech/jsonrpc.git' }

benches/benches/helper_functions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use std::sync::Arc;
88

99
use allocator as _;
10+
use bls::traits::CachedPublicKey as _;
1011
use criterion::{BatchSize, Criterion, Throughput};
1112
use easy_ext::ext;
1213
use eth2_cache_utils::{goerli, mainnet, medalla, LazyBeaconState};

block_producer/src/block_producer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use std::{
55
};
66

77
use anyhow::{Context as _, Error as AnyhowError, Result};
8-
use bls::{AggregateSignature, PublicKeyBytes, SignatureBytes};
8+
use bls::{
9+
traits::{CachedPublicKey as _, Signature as _},
10+
AggregateSignature, PublicKeyBytes, SignatureBytes,
11+
};
912
use builder_api::{combined::SignedBuilderBid, BuilderApi};
1013
use cached::{Cached as _, SizedCache};
1114
use dedicated_executor::{DedicatedExecutor, Job};

bls/Cargo.toml

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,11 @@ authors = ["Grandine <[email protected]>"]
66
[lints]
77
workspace = true
88

9-
[dependencies]
10-
blst = { workspace = true }
11-
derivative = { workspace = true }
12-
derive_more = { workspace = true }
13-
fixed-hash = { workspace = true }
14-
hex = { workspace = true }
15-
impl-serde = { workspace = true }
16-
itertools = { workspace = true }
17-
once_cell = { workspace = true }
18-
rand = { workspace = true }
19-
serde = { workspace = true }
20-
serde_utils = { workspace = true }
21-
ssz = { workspace = true }
22-
static_assertions = { workspace = true }
23-
thiserror = { workspace = true }
24-
typenum = { workspace = true }
25-
zeroize = { workspace = true }
9+
[features]
10+
blst = ["dep:bls-blst"]
11+
zkcrypto = ["dep:bls-zkcrypto"]
2612

27-
[dev-dependencies]
28-
std_ext = { workspace = true }
29-
tap = { workspace = true }
13+
[dependencies]
14+
bls-core = { workspace = true }
15+
bls-blst = { workspace = true, optional = true }
16+
bls-zkcrypto = { workspace = true, optional = true }

bls/bls-blst/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "bls-blst"
3+
edition = { workspace = true }
4+
5+
[dependencies]
6+
bls-core = { workspace = true }
7+
blst = { workspace = true }
8+
derivative = { workspace = true }
9+
derive_more = { workspace = true }
10+
fixed-hash = { workspace = true }
11+
hex = { workspace = true }
12+
impl-serde = { workspace = true }
13+
itertools = { workspace = true }
14+
once_cell = { workspace = true }
15+
rand = { workspace = true }
16+
serde = { workspace = true }
17+
serde_utils = { workspace = true }
18+
ssz = { workspace = true }
19+
static_assertions = { workspace = true }
20+
typenum = { workspace = true }
21+
zeroize = { workspace = true }
22+
23+
[dev-dependencies]
24+
std_ext = { workspace = true }
25+
tap = { workspace = true }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use super::{public_key::PublicKey, public_key_bytes::PublicKeyBytes};
2+
3+
use bls_core::{impl_cached_public_key, traits::CachedPublicKey as CachedPublicKeyTrait};
4+
5+
impl_cached_public_key!(
6+
CachedPublicKeyTrait,
7+
CachedPublicKey,
8+
PublicKeyBytes,
9+
PublicKey
10+
);

bls/bls-blst/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub mod cached_public_key;
2+
pub mod public_key;
3+
pub mod public_key_bytes;
4+
pub mod secret_key;
5+
pub mod secret_key_bytes;
6+
pub mod signature;
7+
pub mod signature_bytes;

bls/src/public_key.rs renamed to bls/bls-blst/src/public_key.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,44 @@
11
use blst::min_pk::{AggregatePublicKey as RawAggregatePublicKey, PublicKey as RawPublicKey};
22
use derive_more::From;
33

4-
use crate::{Error, PublicKeyBytes};
4+
use bls_core::{error::Error, traits::PublicKey as PublicKeyTrait};
5+
6+
use super::public_key_bytes::PublicKeyBytes;
57

68
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, From)]
79
pub struct PublicKey(RawPublicKey);
810

9-
impl From<PublicKey> for PublicKeyBytes {
10-
#[inline]
11-
fn from(public_key: PublicKey) -> Self {
12-
Self(public_key.as_raw().compress())
13-
}
14-
}
15-
1611
impl TryFrom<PublicKeyBytes> for PublicKey {
1712
type Error = Error;
1813

1914
#[inline]
2015
fn try_from(bytes: PublicKeyBytes) -> Result<Self, Self::Error> {
21-
let raw = RawPublicKey::uncompress(bytes.as_bytes())?;
16+
let raw =
17+
RawPublicKey::uncompress(bytes.as_bytes()).map_err(|_| Error::InvalidPublicKey)?;
2218

2319
// This is needed to pass `fast_aggregate_verify` tests.
2420
// See the following for more information:
2521
// - <https://github.com/supranational/blst/issues/11>
2622
// - <https://github.com/ethereum/consensus-specs/releases/tag/v1.0.0>
27-
raw.validate()?;
23+
raw.validate().map_err(|_| Error::InvalidPublicKey)?;
2824

2925
Ok(Self(raw))
3026
}
3127
}
3228

33-
impl PublicKey {
34-
/// [`eth_aggregate_pubkeys`](https://github.com/ethereum/consensus-specs/blob/86fb82b221474cc89387fa6436806507b3849d88/specs/altair/bls.md#eth_aggregate_pubkeys)
35-
pub fn aggregate_nonempty(public_keys: impl IntoIterator<Item = Self>) -> Result<Self, Error> {
36-
public_keys
37-
.into_iter()
38-
.reduce(Self::aggregate)
39-
.ok_or(Error::NoPublicKeysToAggregate)
40-
}
41-
42-
#[inline]
43-
#[must_use]
44-
pub fn aggregate(mut self, other: Self) -> Self {
45-
self.aggregate_in_place(other);
46-
self
47-
}
29+
impl PublicKeyTrait for PublicKey {
30+
type PublicKeyBytes = PublicKeyBytes;
4831

4932
#[inline]
50-
pub fn aggregate_in_place(&mut self, other: Self) {
33+
fn aggregate_in_place(&mut self, other: Self) {
5134
let mut self_aggregate = RawAggregatePublicKey::from_public_key(self.as_raw());
5235
let other_aggregate = RawAggregatePublicKey::from_public_key(other.as_raw());
5336
self_aggregate.add_aggregate(&other_aggregate);
5437
self.0 = self_aggregate.to_public_key();
5538
}
39+
}
5640

41+
impl PublicKey {
5742
pub(crate) const fn as_raw(&self) -> &RawPublicKey {
5843
&self.0
5944
}

0 commit comments

Comments
 (0)