|
|
@ -1,74 +1,13 @@ |
|
|
|
use std::borrow::Borrow; |
|
|
|
/// Serialization and deserialization of a group element's compressed representation.
|
|
|
|
use std::marker::PhantomData; |
|
|
|
pub mod projective { |
|
|
|
|
|
|
|
use pairing::{CurveAffine, CurveProjective, EncodedPoint}; |
|
|
|
use pairing::{CurveAffine, CurveProjective, EncodedPoint, Engine}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use super::{DecryptionShare, PublicKey, Signature}; |
|
|
|
|
|
|
|
use serde::de::Error as DeserializeError; |
|
|
|
use serde::de::Error as DeserializeError; |
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
|
|
|
|
|
|
|
|
|
|
|
const ERR_LEN: &str = "wrong length of deserialized group element"; |
|
|
|
const ERR_LEN: &str = "wrong length of deserialized group element"; |
|
|
|
const ERR_CODE: &str = "deserialized bytes don't encode a 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.
|
|
|
|
pub fn serialize<S, C>(c: &C, s: S) -> Result<S::Ok, S::Error> |
|
|
|
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> { |
|
|
|
|
|
|
|
serialize_projective(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(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 |
|
|
|
where |
|
|
|
S: Serializer, |
|
|
|
S: Serializer, |
|
|
|
C: CurveProjective, |
|
|
|
C: CurveProjective, |
|
|
@ -76,8 +15,7 @@ where |
|
|
|
c.into_affine().into_compressed().as_ref().serialize(s) |
|
|
|
c.into_affine().into_compressed().as_ref().serialize(s) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Deserializes the compressed representation of a group element.
|
|
|
|
pub fn deserialize<'de, D, C>(d: D) -> Result<C, D::Error> |
|
|
|
fn deserialize_projective<'de, D, C>(d: D) -> Result<C, D::Error> |
|
|
|
|
|
|
|
where |
|
|
|
where |
|
|
|
D: Deserializer<'de>, |
|
|
|
D: Deserializer<'de>, |
|
|
|
C: CurveProjective, |
|
|
|
C: CurveProjective, |
|
|
@ -91,14 +29,39 @@ where |
|
|
|
let to_err = |_| D::Error::custom(ERR_CODE); |
|
|
|
let to_err = |_| D::Error::custom(ERR_CODE); |
|
|
|
Ok(compressed.into_affine().map_err(to_err)?.into_projective()) |
|
|
|
Ok(compressed.into_affine().map_err(to_err)?.into_projective()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Serialization and deserialization of vectors of projective curve elements.
|
|
|
|
/// Serialization and deserialization of vectors of projective curve elements.
|
|
|
|
pub mod projective_vec { |
|
|
|
pub mod projective_vec { |
|
|
|
use super::CurveWrap; |
|
|
|
use std::borrow::Borrow; |
|
|
|
|
|
|
|
use std::marker::PhantomData; |
|
|
|
|
|
|
|
|
|
|
|
use pairing::CurveProjective; |
|
|
|
use pairing::CurveProjective; |
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
|
|
|
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> |
|
|
|
pub fn serialize<S, C>(vec: &[C], s: S) -> Result<S::Ok, S::Error> |
|
|
|
where |
|
|
|
where |
|
|
|
S: Serializer, |
|
|
|
S: Serializer, |
|
|
|