Files
voprf-vx/src/serialization.rs
T

187 lines
5.6 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-12-23 01:17:03 +01:00
//! Handles the serialization of each of the components used in the VOPRF
//! protocol
2021-09-15 17:49:31 -07:00
2021-10-06 00:53:18 +02:00
use core::marker::PhantomData;
2021-12-23 21:03:38 +01:00
use core::ops::Add;
2021-12-23 01:17:03 +01:00
2021-12-23 21:58:00 +01:00
use digest::core_api::BlockSizeUser;
use digest::{Digest, FixedOutputReset};
2021-12-23 21:03:38 +01:00
use generic_array::sequence::Concat;
use generic_array::typenum::Sum;
use generic_array::{ArrayLength, GenericArray};
2021-09-15 17:49:31 -07:00
2021-12-25 22:54:27 +01:00
use crate::{
BlindedElement, Error, EvaluationElement, Group, NonVerifiableClient, NonVerifiableServer,
Proof, Result, VerifiableClient, VerifiableServer,
2021-12-23 01:17:03 +01:00
};
2021-09-15 17:49:31 -07:00
//////////////////////////////////////////////////////////
// Serialization and Deserialization for High-Level API //
// ==================================================== //
//////////////////////////////////////////////////////////
2021-12-23 21:58:00 +01:00
impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> NonVerifiableClient<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2021-12-23 21:03:38 +01:00
pub fn serialize(&self) -> GenericArray<u8, G::ScalarLen> {
2022-01-18 12:34:28 +01:00
G::serialize_scalar(self.blind)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2021-12-25 22:54:27 +01:00
pub fn deserialize(input: &[u8]) -> Result<Self> {
2021-12-23 21:03:38 +01:00
let mut input = input.iter().copied();
2021-09-15 17:49:31 -07:00
2022-01-18 12:34:28 +01:00
let blind = G::deserialize_scalar(&deserialize(&mut input)?)?;
2021-09-15 17:49:31 -07:00
2021-10-06 00:53:18 +02:00
Ok(Self {
blind,
hash: PhantomData,
})
2021-09-15 17:49:31 -07:00
}
}
2021-12-23 21:58:00 +01:00
impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> VerifiableClient<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2021-12-23 21:03:38 +01:00
pub fn serialize(&self) -> GenericArray<u8, Sum<G::ScalarLen, G::ElemLen>>
where
G::ScalarLen: Add<G::ElemLen>,
Sum<G::ScalarLen, G::ElemLen>: ArrayLength<u8>,
{
2022-01-18 12:34:28 +01:00
G::serialize_scalar(self.blind).concat(G::serialize_elem(self.blinded_element))
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2021-12-25 22:54:27 +01:00
pub fn deserialize(input: &[u8]) -> Result<Self> {
2021-12-23 21:03:38 +01:00
let mut input = input.iter().copied();
2021-09-15 17:49:31 -07:00
2022-01-18 12:34:28 +01:00
let blind = G::deserialize_scalar(&deserialize(&mut input)?)?;
let blinded_element = G::deserialize_elem(&deserialize(&mut input)?)?;
2021-09-15 17:49:31 -07:00
Ok(Self {
blind,
blinded_element,
2021-10-06 00:53:18 +02:00
hash: PhantomData,
2021-09-15 17:49:31 -07:00
})
}
}
2021-12-23 21:58:00 +01:00
impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> NonVerifiableServer<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2021-12-23 21:03:38 +01:00
pub fn serialize(&self) -> GenericArray<u8, G::ScalarLen> {
2022-01-18 12:34:28 +01:00
G::serialize_scalar(self.sk)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2021-12-25 22:54:27 +01:00
pub fn deserialize(input: &[u8]) -> Result<Self> {
2021-12-23 21:03:38 +01:00
let mut input = input.iter().copied();
2021-09-15 17:49:31 -07:00
2022-01-18 12:34:28 +01:00
let sk = G::deserialize_scalar(&deserialize(&mut 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-12-23 21:58:00 +01:00
impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> VerifiableServer<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2021-12-23 21:03:38 +01:00
pub fn serialize(&self) -> GenericArray<u8, Sum<G::ScalarLen, G::ElemLen>>
where
G::ScalarLen: Add<G::ElemLen>,
Sum<G::ScalarLen, G::ElemLen>: ArrayLength<u8>,
{
2022-01-18 12:34:28 +01:00
G::serialize_scalar(self.sk).concat(G::serialize_elem(self.pk))
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2021-12-25 22:54:27 +01:00
pub fn deserialize(input: &[u8]) -> Result<Self> {
2021-12-23 21:03:38 +01:00
let mut input = input.iter().copied();
2021-09-15 17:49:31 -07:00
2022-01-18 12:34:28 +01:00
let sk = G::deserialize_scalar(&deserialize(&mut input)?)?;
let pk = G::deserialize_elem(&deserialize(&mut input)?)?;
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-12-23 21:58:00 +01:00
impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> Proof<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2021-12-23 21:03:38 +01:00
pub fn serialize(&self) -> GenericArray<u8, Sum<G::ScalarLen, G::ScalarLen>>
where
G::ScalarLen: Add<G::ScalarLen>,
Sum<G::ScalarLen, G::ScalarLen>: ArrayLength<u8>,
{
2022-01-18 12:34:28 +01:00
G::serialize_scalar(self.c_scalar).concat(G::serialize_scalar(self.s_scalar))
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2021-12-25 22:54:27 +01:00
pub fn deserialize(input: &[u8]) -> Result<Self> {
2021-12-23 21:03:38 +01:00
let mut input = input.iter().copied();
2022-01-18 12:34:28 +01:00
let c_scalar = G::deserialize_scalar(&deserialize(&mut input)?)?;
let s_scalar = G::deserialize_scalar(&deserialize(&mut input)?)?;
2021-12-23 21:03:38 +01:00
2021-09-15 17:49:31 -07:00
Ok(Proof {
2021-12-23 21:03:38 +01:00
c_scalar,
s_scalar,
2021-10-06 00:53:18 +02:00
hash: PhantomData,
2021-09-15 17:49:31 -07:00
})
}
}
2021-12-23 21:58:00 +01:00
impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> BlindedElement<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2021-12-23 21:03:38 +01:00
pub fn serialize(&self) -> GenericArray<u8, G::ElemLen> {
2022-01-18 12:34:28 +01:00
G::serialize_elem(self.value)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2021-12-25 22:54:27 +01:00
pub fn deserialize(input: &[u8]) -> Result<Self> {
2021-12-23 21:03:38 +01:00
let mut input = input.iter().copied();
2022-01-18 12:34:28 +01:00
let value = G::deserialize_elem(&deserialize(&mut input)?)?;
2021-12-23 21:03:38 +01:00
2021-09-28 19:44:57 -07:00
Ok(Self {
2021-12-23 21:03:38 +01:00
value,
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-23 21:58:00 +01:00
impl<G: Group, H: BlockSizeUser + Digest + FixedOutputReset> EvaluationElement<G, H> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2021-12-23 21:03:38 +01:00
pub fn serialize(&self) -> GenericArray<u8, G::ElemLen> {
2022-01-18 12:34:28 +01:00
G::serialize_elem(self.value)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2021-12-25 22:54:27 +01:00
pub fn deserialize(input: &[u8]) -> Result<Self> {
2021-12-23 21:03:38 +01:00
let mut input = input.iter().copied();
2022-01-18 12:34:28 +01:00
let value = G::deserialize_elem(&deserialize(&mut input)?)?;
2021-12-23 21:03:38 +01:00
2021-09-28 19:44:57 -07:00
Ok(Self {
2021-12-23 21:03:38 +01:00
value,
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-25 22:54:27 +01:00
fn deserialize<L: ArrayLength<u8>>(
input: &mut impl Iterator<Item = u8>,
) -> Result<GenericArray<u8, L>> {
let input = input.by_ref().take(L::USIZE);
GenericArray::from_exact_iter(input).ok_or(Error::SizeError)
}