Skip to content

Commit

Permalink
fix: regularize verbose ToFromBytes / EncodeDecodeBase64
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker authored and punwai committed Jul 15, 2022
1 parent 290ce99 commit 7f7db14
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 50 deletions.
11 changes: 0 additions & 11 deletions crypto/src/bls12377/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,6 @@ impl PartialEq for BLS12377Signature {

impl Eq for BLS12377Signature {}

impl ToFromBytes for BLS12377Signature {
fn from_bytes(bytes: &[u8]) -> Result<Self, signature::Error> {
let g1 = <G1Projective as CanonicalDeserialize>::deserialize(bytes)
.map_err(|_| signature::Error::new())?;
Ok(BLS12377Signature {
sig: g1.into(),
bytes: OnceCell::new(),
})
}
}

impl Authenticator for BLS12377Signature {
type PubKey = BLS12377PublicKey;
type PrivKey = BLS12377PrivateKey;
Expand Down
23 changes: 5 additions & 18 deletions crypto/src/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ use std::{
use ::blst::{blst_scalar, blst_scalar_from_uint64, BLST_ERROR};
use base64ct::{Base64, Encoding};
use blst::min_sig as blst;
use eyre::eyre;

use once_cell::sync::OnceCell;
use rand::{rngs::OsRng, RngCore};

use crate::serde_helpers::BlsSignature;
use serde::{
de::{self},
Deserialize, Serialize,
};
use serde_with::serde_as;
use serde_with::Bytes;
use crate::serde_helpers::BlsSignature;

use signature::{Signature, Signer, Verifier};

use crate::{
traits::{
AggregateAuthenticator, Authenticator, EncodeDecodeBase64, KeyPair, SigningKey,
ToFromBytes, VerifyingKey, VerifyingKeyBytes,
},
use crate::traits::{
AggregateAuthenticator, Authenticator, EncodeDecodeBase64, KeyPair, SigningKey, ToFromBytes,
VerifyingKey, VerifyingKeyBytes,
};

pub const BLS_PRIVATE_KEY_LENGTH: usize = 32;
Expand Down Expand Up @@ -266,17 +264,6 @@ impl Display for BLS12381Signature {
}
}

impl EncodeDecodeBase64 for BLS12381Signature {
fn encode_base64(&self) -> String {
base64ct::Base64::encode_string(self.as_bytes())
}

fn decode_base64(value: &str) -> Result<Self, eyre::Report> {
let bytes = base64ct::Base64::decode_vec(value).map_err(|e| eyre!("{}", e.to_string()))?;
BLS12381Signature::from_bytes(&bytes).map_err(|e| e.into())
}
}

impl Authenticator for BLS12381Signature {
type PubKey = BLS12381PublicKey;
type PrivKey = BLS12381PrivateKey;
Expand Down
24 changes: 4 additions & 20 deletions crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
use base64ct::{Base64, Encoding};
use ed25519_dalek::SECRET_KEY_LENGTH;
use eyre::eyre;

use hkdf::Hkdf;
use serde::{de, Deserialize, Serialize};
use serde_with::serde_as;
Expand All @@ -21,19 +21,15 @@ pub struct Ed25519PublicKey(pub ed25519_dalek::PublicKey);

#[derive(Debug)]
pub struct Ed25519PrivateKey(pub ed25519_dalek::SecretKey);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde_as]
pub struct Ed25519Signature(
#[serde_as(as = "Ed25519Signature")]
pub ed25519_dalek::Signature
);
pub struct Ed25519Signature(#[serde_as(as = "Ed25519Signature")] pub ed25519_dalek::Signature);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde_as]
pub struct Ed25519AggregateSignature(
#[serde_as(as = "Vec<Ed25519Signature>")]
pub Vec<ed25519_dalek::Signature>
#[serde_as(as = "Vec<Ed25519Signature>")] pub Vec<ed25519_dalek::Signature>,
);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Ord, PartialOrd, Copy, Hash)]
Expand Down Expand Up @@ -154,18 +150,6 @@ impl<'de> Deserialize<'de> for Ed25519PrivateKey {
}
}

impl EncodeDecodeBase64 for Ed25519Signature {
fn encode_base64(&self) -> String {
base64ct::Base64::encode_string(self.as_bytes())
}

fn decode_base64(value: &str) -> Result<Self, eyre::Report> {
let bytes: Vec<u8> =
base64ct::Base64::decode_vec(value).map_err(|e| eyre!("{}", e.to_string()))?;
Ed25519Signature::from_bytes(&bytes).map_err(|e| e.into())
}
}

impl Authenticator for Ed25519Signature {
type PubKey = Ed25519PublicKey;
type PrivKey = Ed25519PrivateKey;
Expand Down
7 changes: 6 additions & 1 deletion crypto/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ pub trait ToFromBytes: AsRef<[u8]> + Debug + Sized {
}
}

impl<T: signature::Signature> ToFromBytes for T {
fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
<Self as signature::Signature>::from_bytes(bytes)
}
}

/// Cryptographic material with an immediate conversion to/from Base64 strings.
///
/// This is an [extension trait](https://rust-lang.github.io/rfcs/0445-extension-trait-conventions.html) of `ToFromBytes` above.
Expand Down Expand Up @@ -124,7 +130,6 @@ pub trait Authenticator:
+ Sync
+ 'static
+ Clone
+ EncodeDecodeBase64
{
type PubKey: VerifyingKey<Sig = Self>;
type PrivKey: SigningKey<Sig = Self>;
Expand Down

0 comments on commit 7f7db14

Please sign in to comment.