Skip to content

Commit

Permalink
Temporarily disable BLS aggregate verify
Browse files Browse the repository at this point in the history
It does not work for WASM and no_std
  • Loading branch information
lrubasze committed Jan 11, 2024
1 parent 7c6f79d commit 3d404ce
Show file tree
Hide file tree
Showing 19 changed files with 43 additions and 3 deletions.
7 changes: 4 additions & 3 deletions radix-engine-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ harness = false
default = ["std"]
serde = ["dep:serde", "utils/serde", "sbor/serde", "hex/serde"]
std = ["hex/std", "sbor/std", "utils/std", "radix-engine-derive/std", "serde_json/std", "ed25519-dalek/std", "secp256k1/std", "blake2/std", "sha3/std" ]
alloc = ["hex/alloc", "sbor/alloc", "utils/alloc", "radix-engine-derive/alloc", "serde_json/alloc", "ed25519-dalek/alloc", "secp256k1/alloc", "lazy_static/spin_no_std"]
alloc = ["hex/alloc", "sbor/alloc", "utils/alloc", "radix-engine-derive/alloc", "serde_json/alloc", "ed25519-dalek/alloc", "secp256k1/alloc", "lazy_static/spin_no_std" ]

# Include crypto primitives
#crypto = ["dep:ed25519-dalek", "dep:secp256k1", "dep:blst", "dep:sha3"]
# Temporary switch to disable code related to BLS aggregate verify
# It does not work for WASM32 and no_std
enable_bls_aggregate_verify = []

# This flag is set by fuzz-tests framework and it is used to disable/enable some optional features
# to let fuzzing work
Expand Down
1 change: 1 addition & 0 deletions radix-engine-common/src/crypto/bls12381/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mod tests {
}

