Skip to content

Commit

Permalink
improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
punwai committed Jul 15, 2022
1 parent 15f9229 commit c77b001
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tokio = { version = "1.19.2", features = ["sync", "rt", "macros"] }
ark-bls12-377 = { version = "0.3.0", features = ["std"], optional = true }
hkdf = { version = "0.12.3", features = ["std"] }
serde_with = "1.14.0"
serde_json = "1.0.80"
schemars ="0.8.10"

# TODO: switch to https://github.com/celo-org/celo-bls-snark-rs
Expand Down
8 changes: 8 additions & 0 deletions crypto/src/bls12377/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,11 @@ impl TryInto<BLS12377PublicKey> for BLS12377PublicKeyBytes {
BLS12377PublicKey::from_bytes(self.as_ref()).map_err(|_| Self::Error::new())
}
}

impl Into<BLS12377PublicKeyBytes> for BLS12377PublicKey {
fn into(self) -> BLS12377PublicKeyBytes {
let mut bytes = [0u8; CELO_BLS_PUBLIC_KEY_LENGTH];
self.pubkey.serialize(&mut bytes[..]).unwrap();
BLS12377PublicKeyBytes::new(bytes)
}
}
6 changes: 6 additions & 0 deletions crypto/src/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,9 @@ impl TryInto<BLS12381PublicKey> for BLS12381PublicKeyBytes {
BLS12381PublicKey::from_bytes(self.as_ref()).map_err(|_| Self::Error::new())
}
}

impl Into<BLS12381PublicKeyBytes> for BLS12381PublicKey {
fn into(self) -> BLS12381PublicKeyBytes {
BLS12381PublicKeyBytes::new(self.pubkey.to_bytes())
}
}
6 changes: 6 additions & 0 deletions crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,9 @@ impl TryInto<Ed25519PublicKey> for Ed25519PublicKeyBytes {
Ed25519PublicKey::from_bytes(self.as_ref()).map_err(|_| Self::Error::new())
}
}

impl Into<Ed25519PublicKeyBytes> for Ed25519PublicKey {
fn into(self) -> Ed25519PublicKeyBytes {
Ed25519PublicKeyBytes::new(self.0.to_bytes())
}
}
28 changes: 26 additions & 2 deletions crypto/src/tests/bls12377_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use super::*;
use crate::{
bls12377::{
BLS12377AggregateSignature, BLS12377KeyPair, BLS12377PrivateKey, BLS12377PublicKey,
BLS12377Signature,
BLS12377Signature, BLS12377PublicKeyBytes,
},
traits::{AggregateAuthenticator, EncodeDecodeBase64, ToFromBytes, VerifyingKey},
traits::{AggregateAuthenticator, EncodeDecodeBase64, ToFromBytes, VerifyingKey}, hkdf::hkdf_generate_from_ikm, ed25519::Ed25519KeyPair, bls12381::BLS12381KeyPair,
};
use rand::{rngs::StdRng, SeedableRng as _};
use sha3::{Sha3_256, Sha3_512};
use signature::{Signer, Verifier};

