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

Add Serde Feature #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/dusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ jobs:
name: Nightly std tests
uses: dusk-network/.github/.github/workflows/run-tests.yml@main
with:
test_flags: --features=rkyv-impl,rkyv/size_16
test_flags: --features=rkyv-impl,rkyv/size_16,serde

test_parallel:
name: Nightly std tests parallel
uses: dusk-network/.github/.github/workflows/run-tests.yml@main
with:
test_flags: --features=parallel,rkyv-impl,rkyv/size_16
test_flags: --features=parallel,rkyv-impl,rkyv/size_16,serde
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add serde `Serialize` and `Deserialize` implementations for `PublicKey`, `MultisigPublicKey`, `Signature`,
`MultisigSignature` and `SecretKey` [#21]
- Add `serde`, `bs58` and `serde_json` optional dependencies [#21]
- Add `serde` feature [#21]

## [0.4.0] - 2024-08-01

### Added
Expand Down Expand Up @@ -67,6 +74,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add initial commit, this package continues the development of [dusk-bls12_381-sign](https://github.com/dusk-network/bls12_381-sign/) at version `0.6.0` under the new name: `bls12_381-bls` and without the go related code.

<!-- ISSUES -->
[#21]: https://github.com/dusk-network/bls12_381-bls/issues/21
[#18]: https://github.com/dusk-network/bls12_381-bls/issues/18
[#8]: https://github.com/dusk-network/bls12_381-bls/issues/8
[#7]: https://github.com/dusk-network/bls12_381-bls/issues/7
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ zeroize = { version = "1", default-features = false, features = ["derive"] }
rkyv = { version = "0.7", optional = true, default-features = false }
bytecheck = { version = "0.6", optional = true, default-features = false }
rayon = { version = "1.8", optional = true }
serde = { version = "1.0", optional = true }
bs58 = { version = "0.4" , optional = true }
serde_json = { version = "1.0", optional = true }

[dev-dependencies]
rand = "0.8"
Expand All @@ -34,3 +37,4 @@ rkyv-impl = [
"bytecheck",
]
parallel = ["dep:rayon"]
serde = ["dep:serde", "bs58", "serde_json"]
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub use keys::{
};
pub use signatures::{MultisigSignature, Signature};

#[cfg(feature = "serde")]
mod serde_support;

#[cfg(feature = "rkyv-impl")]
pub use crate::keys::{
public::{
Expand Down
154 changes: 154 additions & 0 deletions src/serde_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

extern crate alloc;

use alloc::format;
use alloc::string::String;

use bs58;
use dusk_bytes::Serializable;
use serde::de::Error as SerdeError;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{
MultisigPublicKey, MultisigSignature, PublicKey, SecretKey, Signature,
};

impl Serialize for PublicKey {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for PublicKey {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let decoded =
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?;
let decoded_len = decoded.len();
let byte_length_str = format!("{}", Self::SIZE);
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| {
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str())
})?;
PublicKey::from_bytes(&bytes)
.map_err(|err| SerdeError::custom(format!("{err:?}")))
}
}

impl Serialize for MultisigPublicKey {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for MultisigPublicKey {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let decoded =
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?;
let decoded_len = decoded.len();
let byte_length_str = format!("{}", Self::SIZE);
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| {
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str())
})?;
MultisigPublicKey::from_bytes(&bytes)
.map_err(|err| SerdeError::custom(format!("{err:?}")))
}
}

impl Serialize for Signature {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for Signature {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let decoded =
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?;
let decoded_len = decoded.len();
let byte_length_str = format!("{}", Self::SIZE);
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| {
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str())
})?;
Signature::from_bytes(&bytes)
.map_err(|err| SerdeError::custom(format!("{err:?}")))
}
}

impl Serialize for MultisigSignature {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for MultisigSignature {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let decoded =
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?;
let decoded_len = decoded.len();
let byte_length_str = format!("{}", Self::SIZE);
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| {
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str())
})?;
MultisigSignature::from_bytes(&bytes)
.map_err(|err| SerdeError::custom(format!("{err:?}")))
}
}

impl Serialize for SecretKey {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
let s = bs58::encode(self.to_bytes()).into_string();
serializer.serialize_str(&s)
}
}

impl<'de> Deserialize<'de> for SecretKey {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let decoded =
bs58::decode(&s).into_vec().map_err(SerdeError::custom)?;
let decoded_len = decoded.len();
let byte_length_str = format!("{}", Self::SIZE);
let bytes: [u8; Self::SIZE] = decoded.try_into().map_err(|_| {
SerdeError::invalid_length(decoded_len, &byte_length_str.as_str())
})?;
SecretKey::from_bytes(&bytes)
.map_err(|err| SerdeError::custom(format!("{err:?}")))
}
}
134 changes: 134 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

#![cfg(feature = "serde")]

use bls12_381_bls::{
MultisigPublicKey, MultisigSignature, PublicKey, SecretKey, Signature,
};
use rand::rngs::StdRng;
use rand::SeedableRng;

