Files
voprf-vx/src/serialization.rs
T

386 lines
12 KiB
Rust
Raw Normal View History

2023-05-22 23:04:39 -07:00
// Copyright (c) Meta Platforms, Inc. and affiliates.
2021-09-09 01:56:54 -07:00
//
2023-05-22 23:04:39 -07:00
// This source code is dual-licensed under either the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree or the Apache
2021-09-27 18:53:06 -07:00
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
2023-05-22 23:04:39 -07:00
// of this source tree. You may select, at your option, one of the above-listed
// licenses.
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-12-23 21:03:38 +01:00
use generic_array::sequence::Concat;
2025-04-15 22:30:56 +02:00
use generic_array::typenum::{Sum, Unsigned};
use generic_array::GenericArray;
2021-09-15 17:49:31 -07:00
2021-12-25 22:54:27 +01:00
use crate::{
2022-02-13 04:00:11 -08:00
BlindedElement, CipherSuite, Error, EvaluationElement, Group, OprfClient, OprfServer,
PoprfClient, PoprfServer, Proof, Result, VoprfClient, VoprfServer,
2021-12-23 01:17:03 +01:00
};
2021-09-15 17:49:31 -07:00
//////////////////////////////////////////////////////////
// Serialization and Deserialization for High-Level API //
// ==================================================== //
//////////////////////////////////////////////////////////
2022-02-13 04:00:11 -08:00
/// Length of [`OprfClient`] in bytes for serialization.
pub type OprfClientLen<CS> = <<CS as CipherSuite>::Group as Group>::ScalarLen;
2022-01-21 22:52:09 +01:00
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> OprfClient<CS> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2022-02-13 04:00:11 -08:00
pub fn serialize(&self) -> GenericArray<u8, OprfClientLen<CS>> {
2022-01-21 22:52:09 +01:00
CS::Group::serialize_scalar(self.blind)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let blind = deserialize_scalar::<CS::Group>(&mut input)?;
2021-09-15 17:49:31 -07:00
2022-01-21 22:52:09 +01:00
Ok(Self { blind })
2021-09-15 17:49:31 -07:00
}
}
2022-02-13 04:00:11 -08:00
/// Length of [`VoprfClient`] in bytes for serialization.
pub type VoprfClientLen<CS> = Sum<
2022-01-21 22:52:09 +01:00
<<CS as CipherSuite>::Group as Group>::ScalarLen,
<<CS as CipherSuite>::Group as Group>::ElemLen,
>;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> VoprfClient<CS> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2025-04-15 22:30:56 +02:00
pub fn serialize(&self) -> GenericArray<u8, VoprfClientLen<CS>> {
2022-01-21 22:52:09 +01:00
<CS::Group as Group>::serialize_scalar(self.blind)
.concat(<CS::Group as Group>::serialize_elem(self.blinded_element))
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let blind = deserialize_scalar::<CS::Group>(&mut input)?;
let blinded_element = deserialize_elem::<CS::Group>(&mut input)?;
2021-09-15 17:49:31 -07:00
Ok(Self {
blind,
blinded_element,
})
}
}
2022-02-13 04:00:11 -08:00
/// Length of [`PoprfClient`] in bytes for serialization.
pub type PoprfClientLen<CS> = Sum<
<<CS as CipherSuite>::Group as Group>::ScalarLen,
<<CS as CipherSuite>::Group as Group>::ElemLen,
>;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> PoprfClient<CS> {
2022-02-13 04:00:11 -08:00
/// Serialization into bytes
2025-04-15 22:30:56 +02:00
pub fn serialize(&self) -> GenericArray<u8, PoprfClientLen<CS>> {
2022-02-13 04:00:11 -08:00
<CS::Group as Group>::serialize_scalar(self.blind)
.concat(<CS::Group as Group>::serialize_elem(self.blinded_element))
}
/// Deserialization from bytes
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let blind = deserialize_scalar::<CS::Group>(&mut input)?;
let blinded_element = deserialize_elem::<CS::Group>(&mut input)?;
2022-02-13 04:00:11 -08:00
Ok(Self {
blind,
blinded_element,
})
}
}
2022-01-21 22:52:09 +01:00
2022-02-13 04:00:11 -08:00
/// Length of [`OprfServer`] in bytes for serialization.
pub type OprfServerLen<CS> = <<CS as CipherSuite>::Group as Group>::ScalarLen;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> OprfServer<CS> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2022-02-13 04:00:11 -08:00
pub fn serialize(&self) -> GenericArray<u8, OprfServerLen<CS>> {
2022-01-21 22:52:09 +01:00
CS::Group::serialize_scalar(self.sk)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let sk = deserialize_scalar::<CS::Group>(&mut input)?;
2021-09-15 17:49:31 -07:00
2022-01-21 22:52:09 +01:00
Ok(Self { sk })
2021-09-15 17:49:31 -07:00
}
}
2022-02-13 04:00:11 -08:00
/// Length of [`VoprfServer`] in bytes for serialization.
pub type VoprfServerLen<CS> = Sum<
<<CS as CipherSuite>::Group as Group>::ScalarLen,
<<CS as CipherSuite>::Group as Group>::ElemLen,
>;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> VoprfServer<CS> {
2022-02-13 04:00:11 -08:00
/// Serialization into bytes
2025-04-15 22:30:56 +02:00
pub fn serialize(&self) -> GenericArray<u8, VoprfServerLen<CS>> {
2022-02-13 04:00:11 -08:00
CS::Group::serialize_scalar(self.sk).concat(CS::Group::serialize_elem(self.pk))
}
/// Deserialization from bytes
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let sk = deserialize_scalar::<CS::Group>(&mut input)?;
let pk = deserialize_elem::<CS::Group>(&mut input)?;
2022-02-13 04:00:11 -08:00
Ok(Self { sk, pk })
}
}
/// Length of [`PoprfServer`] in bytes for serialization.
pub type PoprfServerLen<CS> = Sum<
2022-01-21 22:52:09 +01:00
<<CS as CipherSuite>::Group as Group>::ScalarLen,
<<CS as CipherSuite>::Group as Group>::ElemLen,
>;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> PoprfServer<CS> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2025-04-15 22:30:56 +02:00
pub fn serialize(&self) -> GenericArray<u8, PoprfServerLen<CS>> {
2022-01-21 22:52:09 +01:00
CS::Group::serialize_scalar(self.sk).concat(CS::Group::serialize_elem(self.pk))
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let sk = deserialize_scalar::<CS::Group>(&mut input)?;
let pk = deserialize_elem::<CS::Group>(&mut input)?;
2021-09-15 17:49:31 -07:00
2022-01-21 22:52:09 +01:00
Ok(Self { sk, pk })
2021-09-15 17:49:31 -07:00
}
}
2022-01-21 22:52:09 +01:00
/// Length of [`Proof`] in bytes for serialization.
pub type ProofLen<CS> = Sum<
<<CS as CipherSuite>::Group as Group>::ScalarLen,
<<CS as CipherSuite>::Group as Group>::ScalarLen,
>;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> Proof<CS> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2025-04-15 22:30:56 +02:00
pub fn serialize(&self) -> GenericArray<u8, ProofLen<CS>> {
2022-01-21 22:52:09 +01:00
CS::Group::serialize_scalar(self.c_scalar)
.concat(CS::Group::serialize_scalar(self.s_scalar))
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let c_scalar = deserialize_scalar::<CS::Group>(&mut input)?;
let s_scalar = deserialize_scalar::<CS::Group>(&mut input)?;
2021-12-23 21:03:38 +01:00
2022-01-21 22:52:09 +01:00
Ok(Proof { c_scalar, s_scalar })
2021-09-15 17:49:31 -07:00
}
}
2022-01-21 22:52:09 +01:00
/// Length of [`BlindedElement`] in bytes for serialization.
pub type BlindedElementLen<CS> = <<CS as CipherSuite>::Group as Group>::ElemLen;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> BlindedElement<CS> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2022-01-21 22:52:09 +01:00
pub fn serialize(&self) -> GenericArray<u8, BlindedElementLen<CS>> {
CS::Group::serialize_elem(self.0)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let value = deserialize_elem::<CS::Group>(&mut input)?;
2021-12-23 21:03:38 +01:00
2022-01-21 22:52:09 +01:00
Ok(Self(value))
2021-09-15 17:49:31 -07:00
}
}
2022-01-21 22:52:09 +01:00
/// Length of [`EvaluationElement`] in bytes for serialization.
pub type EvaluationElementLen<CS> = <<CS as CipherSuite>::Group as Group>::ElemLen;
2025-04-15 22:30:56 +02:00
impl<CS: CipherSuite> EvaluationElement<CS> {
2021-09-15 17:49:31 -07:00
/// Serialization into bytes
2022-01-21 22:52:09 +01:00
pub fn serialize(&self) -> GenericArray<u8, EvaluationElementLen<CS>> {
CS::Group::serialize_elem(self.0)
2021-09-15 17:49:31 -07:00
}
/// Deserialization from bytes
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`] if failed to deserialize `input`.
2022-04-01 21:18:32 +02:00
pub fn deserialize(mut input: &[u8]) -> Result<Self> {
let value = deserialize_elem::<CS::Group>(&mut input)?;
2021-12-23 21:03:38 +01:00
2022-01-21 22:52:09 +01:00
Ok(Self(value))
2021-09-15 17:49:31 -07:00
}
}
2021-12-25 22:54:27 +01:00
2022-04-01 21:18:32 +02:00
fn deserialize_elem<G: Group>(input: &mut &[u8]) -> Result<G::Elem> {
let input = input
.take_ext(G::ElemLen::USIZE)
.ok_or(Error::Deserialization)?;
G::deserialize_elem(input)
}
fn deserialize_scalar<G: Group>(input: &mut &[u8]) -> Result<G::Scalar> {
let input = input
.take_ext(G::ScalarLen::USIZE)
.ok_or(Error::Deserialization)?;
G::deserialize_scalar(input)
}
trait SliceExt {
2024-01-17 21:09:32 +01:00
fn take_ext<'a>(self: &mut &'a Self, take: usize) -> Option<&'a Self>;
2022-01-28 01:38:17 +01:00
}
2022-04-01 21:18:32 +02:00
impl<T> SliceExt for [T] {
2024-01-17 21:09:32 +01:00
fn take_ext<'a>(self: &mut &'a Self, take: usize) -> Option<&'a Self> {
2022-04-01 21:18:32 +02:00
if take > self.len() {
return None;
}
let (front, back) = self.split_at(take);
*self = back;
Some(front)
}
2022-01-28 01:38:17 +01:00
}
#[cfg(feature = "serde")]
pub(crate) mod serde {
use core::marker::PhantomData;
use generic_array::GenericArray;
use serde::de::{Deserializer, Error};
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
use crate::Group;
pub(crate) struct Element<G: Group>(PhantomData<G>);
impl<'de, G: Group> Element<G> {
pub(crate) fn deserialize<D>(deserializer: D) -> Result<G::Elem, D::Error>
where
D: Deserializer<'de>,
{
GenericArray::<_, G::ElemLen>::deserialize(deserializer)
.and_then(|bytes| G::deserialize_elem(&bytes).map_err(D::Error::custom))
}
pub(crate) fn serialize<S>(self_: &G::Elem, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
G::serialize_elem(*self_).serialize(serializer)
}
}
pub(crate) struct Scalar<G: Group>(PhantomData<G>);
impl<'de, G: Group> Scalar<G> {
pub(crate) fn deserialize<D>(deserializer: D) -> Result<G::Scalar, D::Error>
where
D: Deserializer<'de>,
{
GenericArray::<_, G::ScalarLen>::deserialize(deserializer)
.and_then(|bytes| G::deserialize_scalar(&bytes).map_err(D::Error::custom))
}
pub(crate) fn serialize<S>(self_: &G::Scalar, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
G::serialize_scalar(*self_).serialize(serializer)
}
}
2021-12-25 22:54:27 +01:00
}
2022-04-01 21:18:32 +02:00
#[cfg(test)]
mod test {
use proptest::collection::vec;
use proptest::prelude::*;
use crate::{
BlindedElement, EvaluationElement, OprfClient, OprfServer, PoprfClient, PoprfServer, Proof,
VoprfClient, VoprfServer,
};
macro_rules! test_deserialize {
($item:ident, $bytes:ident) => {
#[cfg(feature = "ristretto255")]
{
let _ = $item::<crate::Ristretto255>::deserialize(&$bytes[..]);
}
let _ = $item::<p256::NistP256>::deserialize(&$bytes[..]);
2023-02-09 00:48:12 +01:00
let _ = $item::<p384::NistP384>::deserialize(&$bytes[..]);
2023-11-13 01:14:52 +01:00
let _ = $item::<p521::NistP521>::deserialize(&$bytes[..]);
2022-04-01 21:18:32 +02:00
};
}
proptest! {
#[test]
fn test_nocrash_oprf_client(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(OprfClient, bytes);
}
#[test]
fn test_nocrash_voprf_client(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(VoprfClient, bytes);
}
#[test]
fn test_nocrash_poprf_client(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(PoprfClient, bytes);
}
#[test]
fn test_nocrash_oprf_server(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(OprfServer, bytes);
}
#[test]
fn test_nocrash_voprf_server(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(VoprfServer, bytes);
}
#[test]
fn test_nocrash_poprf_server(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(PoprfServer, bytes);
}
#[test]
fn test_nocrash_blinded_element(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(BlindedElement, bytes);
}
#[test]
fn test_nocrash_evaluation_element(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(EvaluationElement, bytes);
}
#[test]
fn test_nocrash_proof(bytes in vec(any::<u8>(), 0..200)) {
test_deserialize!(Proof, bytes);
}
}
}