-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make crypto implementations replaceable (#1238)
* define CryptoProvider trait * wip * implement CryptoProvider for Substrate (WIP) * comment issues * ed25519_verify implementation for SubstrateHostFunctionsManager * introduce EcdsaSecp256k1Signer * doc a bit * move CryptoProvider to tendermint crate instead * rename host_functions module to crypto * tendermint: Remove ED25519_SIGNATURE_SIZE It has been deprecated for some time. * tendermint: clean up mod crypto * Disentangle CryptoProvider from k256 crate The API needs to be abstract, so make p256k1 signature an associated type of CryptoProvider. * Add DefaultCryptoProvider Guarded by the rust-crypto feature, this is an implementation of CryptoProvider with pure Rust crates. * tendermint: reorg mod crypto Move CryptoProvider definition out of the crypto module into the provider sub-module. Rename default_provider to default. * tendermint: generalize hash methods Add generic methods Header::hash_with and ValidatorSet::hash_with enabling a custom CryptoProvider to be plugged in for calculating hashes. The .hash() methods, implemented with the DefaultCryptoProvider, are feature-gated behind "rust-crypto". * tendermint: eliminate get_split_point helper 2usize.next_power_of_two() / 1 == 1, and we have eliminated the other two explicitly matched cases at the call site, so the non-catchall match branches and the panics are dead code. * light-client: eliminate Hasher The Hasher trait is obviated by CryptoProvider. Also, we could do with less dynamic dispatching. * fix no_std * Fix the tools build * Fix wasm-light-client build * kvstore-test: No need to copy vec-of-vecs * Break down CryptoProvider into functional traits Instead of a super-trait whose sole purpose is to bind down some associated types that provide the actual functionality, provide: - Sha256, a purpose-specific trait for SHA256 hashing that has a more human-friendly interface than rust-crypto. - Nothing else for signing and verifying! These are covered by the signature framework, and it's easy to plug into that as the alt_crypto test demonstrates. The crypto::default module, gated by the "rust-crypto" feature, provides aliases for pure Rust implementations. * alt_crypto test: Roll our own signature type An alt implementation would not be able to reuse the signature type from k256. * Fix the wasm build * Fix a clippy lint * Fix a clippy lint in alt_crypto test * Disentangle CommitValidator from the hasher Remove the only purpose for using a hasher in the implementation by returning the set of validators in VerificationErrorDetail::FaultySigner. CommitValidator can now revert to being a (weird) default-implemented trait, with a ProdCommitValidator to anchor the implementation. * light-client: Enable shrinking in backward test * light-client: update edition to rust2021 * Recover LightClient::verify_backward A critical code block was hastily commented out in this "unstable" part of code. * Fix light-client-js build, record technical debt * tendermint: Make sha2 an optional dependency Use the Sha2 alias from crypto::default instead of the direct references to sha2 crate. All code that used Sha2 non-generically is feature-gated behind "rust-crypto". * Redesign crypto::Sha256, add MerkleHash trait The host API for obtaining SHA256 digests on Substrate is a simple function, it cannot work incrementally. Change the Sha256 trait to match this, but provide a MerkleHash trait to retain incremental Merkle hashing with Rust-Crypto conformant digest implementations. The merkle::NonIncremental adapter type is provided to fit the lowest common denominator Sha256 API to a Merkle hash implementation, at the cost of some allocations and extra copying. * Fix all-features build * chore: fix clippy lints * tendermint: "rust-crypto" does not imply "k256" Also rename crypto::default::ecdsa_secp256 to ecdsa_secp256k1, to harmonize the naming with the feature that gates this module. * rpc: require tendermint/rust-crypto Needed for NodeKey. * Remove a bogus lint override * Changelog entry for #1238 * tendermint: crypto::signature::Verifier trait Define Verifier trait to abstract signature verification given a PublicKey. The ed25519-consensus dependency is made optional and gated by the "rust-crypto" feature. The Verifier implementation is provided for a dummy type crate::crypto::default::signature::Verifier, using ed25519-consensus. The Ed25519 key types in public_key and private_key module are redefined to in-crate newtypes. * tendermint: fix secp256k1 verification tests * Guard public key extraction with rust-crypto * tendermint: remove re-exports from signature crate As we don't currently use the signature framework, it makes no sense to provide its traits through the crate. * tendermint: rework signature in alt_crypto test Implement the new Verifier trait instead of the signature traits. * Updated the changelog entry for #1238 --------- Co-authored-by: Blas Rodriguez Irizar <[email protected]> Co-authored-by: Romain Ruetschi <[email protected]>
- Loading branch information
1 parent
3c79d14
commit 47e28b5
Showing
48 changed files
with
1,180 additions
and
825 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
.changelog/unreleased/breaking-changes/1238-crypto-provider-traits.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
- `[tendermint]` Make implementations of cryptographic primitives replaceable | ||
([#1238](https://github.com/informalsystems/tendermint-rs/pull/1238)). | ||
* Provide a `Sha256` trait in module `crypto` and make digest hashing | ||
implementations available through it. | ||
* Provide a `Verifier` trait in module `crypto::signature` to enable | ||
alternative implementations of signature verification available through it. | ||
An `Error` enum is defined in the same module, representing the error cases | ||
that can arise in the implementation in a deliberately opaque way. | ||
* The module `crypto::default` provides pure Rust implementations of the | ||
cryptographic traits. The module is made available by a | ||
new `rust-crypto` feature, enabled by default. | ||
* `merkle::simple_hash_from_byte_vectors` is made generic over an | ||
implementation of the new `MerkleHash` trait. Implementations for | ||
Rust-Crypto conformant digest objects and the non-incremental | ||
`crypto::Sha256` API are provided in the crate. | ||
* The `Header::hash` and `ValidatorSet::hash` methods are gated by the | ||
`rust-crypto` feature. Generic hashing methods not dependent on | ||
the default crypto implementations are added for both types, | ||
named `hash_with`. | ||
* Conversions to `account::Id` and `node::Id` from `PublicKey` and | ||
curve-specific key types are gated by the `rust-crypto` feature. | ||
* The `validator::Info::new` method is gated by the `rust-crypto` feature. | ||
* Remove a deprecated constant `signature::ED25519_SIGNATURE_SIZE`. | ||
|
||
- `[tendermint-light-client-verifier]` Changes for the new Tendermint crypto API | ||
([#1238](https://github.com/informalsystems/tendermint-rs/pull/1238)). | ||
* The `rust-crypto` feature, enabled by default, guards the | ||
batteries-included implementation types: `ProdVerifier`, `ProdPredicates`, | ||
`ProdVotingPowerCalculator`. | ||
* Remove the `operations::hasher` API (`Hasher` and `ProdHasher`), | ||
made unnecessary by the new crypto abstractions in the `tendermint` crate. | ||
* The `VerificationPredicates` trait features a `Sha256` associated type | ||
to represent the hasher implementation, replacing the `&dyn Hasher` | ||
parameter passed to methods. | ||
* Change the type of the `VerificationErrorDetail::FaultySigner` field | ||
`validator_set` to `ValidatorSet`. This removes a hasher dependency from | ||
`CommitValidator`, and `ProdCommitValidator` is now an empty dummy type. | ||
|
||
- `[tendermint-light-client]` Changes for the new Tendermint crypto API | ||
([#1238](https://github.com/informalsystems/tendermint-rs/pull/1238)). | ||
* The `rust-crypto` feature enables the default crypto implementations, | ||
and is required by the `rpc-client` and `unstable` features. | ||
`ProdForkDetector` is guarded by this feature, and is made a specific | ||
type alias to the hasher-generic `ProvidedForkDetector` type. | ||
* `LightClientBuilder` gets another type parameter for the Merkle hasher. | ||
Its generic constructors lose the `Hasher` parameter. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.