use ark_ff::FftField;
use ark_serialize::SerializationError;
use bincode::ErrorKind;
use stoffelnet::network_utils::NetworkError;
use thiserror::Error;
use tokio::{
sync::oneshot::{channel, Receiver, Sender},
task::JoinError,
};
use crate::{
common::share::ShareError,
honeybadger::{
batch_recon::BatchReconError, double_share::DoubleShamirShare,
robust_interpolate::robust_interpolate::RobustShare,
triple_gen::triple_generation::ProtocolState, SessionId,
},
};
pub mod triple_generation;
#[derive(Debug, Error)]
pub enum TripleGenError {
#[error("network error: {0:?}")]
NetworkError(#[from] NetworkError),
#[error("share error: {0:?}")]
ShareError(#[from] ShareError),
#[error("not enough preprocessing")]
NotEnoughPreprocessing,
#[error("error during the serialization using bincode: {0:?}")]
BincodeSerializationError(#[from] Box<ErrorKind>),
#[error("error during the serialization using bincode: {0:?}")]
ArkSerializationError(#[from] SerializationError),
#[error("wrong ammount of shares")]
NotEnoughShares,
#[error("batch reconstruction error: {0:?}")]
BatchReconError(#[from] BatchReconError),
#[error("async error: {0:?}")]
AsyncError(#[from] JoinError),
#[error("the session IDs do not match")]
SessionIdMismatch,
#[error("error sending the result: {0:?}")]
SendError(SessionId),
#[error("error receiving the result: {0:?}")]
ReceiveError(SessionId),
#[error("session ID {0:?} malformed")]
SessionIdError(SessionId),
#[error("limit reached")]
LimitError,
#[error("no such session ID exists: {0:?}")]
NoSuchSessionId(SessionId),
#[error("result already received: {0:?}")]
ResultAlreadyReceived(SessionId),
#[error("multiplication {0:?} did not complete in time")]
Timeout(SessionId),
#[error("received abort signal")]
Abort,
}
#[derive(Clone, Debug)]
pub struct ShamirBeaverTriple<F: FftField> {
pub a: RobustShare<F>,
pub b: RobustShare<F>,
pub mult: RobustShare<F>,
}
impl<F> ShamirBeaverTriple<F>
where
F: FftField,
{
pub fn new(a: RobustShare<F>, b: RobustShare<F>, mult: RobustShare<F>) -> Self {
Self { a, b, mult }
}
}
#[derive(Debug)]
pub struct TripleGenStorage<F>
where
F: FftField,
{
pub protocol_state: ProtocolState,
pub batch_recon_result: Option<Vec<F>>,
pub randousha_pairs: Vec<DoubleShamirShare<F>>,
pub random_shares_a_input: Vec<RobustShare<F>>,
pub random_shares_b_input: Vec<RobustShare<F>>,
pub protocol_output: Vec<ShamirBeaverTriple<F>>,
pub output_sender: Option<Sender<Vec<ShamirBeaverTriple<F>>>>,
pub output_receiver: Option<Receiver<Vec<ShamirBeaverTriple<F>>>>,
}
impl<F> TripleGenStorage<F>
where
F: FftField,
{
pub fn empty() -> Self {
let (output_sender, output_receiver) = channel();
Self {
protocol_state: ProtocolState::NotInitialized,
batch_recon_result: None,
randousha_pairs: Vec::new(),
random_shares_a_input: Vec::new(),
random_shares_b_input: Vec::new(),
protocol_output: Vec::new(),
output_sender: Some(output_sender),
output_receiver: Some(output_receiver),
}
}
}