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

Add BLS signatures #1661

Merged
merged 86 commits into from
Jan 13, 2024
Merged

Add BLS signatures #1661

merged 86 commits into from
Jan 13, 2024

Conversation

lrubasze
Copy link
Contributor

@lrubasze lrubasze commented Dec 4, 2023

Summary

This PR adds support for:

  • BLS12-381 signatures
  • Keccak256 hashing

It also implements CryptoUtils module for Client API, which allows to do some crypto-related stuff natively from Scrypto.
CryptoUtils functions:

  • keccak_hash() - generate Keccak-256 hash of the given message
  • bls_verify() - do the given BLS signature verification of the given message hash using given public key

Details

BLS details:

  • Algorithm: BLS12-381
  • Public key size: 48 bytes (G1)
  • Signature size 96 bytes (G2)

In order to get it working I had to move cryptographic primitives (PrivateKey, Signature definitions) from transaction to radix-engine-common. What is left in transaction are the Radix Engine cryptographics, eg. IntentSignatureV1 , NotarySignatureV1.

Cryptographic dependencies in radix-engine-common are not built for WASM.
Reasons:

  • ATM they are not required in WASM in Scrypto.
  • They (eg. blst) or their deps (eg. secp256k1-sys) are not implemented in Rust. And clang used in macOS GH runner does not support WASM target. For now we skip it (maybe eventually we would need the WASM support there anyway)

TODO: Costing

Testing

  • Test CryptoUtils module using CryptoScrypto test blueprint

Copy link

github-actions bot commented Dec 5, 2023

Benchmark for ce7c01f

