# mpz.rs - source
Source of the Rust file `crates/tlsn/src/mpz.rs`.

Skip to main content

#
tlsn/
mpz.rs

    1use std::sync::Arc;
    2
    3use mpc_tls::{MpcTlsFollower, MpcTlsLeader, SessionKeys};
    4use mpz_common::Context;
    5use mpz_core::Block;
    6#[cfg(not(tlsn_insecure))]
    7use mpz_garble::protocol::semihonest::{Evaluator, Garbler};
    8use mpz_garble_core::Delta;
    9#[cfg(not(tlsn_insecure))]
    10use mpz_ot::cot::{DerandCOTReceiver, DerandCOTSender};
    11use mpz_ot::{
    12    chou_orlandi as co, ferret, kos,
    13    rcot::shared::{SharedRCOTReceiver, SharedRCOTSender},
    14};
    15use mpz_zk::{Prover, Verifier};
    16#[cfg(not(tlsn_insecure))]
    17use rand::Rng;
    18use tlsn_core::config::tls_commit::mpc::{MpcTlsConfig, NetworkSetting};
    19use tlsn_deap::Deap;
    20use tokio::sync::Mutex;
    21
    22#[cfg(not(tlsn_insecure))]
    23pub(crate) type ProverMpc =
    24    Garbler<DerandCOTSender<SharedRCOTSender<kos::Sender<co::Receiver>, Block>>>;
    25#[cfg(tlsn_insecure)]
    26pub(crate) type ProverMpc = mpz_ideal_vm::IdealVm;
    27
    28#[cfg(not(tlsn_insecure))]
    29pub(crate) type ProverZk =
    30    Prover<SharedRCOTReceiver<ferret::Receiver<kos::Receiver<co::Sender>>, bool, Block>>;
    31#[cfg(tlsn_insecure)]
    32pub(crate) type ProverZk = mpz_ideal_vm::IdealVm;
    33
    34#[cfg(not(tlsn_insecure))]
    35pub(crate) type VerifierMpc =
    36    Evaluator<DerandCOTReceiver<SharedRCOTReceiver<kos::Receiver<co::Sender>, bool, Block>>>;
    37#[cfg(tlsn_insecure)]
    38pub(crate) type VerifierMpc = mpz_ideal_vm::IdealVm;
    39
    40#[cfg(not(tlsn_insecure))]
    41pub(crate) type VerifierZk =
    42    Verifier<SharedRCOTSender<ferret::Sender<kos::Sender<co::Receiver>>, Block>>;
    43#[cfg(tlsn_insecure)]
    44pub(crate) type VerifierZk = mpz_ideal_vm::IdealVm;
    45
    46pub(crate) struct ProverDeps {
    47    pub(crate) vm: Arc<Mutex<Deap<ProverMpc, ProverZk>>>,
    48    pub(crate) mpc_tls: MpcTlsLeader,
    49}
    50
    51pub(crate) fn build_prover_deps(config: MpcTlsConfig, ctx: Context) -> ProverDeps {
    52    let mut rng = rand::rng();
    53    let delta = Delta::new(Block::random(&mut rng));
    54
    55    let base_ot_send = co::Sender::default();
    56    let base_ot_recv = co::Receiver::default();
    57    let rcot_send = kos::Sender::new(
    58        kos::SenderConfig::default(),
    59        delta.into_inner(),
    60        base_ot_recv,
    61    );
    62    let rcot_recv = kos::Receiver::new(kos::ReceiverConfig::default(), base_ot_send);
    63    let rcot_recv = ferret::Receiver::new(
    64        ferret::FerretConfig::builder()
    65            .lpn_type(ferret::LpnType::Regular)
    66            .build()
    67            .expect("ferret config is valid"),
    68        Block::random(&mut rng),
    69        rcot_recv,
    70    );
    71
    72    let rcot_send = SharedRCOTSender::new(rcot_send);
    73    let rcot_recv = SharedRCOTReceiver::new(rcot_recv);
    74
    75    #[cfg(not(tlsn_insecure))]
    76    let mpc = ProverMpc::new(DerandCOTSender::new(rcot_send.clone()), rng.random(), delta);
    77    #[cfg(tlsn_insecure)]
    78    let mpc = mpz_ideal_vm::IdealVm::new();
    79
    80    #[cfg(not(tlsn_insecure))]
    81    let zk = ProverZk::new(Default::default(), rcot_recv.clone());
    82    #[cfg(tlsn_insecure)]
    83    let zk = mpz_ideal_vm::IdealVm::new();
    84
    85    let vm = Arc::new(Mutex::new(Deap::new(tlsn_deap::Role::Leader, mpc, zk)));
    86    let mpc_tls = MpcTlsLeader::new(
    87        build_mpc_tls_config(config),
    88        ctx,
    89        vm.clone(),
    90        (rcot_send.clone(), rcot_send.clone(), rcot_send),
    91        rcot_recv,
    92    );
    93
    94    ProverDeps { vm, mpc_tls }
    95}
    96
    97pub(crate) struct VerifierDeps {
    98    pub(crate) vm: Arc<Mutex<Deap<VerifierMpc, VerifierZk>>>,
    99    pub(crate) mpc_tls: MpcTlsFollower,
    100}
    101
    102pub(crate) fn build_verifier_deps(config: MpcTlsConfig, ctx: Context) -> VerifierDeps {
    103    let mut rng = rand::rng();
    104
    105    let delta = Delta::random(&mut rng);
    106    let base_ot_send = co::Sender::default();
    107    let base_ot_recv = co::Receiver::default();
    108    let rcot_send = kos::Sender::new(
    109        kos::SenderConfig::default(),
    110        delta.into_inner(),
    111        base_ot_recv,
    112    );
    113    let rcot_send = ferret::Sender::new(
    114        ferret::FerretConfig::builder()
    115            .lpn_type(ferret::LpnType::Regular)
    116            .build()
    117            .expect("ferret config is valid"),
    118        Block::random(&mut rng),
    119        rcot_send,
    120    );
    121    let rcot_recv = kos::Receiver::new(kos::ReceiverConfig::default(), base_ot_send);
    122
    123    let rcot_send = SharedRCOTSender::new(rcot_send);
    124    let rcot_recv = SharedRCOTReceiver::new(rcot_recv);
    125
    126    #[cfg(not(tlsn_insecure))]
    127    let mpc = VerifierMpc::new(DerandCOTReceiver::new(rcot_recv.clone()));
    128    #[cfg(tlsn_insecure)]
    129    let mpc = mpz_ideal_vm::IdealVm::new();
    130
    131    #[cfg(not(tlsn_insecure))]
    132    let zk = VerifierZk::new(Default::default(), delta, rcot_send.clone());
    133    #[cfg(tlsn_insecure)]
    134    let zk = mpz_ideal_vm::IdealVm::new();
    135
    136    let vm = Arc::new(Mutex::new(Deap::new(tlsn_deap::Role::Follower, mpc, zk)));
    137    let mpc_tls = MpcTlsFollower::new(
    138        build_mpc_tls_config(config),
    139        ctx,
    140        vm.clone(),
    141        rcot_send,
    142        (rcot_recv.clone(), rcot_recv.clone(), rcot_recv),
    143    );
    144
    145    VerifierDeps { vm, mpc_tls }
    146}
    147
    148fn build_mpc_tls_config(config: MpcTlsConfig) -> mpc_tls::Config {
    149    let mut builder = mpc_tls::Config::builder();
    150
    151    builder
    152        .defer_decryption(config.defer_decryption_from_start())
    153        .max_sent(config.max_sent_data())
    154        .max_recv_online(config.max_recv_data_online())
    155        .max_recv(config.max_recv_data());
    156
    157    if let Some(max_sent_records) = config.max_sent_records() {
    158        builder.max_sent_records(max_sent_records);
    159    }
    160
    161    if let Some(max_recv_records_online) = config.max_recv_records_online() {
    162        builder.max_recv_records_online(max_recv_records_online);
    163    }
    164
    165    if let NetworkSetting::Latency = config.network() {
    166        builder.low_bandwidth();
    167    }
    168
    169    builder.build().unwrap()
    170}
    171
    172pub(crate) fn translate_keys<Mpc, Zk>(keys: &mut SessionKeys, vm: &Deap<Mpc, Zk>) {
    173    keys.client_write_key = vm
    174        .translate(keys.client_write_key)
    175        .expect("VM memory should be consistent");
    176    keys.client_write_iv = vm
    177        .translate(keys.client_write_iv)
    178        .expect("VM memory should be consistent");
    179    keys.server_write_key = vm
    180        .translate(keys.server_write_key)
    181        .expect("VM memory should be consistent");
    182    keys.server_write_iv = vm
    183        .translate(keys.server_write_iv)
    184        .expect("VM memory should be consistent");
    185    keys.server_write_mac_key = vm
    186        .translate(keys.server_write_mac_key)
    187        .expect("VM memory should be consistent");
    188}