#[test]
#[cfg(feature = "enable_bls_aggregate_verify")]
fn sign_and_verify_aggregated() {
let sks: Vec<Bls12381G1PrivateKey> = (1..11)
.map(|i| Bls12381G1PrivateKey::from_u64(i).unwrap())
Expand Down
1 change: 1 addition & 0 deletions radix-engine-common/src/crypto/signature_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub fn verify_bls12381_v1(
/// Performs BLS12-381 G2 aggregated signature verification of
/// multiple messages each signed with different key.
/// Domain specifier tag: BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_
#[cfg(feature = "enable_bls_aggregate_verify")]
pub fn aggregate_verify_bls12381_v1(
pub_keys_and_msgs: &[(Bls12381G1PublicKey, Vec<u8>)],
signature: &Bls12381G2Signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub trait ClientCryptoUtilsApi<E> {
signature: &Bls12381G2Signature,
) -> Result<u32, E>;

#[cfg(feature = "enable_bls_aggregate_verify")]
fn bls12381_v1_aggregate_verify(
&mut self,
pub_keys_and_msgs: &[(Bls12381G1PublicKey, Vec<u8>)],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ mod component_module {
CryptoUtils::bls12381_v1_verify(message, pub_key, signature)
}

/*
* Uncomment once supported again: #[cfg(feature = "enable_bls_aggregate_verify")]
pub fn bls12381_v1_aggregate_verify(
pub_keys_msgs: Vec<(Bls12381G1PublicKey, Vec<u8>)>,
signature: Bls12381G2Signature,
) -> bool {
CryptoUtils::bls12381_v1_aggregate_verify(pub_keys_msgs, signature)
}
*/

pub fn bls12381_v1_fast_aggregate_verify(
message: Vec<u8>,
Expand Down
4 changes: 4 additions & 0 deletions radix-engine-tests/tests/system/crypto_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn crypto_scrypto_bls12381_v1_verify(
)
}

#[cfg(feature = "enable_bls_aggregate_verify")]
fn crypto_scrypto_bls12381_v1_aggregate_verify(
runner: &mut TestRunner<NoExtension, InMemorySubstateDatabase>,
package_address: PackageAddress,
Expand Down Expand Up @@ -236,6 +237,7 @@ fn test_crypto_scrypto_bls12381_g2_signature_aggregate() {
}

#[test]
#[cfg(feature = "enable_bls_aggregate_verify")]
fn test_crypto_scrypto_bls12381_aggregate_verify() {
// Arrange
let mut test_runner = TestRunnerBuilder::new().build();
Expand Down Expand Up @@ -546,6 +548,7 @@ fn test_crypto_scrypto_bls12381_g2_signature_aggregate_costing() {
}

#[test]
#[cfg(feature = "enable_bls_aggregate_verify")]
fn test_crypto_scrypto_bls12381_v1_aggregate_verify_costing() {
let mut test_runner = TestRunnerBuilder::new().build();

Expand All @@ -569,6 +572,7 @@ fn test_crypto_scrypto_bls12381_v1_aggregate_verify_costing() {
}

#[test]
#[cfg(feature = "enable_bls_aggregate_verify")]
fn test_crypto_scrypto_bls12381_v1_aggregate_verify_costing_2() {
let mut test_runner = TestRunnerBuilder::new().build();

Expand Down
1 change: 1 addition & 0 deletions radix-engine/src/system/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2875,6 +2875,7 @@ where

// Trace average message length and number of public_keys
#[trace_resources(log={pub_keys_and_msgs.iter().flat_map(|(_, msg)| msg).count()/pub_keys_and_msgs.len()},log=pub_keys_and_msgs.len())]
#[cfg(feature = "enable_bls_aggregate_verify")]
fn bls12381_v1_aggregate_verify(
&mut self,
pub_keys_and_msgs: &[(Bls12381G1PublicKey, Vec<u8>)],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub enum ExecutionCostingEntry<'a> {
Bls12381V1Verify {
size: usize,
},
#[cfg(feature = "enable_bls_aggregate_verify")]
Bls12381V1AggregateVerify {
sizes: &'a [usize],
},
Expand Down Expand Up @@ -191,6 +192,7 @@ impl<'a> ExecutionCostingEntry<'a> {
ExecutionCostingEntry::EmitLog { size } => ft.emit_log_cost(*size),
ExecutionCostingEntry::Panic { size } => ft.panic_cost(*size),
ExecutionCostingEntry::Bls12381V1Verify { size } => ft.bls12381_v1_verify_cost(*size),
#[cfg(feature = "enable_bls_aggregate_verify")]
ExecutionCostingEntry::Bls12381V1AggregateVerify { sizes } => {
ft.bls12381_v1_aggregate_verify_cost(sizes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ impl FeeTable {
}

#[inline]
#[cfg(feature = "enable_bls_aggregate_verify")]
pub fn bls12381_v1_aggregate_verify_cost(&self, sizes: &[usize]) -> u32 {
// Below approach does not take aggregation into account.
// Summing costs pers size gives greater values.
Expand Down
1 change: 1 addition & 0 deletions radix-engine/src/vm/wasm/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub const SYS_PANIC_FUNCTION_NAME: &str = "sys_panic";
// Crypto Utils
//=================
pub const CRYPTO_UTILS_BLS12381_V1_VERIFY_FUNCTION_NAME: &str = "crypto_utils_bls12381_v1_verify";
#[cfg(feature = "enable_bls_aggregate_verify")]
pub const CRYPTO_UTILS_BLS12381_V1_AGGREGATE_VERIFY_FUNCTION_NAME: &str =
"crypto_utils_bls12381_v1_aggregate_verify";
pub const CRYPTO_UTILS_BLS12381_V1_FAST_AGGREGATE_VERIFY_FUNCTION_NAME: &str =
Expand Down
1 change: 1 addition & 0 deletions radix-engine/src/vm/wasm/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ impl WasmModule {
));
}
}
#[cfg(feature = "enable_bls_aggregate_verify")]
CRYPTO_UTILS_BLS12381_V1_AGGREGATE_VERIFY_FUNCTION_NAME => {
if let TypeRef::Func(type_index) = entry.ty {
if Self::function_type_matches(
Expand Down
1 change: 1 addition & 0 deletions radix-engine/src/vm/wasm/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub trait WasmRuntime {
signature: Vec<u8>,
) -> Result<u32, InvokeError<WasmRuntimeError>>;

#[cfg(feature = "enable_bls_aggregate_verify")]
fn crypto_utils_bls12381_v1_aggregate_verify(
&mut self,
pub_keys_and_msgs: Vec<u8>,
Expand Down
2 changes: 2 additions & 0 deletions radix-engine/src/vm/wasm/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ impl WasmerModule {
runtime.crypto_utils_bls12381_v1_verify(message, public_key, signature)
}

#[cfg(feature = "enable_bls_aggregate_verify")]
pub fn bls12381_v1_aggregate_verify(
env: &WasmerInstanceEnv,
pub_keys_and_msgs_ptr: u32,
Expand Down Expand Up @@ -862,6 +863,7 @@ impl WasmerModule {
SYS_GENERATE_RUID_FUNCTION_NAME => Function::new_native_with_env(self.module.store(), env.clone(), sys_generate_ruid),
BUFFER_CONSUME_FUNCTION_NAME => Function::new_native_with_env(self.module.store(), env.clone(), buffer_consume),
CRYPTO_UTILS_BLS12381_V1_VERIFY_FUNCTION_NAME => Function::new_native_with_env(self.module.store(), env.clone(), bls12381_v1_verify),
#[cfg(feature = "enable_bls_aggregate_verify")]
CRYPTO_UTILS_BLS12381_V1_AGGREGATE_VERIFY_FUNCTION_NAME => Function::new_native_with_env(self.module.store(), env.clone(), bls12381_v1_aggregate_verify),
CRYPTO_UTILS_BLS12381_V1_FAST_AGGREGATE_VERIFY_FUNCTION_NAME => Function::new_native_with_env(self.module.store(), env.clone(), bls12381_v1_fast_aggregate_verify),
CRYPTO_UTILS_BLS12381_G2_SIGNATURE_AGGREGATE_FUNCTION_NAME => Function::new_native_with_env(self.module.store(), env.clone(), bls12381_g2_signature_aggregate),
Expand Down
3 changes: 3 additions & 0 deletions radix-engine/src/vm/wasm/wasmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ fn bls12381_v1_verify(
runtime.crypto_utils_bls12381_v1_verify(message, public_key, signature)
}

#[cfg(feature = "enable_bls_aggregate_verify")]
fn bls12381_v1_aggregate_verify(
mut caller: Caller<'_, HostState>,
pub_keys_and_msgs_ptr: u32,
Expand Down Expand Up @@ -1363,6 +1364,7 @@ impl WasmiModule {
},
);

#[cfg(feature = "enable_bls_aggregate_verify")]
let host_bls12381_v1_aggregate_verify = Func::wrap(
store.as_context_mut(),
|caller: Caller<'_, HostState>,
Expand Down Expand Up @@ -1585,6 +1587,7 @@ impl WasmiModule {
CRYPTO_UTILS_BLS12381_V1_VERIFY_FUNCTION_NAME,
host_bls12381_v1_verify
);
#[cfg(feature = "enable_bls_aggregate_verify")]
linker_define!(
linker,
CRYPTO_UTILS_BLS12381_V1_AGGREGATE_VERIFY_FUNCTION_NAME,
Expand Down
1 change: 1 addition & 0 deletions radix-engine/src/vm/wasm_runtime/no_op_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ impl<'a> WasmRuntime for NoOpWasmRuntime<'a> {
Err(InvokeError::SelfError(WasmRuntimeError::NotImplemented))
}

#[cfg(feature = "enable_bls_aggregate_verify")]
fn crypto_utils_bls12381_v1_aggregate_verify(
&mut self,
pub_keys_and_msgs: Vec<u8>,
Expand Down
1 change: 1 addition & 0 deletions radix-engine/src/vm/wasm_runtime/scrypto_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ where
Ok(result)
}

#[cfg(feature = "enable_bls_aggregate_verify")]
fn crypto_utils_bls12381_v1_aggregate_verify(
&mut self,
pub_keys_and_msgs: Vec<u8>,
Expand Down
13 changes: 13 additions & 0 deletions scrypto-test/src/environment/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@ implement_client_api! {
tip_percentage: (&mut self) -> Result<u32, RuntimeError>,
fee_balance: (&mut self) -> Result<Decimal, RuntimeError>,
},
}

#[cfg(not(feature = "enable_bls_aggregate_verify"))]
implement_client_api! {
ClientCryptoUtilsApi: {
bls12381_v1_verify: (&mut self, message: &[u8], public_key: &Bls12381G1PublicKey, signature: &Bls12381G2Signature) -> Result<u32, RuntimeError>,
bls12381_v1_fast_aggregate_verify: (&mut self, message: &[u8], public_keys: &[Bls12381G1PublicKey], signature: &Bls12381G2Signature) -> Result<u32, RuntimeError>,
bls12381_g2_signature_aggregate: (&mut self, signatures: &[Bls12381G2Signature]) -> Result<Bls12381G2Signature, RuntimeError>,
keccak256_hash: (&mut self, data: &[u8]) -> Result<Hash, RuntimeError>,
},
}
#[cfg(feature = "enable_bls_aggregate_verify")]
implement_client_api! {
ClientCryptoUtilsApi: {
bls12381_v1_verify: (&mut self, message: &[u8], public_key: &Bls12381G1PublicKey, signature: &Bls12381G2Signature) -> Result<u32, RuntimeError>,
bls12381_v1_aggregate_verify: (&mut self, pub_keys_and_msgs: &[(Bls12381G1PublicKey, Vec<u8>)], signature: &Bls12381G2Signature) -> Result<u32, RuntimeError>,
Expand Down
1 change: 1 addition & 0 deletions scrypto/src/crypto_utils/crypto_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl CryptoUtils {
/// Performs BLS12-381 G2 aggregated signature verification of
/// multiple messages each signed with different key.
/// Domain specifier tag: BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_
#[cfg(feature = "enable_bls_aggregate_verify")]
pub fn bls12381_v1_aggregate_verify(
pub_keys_and_msgs: Vec<(Bls12381G1PublicKey, Vec<u8>)>,
signature: Bls12381G2Signature,
Expand Down
1 change: 1 addition & 0 deletions scrypto/src/engine/wasm_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ pub mod crypto_utils {
signature_ptr: *const u8,
signature_len: usize) -> u32;

#[cfg(feature = "enable_bls_aggregate_verify")]
pub fn crypto_utils_bls12381_v1_aggregate_verify(
pub_keys_and_msgs_ptr: *const u8,
pub_keys_and_msgs_len: usize,
Expand Down

0 comments on commit 3d404ce

Please sign in to comment.