use alloc::boxed::Box;
use alloc::string::String;
use serde::{Deserialize, Serialize};
use umbral_pre::serde_bytes;
use crate::conditions::{Conditions, Context};
use crate::versioning::{
messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Copy, Clone)]
pub enum FerveoVariant {
SIMPLE,
PRECOMPUTED,
}
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct ThresholdDecryptionRequest {
pub ritual_id: u16,
#[serde(with = "serde_bytes::as_base64")]
pub ciphertext: Box<[u8]>,
pub conditions: Option<Conditions>,
pub context: Option<Context>,
pub variant: FerveoVariant,
}
impl ThresholdDecryptionRequest {
pub fn new(
ritual_id: u16,
ciphertext: &[u8],
conditions: Option<&Conditions>,
context: Option<&Context>,
variant: FerveoVariant,
) -> Self {
Self {
ritual_id,
ciphertext: ciphertext.to_vec().into(),
conditions: conditions.cloned(),
context: context.cloned(),
variant,
}
}
}
impl<'a> ProtocolObjectInner<'a> for ThresholdDecryptionRequest {
fn version() -> (u16, u16) {
(1, 0)
}
fn brand() -> [u8; 4] {
*b"ThRq"
}
fn unversioned_to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(&self)
}
fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
if minor_version == 0 {
Some(messagepack_deserialize(bytes))
} else {
None
}
}
}
impl<'a> ProtocolObject<'a> for ThresholdDecryptionRequest {}
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
pub struct ThresholdDecryptionResponse {
#[serde(with = "serde_bytes::as_base64")]
pub decryption_share: Box<[u8]>,
}
impl ThresholdDecryptionResponse {
pub fn new(decryption_share: &[u8]) -> Self {
ThresholdDecryptionResponse {
decryption_share: decryption_share.to_vec().into(),
}
}
}
impl<'a> ProtocolObjectInner<'a> for ThresholdDecryptionResponse {
fn version() -> (u16, u16) {
(1, 0)
}
fn brand() -> [u8; 4] {
*b"ThRs"
}
fn unversioned_to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(&self)
}
fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
if minor_version == 0 {
Some(messagepack_deserialize(bytes))
} else {
None
}
}
}
impl<'a> ProtocolObject<'a> for ThresholdDecryptionResponse {}