tlsn_core/config/tls_commit/
mpc.rs

1//! MPC-TLS commitment protocol configuration.
2
3use serde::{Deserialize, Serialize};
4
5// Default is 32 bytes to decrypt the TLS protocol messages.
6const DEFAULT_MAX_RECV_ONLINE: usize = 32;
7
8/// MPC-TLS commitment protocol configuration.
9#[derive(Clone, Debug, Deserialize, Serialize)]
10#[serde(try_from = "unchecked::MpcTlsConfigUnchecked")]
11pub struct MpcTlsConfig {
12    /// Maximum number of bytes that can be sent.
13    max_sent_data: usize,
14    /// Maximum number of application data records that can be sent.
15    max_sent_records: Option<usize>,
16    /// Maximum number of bytes that can be decrypted online, i.e. while the
17    /// MPC-TLS connection is active.
18    max_recv_data_online: usize,
19    /// Maximum number of bytes that can be received.
20    max_recv_data: usize,
21    /// Maximum number of received application data records that can be
22    /// decrypted online, i.e. while the MPC-TLS connection is active.
23    max_recv_records_online: Option<usize>,
24    /// Whether the `deferred decryption` feature is toggled on from the start
25    /// of the MPC-TLS connection.
26    defer_decryption_from_start: bool,
27    /// Network settings.
28    network: NetworkSetting,
29}
30
31impl MpcTlsConfig {
32    /// Creates a new builder.
33    pub fn builder() -> MpcTlsConfigBuilder {
34        MpcTlsConfigBuilder::default()
35    }
36
37    /// Returns the maximum number of bytes that can be sent.
38    pub fn max_sent_data(&self) -> usize {
39        self.max_sent_data
40    }
41
42    /// Returns the maximum number of application data records that can
43    /// be sent.
44    pub fn max_sent_records(&self) -> Option<usize> {
45        self.max_sent_records
46    }
47
48    /// Returns the maximum number of bytes that can be decrypted online.
49    pub fn max_recv_data_online(&self) -> usize {
50        self.max_recv_data_online
51    }
52
53    /// Returns the maximum number of bytes that can be received.
54    pub fn max_recv_data(&self) -> usize {
55        self.max_recv_data
56    }
57
58    /// Returns the maximum number of received application data records that
59    /// can be decrypted online.
60    pub fn max_recv_records_online(&self) -> Option<usize> {
61        self.max_recv_records_online
62    }
63
64    /// Returns whether the `deferred decryption` feature is toggled on from the
65    /// start of the MPC-TLS connection.
66    pub fn defer_decryption_from_start(&self) -> bool {
67        self.defer_decryption_from_start
68    }
69
70    /// Returns the network settings.
71    pub fn network(&self) -> NetworkSetting {
72        self.network
73    }
74}
75
76fn validate(config: MpcTlsConfig) -> Result<MpcTlsConfig, MpcTlsConfigError> {
77    if config.max_recv_data_online > config.max_recv_data {
78        return Err(ErrorRepr::InvalidValue {
79            name: "max_recv_data_online",
80            reason: format!(
81                "must be <= max_recv_data ({} > {})",
82                config.max_recv_data_online, config.max_recv_data
83            ),
84        }
85        .into());
86    }
87
88    Ok(config)
89}
90
91/// Builder for [`MpcTlsConfig`].
92#[derive(Debug, Default)]
93pub struct MpcTlsConfigBuilder {
94    max_sent_data: Option<usize>,
95    max_sent_records: Option<usize>,
96    max_recv_data_online: Option<usize>,
97    max_recv_data: Option<usize>,
98    max_recv_records_online: Option<usize>,
99    defer_decryption_from_start: Option<bool>,
100    network: Option<NetworkSetting>,
101}
102
103impl MpcTlsConfigBuilder {
104    /// Sets the maximum number of bytes that can be sent.
105    pub fn max_sent_data(mut self, max_sent_data: usize) -> Self {
106        self.max_sent_data = Some(max_sent_data);
107        self
108    }
109
110    /// Sets the maximum number of application data records that can be sent.
111    pub fn max_sent_records(mut self, max_sent_records: usize) -> Self {
112        self.max_sent_records = Some(max_sent_records);
113        self
114    }
115
116    /// Sets the maximum number of bytes that can be decrypted online.
117    pub fn max_recv_data_online(mut self, max_recv_data_online: usize) -> Self {
118        self.max_recv_data_online = Some(max_recv_data_online);
119        self
120    }
121
122    /// Sets the maximum number of bytes that can be received.
123    pub fn max_recv_data(mut self, max_recv_data: usize) -> Self {
124        self.max_recv_data = Some(max_recv_data);
125        self
126    }
127
128    /// Sets the maximum number of received application data records that can
129    /// be decrypted online.
130    pub fn max_recv_records_online(mut self, max_recv_records_online: usize) -> Self {
131        self.max_recv_records_online = Some(max_recv_records_online);
132        self
133    }
134
135    /// Sets whether the `deferred decryption` feature is toggled on from the
136    /// start of the MPC-TLS connection.
137    pub fn defer_decryption_from_start(mut self, defer_decryption_from_start: bool) -> Self {
138        self.defer_decryption_from_start = Some(defer_decryption_from_start);
139        self
140    }
141
142    /// Sets the network settings.
143    pub fn network(mut self, network: NetworkSetting) -> Self {
144        self.network = Some(network);
145        self
146    }
147
148    /// Builds the configuration.
149    pub fn build(self) -> Result<MpcTlsConfig, MpcTlsConfigError> {
150        let Self {
151            max_sent_data,
152            max_sent_records,
153            max_recv_data_online,
154            max_recv_data,
155            max_recv_records_online,
156            defer_decryption_from_start,
157            network,
158        } = self;
159
160        let max_sent_data = max_sent_data.ok_or(ErrorRepr::MissingField {
161            name: "max_sent_data",
162        })?;
163
164        let max_recv_data_online = max_recv_data_online.unwrap_or(DEFAULT_MAX_RECV_ONLINE);
165        let max_recv_data = max_recv_data.ok_or(ErrorRepr::MissingField {
166            name: "max_recv_data",
167        })?;
168
169        let defer_decryption_from_start = defer_decryption_from_start.unwrap_or(true);
170        let network = network.unwrap_or_default();
171
172        validate(MpcTlsConfig {
173            max_sent_data,
174            max_sent_records,
175            max_recv_data_online,
176            max_recv_data,
177            max_recv_records_online,
178            defer_decryption_from_start,
179            network,
180        })
181    }
182}
183
184/// Settings for the network environment.
185///
186/// Provides optimization options to adapt the protocol to different network
187/// situations.
188#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
189pub enum NetworkSetting {
190    /// Reduces network round-trips at the expense of consuming more network
191    /// bandwidth.
192    Bandwidth,
193    /// Reduces network bandwidth utilization at the expense of more network
194    /// round-trips.
195    Latency,
196}
197
198impl Default for NetworkSetting {
199    fn default() -> Self {
200        Self::Latency
201    }
202}
203
204/// Error for [`MpcTlsConfig`].
205#[derive(Debug, thiserror::Error)]
206#[error(transparent)]
207pub struct MpcTlsConfigError(#[from] ErrorRepr);
208
209#[derive(Debug, thiserror::Error)]
210enum ErrorRepr {
211    #[error("missing field: {name}")]
212    MissingField { name: &'static str },
213    #[error("invalid value for field({name}): {reason}")]
214    InvalidValue { name: &'static str, reason: String },
215}
216
217mod unchecked {
218    use super::*;
219
220    #[derive(Deserialize)]
221    pub(super) struct MpcTlsConfigUnchecked {
222        max_sent_data: usize,
223        max_sent_records: Option<usize>,
224        max_recv_data_online: usize,
225        max_recv_data: usize,
226        max_recv_records_online: Option<usize>,
227        defer_decryption_from_start: bool,
228        network: NetworkSetting,
229    }
230
231    impl TryFrom<MpcTlsConfigUnchecked> for MpcTlsConfig {
232        type Error = MpcTlsConfigError;
233
234        fn try_from(value: MpcTlsConfigUnchecked) -> Result<Self, Self::Error> {
235            validate(MpcTlsConfig {
236                max_sent_data: value.max_sent_data,
237                max_sent_records: value.max_sent_records,
238                max_recv_data_online: value.max_recv_data_online,
239                max_recv_data: value.max_recv_data,
240                max_recv_records_online: value.max_recv_records_online,
241                defer_decryption_from_start: value.defer_decryption_from_start,
242                network: value.network,
243            })
244        }
245    }
246}