Skip to content

Commit

Permalink
Unify & optimise serialization (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Varlakov authored Sep 15, 2021
1 parent 6126e62 commit 5ee0400
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/arithmetic/big_gmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::{fmt, ops, ptr};
use gmp::mpz::Mpz;
use gmp::sign::Sign;
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;

use super::errors::*;
Expand All @@ -35,8 +34,7 @@ type BN = Mpz;
/// very limited API that allows easily switching between implementations.
///
/// Set of traits implemented on BigInt remains the same regardless of underlying implementation.
#[derive(PartialOrd, PartialEq, Ord, Eq, Clone, Serialize, Deserialize)]
#[serde(transparent)]
#[derive(PartialOrd, PartialEq, Ord, Eq, Clone)]
pub struct BigInt {
gmp: Mpz,
}
Expand Down
4 changes: 1 addition & 3 deletions src/arithmetic/big_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::convert::{TryFrom, TryInto};
use std::{fmt, ops};

use num_traits::Signed;
use serde::{Deserialize, Serialize};

use super::errors::*;
use super::traits::*;
Expand All @@ -19,8 +18,7 @@ mod ring_algorithms;
/// very limited API that allows easily switching between implementations.
///
/// Set of traits implemented on BigInt remains the same regardless of underlying implementation.
#[derive(PartialOrd, PartialEq, Ord, Eq, Clone, Serialize, Deserialize)]
#[serde(transparent)]
#[derive(PartialOrd, PartialEq, Ord, Eq, Clone)]
pub struct BigInt {
num: BN,
}
Expand Down
12 changes: 12 additions & 0 deletions src/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
mod errors;
mod macros;
mod samplable;
mod serde_support;
pub mod traits;

#[cfg(not(any(feature = "rust-gmp-kzen", feature = "num-bigint")))]
Expand All @@ -31,6 +32,7 @@ pub use big_gmp::BigInt;

#[cfg(feature = "num-bigint")]
mod big_native;

#[cfg(feature = "num-bigint")]
pub use big_native::BigInt;

Expand All @@ -45,6 +47,16 @@ mod test {

use super::*;

#[test]
fn serde() {
use serde_test::{assert_tokens, Token::*};
for bigint in [BigInt::zero(), BigInt::sample(1024)] {
let bytes = bigint.to_bytes();
let tokens = vec![Bytes(bytes.leak())];
assert_tokens(&bigint, &tokens)
}
}

#[test]
fn serializing_to_hex() {
let n = BigInt::from(1_000_000_u32);
Expand Down
43 changes: 43 additions & 0 deletions src/arithmetic/serde_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::fmt;

use serde::de::Visitor;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::traits::Converter;
use super::BigInt;

impl Serialize for BigInt {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = self.to_bytes();
serializer.serialize_bytes(&bytes)
}
}

impl<'de> Deserialize<'de> for BigInt {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct BigintVisitor;

impl<'de> Visitor<'de> for BigintVisitor {
type Value = BigInt;

fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "bigint")
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(BigInt::from_bytes(v))
}
}

deserializer.deserialize_bytes(BigintVisitor)
}
}

0 comments on commit 5ee0400

Please sign in to comment.