Skip to content

Commit

Permalink
(alpha.8) - Refactor key-exchange crate (#685)
Browse files Browse the repository at this point in the history
* refactor(key-exchange): adapt key-exchange to new vm

* fix: fix feature flags

* simplify

* delete old msg module

* clean up error

---------

Co-authored-by: sinu <[email protected]>
  • Loading branch information
th4s and sinui0 authored Jan 22, 2025
1 parent c9d8404 commit 001b050
Show file tree
Hide file tree
Showing 9 changed files with 779 additions and 610 deletions.
24 changes: 11 additions & 13 deletions crates/components/key-exchange/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,29 @@ name = "key_exchange"

[features]
default = ["mock"]
mock = []
mock = ["mpz-share-conversion/test-utils", "mpz-common/ideal"]

[dependencies]
mpz-garble = { git = "https://github.com/privacy-scaling-explorations/mpz", rev = "b8ae7ac" }
mpz-common = { git = "https://github.com/privacy-scaling-explorations/mpz", rev = "b8ae7ac" }
mpz-fields = { git = "https://github.com/privacy-scaling-explorations/mpz", rev = "b8ae7ac" }
mpz-share-conversion = { git = "https://github.com/privacy-scaling-explorations/mpz", rev = "b8ae7ac", features = [
"ideal",
] }
mpz-circuits = { git = "https://github.com/privacy-scaling-explorations/mpz", rev = "b8ae7ac" }
mpz-vm-core = { workspace = true }
mpz-memory-core = { workspace = true }
mpz-common = { workspace = true }
mpz-fields = { workspace = true }
mpz-share-conversion = { workspace = true }
mpz-circuits = { workspace = true }
mpz-core = { workspace = true }

p256 = { workspace = true, features = ["ecdh", "serde"] }
async-trait = { workspace = true }
thiserror = { workspace = true }
serde = { workspace = true }
futures = { workspace = true }
serio = { workspace = true }
derive_builder = { workspace = true }
tracing = { workspace = true }
rand = { workspace = true }
tokio = { workspace = true, features = ["sync"] }

[dev-dependencies]
mpz-share-conversion = { git = "https://github.com/privacy-scaling-explorations/mpz", rev = "b8ae7ac", features = [
"ideal",
] }
mpz-ot = { workspace = true, features = ["ideal"] }
mpz-garble = { workspace = true }

rand_chacha = { workspace = true }
rand_core = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions crates/components/key-exchange/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! This module provides the circuits used in the key exchange protocol.
use std::sync::Arc;

use mpz_circuits::{circuits::big_num::nbyte_add_mod_trace, Circuit, CircuitBuilder};
use std::sync::Arc;

/// NIST P-256 prime big-endian.
static P: [u8; 32] = [
Expand Down
4 changes: 2 additions & 2 deletions crates/components/key-exchange/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl KeyExchangeConfig {
}

/// Get the role of this instance.
pub fn role(&self) -> &Role {
&self.role
pub fn role(&self) -> Role {
self.role
}
}
146 changes: 59 additions & 87 deletions crates/components/key-exchange/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,120 +1,92 @@
use core::fmt;
use std::error::Error;

/// A key exchange error.
/// MPC-TLS protocol error.
#[derive(Debug, thiserror::Error)]
pub struct KeyExchangeError {
kind: ErrorKind,
#[source]
source: Option<Box<dyn Error + Send + Sync>>,
#[error(transparent)]
pub struct KeyExchangeError(#[from] ErrorRepr);

#[derive(Debug, thiserror::Error)]
#[error("key exchange error: {0}")]
pub(crate) enum ErrorRepr {
#[error("state error: {0}")]
State(Box<dyn Error + Send + Sync + 'static>),
#[error("context error: {0}")]
Ctx(Box<dyn Error + Send + Sync + 'static>),
#[error("io error: {0}")]
Io(std::io::Error),
#[error("vm error: {0}")]
Vm(Box<dyn Error + Send + Sync + 'static>),
#[error("share conversion error: {0}")]
ShareConversion(Box<dyn Error + Send + Sync + 'static>),
#[error("role error: {0}")]
Role(Box<dyn Error + Send + Sync + 'static>),
#[error("key error: {0}")]
Key(Box<dyn Error + Send + Sync + 'static>),
}

impl KeyExchangeError {
pub(crate) fn new<E>(kind: ErrorKind, source: E) -> Self
pub(crate) fn state<E>(err: E) -> KeyExchangeError
where
E: Into<Box<dyn Error + Send + Sync>>,
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
Self {
kind,
source: Some(source.into()),
}
}

#[cfg(test)]
pub(crate) fn kind(&self) -> &ErrorKind {
&self.kind
}

pub(crate) fn state(msg: impl Into<String>) -> Self {
Self {
kind: ErrorKind::State,
source: Some(msg.into().into()),
}
}

pub(crate) fn role(msg: impl Into<String>) -> Self {
Self {
kind: ErrorKind::Role,
source: Some(msg.into().into()),
}
Self(ErrorRepr::State(err.into()))
}
}

#[derive(Debug)]
pub(crate) enum ErrorKind {
Io,
Context,
Vm,
ShareConversion,
Key,
State,
Role,
}

impl fmt::Display for KeyExchangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
ErrorKind::Io => write!(f, "io error")?,
ErrorKind::Context => write!(f, "context error")?,
ErrorKind::Vm => write!(f, "vm error")?,
ErrorKind::ShareConversion => write!(f, "share conversion error")?,
ErrorKind::Key => write!(f, "key error")?,
ErrorKind::State => write!(f, "state error")?,
ErrorKind::Role => write!(f, "role error")?,
}

if let Some(ref source) = self.source {
write!(f, " caused by: {}", source)?;
}

Ok(())
pub(crate) fn ctx<E>(err: E) -> KeyExchangeError
where
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
Self(ErrorRepr::Ctx(err.into()))
}
}

impl From<mpz_common::ContextError> for KeyExchangeError {
fn from(error: mpz_common::ContextError) -> Self {
Self::new(ErrorKind::Context, error)
pub(crate) fn vm<E>(err: E) -> KeyExchangeError
where
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
Self(ErrorRepr::Vm(err.into()))
}
}

impl From<mpz_garble::MemoryError> for KeyExchangeError {
fn from(error: mpz_garble::MemoryError) -> Self {
Self::new(ErrorKind::Vm, error)
pub(crate) fn share_conversion<E>(err: E) -> KeyExchangeError
where
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
Self(ErrorRepr::ShareConversion(err.into()))
}
}

impl From<mpz_garble::LoadError> for KeyExchangeError {
fn from(error: mpz_garble::LoadError) -> Self {
Self::new(ErrorKind::Vm, error)
pub(crate) fn role<E>(err: E) -> KeyExchangeError
where
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
Self(ErrorRepr::Role(err.into()))
}
}

impl From<mpz_garble::ExecutionError> for KeyExchangeError {
fn from(error: mpz_garble::ExecutionError) -> Self {
Self::new(ErrorKind::Vm, error)
pub(crate) fn key<E>(err: E) -> KeyExchangeError
where
E: Into<Box<dyn Error + Send + Sync + 'static>>,
{
Self(ErrorRepr::Key(err.into()))
}
}

impl From<mpz_garble::DecodeError> for KeyExchangeError {
fn from(error: mpz_garble::DecodeError) -> Self {
Self::new(ErrorKind::Vm, error)
#[cfg(test)]
pub(crate) fn kind(&self) -> &ErrorRepr {
&self.0
}
}

impl From<mpz_share_conversion::ShareConversionError> for KeyExchangeError {
fn from(error: mpz_share_conversion::ShareConversionError) -> Self {
Self::new(ErrorKind::ShareConversion, error)
impl From<mpz_common::ContextError> for KeyExchangeError {
fn from(value: mpz_common::ContextError) -> Self {
Self::ctx(value)
}
}

impl From<p256::elliptic_curve::Error> for KeyExchangeError {
fn from(error: p256::elliptic_curve::Error) -> Self {
Self::new(ErrorKind::Key, error)
fn from(value: p256::elliptic_curve::Error) -> Self {
Self::key(value)
}
}

impl From<std::io::Error> for KeyExchangeError {
fn from(error: std::io::Error) -> Self {
Self::new(ErrorKind::Io, error)
fn from(err: std::io::Error) -> Self {
Self(ErrorRepr::Io(err))
}
}
Loading

0 comments on commit 001b050

Please sign in to comment.