#[test]
fn public_key() {
let mut rng = StdRng::seed_from_u64(0xbeef);
let pk = PublicKey::from(&SecretKey::random(&mut rng));
let ser = serde_json::to_string(&pk);
let deser = serde_json::from_str(&ser.unwrap());
assert_eq!(pk, deser.unwrap());
}

#[test]
fn multisig_public_key() {
let mut rng = StdRng::seed_from_u64(0xbeef);
let pk = MultisigPublicKey::aggregate(&[PublicKey::from(
&SecretKey::random(&mut rng),
)])
.unwrap();
let ser = serde_json::to_string(&pk);
let deser = serde_json::from_str(&ser.unwrap());
assert_eq!(pk, deser.unwrap());
}

#[test]
fn signature() {
let mut rng = StdRng::seed_from_u64(0xbeef);
let sk = SecretKey::random(&mut rng);
let signature = sk.sign(b"a message");
let ser = serde_json::to_string(&signature).unwrap();
let deser = serde_json::from_str(&ser).unwrap();
assert_eq!(signature, deser);
}

#[test]
fn multisig_signature() {
let mut rng = StdRng::seed_from_u64(0xbeef);
let sk = SecretKey::random(&mut rng);
let pk = PublicKey::from(&sk);
let signature = sk.sign_multisig(&pk, b"a message");
let ser = serde_json::to_string(&signature).unwrap();
let deser = serde_json::from_str(&ser).unwrap();
assert_eq!(signature, deser);
}

#[test]
fn secret_key() {
let mut rng = StdRng::seed_from_u64(0xbeef);
let sk = SecretKey::random(&mut rng);
let ser = serde_json::to_string(&sk).unwrap();
let deser = serde_json::from_str(&ser).unwrap();
assert_eq!(sk, deser);
}

#[test]
fn wrong_encoded() {
let wrong_encoded = "wrong-encoded";
let public_key: Result<PublicKey, _> = serde_json::from_str(&wrong_encoded);
assert!(public_key.is_err());

let secret_key: Result<SecretKey, _> = serde_json::from_str(&wrong_encoded);
assert!(secret_key.is_err());

let signature: Result<Signature, _> = serde_json::from_str(&wrong_encoded);
assert!(signature.is_err());

let public_key_double: Result<MultisigPublicKey, _> =
serde_json::from_str(&wrong_encoded);
assert!(public_key_double.is_err());

let signature_double: Result<MultisigSignature, _> =
serde_json::from_str(&wrong_encoded);
assert!(signature_double.is_err());
}

#[test]
fn too_long_encoded() {
let length_33_enc = "\"yaujE5CNg7SRYuf3Vw7G8QQdM7267QxJtfqGUEjLbxyCC\"";
let length_49_enc= "\"RCR6kPYZDuew8ovT9MoxVv7mKRsbygumf2UTjvzs6AJhnukLj3BiFvjaE45Q41tKqdA\"";
let length_97_enc = "\"7a5RpCdtr1aaXvaR3AofnEnVRh7kpzyqE8eYJpCBVLKLLpXVeN9UrXGRTZyq2upTVaJT5QnPQwZCGXW1oxrEAzrPvQ4vbWFwiHMJijZMzrPsTjQJFju1H4shrajuqUG4fYFpC\"";

let public_key: Result<PublicKey, _> = serde_json::from_str(&length_97_enc);
assert!(public_key.is_err());

let secret_key: Result<SecretKey, _> = serde_json::from_str(&length_33_enc);
assert!(secret_key.is_err());

let signature: Result<Signature, _> = serde_json::from_str(&length_49_enc);
assert!(signature.is_err());

let multisig_public_key: Result<MultisigPublicKey, _> =
serde_json::from_str(&length_97_enc);
assert!(multisig_public_key.is_err());

let multisig_signature: Result<MultisigSignature, _> =
serde_json::from_str(&length_49_enc);
assert!(multisig_signature.is_err());
}

#[test]
fn too_short_encoded() {
let length_31_enc = "\"3uTp29S3e2HQBekFYvVwsmoeEzk4uVWwQUjvJPwWKwU\"";
let length_47_enc =
"\"2F3DDEDEuxrszs3JfzFq51tnGNm3ZtrHwa7sAA4pkeo1JkqGTEYudnBZLNAkCohAd\"";
let length_95_enc = "\"LZXkPWnz5xKxYnyDRZyJvL9vF44oQynzozqRBcpgWA3yZicbaxNeKKJrAMv3eXBbyEvk24mgz9Kg9tck5yEW6k16chN4hDWYUr5gDb9PJJ3YmUqcjG8yPaAuz3cNCE8dHv\"";

let public_key: Result<PublicKey, _> = serde_json::from_str(&length_95_enc);
assert!(public_key.is_err());

let secret_key: Result<SecretKey, _> = serde_json::from_str(&length_31_enc);
assert!(secret_key.is_err());

let signature: Result<Signature, _> = serde_json::from_str(&length_47_enc);
assert!(signature.is_err());

let multisig_public_key: Result<MultisigPublicKey, _> =
serde_json::from_str(&length_95_enc);
assert!(multisig_public_key.is_err());

let multisig_signature: Result<MultisigSignature, _> =
serde_json::from_str(&length_47_enc);
assert!(multisig_signature.is_err());
}