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

Paired-key Crypto Scheme #1705

Merged
merged 42 commits into from
Oct 15, 2023
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5d3dce1
First definition for pair public keys
drskalman Aug 3, 2023
be890d4
Two example of implementation of pair for demonestration
drskalman Aug 3, 2023
fb6a018
- implement paired crypto `Public` as tuple of two `Public`s - unsuce…
drskalman Aug 14, 2023
62ef8b1
keep both public key object and their continous serialization in pair…
drskalman Aug 21, 2023
8d4e523
implement PassBy and From<Pair> for paired_crypto
drskalman Aug 21, 2023
eea74e2
implement rest of aux traits for `paired_crypto::Public` implement so…
drskalman Aug 22, 2023
f407d87
Attempt to implement trait `Pair` for `pair_cyrpto::Pair`
drskalman Aug 29, 2023
2606cc4
- Implement trait `Pair` for `paired_crypto::Pair` - Implement a pair…
drskalman Aug 31, 2023
00933da
implement sgin and verify for
drskalman Sep 5, 2023
e7719ab
Actually implementing `paired_crypto::{Pair, Public, Signatrue}` for …
drskalman Sep 10, 2023
7a9b677
Implement and pass all test for `paired_crypto`
drskalman Sep 11, 2023
a01a814
- move to signle seed for both schemes in `primitives/core/src/paired…
drskalman Sep 25, 2023
7c02658
replace `hex!` → `array_bytes::hex2xx`
drskalman Sep 25, 2023
7f8d958
Apply suggestions from `paired_crypto` code review on type nam, hash …
drskalman Oct 5, 2023
eef2cec
Do not panic in `paired::Signature::try_from`
drskalman Oct 5, 2023
efb215f
Remove `DoublePair` trait.
drskalman Oct 5, 2023
65584bd
Do not empty implement `paired::Pair`
drskalman Oct 5, 2023
e047f75
Use `paired_crypto::Seed` instead of `[u8; SECURE_SEED_LEN]`
drskalman Oct 5, 2023
c12b82e
use `ecdsa::PUBLIC_KEY_SERIALIZED_SIZE` and `ecdsa::SIGNATURE_SERIALI…
drskalman Oct 5, 2023
dba854a
Remove `paired::DoublePair` impl as well
drskalman Oct 5, 2023
5182c86
- Implement `BytesArray` for both ecdsa and bls Signatures
drskalman Oct 5, 2023
17d0077
Implement encode_and_decode_(public_key/signature)_works test for pai…
drskalman Oct 5, 2023
a740589
cargo fmt
drskalman Oct 5, 2023
7e34167
Merge branch 'master' into skalman-paired-crypto-scheme
drskalman Oct 5, 2023
6c0c107
Cleanup
davxy Oct 7, 2023
67152d8
Remove TODO
davxy Oct 7, 2023
a0cc64e
Merge pull request #1 from davxy/skalman-paired-crypto-scheme
drskalman Oct 7, 2023
685c21d
Put (De)serialization under `serde` feature instead of `std` in bls a…
drskalman Oct 7, 2023
9b46ba8
cargo +nightly fmt
drskalman Oct 7, 2023
9fbd4d2
Make clippy happy
davxy Oct 7, 2023
b28d0ad
Fix
davxy Oct 7, 2023
78c75e3
More tweaks
davxy Oct 7, 2023
96acd6c
Ss58 requires serde
davxy Oct 7, 2023
3c4311a
Tweaks
davxy Oct 8, 2023
6aa8281
Fix typo
davxy Oct 8, 2023
f568441
- replace `unwrap` with `expect`
drskalman Oct 10, 2023
1e3b915
Merge branch 'skalman-paired-crypto-scheme' of https://github.com/w3f…
drskalman Oct 10, 2023
f07f05a
Rename `TraitPair` -> `PairT`
drskalman Oct 10, 2023
a6da3f5
Remove internal Left and Right Public and Signature objects.
drskalman Oct 10, 2023
a069dcd
Trivial things
davxy Oct 11, 2023
808a53d
Update substrate/primitives/core/src/paired_crypto.rs
drskalman Oct 11, 2023
616f503
Merge branch 'master' into skalman-paired-crypto-scheme
drskalman Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
keep both public key object and their continous serialization in pair…
…ed crypto object in favor of avoiding copy
  • Loading branch information
