mirror of https://github.com/poanetwork/hbbft.git
Simplify serde implementations, add Ciphertext.
This commit is contained in:
parent
83f8d61402
commit
6484242afc
|
@ -4,8 +4,6 @@ pub mod poly;
|
|||
pub mod protobuf_impl;
|
||||
mod serde_impl;
|
||||
|
||||
use self::poly::{Commitment, Poly};
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
|
@ -15,6 +13,7 @@ use rand::{ChaChaRng, OsRng, Rng, SeedableRng};
|
|||
use ring::digest;
|
||||
|
||||
use self::error::{ErrorKind, Result};
|
||||
use self::poly::{Commitment, Poly};
|
||||
use fmt::HexBytes;
|
||||
|
||||
/// The number of words (`u32`) in a ChaCha RNG seed.
|
||||
|
@ -23,8 +22,8 @@ const CHACHA_RNG_SEED_SIZE: usize = 8;
|
|||
const ERR_OS_RNG: &str = "could not initialize the OS random number generator";
|
||||
|
||||
/// A public key, or a public key share.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PublicKey<E: Engine>(E::G1);
|
||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||
pub struct PublicKey<E: Engine>(#[serde(with = "serde_impl::projective")] E::G1);
|
||||
|
||||
impl<E: Engine> PartialEq for PublicKey<E> {
|
||||
fn eq(&self, other: &PublicKey<E>) -> bool {
|
||||
|
@ -69,8 +68,8 @@ impl<E: Engine> PublicKey<E> {
|
|||
}
|
||||
|
||||
/// A signature, or a signature share.
|
||||
#[derive(Clone)]
|
||||
pub struct Signature<E: Engine>(E::G2);
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
pub struct Signature<E: Engine>(#[serde(with = "serde_impl::projective")] E::G2);
|
||||
|
||||
impl<E: Engine> fmt::Debug for Signature<E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
@ -148,8 +147,12 @@ impl<E: Engine> SecretKey<E> {
|
|||
}
|
||||
|
||||
/// An encrypted message.
|
||||
#[derive(Debug)]
|
||||
pub struct Ciphertext<E: Engine>(E::G1, Vec<u8>, E::G2);
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Ciphertext<E: Engine>(
|
||||
#[serde(with = "serde_impl::projective")] E::G1,
|
||||
Vec<u8>,
|
||||
#[serde(with = "serde_impl::projective")] E::G2,
|
||||
);
|
||||
|
||||
impl<E: Engine> PartialEq for Ciphertext<E> {
|
||||
fn eq(&self, other: &Ciphertext<E>) -> bool {
|
||||
|
@ -168,8 +171,8 @@ impl<E: Engine> Ciphertext<E> {
|
|||
}
|
||||
|
||||
/// A decryption share. A threshold of decryption shares can be used to decrypt a message.
|
||||
#[derive(Debug)]
|
||||
pub struct DecryptionShare<E: Engine>(E::G1);
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct DecryptionShare<E: Engine>(#[serde(with = "serde_impl::projective")] E::G1);
|
||||
|
||||
impl<E: Engine> PartialEq for DecryptionShare<E> {
|
||||
fn eq(&self, other: &DecryptionShare<E>) -> bool {
|
||||
|
|
|
@ -1,104 +1,67 @@
|
|||
use std::borrow::Borrow;
|
||||
use std::marker::PhantomData;
|
||||
/// Serialization and deserialization of a group element's compressed representation.
|
||||
pub mod projective {
|
||||
use pairing::{CurveAffine, CurveProjective, EncodedPoint};
|
||||
use serde::de::Error as DeserializeError;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use pairing::{CurveAffine, CurveProjective, EncodedPoint, Engine};
|
||||
const ERR_LEN: &str = "wrong length of deserialized group element";
|
||||
const ERR_CODE: &str = "deserialized bytes don't encode a group element";
|
||||
|
||||
use super::{DecryptionShare, PublicKey, Signature};
|
||||
use serde::de::Error as DeserializeError;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
const ERR_LEN: &str = "wrong length of deserialized group element";
|
||||
const ERR_CODE: &str = "deserialized bytes don't encode a group element";
|
||||
|
||||
/// A wrapper type to facilitate serialization and deserialization of group elements.
|
||||
struct CurveWrap<C, B>(B, PhantomData<C>);
|
||||
|
||||
impl<C, B> CurveWrap<C, B> {
|
||||
fn new(c: B) -> Self {
|
||||
CurveWrap(c, PhantomData)
|
||||
pub fn serialize<S, C>(c: &C, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
C: CurveProjective,
|
||||
{
|
||||
c.into_affine().into_compressed().as_ref().serialize(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: CurveProjective, B: Borrow<C>> Serialize for CurveWrap<C, B> {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_projective(self.0.borrow(), s)
|
||||
pub fn deserialize<'de, D, C>(d: D) -> Result<C, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
C: CurveProjective,
|
||||
{
|
||||
let bytes = <Vec<u8>>::deserialize(d)?;
|
||||
if bytes.len() != <C::Affine as CurveAffine>::Compressed::size() {
|
||||
return Err(D::Error::custom(ERR_LEN));
|
||||
}
|
||||
let mut compressed = <C::Affine as CurveAffine>::Compressed::empty();
|
||||
compressed.as_mut().copy_from_slice(&bytes);
|
||||
let to_err = |_| D::Error::custom(ERR_CODE);
|
||||
Ok(compressed.into_affine().map_err(to_err)?.into_projective())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, C: CurveProjective> Deserialize<'de> for CurveWrap<C, C> {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
Ok(CurveWrap::new(deserialize_projective(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> Serialize for PublicKey<E> {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_projective(&self.0, s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E: Engine> Deserialize<'de> for PublicKey<E> {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
Ok(PublicKey(deserialize_projective(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> Serialize for Signature<E> {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_projective(&self.0, s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E: Engine> Deserialize<'de> for Signature<E> {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
Ok(Signature(deserialize_projective(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> Serialize for DecryptionShare<E> {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
serialize_projective(&self.0, s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, E: Engine> Deserialize<'de> for DecryptionShare<E> {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
Ok(DecryptionShare(deserialize_projective(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializes the compressed representation of a group element.
|
||||
fn serialize_projective<S, C>(c: &C, s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
C: CurveProjective,
|
||||
{
|
||||
c.into_affine().into_compressed().as_ref().serialize(s)
|
||||
}
|
||||
|
||||
/// Deserializes the compressed representation of a group element.
|
||||
fn deserialize_projective<'de, D, C>(d: D) -> Result<C, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
C: CurveProjective,
|
||||
{
|
||||
let bytes = <Vec<u8>>::deserialize(d)?;
|
||||
if bytes.len() != <C::Affine as CurveAffine>::Compressed::size() {
|
||||
return Err(D::Error::custom(ERR_LEN));
|
||||
}
|
||||
let mut compressed = <C::Affine as CurveAffine>::Compressed::empty();
|
||||
compressed.as_mut().copy_from_slice(&bytes);
|
||||
let to_err = |_| D::Error::custom(ERR_CODE);
|
||||
Ok(compressed.into_affine().map_err(to_err)?.into_projective())
|
||||
}
|
||||
|
||||
/// Serialization and deserialization of vectors of projective curve elements.
|
||||
pub mod projective_vec {
|
||||
use super::CurveWrap;
|
||||
use std::borrow::Borrow;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use pairing::CurveProjective;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use super::projective;
|
||||
|
||||
/// A wrapper type to facilitate serialization and deserialization of group elements.
|
||||
struct CurveWrap<C, B>(B, PhantomData<C>);
|
||||
|
||||
impl<C, B> CurveWrap<C, B> {
|
||||
fn new(c: B) -> Self {
|
||||
CurveWrap(c, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: CurveProjective, B: Borrow<C>> Serialize for CurveWrap<C, B> {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
projective::serialize(self.0.borrow(), s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, C: CurveProjective> Deserialize<'de> for CurveWrap<C, C> {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
Ok(CurveWrap::new(projective::deserialize(d)?))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialize<S, C>(vec: &[C], s: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
|
|
Loading…
Reference in New Issue