Skip to content

Commit 4dc5570

Browse files
themighty1sinui0
andauthored
MIsc comments (#747)
* fix comments * fix comment Co-authored-by: sinu.eth <[email protected]> * describe all args * change decrypted plaintext -> plaintext * remove redundant comments --------- Co-authored-by: sinu.eth <[email protected]>
1 parent 198e24c commit 4dc5570

File tree

13 files changed

+45
-26
lines changed

13 files changed

+45
-26
lines changed

crates/common/src/zk_aes.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ impl ZkAesCtr {
7878

7979
/// Proves the encryption of `len` bytes.
8080
///
81+
/// Here we only assign certain values in the VM but the actual proving
82+
/// happens later when the plaintext is assigned and the VM is executed.
83+
///
8184
/// # Arguments
8285
///
8386
/// * `vm` - Virtual machine.
8487
/// * `explicit_nonce` - Explicit nonce.
8588
/// * `len` - Length of the plaintext in bytes.
89+
///
90+
/// # Returns
91+
///
92+
/// A VM reference to the plaintext and the ciphertext.
8693
pub fn encrypt(
8794
&mut self,
8895
vm: &mut dyn Vm<Binary>,
@@ -132,6 +139,8 @@ impl ZkAesCtr {
132139
// Assign zeroes to the padding.
133140
if padding_len > 0 {
134141
let padding = input.split_off(input.len() - padding_len);
142+
// To simplify the impl, we don't mark the padding as public, that's why only
143+
// the prover assigns it.
135144
if let Role::Prover = self.role {
136145
vm.assign(padding, vec![0; padding_len])
137146
.map_err(ZkAesCtrError::vm)?;

crates/components/cipher/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub struct CtrBlock<N, C, O> {
9999
/// Can be used to XOR with the cipher input to operate the cipher in counter
100100
/// mode.
101101
pub struct Keystream<N, C, O> {
102+
/// Sequential keystream blocks. Outputs are stored in contiguous memory.
102103
blocks: VecDeque<CtrBlock<N, C, O>>,
103104
}
104105

crates/components/key-exchange/src/exchange.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ where
297297
} = self.state.take()
298298
else {
299299
return Err(KeyExchangeError::state(
300-
"can not compute shares before performing setup",
300+
"cannot compute shares before performing setup",
301301
));
302302
};
303303

crates/mpc-tls/src/leader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl MpcTlsLeader {
148148

149149
let client_random = Random::new().expect("rng is available");
150150

151-
// Allocate
151+
// Allocate.
152152
let pms = ke.alloc(&mut (*vm_lock))?;
153153
let PrfOutput { keys, cf_vd, sf_vd } = prf.alloc(&mut (*vm_lock), pms)?;
154154
record_layer.set_keys(

crates/mpc-tls/src/leader/actor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ impl Wrap<BackendMsgServerClosed> for MpcTlsLeaderMsg {
17281728
}
17291729
}
17301730

1731-
/// Message to start deferring the decryption
1731+
/// Message to start deferring the decryption.
17321732
#[allow(missing_docs)]
17331733
#[derive(Debug)]
17341734
pub struct DeferDecryption;

crates/mpc-tls/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ pub struct LeaderOutput {
8484
pub server_cert_details: ServerCertDetails,
8585
/// Key exchange details.
8686
pub server_kx_details: ServerKxDetails,
87-
/// Client random
87+
/// Client random.
8888
pub client_random: Random,
89-
/// Server random
89+
/// Server random.
9090
pub server_random: Random,
9191
/// TLS transcript.
9292
pub transcript: TlsTranscript,

crates/mpc-tls/src/record_layer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl RecordLayer {
178178
.try_lock_owned()
179179
.map_err(|_| MpcTlsError::other("decrypt lock is held"))?;
180180

181-
// Computes GHASH keys in parallel.
181+
// Preprocesses GHASH keys in parallel.
182182
ctx.try_join(
183183
|ctx| async move { encrypt.preprocess(ctx).await }.scope_boxed(),
184184
|ctx| async move { decrypt.preprocess(ctx).await }.scope_boxed(),
@@ -486,7 +486,7 @@ impl RecordLayer {
486486

487487
if !self.encrypt_buffer.is_empty() {
488488
return Err(MpcTlsError::state(
489-
"record layer can not commit with pending encrypt operations",
489+
"record layer cannot commit with pending encrypt operations",
490490
));
491491
}
492492

crates/mpc-tls/src/record_layer/aead/aes_gcm.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl MpcAesGcm {
111111

112112
// Allocate encryption/decryption.
113113

114-
// Round up the length to the nearest multiple of the block count.
114+
// Round up the length to the nearest multiple of the block size.
115115
let len = 16 * len.div_ceil(16);
116116

117117
let input = vm.alloc_vec::<U8>(len)?;
@@ -141,7 +141,7 @@ impl MpcAesGcm {
141141

142142
pub(crate) async fn preprocess(&mut self, ctx: &mut Context) -> Result<(), AeadError> {
143143
let State::Setup { ghash, .. } = &mut self.state else {
144-
return Err(AeadError::state("must be in setup state to allocate"));
144+
return Err(AeadError::state("must be in setup state to preprocess"));
145145
};
146146

147147
ghash.preprocess(ctx).await?;
@@ -216,7 +216,7 @@ impl MpcAesGcm {
216216
let explicit_nonce: [u8; 8] = explicit_nonce.try_into().map_err(|nonce: Vec<_>| {
217217
AeadError::cipher(format!(
218218
"explicit nonce length: expected {}, got {}",
219-
16,
219+
8,
220220
nonce.len()
221221
))
222222
})?;
@@ -246,6 +246,8 @@ impl MpcAesGcm {
246246
// Assign zeroes to the padding.
247247
if padding_len > 0 {
248248
let padding = input.split_off(input.len() - padding_len);
249+
// To simplify the impl, we don't mark the padding as public, that's why only
250+
// the prover assigns it.
249251
if let Role::Leader = self.role {
250252
vm.assign(padding, vec![0; padding_len])?;
251253
}
@@ -283,7 +285,7 @@ impl MpcAesGcm {
283285
let explicit_nonce: [u8; 8] = explicit_nonce.try_into().map_err(|nonce: Vec<_>| {
284286
AeadError::cipher(format!(
285287
"explicit nonce length: expected {}, got {}",
286-
16,
288+
8,
287289
nonce.len()
288290
))
289291
})?;
@@ -375,7 +377,9 @@ impl MpcAesGcm {
375377
/// # Arguments
376378
///
377379
/// * `vm` - Virtual machine.
378-
/// * `inputs` - Data to verify the tags for.
380+
/// * `data` - Tag data associated with `tags`.
381+
/// * `ciphertexts` - Ciphertexts to verify the tags for.
382+
/// * `tags` - Tags to verify.
379383
pub(crate) fn verify_tags(
380384
&mut self,
381385
vm: &mut dyn Vm<Binary>,

crates/mpc-tls/src/record_layer/aead/ghash.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
1515

1616
use crate::record_layer::aead::AeadError;
1717

18-
/// Maximum key share power.
18+
/// Maximum exponent used in GHASH.
1919
const MAX_POWER: usize = 1026;
2020

2121
#[async_trait]
@@ -26,13 +26,13 @@ pub(crate) trait Ghash {
2626
/// Preprocesses GHASH.
2727
async fn preprocess(&mut self, ctx: &mut Context) -> Result<(), GhashError>;
2828

29-
/// Sets the key for the hash function.
29+
/// Sets the additive key share for the hash function.
3030
fn set_key(&mut self, key: Vec<u8>) -> Result<(), GhashError>;
3131

3232
/// Sets up GHASH, computing the key shares.
3333
async fn setup(&mut self, ctx: &mut Context) -> Result<(), GhashError>;
3434

35-
/// Computes the GHASH tag.
35+
/// Computes the GHASH tag share.
3636
fn compute(&self, input: &[u8]) -> Result<Vec<u8>, GhashError>;
3737
}
3838

@@ -80,12 +80,13 @@ where
8080
{
8181
fn alloc(&mut self) -> Result<(), GhashError> {
8282
if !self.alloc {
83-
// We need only half the number of `MAX_POWER` M2As because of the free
84-
// squaring trick and we need one extra A2M conversion in the beginning.
83+
// Odd powers are computed using M2A, even powers are computed
84+
// locally. We need one extra A2M conversion in the beginning.
8585
// Both M2A and A2M, each require a single OLE.
8686
AdditiveToMultiplicative::<Gf2_128>::alloc(&mut self.converter, 1)
8787
.map_err(GhashError::conversion)?;
8888

89+
// -1 because the odd power H^1 is already known at this point.
8990
MultiplicativeToAdditive::<Gf2_128>::alloc(&mut self.converter, (MAX_POWER / 2) - 1)
9091
.map_err(GhashError::conversion)?;
9192

@@ -128,7 +129,7 @@ where
128129

129130
async fn setup(&mut self, ctx: &mut Context) -> Result<(), GhashError> {
130131
let State::SetKey { key: add_key } = self.state.take() else {
131-
return Err(GhashError::state("can not setup before key is set"));
132+
return Err(GhashError::state("cannot setup before key is set"));
132133
};
133134

134135
let mut mult_key = self
@@ -156,9 +157,9 @@ where
156157
*acc = power_n * mult_key;
157158
Some(power_n)
158159
})
159-
// Start from H^3
160+
// Start from H^3.
160161
.skip(2)
161-
// Skip even powers
162+
// Skip even powers.
162163
.step_by(2)
163164
.collect();
164165

@@ -249,7 +250,7 @@ fn compute_shares(key: Gf2_128, odd_powers: &[Gf2_128]) -> Vec<Gf2_128> {
249250
let base = shares[i / 2 - 1];
250251
shares.push(base * base);
251252
} else {
252-
// Odd power
253+
// Odd power.
253254
shares.push(odd_powers[odd_idx]);
254255
odd_idx += 1;
255256
}
@@ -423,7 +424,7 @@ mod tests {
423424
let sender_key: u128 = rng.random();
424425
let receiver_key: u128 = h ^ sender_key;
425426

426-
// Message length is not a multiple of the block length
427+
// Message length is not a multiple of the block length.
427428
let message: Vec<u8> = (0..14).map(|_| rng.random()).collect();
428429

429430
let (mut sender, mut receiver) = create_pair();

crates/mpc-tls/src/record_layer/decrypt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ impl DecryptOp {
197197

198198
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
199199
pub(crate) enum DecryptMode {
200+
/// The plaintext is private.
200201
Private,
202+
/// The plaintext is public.
201203
Public,
202204
}
203205

0 commit comments

Comments
 (0)