drskalman committed Sep 25, 2023
commit 62ef8b14ce394d7919a26abd0877705b826d1012
98 changes: 54 additions & 44 deletions substrate/primitives/core/src/paired_crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use sp_std::{convert::TryFrom, marker::PhantomData, ops::Deref};

/// ECDSA and BLS-377 specialized types
pub mod ecdsa_n_bls377 {
use crate::crypto::{CryptoTypeId};
use crate::crypto::{CryptoTypeId};
use crate::{ecdsa, ed25519};

/// An identifier used to match public keys against BLS12-377 keys
Expand All @@ -47,7 +47,7 @@ pub mod ecdsa_n_bls377 {
#[cfg(feature = "full_crypto")]
//pub type Pair = super::Pair<ecdsa:Pair, bls377:Pair>;
/// BLS12-377 public key.
pub type Public = super::Public<ecdsa::Public, ed25519::Public, 32, 32>;
pub type Public = super::Public<ecdsa::Public, ed25519::Public, 64>;
// /// BLS12-377 signature.
//pub type Signature = super::Signature<ecdsa:Signature, bls377:Signature>;

Expand All @@ -64,61 +64,71 @@ pub mod ecdsa_n_bls377 {
#[cfg(feature = "full_crypto")]
//type Seed = [u8; SECRET_KEY_SERIALIZED_SIZE];

pub trait PublicKeyBound: TraitPublic + sp_std::hash::Hash + ByteArray {}
pub trait PublicKeyBound: TraitPublic + sp_std::hash::Hash + ByteArray + for<'a> TryFrom<&'a[u8]> {}
/// A public key.
#[derive(Clone, Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Eq, PartialOrd, Ord)]
davxy marked this conversation as resolved.
Show resolved Hide resolved
#[scale_info(skip_type_params(T))]
drskalman marked this conversation as resolved.
Show resolved Hide resolved
pub struct Public<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_LEN: usize, const RIGHT_LEN: usize,> (LeftPublic, RightPublic);
pub struct Public<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize,> {
left: LeftPublic,
right: RightPublic,
inner: [u8; LEFT_PLUS_RIGHT_LEN],
}

// We essentially could implement this instead of storing left and right but we are going to end up copying left and right.
// impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize> Public<LeftPublic, RightPublic, LEFT_PLUS_RIGHT_LEN> {
// inline fn left<'a>(&self)-> &'a LeftPublic {
// &LeftPublic::try_from(&self.inner[0..LeftPublic::LEN]).unwrap()
// }

// fn right<'a>(&self)-> &'a RightPublic {
// &RightPublic::try_from(&self.inner[LeftPublic::LEN..LEFT_PLUS_RIGHT_LEN]).unwrap()
// }


// }

#[cfg(feature = "full_crypto")]
impl<LeftPublic: PublicKeyBound + UncheckedFrom<[u8; LEFT_LEN]>, RightPublic: PublicKeyBound + UncheckedFrom<[u8; RIGHT_LEN]>, const LEFT_LEN: usize, const RIGHT_LEN: usize> sp_std::hash::Hash for Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN> {
fn hash<H: sp_std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
self.1.hash(state);
impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize> sp_std::hash::Hash for Public<LeftPublic, RightPublic, LEFT_PLUS_RIGHT_LEN> {
fn hash<H: sp_std::hash::Hasher>(&self, state: &mut H) {
self.left.hash(state);
self.right.hash(state);
}
}

impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_LEN: usize, const RIGHT_LEN: usize> ByteArray for Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN> where for<'a> Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN>: TryFrom<&'a [u8], Error = ()> + AsRef<[u8]> + AsMut<[u8]> {
const LEN: usize = LeftPublic::LEN + RightPublic::LEN;
impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize> ByteArray for Public<LeftPublic, RightPublic, LEFT_PLUS_RIGHT_LEN> {
const LEN: usize = LEFT_PLUS_RIGHT_LEN;
}

impl<'a,LeftPublic: PublicKeyBound + UncheckedFrom<[u8; LEFT_LEN]>, RightPublic: PublicKeyBound + UncheckedFrom<[u8; RIGHT_LEN]>, const LEFT_LEN: usize, const RIGHT_LEN: usize> TryFrom<&'a[u8]> for Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN> {
type Error = ();
impl<'a,LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize> TryFrom<&'a[u8]> for Public<LeftPublic, RightPublic, LEFT_PLUS_RIGHT_LEN> {
type Error = ();

fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
if data.len() != LEFT_PLUS_RIGHT_LEN {
return Err(())
}
let mut left : LeftPublic = data[0..LeftPublic::LEN].try_into().unwrap();
let mut right : RightPublic = data[LeftPublic::LEN..LEFT_PLUS_RIGHT_LEN].try_into().unwrap();

fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
if data.len() != LEFT_LEN + RIGHT_LEN {
return Err(())
}
let mut inner = [0u8; LEFT_PLUS_RIGHT_LEN];
Ok(Public { left, right, inner })

}
}

let mut r0 = [0u8; LEFT_LEN];
let mut r1 = [0u8; RIGHT_LEN];
r0.copy_from_slice(&data[0..LEFT_LEN]);
r1.copy_from_slice(&data[LEFT_LEN..RIGHT_LEN]);
Ok(Self(LeftPublic::unchecked_from(r0),RightPublic::unchecked_from(r1)))
impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize> AsMut<[u8]> for Public<LeftPublic, RightPublic, LEFT_PLUS_RIGHT_LEN> {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.inner[..]
}
}

// impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_LEN: usize, const RIGHT_LEN: usize> AsMut<[u8]> for Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN> {
// fn as_mut(&mut self) -> &mut [u8] {
// &mut [self.0.as_mut(), self.1.as_mut()].concat()
// }
// }

// impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_LEN: usize, const RIGHT_LEN: usize, const RIGHT_PLUS_LEFT_LEN: usize> AsRef<[u8; RIGHT_PLUS_LEFT_LEN]> for Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN> where for<'a> Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN>: TryFrom<&'a [u8], Error = ()> {
// fn as_ref(&self) -> &[u8; RIGHT_PLUS_LEFT_LEN] {
// let mut r = [0u8; RIGHT_PLUS_LEFT_LEN];
// r.copy_from_slice(self.0.as_ref());
// r[LeftPublic::LEN..].copy_from_slice(self.1.as_ref());
// &r
// }
// }
impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize> AsRef<[u8; LEFT_PLUS_RIGHT_LEN]> for Public<LeftPublic, RightPublic, LEFT_PLUS_RIGHT_LEN> {
fn as_ref(&self) -> &[u8; LEFT_PLUS_RIGHT_LEN] {
&self.inner
}
}

// impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_LEN: usize, const RIGHT_LEN: usize,> AsRef<[u8]> for Public<LeftPublic, RightPublic, LEFT_LEN, RIGHT_LEN> {
// fn as_ref(&self) -> &[u8] {
// //let mut r : Vec<u8> = vec![0u8; LEFT_LEN + RIGHT_LEN];
// //r.copy_from_slice(self.0.as_ref(), LeftPublic::LEN);
// //r[LeftPublic::LEN..].copy_from_slice(self.1.as_ref(), RightPublic::LEN);
// let mut r :Vec<u8> = [self.0.as_ref(), self.1.as_ref()].concat();
// &r[..]
// }
// }
impl<LeftPublic: PublicKeyBound, RightPublic: PublicKeyBound, const LEFT_PLUS_RIGHT_LEN: usize,> AsRef<[u8]> for Public<LeftPublic, RightPublic, LEFT_PLUS_RIGHT_LEN> {
fn as_ref(&self) -> &[u8] {
&self.inner[..]
}
}