Files
voprf-vx/src/serialization.rs
T

258 lines
8.1 KiB
Rust
Raw Normal View History

2021-09-09 01:56:54 -07:00
// Copyright (c) Facebook, Inc. and its affiliates.
//
2021-09-27 18:53:06 -07:00
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree and the Apache
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
// of this source tree.
2021-09-09 01:56:54 -07:00
2021-09-15 17:49:31 -07:00
//! Handles the serialization of each of the components used
//! in the VOPRF protocol
use crate::{
errors::InternalError,
group::Group,
voprf::{
BlindedElement, EvaluationElement, NonVerifiableClient, NonVerifiableServer, Proof,
VerifiableClient, VerifiableServer,
},
};
2021-09-09 01:56:54 -07:00
use alloc::vec::Vec;
2021-10-06 00:53:18 +02:00
use core::marker::PhantomData;
use digest::{BlockInput, Digest};
2021-10-16 01:56:21 +02:00
use generic_array::typenum::Unsigned;
2021-09-15 17:49:31 -07:00
//////////////////////////////////////////////////////////
// Serialization and Deserialization for High-Level API //
// ==================================================== //
//////////////////////////////////////////////////////////
2021-10-06 00:53:18 +02:00
impl<G: Group, H: BlockInput + Digest> NonVerifiableClient<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
2021-10-11 02:51:12 +02:00
[G::scalar_as_bytes(self.blind).as_slice(), &self.data].concat()
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
2021-12-21 20:17:02 +01:00
let scalar_len = G::ScalarLen::USIZE;
2021-09-15 17:49:31 -07:00
if input.len() < scalar_len {
return Err(InternalError::SizeError);
}
2021-10-11 02:51:12 +02:00
let blind = G::from_scalar_slice(&input[..scalar_len])?;
2021-09-15 17:49:31 -07:00
let data = input[scalar_len..].to_vec();
2021-10-06 00:53:18 +02:00
Ok(Self {
blind,
data,
hash: PhantomData,
})
2021-09-15 17:49:31 -07:00
}
}
2021-10-06 00:53:18 +02:00
impl<G: Group, H: BlockInput + Digest> VerifiableClient<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
[
2021-10-11 02:51:12 +02:00
G::scalar_as_bytes(self.blind).as_slice(),
&self.blinded_element.to_arr(),
&self.data,
2021-09-15 17:49:31 -07:00
]
.concat()
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
2021-12-21 20:17:02 +01:00
let scalar_len = G::ScalarLen::USIZE;
let elem_len = G::ElemLen::USIZE;
2021-09-15 17:49:31 -07:00
if input.len() < scalar_len + elem_len {
return Err(InternalError::SizeError);
}
2021-10-11 02:51:12 +02:00
let blind = G::from_scalar_slice(&input[..scalar_len])?;
let blinded_element = G::from_element_slice(&input[scalar_len..scalar_len + elem_len])?;
2021-09-15 17:49:31 -07:00
let data = input[scalar_len + elem_len..].to_vec();
Ok(Self {
blind,
blinded_element,
data,
2021-10-06 00:53:18 +02:00
hash: PhantomData,
2021-09-15 17:49:31 -07:00
})
}
}
2021-10-06 00:53:18 +02:00
impl<G: Group, H: BlockInput + Digest> NonVerifiableServer<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
2021-10-06 00:53:18 +02:00
G::scalar_as_bytes(self.sk).to_vec()
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
2021-12-21 20:17:02 +01:00
let scalar_len = G::ScalarLen::USIZE;
2021-09-15 17:49:31 -07:00
if input.len() != scalar_len {
return Err(InternalError::SizeError);
}
2021-10-11 02:51:12 +02:00
let sk = G::from_scalar_slice(input)?;
2021-09-15 17:49:31 -07:00
2021-10-06 00:53:18 +02:00
Ok(Self {
sk,
hash: PhantomData,
})
2021-09-15 17:49:31 -07:00
}
}
2021-10-06 00:53:18 +02:00
impl<G: Group, H: BlockInput + Digest> VerifiableServer<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
2021-10-11 02:51:12 +02:00
[G::scalar_as_bytes(self.sk).as_slice(), &self.pk.to_arr()].concat()
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
2021-12-21 20:17:02 +01:00
let scalar_len = G::ScalarLen::USIZE;
let elem_len = G::ElemLen::USIZE;
2021-09-15 17:49:31 -07:00
if input.len() != scalar_len + elem_len {
return Err(InternalError::SizeError);
}
2021-10-11 02:51:12 +02:00
let sk = G::from_scalar_slice(&input[..scalar_len])?;
let pk = G::from_element_slice(&input[scalar_len..])?;
2021-09-15 17:49:31 -07:00
2021-10-06 00:53:18 +02:00
Ok(Self {
sk,
pk,
hash: PhantomData,
})
2021-09-15 17:49:31 -07:00
}
}
2021-10-06 00:53:18 +02:00
impl<G: Group, H: BlockInput + Digest> Proof<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
[
2021-10-06 00:53:18 +02:00
G::scalar_as_bytes(self.c_scalar),
G::scalar_as_bytes(self.s_scalar),
2021-09-15 17:49:31 -07:00
]
.concat()
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
2021-12-21 20:17:02 +01:00
let scalar_len = G::ScalarLen::USIZE;
if input.len() != scalar_len + scalar_len {
2021-09-15 17:49:31 -07:00
return Err(InternalError::SizeError);
}
Ok(Proof {
2021-10-11 02:51:12 +02:00
c_scalar: G::from_scalar_slice(&input[..scalar_len])?,
s_scalar: G::from_scalar_slice(&input[scalar_len..])?,
2021-10-06 00:53:18 +02:00
hash: PhantomData,
2021-09-15 17:49:31 -07:00
})
}
}
2021-10-06 00:53:18 +02:00
impl<G: Group, H: BlockInput + Digest> BlindedElement<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
2021-09-28 19:44:57 -07:00
self.value.to_arr().to_vec()
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
2021-12-21 20:17:02 +01:00
let elem_len = G::ElemLen::USIZE;
if input.len() != elem_len {
return Err(InternalError::SizeError);
}
2021-09-28 19:44:57 -07:00
Ok(Self {
2021-10-11 02:51:12 +02:00
value: G::from_element_slice(input)?,
2021-10-06 00:53:18 +02:00
hash: PhantomData,
2021-09-28 19:44:57 -07:00
})
2021-09-15 17:49:31 -07:00
}
}
2021-10-06 00:53:18 +02:00
impl<G: Group, H: BlockInput + Digest> EvaluationElement<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
2021-09-28 19:44:57 -07:00
self.value.to_arr().to_vec()
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
2021-12-21 20:17:02 +01:00
let elem_len = G::ElemLen::USIZE;
if input.len() != elem_len {
return Err(InternalError::SizeError);
}
2021-09-28 19:44:57 -07:00
Ok(Self {
2021-10-11 02:51:12 +02:00
value: G::from_element_slice(input)?,
2021-10-06 00:53:18 +02:00
hash: PhantomData,
2021-09-28 19:44:57 -07:00
})
2021-09-15 17:49:31 -07:00
}
}
2021-12-21 20:17:02 +01:00
/////////////////////////////////////////////
// Serde implementation for High-Level API //
// ======================================= //
/////////////////////////////////////////////
/// Macro used for deriving `serde`'s `Serialize` and `Deserialize` traits.
macro_rules! impl_serialize_and_deserialize_for {
($item:ident) => {
#[cfg(feature = "serde")]
impl<G: Group, H: BlockInput + Digest> serde::Serialize for $item<G, H> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(&self.serialize())
}
}
#[cfg(feature = "serde")]
impl<'de, G: Group, H: BlockInput + Digest> serde::Deserialize<'de> for $item<G, H> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::Error;
struct ByteVisitor<G: Group, H: BlockInput + Digest>(core::marker::PhantomData<(G, H)>);
impl<'de, G: Group, H: BlockInput + Digest> serde::de::Visitor<'de> for ByteVisitor<G, H> {
type Value = $item<G, H>;
fn expecting(
&self,
formatter: &mut core::fmt::Formatter,
) -> core::fmt::Result {
formatter.write_str(core::concat!(
"the byte representation of a ",
core::stringify!($item)
))
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
$item::<G, H>::deserialize(value).map_err(|_| {
Error::invalid_value(
serde::de::Unexpected::Bytes(value),
&core::concat!(
"invalid byte sequence for ",
core::stringify!($item)
),
)
})
}
}
deserializer
.deserialize_bytes(ByteVisitor::<G, H>(core::marker::PhantomData))
.map_err(Error::custom)
}
}
};
}