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-10-06 00:53:18 +02:00
|
|
|
let scalar_len = <G as Group>::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-10-06 00:53:18 +02:00
|
|
|
let scalar_len = <G as Group>::ScalarLen::USIZE;
|
|
|
|
|
let elem_len = <G as Group>::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-10-06 00:53:18 +02:00
|
|
|
let scalar_len = <G as Group>::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-10-06 00:53:18 +02:00
|
|
|
let scalar_len = <G as Group>::ScalarLen::USIZE;
|
|
|
|
|
let elem_len = <G as Group>::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-10-06 00:53:18 +02:00
|
|
|
let scalar_len = <G as Group>::ScalarLen::USIZE;
|
2021-10-14 13:55:43 -07:00
|
|
|
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-10-14 13:55:43 -07:00
|
|
|
let elem_len = <G as Group>::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-10-14 13:55:43 -07:00
|
|
|
let elem_len = <G as Group>::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
|
|
|
}
|
|
|
|
|
}
|