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-23 01:17:03 +01:00
|
|
|
use crate::errors::InternalError;
|
|
|
|
|
use crate::group::Group;
|
2021-12-23 21:03:38 +01:00
|
|
|
use crate::util::deserialize;
|
2021-12-23 01:17:03 +01:00
|
|
|
use crate::voprf::{
|
|
|
|
|
BlindedElement, EvaluationElement, NonVerifiableClient, NonVerifiableServer, Proof,
|
|
|
|
|
VerifiableClient, VerifiableServer,
|
|
|
|
|
};
|
|
|
|
|
|
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> {
|
|
|
|
|
G::scalar_as_bytes(self.blind)
|
2021-09-15 17:49:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deserialization from bytes
|
|
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
|
2021-12-23 21:03:38 +01:00
|
|
|
let mut input = input.iter().copied();
|
2021-09-15 17:49:31 -07:00
|
|
|
|
2021-12-23 21:03:38 +01:00
|
|
|
let blind = G::from_scalar_slice(&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>,
|
|
|
|
|
{
|
|
|
|
|
G::scalar_as_bytes(self.blind).concat(self.blinded_element.to_arr())
|
2021-09-15 17:49:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deserialization from bytes
|
|
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
|
2021-12-23 21:03:38 +01:00
|
|
|
let mut input = input.iter().copied();
|
2021-09-15 17:49:31 -07:00
|
|
|
|
2021-12-23 21:03:38 +01:00
|
|
|
let blind = G::from_scalar_slice(&deserialize(&mut input)?)?;
|
|
|
|
|
let blinded_element = G::from_element_slice(&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> {
|
|
|
|
|
G::scalar_as_bytes(self.sk)
|
2021-09-15 17:49:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deserialization from bytes
|
|
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
|
2021-12-23 21:03:38 +01:00
|
|
|
let mut input = input.iter().copied();
|
2021-09-15 17:49:31 -07:00
|
|
|
|
2021-12-23 21:03:38 +01:00
|
|
|
let sk = G::from_scalar_slice(&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>,
|
|
|
|
|
{
|
|
|
|
|
G::scalar_as_bytes(self.sk).concat(self.pk.to_arr())
|
2021-09-15 17:49:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deserialization from bytes
|
|
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
|
2021-12-23 21:03:38 +01:00
|
|
|
let mut input = input.iter().copied();
|
2021-09-15 17:49:31 -07:00
|
|
|
|
2021-12-23 21:03:38 +01:00
|
|
|
let sk = G::from_scalar_slice(&deserialize(&mut input)?)?;
|
|
|
|
|
let pk = G::from_element_slice(&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>,
|
|
|
|
|
{
|
|
|
|
|
G::scalar_as_bytes(self.c_scalar).concat(G::scalar_as_bytes(self.s_scalar))
|
2021-09-15 17:49:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deserialization from bytes
|
|
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
|
2021-12-23 21:03:38 +01:00
|
|
|
let mut input = input.iter().copied();
|
|
|
|
|
|
|
|
|
|
let c_scalar = G::from_scalar_slice(&deserialize(&mut input)?)?;
|
|
|
|
|
let s_scalar = G::from_scalar_slice(&deserialize(&mut input)?)?;
|
|
|
|
|
|
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> {
|
|
|
|
|
self.value.to_arr()
|
2021-09-15 17:49:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deserialization from bytes
|
|
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
|
2021-12-23 21:03:38 +01:00
|
|
|
let mut input = input.iter().copied();
|
|
|
|
|
|
|
|
|
|
let value = G::from_element_slice(&deserialize(&mut input)?)?;
|
|
|
|
|
|
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> {
|
|
|
|
|
self.value.to_arr()
|
2021-09-15 17:49:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deserialization from bytes
|
|
|
|
|
pub fn deserialize(input: &[u8]) -> Result<Self, InternalError> {
|
2021-12-23 21:03:38 +01:00
|
|
|
let mut input = input.iter().copied();
|
|
|
|
|
|
|
|
|
|
let value = G::from_element_slice(&deserialize(&mut input)?)?;
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|