pub fn keys() -> Vec<BLS12377KeyPair> {
Expand Down Expand Up @@ -362,6 +363,29 @@ fn test_add_signatures_to_aggregate() {
assert!(sig2.verify(&pks, message).is_ok());
}

#[test]
fn test_hkdf_generate_from_ikm() {
let seed = &[
1, 1, 1, 0, 2, 2, 4, 4,
3, 3, 1, 0, 1, 2, 4, 1,
0, 0, 0, 0, 1, 1, 3, 3,
1, 2, 9, 8, 7, 6, 5, 4,
];
let salt = &[3, 2, 1];
let kp = hkdf_generate_from_ikm::<Sha3_512, BLS12377KeyPair>(seed, salt, None).unwrap();
let kp2 = hkdf_generate_from_ikm::<Sha3_512, BLS12377KeyPair>(seed, salt, None).unwrap();
assert_eq!(kp.private().as_bytes(), kp2.private().as_bytes());
}


#[test]
fn test_public_key_bytes_conversion() {
let kp = keys().pop().unwrap();
let pk_bytes: BLS12377PublicKeyBytes = kp.public().clone().into();
let rebuilded_pk: BLS12377PublicKey = pk_bytes.clone().try_into().unwrap();
assert_eq!(kp.public().as_bytes(), rebuilded_pk.as_bytes());
}

#[tokio::test]
async fn signature_service() {
// Get a keypair.
Expand Down
43 changes: 34 additions & 9 deletions crypto/src/tests/bls12381_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use super::*;
use crate::{
bls12381::{
BLS12381AggregateSignature, BLS12381KeyPair, BLS12381PrivateKey, BLS12381PublicKey,
BLS12381Signature,
BLS12381Signature, BLS12381PublicKeyBytes,
},
traits::{AggregateAuthenticator, EncodeDecodeBase64, ToFromBytes, VerifyingKey},
traits::{AggregateAuthenticator, EncodeDecodeBase64, ToFromBytes, VerifyingKey}, hkdf::hkdf_generate_from_ikm,
};
use rand::{rngs::StdRng, SeedableRng as _};
use sha3::Sha3_256;
use signature::{Signer, Verifier};

pub fn keys() -> Vec<BLS12381KeyPair> {
Expand Down Expand Up @@ -346,14 +347,38 @@ fn test_add_signatures_to_aggregate() {
}

#[test]
fn test_to_from_bytes_aggregate_signature() {
let kpref = keys().pop().unwrap();
let signature = kpref.sign(b"Hello, world");
let aggregated_signature = BLS12381AggregateSignature::aggregate(vec![signature]).unwrap();
let sig_bytes = aggregated_signature.as_ref();
let rebuilt_sig = <BLS12381AggregateSignature as ToFromBytes>::from_bytes(sig_bytes).unwrap();
fn test_human_readable_signatures_serde() {
let kp = keys().pop().unwrap();
let message: &[u8] = b"Hello, world!";
let signature = kp.sign(message);

let serialized = serde_json::to_string(&signature.clone()).unwrap();
assert_eq!(format!("{{\"sig\":\"{}\"}}", base64ct::Base64::encode_string(&signature.sig.to_bytes())), serialized);
let deserialized: BLS12381Signature = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, signature);
}

assert_eq!(rebuilt_sig.sig, aggregated_signature.sig);
#[test]
fn test_hkdf_generate_from_ikm() {
let seed = &[
0, 0, 1, 1, 2, 2, 4, 4,
8, 2, 0, 9, 3, 2, 4, 1,
1, 1, 2, 0, 1, 1, 3, 4,
1, 2, 9, 8, 7, 6, 5, 4,
];
let salt = &[3, 2, 1];
let kp = hkdf_generate_from_ikm::<Sha3_256, BLS12381KeyPair>(seed, salt, Some(&[1])).unwrap();
let kp2 = hkdf_generate_from_ikm::<Sha3_256, BLS12381KeyPair>(seed, salt, Some(&[1])).unwrap();

assert_eq!(kp.private().as_bytes(), kp2.private().as_bytes());
}

#[test]
fn test_public_key_bytes_conversion() {
let kp = keys().pop().unwrap();
let pk_bytes: BLS12381PublicKeyBytes = kp.public().clone().into();
let rebuilded_pk: BLS12381PublicKey = pk_bytes.clone().try_into().unwrap();
assert_eq!(kp.public().as_bytes(), rebuilded_pk.as_bytes());
}

#[tokio::test]
Expand Down
29 changes: 26 additions & 3 deletions crypto/src/tests/ed25519_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use super::*;
use crate::{
ed25519::{
Ed25519AggregateSignature, Ed25519KeyPair, Ed25519PrivateKey, Ed25519PublicKey,
Ed25519Signature,
Ed25519Signature, Ed25519PublicKeyBytes,
},
traits::{AggregateAuthenticator, EncodeDecodeBase64, ToFromBytes, VerifyingKey},
traits::{AggregateAuthenticator, EncodeDecodeBase64, ToFromBytes, VerifyingKey}, hkdf::hkdf_generate_from_ikm,
};

use blake2::digest::Update;
use rand::{rngs::StdRng, SeedableRng as _};
use sha3::Sha3_256;
use signature::{Signer, Verifier};

impl Hash for &[u8] {
Expand Down Expand Up @@ -370,6 +371,28 @@ fn test_add_signatures_to_aggregate() {
assert!(sig2.verify(&pks, message).is_ok());
}

#[test]
fn test_hkdf_generate_from_ikm() {
let seed = &[
0, 0, 1, 1, 2, 2, 4, 4,
8, 2, 0, 9, 3, 2, 4, 1,
1, 1, 2, 0, 1, 1, 3, 4,
1, 2, 9, 8, 7, 6, 5, 4,
];
let salt = &[3, 2, 1];
let kp = hkdf_generate_from_ikm::<Sha3_256, Ed25519KeyPair>(seed, salt, None).unwrap();
let kp2 = hkdf_generate_from_ikm::<Sha3_256, Ed25519KeyPair>(seed, salt, None).unwrap();
assert_eq!(kp.private().as_bytes(), kp2.private().as_bytes());
}

#[test]
fn test_public_key_bytes_conversion() {
let kp = keys().pop().unwrap();
let pk_bytes: Ed25519PublicKeyBytes = kp.public().clone().into();
let rebuilded_pk: Ed25519PublicKey = pk_bytes.clone().try_into().unwrap();
assert_eq!(kp.public().as_bytes(), rebuilded_pk.as_bytes());
}

#[tokio::test]
async fn signature_service() {
// Get a keypair.
Expand All @@ -386,4 +409,4 @@ async fn signature_service() {

// Verify the signature we received.
assert!(pk.verify(digest.as_ref(), &signature).is_ok());
}
}

0 comments on commit c77b001

Please sign in to comment.