Click to view benchmark
Test Base PR %
costing::bench_prepare_wasm 64.5±0.45ms 64.4±0.49ms -0.16%
costing::decode_sbor 14.6±0.02µs 13.2±0.01µs -9.59%
costing::decode_sbor_bytes 35.6±0.04µs 35.9±0.10µs +0.84%
costing::deserialize_wasm 1229.9±0.97µs 1237.2±1.61µs +0.59%
costing::instantiate_flash_loan 3.8±0.39ms 3.8±0.35ms 0.00%
costing::instantiate_radiswap 5.1±0.06ms 5.1±0.07ms 0.00%
costing::spin_loop 21.4±0.01ms 21.5±0.01ms +0.47%
costing::validate_sbor_payload 26.3±0.12µs 24.4±0.02µs -7.22%
costing::validate_sbor_payload_bytes 372.2±4.94ns 339.2±0.26ns -8.87%
costing::validate_secp256k1 80.3±0.06µs 80.3±0.05µs 0.00%
costing::validate_wasm 34.8±0.06ms 34.4±0.08ms -1.15%
decimal::add/0 7.2±0.01ns 7.2±0.00ns 0.00%
decimal::add/rust-native 9.5±0.01ns 9.5±0.00ns 0.00%
decimal::add/wasmer 133.6±0.05ns 134.7±0.10ns +0.82%
decimal::add/wasmer-call-native 516.9±0.22ns 527.0±0.17ns +1.95%
decimal::add/wasmi 437.3±0.07ns 441.3±1.00ns +0.91%
decimal::add/wasmi-call-native 3.3±0.03µs 3.3±0.01µs 0.00%
decimal::div/0 166.1±0.14ns 165.6±0.08ns -0.30%
decimal::from_string/0 155.5±0.07ns 153.8±0.16ns -1.09%
decimal::mul/0 128.5±0.09ns 128.6±0.10ns +0.08%
decimal::mul/rust-native 133.1±0.05ns 132.7±0.08ns -0.30%
decimal::mul/wasmer 1742.8±0.47ns 1721.0±0.47ns -1.25%
decimal::mul/wasmer-call-native 656.4±0.38ns 657.2±0.20ns +0.12%
decimal::mul/wasmi 26.5±0.03µs 26.6±0.04µs +0.38%
decimal::mul/wasmi-call-native 3.4±0.02µs 3.5±0.01µs +2.94%
decimal::pow/0 620.9±0.23ns 621.0±0.14ns +0.02%
decimal::pow/rust-native 608.7±0.25ns 607.6±0.19ns -0.18%
decimal::pow/wasmer 7.5±0.00µs 7.5±0.01µs 0.00%
decimal::pow/wasmer-call-native 1066.5±0.69ns 1077.1±0.50ns +0.99%
decimal::pow/wasmi 126.2±0.10µs 128.3±0.10µs +1.66%
decimal::pow/wasmi-call-native 3.3±0.01µs 3.4±0.01µs +3.03%
decimal::root/0 9.1±0.00µs 9.1±0.03µs 0.00%
decimal::sub/0 7.2±0.00ns 7.2±0.00ns 0.00%
decimal::to_string/0 493.8±0.19ns 487.1±0.20ns -1.36%
precise_decimal::add/0 8.0±0.01ns 8.0±0.01ns 0.00%
precise_decimal::add/rust-native 10.4±0.01ns 10.4±0.03ns 0.00%
precise_decimal::add/wasmer 141.1±0.06ns 141.8±0.05ns +0.50%
precise_decimal::add/wasmer-call-native 528.4±0.44ns 535.6±0.21ns +1.36%
precise_decimal::add/wasmi 530.1±0.07ns 532.3±0.34ns +0.42%
precise_decimal::add/wasmi-call-native 3.5±0.02µs 3.6±0.03µs +2.86%
precise_decimal::div/0 265.2±0.29ns 264.1±0.06ns -0.41%
precise_decimal::from_string/0 195.6±0.08ns 195.2±0.10ns -0.20%
precise_decimal::mul/0 283.3±0.13ns 284.1±0.07ns +0.28%
precise_decimal::mul/rust-native 262.8±0.12ns 260.6±0.33ns -0.84%
precise_decimal::mul/wasmer 4.1±0.00µs 4.1±0.00µs 0.00%
precise_decimal::mul/wasmer-call-native 825.8±0.28ns 821.6±0.23ns -0.51%
precise_decimal::mul/wasmi 74.1±0.44µs 75.0±0.04µs +1.21%
precise_decimal::mul/wasmi-call-native 3.9±0.02µs 3.9±0.02µs 0.00%
precise_decimal::pow/0 1575.9±1.79ns 1572.4±0.43ns -0.22%
precise_decimal::pow/rust-native 1269.0±0.26ns 1271.4±0.46ns +0.19%
precise_decimal::pow/wasmer 19.0±0.01µs 19.0±0.01µs 0.00%
precise_decimal::pow/wasmer-call-native 2.0±0.01µs 1997.2±2.84ns -0.14%
precise_decimal::pow/wasmi 356.4±0.20µs 362.3±0.23µs +1.66%
precise_decimal::pow/wasmi-call-native 7.5±0.04µs 7.4±0.03µs -1.33%
precise_decimal::root/0 60.4±0.02µs 60.9±0.02µs +0.83%
precise_decimal::sub/0 8.3±0.00ns 8.3±0.01ns 0.00%
precise_decimal::to_string/0 752.8±0.37ns 739.5±0.22ns -1.77%
schema::validate_payload 325.6±0.31µs 331.9±0.27µs +1.93%
transaction::radiswap 5.2±0.06ms 5.2±0.04ms 0.00%
transaction::transfer 1694.7±2.95µs 1689.7±3.30µs -0.30%
transaction_processing::prepare 2.4±0.00ms 2.4±0.00ms 0.00%
transaction_processing::prepare_and_decompile 6.1±0.01ms 6.5±0.01ms +6.56%
transaction_processing::prepare_and_decompile_and_recompile 23.4±0.16ms 22.8±0.23ms -2.56%
transaction_validation::validate_manifest 43.7±0.08µs 43.7±0.07µs 0.00%
transaction_validation::verify_bls_2KB 1007.1±22.84µs N/A N/A
transaction_validation::verify_bls_32B 1001.6±1.15µs N/A N/A
transaction_validation::verify_ecdsa 77.9±0.11µs 77.9±0.06µs 0.00%
transaction_validation::verify_ed25519 51.8±0.06µs 51.4±0.04µs -0.77%

@lrubasze lrubasze marked this pull request as ready for review December 5, 2023 15:52
Copy link
Member

@iamyulong iamyulong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall.

@@ -379,10 +379,15 @@ create_well_known_lookup!(
MISC_TYPES_START + 7,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chang well-known types is a protocol change. Will need to be synchronised with a specific protocol update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, well-known types change is not necessary, so theoretically we could skip it. But since we require the protocol update anyway I would leave it as is.

radix-engine/src/system/system.rs Show resolved Hide resolved
Copy link
Member

@iamyulong iamyulong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready to go. The missing part is to handle protocol updates, which should follow.

lrubasze and others added 24 commits January 11, 2024 19:31
This is to make sure public key count matches message count.
Take vector of tuples (public key and message) as argument.
Do not take aggregation into account. Just sum up the costs per size for
all messages.
It does not work for WASM and no_std
Add BLS aggregation features to `CryptoUtils` module
HASH,
MISC_TYPES_START + 8,
named_transparent("Hash", bytes_fixed_length_type_data(Hash::LENGTH),)
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this? As this is still a protocol change

@talekhinezh talekhinezh merged commit e7c5eff into develop Jan 13, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants