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
+3 -3
View File
@@ -1466,14 +1466,14 @@ where
fn sr25519_verify(&self, signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> bool {
sp_io::crypto::sr25519_verify(
&SR25519Signature(*signature),
&SR25519Signature::from(*signature),
message,
&SR25519Public(*pub_key),
&SR25519Public::from(*pub_key),
)
}
fn ecdsa_to_eth_address(&self, pk: &[u8; 33]) -> Result<[u8; 20], ()> {
ECDSAPublic(*pk).to_eth_address()
ECDSAPublic::from(*pk).to_eth_address()
}
#[cfg(test)]
@@ -139,8 +139,8 @@ mod benchmarks {
TicketsIds::<T>::insert((epoch_tag as u8, i), id);
let body = TicketBody {
attempt_idx: i,
erased_public: EphemeralPublic([i as u8; 32]),
revealed_public: EphemeralPublic([i as u8; 32]),
erased_public: EphemeralPublic::from([i as u8; 32]),
revealed_public: EphemeralPublic::from([i as u8; 32]),
};
TicketsData::<T>::set(id, Some(body));
});
@@ -236,8 +236,8 @@ mod benchmarks {
.map(|i| {
let body = TicketBody {
attempt_idx: i,
erased_public: EphemeralPublic([i as u8; 32]),
revealed_public: EphemeralPublic([i as u8; 32]),
erased_public: EphemeralPublic::from([i as u8; 32]),
revealed_public: EphemeralPublic::from([i as u8; 32]),
};
let id_bytes = crate::hashing::blake2_128(&i.to_le_bytes());
let id = TicketId::from_le_bytes(id_bytes);
@@ -13,8 +13,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
@@ -65,8 +65,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
Vec<T>
and $N others
@@ -106,8 +106,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
@@ -148,8 +148,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
Vec<T>
and $N others
@@ -168,8 +168,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
@@ -210,8 +210,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
Vec<T>
and $N others
@@ -13,8 +13,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
@@ -65,8 +65,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
Vec<T>
and $N others
@@ -106,8 +106,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
@@ -148,8 +148,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
Vec<T>
and $N others
@@ -168,8 +168,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
@@ -210,8 +210,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
Arc<T>
Vec<T>
and $N others