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

Feat: bls crate with modular backends #69

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
# Running `rustc` or Cargo should automatically install the toolchain specified in `rust-toolchain.toml`.
- uses: Swatinem/rust-cache@v2
- name: Run cargo build
run: cargo build --release --features default-networks
run: cargo build --release --features default-networks --features blst
- name: Check if code is formatted (Linux)
if: runner.os == 'Linux'
run: cargo fmt --check
Expand All @@ -34,10 +34,10 @@ jobs:
# It's a known issue that networking tests randomly fails on Macos
- name: Run tests
if: runner.os == 'Macos'
run: cargo test --release --no-fail-fast -- --skip behaviour --skip common
run: cargo test --release --no-fail-fast --features blst -- --skip behaviour --skip common
- name: Run tests
if: runner.os != 'Macos'
run: cargo test --release --no-fail-fast
run: cargo test --release --no-fail-fast --features zkcrypto
- name: Check consensus-spec-tests coverage (Linux)
if: runner.os == 'Linux'
run: scripts/ci/consensus-spec-tests-coverage.rb
71 changes: 71 additions & 0 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ members = [
'binary_utils',
'block_producer',
'bls',
'bls/bls-blst',
'bls/bls-core',
'bls/bls-zkcrypto',
'builder_api',
'clock',
'database',
Expand Down Expand Up @@ -291,6 +294,7 @@ base64 = '0.22'
bincode = '1'
bit_field = '0.10'
bitvec = '1'
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 @@ -323,6 +327,7 @@ enum-map = '2'
enumset = '1'
env_logger = '0.11'
ethereum-types = '0.14'
ff = "0.13.0"
fixed-hash = '0.8.0'
fnv = '1'
fs-err = { version = '2', features = ['tokio'] }
Expand Down Expand Up @@ -457,6 +462,9 @@ attestation_verifier = { path = 'attestation_verifier' }
binary_utils = { path = 'binary_utils' }
block_producer = { path = 'block_producer' }
bls = { path = 'bls' }
bls-blst = { path = 'bls/bls-blst' }
bls-core = { path = 'bls/bls-core' }
bls-zkcrypto = { path = 'bls/bls-zkcrypto' }
builder_api = { path = 'builder_api' }
clock = { path = 'clock' }
database = { path = 'database' }
Expand Down Expand Up @@ -566,4 +574,4 @@ codegen-units = 1
[patch.crates-io]
# `geth` responds to invalid payloads with objects containing `method` and `params`.
# We had to fork `jsonrpc` because it does not allow nonstandard members.
jsonrpc-core = { git = 'https://github.com/grandinetech/jsonrpc.git' }
jsonrpc-core = { git = 'https://github.com/grandinetech/jsonrpc.git' }
1 change: 1 addition & 0 deletions benches/benches/helper_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::sync::Arc;

use allocator as _;
use bls::traits::CachedPublicKey as _;
use criterion::{BatchSize, Criterion, Throughput};
use easy_ext::ext;
use eth2_cache_utils::{goerli, mainnet, medalla, LazyBeaconState};
Expand Down
5 changes: 4 additions & 1 deletion block_producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::{
};

use anyhow::{Context as _, Error as AnyhowError, Result};
use bls::{AggregateSignature, PublicKeyBytes, SignatureBytes};
use bls::{
traits::{CachedPublicKey as _, Signature as _},
AggregateSignature, PublicKeyBytes, SignatureBytes,
};
use builder_api::{combined::SignedBuilderBid, BuilderApi};
use cached::{Cached as _, SizedCache};
use dedicated_executor::{DedicatedExecutor, Job};
Expand Down
27 changes: 7 additions & 20 deletions bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,11 @@ authors = ["Grandine <[email protected]>"]
[lints]
workspace = true

[dependencies]
blst = { workspace = true }
derivative = { workspace = true }
derive_more = { workspace = true }
fixed-hash = { workspace = true }
hex = { workspace = true }
impl-serde = { workspace = true }
itertools = { workspace = true }
once_cell = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
serde_utils = { workspace = true }
ssz = { workspace = true }
static_assertions = { workspace = true }
thiserror = { workspace = true }
typenum = { workspace = true }
zeroize = { workspace = true }
[features]
blst = ["dep:bls-blst"]
zkcrypto = ["dep:bls-zkcrypto"]

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

[dependencies]
bls-core = { workspace = true }
blst = { workspace = true }
derivative = { workspace = true }
derive_more = { workspace = true }
fixed-hash = { workspace = true }
hex = { workspace = true }
impl-serde = { workspace = true }
itertools = { workspace = true }
once_cell = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
serde_utils = { workspace = true }
ssz = { workspace = true }
static_assertions = { workspace = true }
typenum = { workspace = true }
zeroize = { workspace = true }

[dev-dependencies]
std_ext = { workspace = true }
tap = { workspace = true }
10 changes: 10 additions & 0 deletions bls/bls-blst/src/cached_public_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::{public_key::PublicKey, public_key_bytes::PublicKeyBytes};

use bls_core::{impl_cached_public_key, traits::CachedPublicKey as CachedPublicKeyTrait};

impl_cached_public_key!(
CachedPublicKeyTrait,
CachedPublicKey,
PublicKeyBytes,
PublicKey
);
7 changes: 7 additions & 0 deletions bls/bls-blst/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;
37 changes: 11 additions & 26 deletions bls/src/public_key.rs → bls/bls-blst/src/public_key.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
use blst::min_pk::{AggregatePublicKey as RawAggregatePublicKey, PublicKey as RawPublicKey};
use derive_more::From;

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

use super::public_key_bytes::PublicKeyBytes;

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

impl From<PublicKey> for PublicKeyBytes {
#[inline]
fn from(public_key: PublicKey) -> Self {
Self(public_key.as_raw().compress())
}
}

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

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

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

Ok(Self(raw))
}
}

impl PublicKey {
/// [`eth_aggregate_pubkeys`](https://github.com/ethereum/consensus-specs/blob/86fb82b221474cc89387fa6436806507b3849d88/specs/altair/bls.md#eth_aggregate_pubkeys)
pub fn aggregate_nonempty(public_keys: impl IntoIterator<Item = Self>) -> Result<Self, Error> {
public_keys
.into_iter()
.reduce(Self::aggregate)
.ok_or(Error::NoPublicKeysToAggregate)
}

#[inline]
#[must_use]
pub fn aggregate(mut self, other: Self) -> Self {
self.aggregate_in_place(other);
self
}
impl PublicKeyTrait for PublicKey {
type PublicKeyBytes = PublicKeyBytes;

#[inline]
pub fn aggregate_in_place(&mut self, other: Self) {
fn aggregate_in_place(&mut self, other: Self) {
let mut self_aggregate = RawAggregatePublicKey::from_public_key(self.as_raw());
let other_aggregate = RawAggregatePublicKey::from_public_key(other.as_raw());
self_aggregate.add_aggregate(&other_aggregate);
self.0 = self_aggregate.to_public_key();
}
}

impl PublicKey {
pub(crate) const fn as_raw(&self) -> &RawPublicKey {
&self.0
}
Expand Down
23 changes: 23 additions & 0 deletions bls/bls-blst/src/public_key_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use derive_more::derive::AsRef;
use fixed_hash::construct_fixed_hash;
use impl_serde::impl_fixed_hash_serde;

use bls_core::{impl_public_key_bytes, traits::COMPRESSED_SIZE};

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 {
Self(public_key.as_raw().compress())
}
}
Loading