Introduce crypto
module and expand cryptographic functions
#6837
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR replaces #5747 and intends to introduce the crypto module.
The std-lib currently contains the
ecr.sw
andvm/evm/ecr.sw
files which have the following functions:There are a number of issues with this including no type safety for signatures from different elliptic curves, functions split across multiple files, poor naming, and generic arguments. All of these are resolved by this PR which deprecates both
ecr.sw
files and replaces them with a crypto module which syntactically matches Rust.The following new types are introduced:
PublicKey
- An Asymmetric public key, supporting both 64 and 32-byte public keysMessage
- Hashed message authenticated by a signature type that handles variable lengthsSecp256k1
- A secp256k1 signatureSecp256r1
- A secp256r1 signatureEd25519
- An ed25519 signatureSignature
- An ECDSA signatureAll original functionality is retained with the new module. The following new functionality has been added:
verify()
- Verify that a signature matches the given public key. NOTE: Only new functionality for secp256k1 and secp256r1 signatures.verify_address()
- Verify that a signature matches the given address.verify_evm_address()
- Verify that a signature matches the given EVM address.The following functions have been deprecated:
std::ecr::ec_recover()
std::ecr::ec_recover_r1()
std::ecr::ed_verify()
std::ecr::ec_recover_address()
std::ecr::ec_recover_address_r1()
std::vm::evm::ecr::ec_recover_evm_address()
Example of changes for recovering a public key:
Example of changes for recovering an Address:
Complete recovery example using the
Signature
type:Checklist
Breaking*
orNew Feature
labels where relevant.