Skip to content

Commit

Permalink
Update BLS config (#1024)
Browse files Browse the repository at this point in the history
* Add ts test

* Update configs

* Code clean

* Reanme

* Rename

* Doc for deserialize_compressed_unchecked(pub_key)

* More doc

* Fmt

* Fmt

---------

Co-authored-by: echo <[email protected]>
  • Loading branch information
boundless-forest and hujw77 authored Mar 16, 2023
1 parent 834bfe9 commit 478f126
Show file tree
Hide file tree
Showing 9 changed files with 634 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion precompile/bls12-381/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pallet-evm = { workspace = true }
precompile-utils = { workspace = true }

# substrate
sp-std = { workspace = true }
frame-support = { workspace = true }
sp-std = { workspace = true }

[dev-dependencies]
rand = { version = "0.8.5" }
Expand All @@ -46,5 +47,6 @@ std = [
"precompile-utils/std",

# substrate
"frame-support/std",
"sp-std/std",
]
32 changes: 25 additions & 7 deletions precompile/bls12-381/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,50 @@ use bls::{hash_to_curve_g2, PublicKey, Signature};

// core
use core::marker::PhantomData;
// frontier
use pallet_evm::GasWeightMapping;
// moonbeam
use precompile_utils::prelude::*;
// substrate
use frame_support::{ensure, weights::Weight};
use sp_std::prelude::*;

pub(crate) const BLS_ESTIMATED_COST: u64 = 100_000;
/// The BLS verification is a computationally intensive process. Normally, it consumes a lot of
/// block weight according to our benchmark test. Tested verifying of 512 public keys signature on
/// the `AMD Ryzen 7 5700G`, this precompile consumed at least 117_954_459_000 weight. So we give
/// them more than that to ensure there is enough time for other machine types.
const BLS_WEIGHT: u64 = 150_000_000_000;

pub struct BLS12381<T>(PhantomData<T>);

#[precompile_utils::precompile]
impl<Runtime: pallet_evm::Config> BLS12381<Runtime> {
/// FastAggregateVerify
///
/// Verifies an aggregate_signature against a list of pub_keys.
/// pub_keys must be trusted the origin of the serialization
/// precompile do not check the keys is valid
/// see more: https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-04#section-2.5
#[precompile::public("fast_aggregate_verify(bytes[],bytes,bytes)")]
#[precompile::view]
fn fast_aggregate_verify(
handle: &mut impl PrecompileHandle,
pubkeys: Vec<UnboundedBytes>,
pub_keys: Vec<UnboundedBytes>,
message: UnboundedBytes,
signature: UnboundedBytes,
) -> EvmResult<bool> {
handle.record_cost(BLS_ESTIMATED_COST)?;
handle.record_cost(<Runtime as pallet_evm::Config>::GasWeightMapping::weight_to_gas(
Weight::from_ref_time(BLS_WEIGHT),
))?;

ensure!(pub_keys.len() <= 512, revert("Too many pub keys"));

let asig =
Signature::from_bytes(signature.as_bytes()).map_err(|_| revert("Invalid signature"))?;
let public_keys: Result<Vec<PublicKey>, _> =
pubkeys.into_iter().map(|k| PublicKey::from_bytes(k.as_bytes())).collect();
let Ok(pks) = public_keys else {
return Err(revert("Invalid pubkeys"));
let pub_keys: Result<Vec<PublicKey>, _> =
pub_keys.into_iter().map(|k| PublicKey::from_bytes(k.as_bytes())).collect();
let Ok(pks) = pub_keys else {
return Err(revert("Invalid pub keys"));
};

let apk = PublicKey::aggregate(pks);
Expand Down
2 changes: 1 addition & 1 deletion runtime/pangolin/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::*;
// frontier
use pallet_evm::Precompile;

const BLOCK_GAS_LIMIT: u64 = 10_000_000;
const BLOCK_GAS_LIMIT: u64 = 20_000_000;
frame_support::parameter_types! {
pub BlockGasLimit: sp_core::U256 = sp_core::U256::from(BLOCK_GAS_LIMIT);
pub PrecompilesValue: PangolinPrecompiles<Runtime> = PangolinPrecompiles::<_>::new();
Expand Down
2 changes: 1 addition & 1 deletion tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { JsonRpcResponse } from "web3-core-helpers";
export const CHAIN_ID = 43;
export const HOST_HTTP_URL = "http://127.0.0.1:9933";
export const HOST_WS_URL = "ws://127.0.0.1:9944";
export const BLOCK_GAS_LIMIT = 10000000;
export const BLOCK_GAS_LIMIT = 20000000;
export const DEFAULT_GAS = 4000000;

// Accounts builtin
Expand Down
34 changes: 34 additions & 0 deletions tests/ethereum/contracts/contracts_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,37 @@ export const eventInfo = {
},
],
};

export const blsInfo = {
abi: [
{
inputs: [
{
internalType: "bytes[]",
name: "pubkeys",
type: "bytes[]",
},
{
internalType: "bytes",
name: "message",
type: "bytes",
},
{
internalType: "bytes",
name: "sig",
type: "bytes",
},
],
name: "fast_aggregate_verify",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "nonpayable",
type: "function",
},
],
};
2 changes: 1 addition & 1 deletion tests/ethereum/test-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Test Block RPC", () => {
miner: "0x0000000000000000000000000000000000000000",
number: 0,
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
size: 504,
size: 505,
timestamp: 0,
totalDifficulty: "0",
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
Expand Down
Loading

0 comments on commit 478f126

Please sign in to comment.