Implement crypto byte array newtypes in term of a shared type (#3684)

Introduces `CryptoBytes` type defined as:

```rust
pub struct CryptoBytes<const N: usize, Tag = ()>(pub [u8; N], PhantomData<fn() -> Tag>);
```

The type implements a bunch of methods and traits which are typically
expected from a byte array newtype
(NOTE: some of the methods and trait implementations IMO are a bit
redundant, but I decided to maintain them all to not change too much
stuff in this PR)

It also introduces two (generic) typical consumers of `CryptoBytes`:
`PublicBytes` and `SignatureBytes`.

```rust
pub struct PublicTag;
pub PublicBytes<const N: usize, CryptoTag> = CryptoBytes<N, (PublicTag, CryptoTag)>;

pub struct SignatureTag;
pub SignatureBytes<const N: usize, CryptoTag> = CryptoBytes<N, (SignatureTag, CryptoTag)>;
```

Both of them use a tag to differentiate the two types at a higher level.
Downstream specializations will further specialize using a dedicated
crypto tag. For example in ECDSA:


```rust
pub struct EcdsaTag;

pub type Public = PublicBytes<PUBLIC_KEY_SERIALIZED_SIZE, EcdsaTag>;
pub type Signature = PublicBytes<PUBLIC_KEY_SERIALIZED_SIZE, EcdsaTag>;
```

Overall we have a cleaner and most importantly **consistent** code for
all the types involved

All these details are opaque to the end user which can use `Public` and
`Signature` for the cryptos as before
This commit is contained in:
Davide Galassi
2024-03-19 16:47:42 +01:00
committed by GitHub
parent 5fd72a1f5e
commit 1e9fd23776
29 changed files with 492 additions and 1163 deletions
@@ -100,7 +100,7 @@ fn cryptos_are_compatible() {
let sp_core_signature = sp_core_secret.sign(message); // no error expected...
assert!(sp_core::ed25519::Pair::verify(
&sp_core::ed25519::Signature::from_slice(&libp2p_signature).unwrap(),
&sp_core::ed25519::Signature::try_from(libp2p_signature.as_slice()).unwrap(),
message,
&sp_core_public
));
@@ -181,7 +181,7 @@ sp_core::wasm_export_functions! {
sig.copy_from_slice(&input[32..96]);
let msg = b"all ok!";
ed25519_verify(&ed25519::Signature(sig), &msg[..], &ed25519::Public(pubkey))
ed25519_verify(&ed25519::Signature::from(sig), &msg[..], &ed25519::Public::from(pubkey))
}
fn test_sr25519_verify(input: Vec<u8>) -> bool {
@@ -192,7 +192,7 @@ sp_core::wasm_export_functions! {
sig.copy_from_slice(&input[32..96]);
let msg = b"all ok!";
sr25519_verify(&sr25519::Signature(sig), &msg[..], &sr25519::Public(pubkey))
sr25519_verify(&sr25519::Signature::from(sig), &msg[..], &sr25519::Public::from(pubkey))
}
fn test_ordered_trie_root() -> Vec<u8> {
@@ -2226,11 +2226,11 @@ fn reorg_triggers_a_notification_even_for_sources_that_should_not_trigger_notifi
#[test]
fn use_dalek_ext_works() {
fn zero_ed_pub() -> sp_core::ed25519::Public {
sp_core::ed25519::Public([0u8; 32])
sp_core::ed25519::Public::default()
}
fn zero_ed_sig() -> sp_core::ed25519::Signature {
sp_core::ed25519::Signature::from_raw([0u8; 64])
sp_core::ed25519::Signature::default()
}
let mut client = TestClientBuilder::new().build();