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-11 02:51:12 +02:00
|
|
|
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
|
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-09-15 17:49:31 -07:00
|
|
|
if input.len() < scalar_len + scalar_len {
|
|
|
|
|
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-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-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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
|
// Helper Functions //
|
|
|
|
|
// ================ //
|
|
|
|
|
//////////////////////
|
2021-09-09 01:56:54 -07:00
|
|
|
|
|
|
|
|
// Corresponds to the I2OSP() function from RFC8017
|
2021-10-11 02:51:12 +02:00
|
|
|
pub(crate) fn i2osp<L: ArrayLength<u8>>(
|
|
|
|
|
input: usize,
|
|
|
|
|
) -> Result<GenericArray<u8, L>, InternalError> {
|
|
|
|
|
const SIZEOF_USIZE: usize = core::mem::size_of::<usize>();
|
2021-09-09 01:56:54 -07:00
|
|
|
|
|
|
|
|
// Check if input >= 256^length
|
2021-10-11 02:51:12 +02:00
|
|
|
if (SIZEOF_USIZE as u32 - input.leading_zeros() / 8) > L::U32 {
|
2021-09-09 01:56:54 -07:00
|
|
|
return Err(InternalError::SerializationError);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-11 02:51:12 +02:00
|
|
|
if L::USIZE <= SIZEOF_USIZE {
|
|
|
|
|
return Ok(GenericArray::clone_from_slice(
|
|
|
|
|
&input.to_be_bytes()[SIZEOF_USIZE - L::USIZE..],
|
|
|
|
|
));
|
2021-09-09 01:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-11 02:51:12 +02:00
|
|
|
let mut output = GenericArray::default();
|
|
|
|
|
output[L::USIZE - SIZEOF_USIZE..L::USIZE].copy_from_slice(&input.to_be_bytes());
|
2021-09-09 01:56:54 -07:00
|
|
|
Ok(output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Computes I2OSP(len(input), max_bytes) || input
|
2021-10-11 02:51:12 +02:00
|
|
|
pub(crate) fn serialize<L: ArrayLength<u8>>(input: &[u8]) -> Result<Vec<u8>, InternalError> {
|
|
|
|
|
Ok([&i2osp::<L>(input.len())?, input].concat())
|
2021-09-09 01:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod unit_tests {
|
|
|
|
|
use super::*;
|
2021-10-11 02:51:12 +02:00
|
|
|
use generic_array::typenum::{U1, U2};
|
2021-09-09 01:56:54 -07:00
|
|
|
|
|
|
|
|
// Test the error condition for I2OSP
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_i2osp_err_check() {
|
2021-10-11 02:51:12 +02:00
|
|
|
assert!(i2osp::<U1>(0).is_ok());
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2021-10-11 02:51:12 +02:00
|
|
|
assert!(i2osp::<U1>(255).is_ok());
|
|
|
|
|
assert!(i2osp::<U1>(256).is_err());
|
|
|
|
|
assert!(i2osp::<U1>(257).is_err());
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2021-10-11 02:51:12 +02:00
|
|
|
assert!(i2osp::<U2>(256 * 256 - 1).is_ok());
|
|
|
|
|
assert!(i2osp::<U2>(256 * 256).is_err());
|
|
|
|
|
assert!(i2osp::<U2>(256 * 256 + 1).is_err());
|
2021-09-09 01:56:54 -07:00
|
|
|
}
|
|
|
|
|
}
|