2.2. The ML-KEM Braid Protocol
2.2.1. Overview
The ML-KEM Braid protocol takes advantage of the incremental interface ML-KEM described above to parallelize message sending and speed recovery from compromise. Specifically, the incremental interface allows ct1 to be sampled after receiving just a header, after which ct1 and ek_vector - the largest components of the ciphertext and encapsulation key - can be sent in parallel.
The following is a high level description of one epoch of the ML-KEM Braid protocol.
-
A samples a new ML-KEM keypair: (dk, ek_seed, ek_vector) = ML-KEM-KeyGen()src/incremental_mlkem768.rs:34.
-
A encodes a header message, ek_seed || SHA3-256(ek_seed || ek_vector), and begins sending it to B in chunksunchunked/send_ek.rs:82-98, chunked/send_ek.rs:54.
-
When B receives enough chunks to reconstruct the message, they decode and compute (encaps_secret, ct1, shared_secret) = ML-KEM-Encaps1(ek_seed, SHA3-256(ek_seed || ek_vector)). B stores encaps_secret and shared_secret for later useunchunked/send_ct.rs:119-148, chunked/send_ct.rs:118-138.
-
B encodes ct1 and begins sending it to A in chunksunchunked/send_ct.rs:119-148, chunked/send_ct.rs:208-223.
-
When A receives the first chunk of ct1, they stop sending chunks of the header and start sending chunks of ek_vectorchunked/send_ek.rs:112.
-
Now A and B send their messages in parallel.
-
When A receives all of ct1 they begin acknowledging the receipt in future messages sent to Bchunked/send_ek.rs:144.
-
Once B receives all of ek_vector and receives an acknowledgment that ct1 was received, they compute ct2 = ML-KEM-Encaps2(encaps_secret, ek_seed, ek_vector)unchunked/send_ct.rs:190.
-
B encodes ct2 and begins sending it to A in chunkschunked/send_ct.rs:298.
-
When A receives the first chunk of ct2, they stop sending chunks of ek_vectorchunked/send_ek.rs:176.
-
When A receives all of ct2, they decapsulate the shared secret: shared_secret = ML-KEM-Decaps(dk, ct1, ct2)unchunked/send_ek.rs:146.
-
Now A and B switch roles. A begins waiting for a header message from Bchunked/send_ek.rs:223, and indicates it has moved to the next epoch when sending messages to B.
-
Once B receives a message showing that A has advanced to the next epoch, they sample a new keypair and begin againchunked/send_ct.rs:308-311.
While this captures the main flow of the protocol, it does not tell us how A and B know when they can use the keys returned by the protocol. Clearly, when B returns shared_secret above, they cannot use it to encrypt messages to A because A does not know shared_secret yet. This will be addressed by the values sending_epoch and receiving_epoch returned from the functions defined below - a value that tells the caller what latest epoch key known by both parties at the time a message was created.
The protocol below also performs optional authentication, with details presented in Section 2.4 and discussed further in Section 3.3.
2.2.2. Parameters
-
KEM: An IND-CPA secure Key Encapsulation Mechanism that offers an incremental interface. For this document it will be one of ML-KEM-512, ML-KEM-768, or ML-KEM-1024. src/incremental_mlkem768.rs itself uses ML-KEM-768 using the rust library libcrux_ml_kem.
-
Constants: Several constants are also associated with the KEM and are needed in the protocol description:
Constant | ML-KEM 512 | ML-KEM 768 | ML-KEM 1024 |
|---|---|---|---|
HEADER_SIZE | 64 | 64 | 64 |
EK_SIZE | 768 | 1152 | 1536 |
CT1_SIZE | 640 | 960 | 1408 |
CT2_SIZE | 128 | 128 | 160 |
-
Encode/Decode: An erasure code or fountain code that can encode a long message into a stream of codewords, or chunks, so that when the receiver gets a sufficient number of these chunks, regardless of order or dropped codewords, they will be able to reconstruct the original message. Reed-Solomon based erasure codes over GF(216)w/2 for a chunk size of w bytes are recommended. libsignal uses Reed-Solomon based erasure codes with chunks of size 32 bytes encoding/polynomial.rs:498. The polynomials used for the encoder are at most of degree 35 in v1 encoding/polynomial.rs:69.
-
Encode(byte_array) → encoder: Returns a stateful encoding object that produces a stream of codewords, or chunks, that can be decoded to reconstruct byte_arrayencoding/polynomial.rs:651. These codewords are accessed by calling the method encoder.next_chunk()encoding/polynomial.rs:711.
-
Decoder.new(message_size) → decoder: Returns a stateful decoding object that will decode a message of length message_size from a set of codewords produced by a single encoder. It exposes the functions:
-
decoder.add_chunk(chunk): Adds a codeword to the decoder's statesrc/encoding.rs:84.
-
decoder.has_message() → bool: Returns true when the decoder has received enough codewords to reconstruct the message.encoding/polynomial.rs:748
-
decoder.message() → maybe_byte_array: Returns the reconstructed message if possible, otherwise returns Null.encoding/polynomial.rs:860
-
-
-
EPOCH_TYPE: The unsigned integer type used to represent epochs. We recommend using unsigned 64-bit integerssrc/lib.rs:39.
-
ToBytes(epoch): Represent an epoch as a byte string. When EPOCH_TYPE is a 64-bit unsigned integer, use of big-endian encoding is recommended.
-
MAC(mac_key, msg): A message authentication code. HMAC-SHA256 is recommended and used in the library itself using libcruxsrc/authenticator.rs:99.
-
MAC_SIZE: Size of MAC's output, in bytes. In libsignal/SPQR the size is 32 bytessrc/authenticator.rs:34.
-
PROTOCOL_INFO: The concatenation of a protocol identifier, a string representation of KEM, and a string representation of MAC, separated with the delimiter "
_", such as "MyProtocol_MLKEM768_SHA-256". The string representations of the ML-KEM Braid parameters are defined by the implementer.src/authenticator.rs:46 -
KDF_AUTH(root_key, update_key, epoch): 64 bytes of output from the HKDF algorithm [5] using hash with inputssrc/authenticator.rs:44:
-
HKDF input key material = update_keysrc/authenticator.rs:45
-
HKDF salt = root_keysrc/authenticator.rs:51
-
HKDF info = PROTOCOL_INFO || ":Authenticator Update" || ToBytes(epoch)src/authenticator.rs:46
-
HKDF length = 64
-
-
KDF_OK(shared_secret, epoch): 32 bytes of output from the HKDF algorithm [5] using hash with inputsunchunked/send_ct.rs:129:
-
HKDF input key material = shared_secret
-
HKDF salt = A zero-filled byte sequence with length equal to the hash output length, in bytes.
-
HKDF info = PROTOCOL_INFO || ":SCKA Key" || ToBytes(epoch)
-
HKDF length = 32
-
2.2.3. Messages
Messages consist of the following fieldschunked/states.rs:41:
-
epoch (unsigned integer): Current epoch being negotiatedproto/pq_ratchet.proto:46
-
type (enum): One of {None, Hdr, Ek, EkCt1Ack, Ct1Ack, Ct1, Ct2} with the following meaningschunked/states.rs:31:
-
None: There is no payload
-
Hdr: The payload contains a chunk of the header.
-
Ek: The payload contains a chunk of the encapsulation key.
-
EkCt1Ack: The payload contains a chunk of the encapsulation key, and the sender has completely received ct1.
-
Ct1Ack: No payload, but the sender has completely received ct1.
-
Ct1: The payload contains a chunk of ct1.
-
Ct2: The payload contains a chunk of ct2.
-
-
data (bytes, optional): Erasure code chunk when type is not one of { None, Ct1Ack }
In what follows we will describe messages logically using object notation. Implementations may use a custom compact binary format or a general purpose serialization tool such as Protocol Buffers [6] to encode these messages. In the presence of bandwidth limits, implementers should consider that a custom format may allow larger chunk sizes and correspondingly improve post-compromise security (See Section 3.4).
2.2.4. Internal Authentication
While messaging protocols such as the Double Ratchet [2] provide ratcheted message authentication through the use of AEAD or explicit MACs on messages, it may be desirable for an SCKA protocol to provide internal authenticity guarantees. We attain this using a Ratcheted Authenticator.
2.2.4.1. Ratcheted Authenticator state variables
The Ratcheted Authenticator holds the following statesrc/authenticator.rs:27:
-
root_key: a 32 byte value.
-
mac_key: a 32 byte key for use with MAC.
2.2.4.2. Ratcheted Authenticator functions
The Ratcheted Authenticator offers a function to update the internal state with new entropy as well as functions to compute and verify MACs on ciphertexts and header messagessrc/authenticator.rs:33-105.
In the event of a verification failure, protocol participants should not proceed with the ML-KEM Braid session and should negotiate a new ML-KEM Braid session .
2.2.5. State Machine and Transitions
We describe the protocol as a state machine that transitions from state to state when sending or receiving messages. The states and transitions can be seen in the following figure, which can serve as a helpful reference in the detailed descriptions that follow.
State machine transitions for the ML-KEM Braid Protocol. Each transition is labeled with a number that can be found in the pseudocode below.
All states of the agents contain at least the following two variables:
-
epoch: an unsigned integer identifying the epoch of the key being negotiated.
-
auth: an Authenticator object.
The following describes the state of an agent when they are transmitting an encapsulation key and awaiting the corresponding ciphertext. For each state we define the SCKA Send() and Receive() functions.
2.2.5.1. KeysUnsampled
Represents an agent that is ready to sample a new KEM keypair on the next send event. It carries no additional statechunked/send_ek.rs:16-18.
When sending a message, the KeysUnsampled agent samples a new keypair, starts sending a header message, and transitions into the KeysSampled state. The KeysUnsampled agent ignores all messages it receives chunked: chunked/send_ek.rs:54-67; unchunked: unchunked/send_ek.rs:82-98.:
// UserA starts off in the state "KeysUnsampled" when sending the very first header chunk.
pub fn send_hdr_chunk<R: Rng + CryptoRng>(self, rng: &mut R) -> (KeysSampled, Chunk) {
// The line below calls the "unchunked" function that actually
// constructs the header
let (uc, hdr, mac) = self.uc.send_header(rng);
let to_send = [hdr, mac].concat();
let encoder = polynomial::PolyEncoder::encode_bytes(&to_send);
let chunk = sending_hdr.next_chunk();
// The line below is where the state transition actually happens to the "KeysSampled" state
(KeysSampled { uc, sending_hdr }, chunk)
}
2.2.5.2. KeysSampled
Represents an agent that has sampled a KEM keypair and is sending the header. Additional state includeschunked/send_ek.rs:21-24, unchunked/send_ek.rs:45-52:
-
dk: a KEM decapsulation key
-
ek_vector: vector part of a KEM encapsulation key
-
header_encoder
The KeysSampled agent sends chunks of the headerchunked/send_ek.rs:71-78. When it receives a message of type Ct1 it knows that the other party has received the complete header so it transitions into the HeaderSent state, in which it will begin sending chunks of ek_vectorchunked/send_ek.rs:81-97:
// UserA stays in the "KeysSampled" state when sending the rest of the header chunks.
pub fn send_hdr_chunk(self) -> (KeysSampled, Chunk) {
let Self {
uc,
mut sending_hdr,
} = self;
let chunk = sending_hdr.next_chunk();
(KeysSampled { uc, sending_hdr }, chunk)
}
// UserA switches from the "KeysSampled" state to the "HeaderSent" state after receiving the first chunk of ct1 from UserB
pub fn recv_ct1_chunk(self, epoch: Epoch, chunk: &Chunk) -> HeaderSent {
assert_eq!(epoch, self.uc.epoch);
let decoder = polynomial::PolyDecoder::new(incremental_mlkem768::CIPHERTEXT1_SIZE);
hax_lib::assume!(decoder.is_ok());
let mut receiving_ct1 = decoder.expect("should be able to decode header size");
receiving_ct1.add_chunk(chunk);
let (uc, ek) = self.uc.send_ek();
let encoder = polynomial::PolyEncoder::encode_bytes(&ek);
hax_lib::assume!(encoder.is_ok());
let sending_ek = encoder.expect("should be able to send ek");
HeaderSent {
uc,
receiving_ct1,
sending_ek,
}
}
2.2.5.3. HeaderSent
Represents an agent that has completed sending a header, is currently sending an ek_vector, and is receiving chunks of ct1. Additional state includeschunked/send_ek.rs:27, unchunked/send_ek.rs:56-61:
-
dk: a KEM decapsulation key
-
ct1_decoder
-
ek_encoder
In the HeaderSent state, an agent sends chunks of its ek_vectorchunked/send_ek.rs:112-127. When receiving a message of type Ct1 for the current epoch, if it has enough chunks to decode the incoming ct1, it transitions to the Ct1Received statechunked/send_ek.rs:130-152:
// UserA sends ek_vector in chunks and remains in state "HeaderState".
pub fn send_ek_chunk(self) -> (HeaderSent, Chunk) {
let Self {
uc,
mut sending_ek,
receiving_ct1,
} = self;
let chunk = sending_ek.next_chunk();
(
HeaderSent {
uc,
sending_ek,
receiving_ct1,
},
chunk,
)
}
// UserA makes the decision here to either switch to the "Ct1Received" state or remain in the "HeaderSent" state depending on whether or not they have received enough chunks to reconstruct ct1. The code structure is similar to what we have seen thus far.
// Note that ct1 should be 960 byte long in total. Each chunk is 32 bytes long. Meaning that the user should expect to receive 30 unique chunks before reconstructing the message.
pub fn recv_ct1_chunk(self, epoch: Epoch, chunk: &Chunk) -> HeaderSentRecvChunk {
// This assertions main purpose is to confirm that there isn't an epoch mismatch between the two users.
assert_eq!(epoch, self.uc.epoch);
let Self {
uc,
sending_ek,
mut receiving_ct1,
} = self;
receiving_ct1.add_chunk(chunk);
// The user should have already changed states if they already had enough points to reconstruct ct1.
hax_lib::assume!(
receiving_ct1.get_pts_needed() <= polynomial::MAX_STORED_POLYNOMIAL_DEGREE_V1
);
if let Some(decoded) = receiving_ct1.decoded_message() {
hax_lib::assume!(decoded.len() == 960);
let uc = uc.recv_ct1(epoch, decoded);
// I believe that the "Ct1Received" state is serialized and sent over to the other user [see]. When serialized, it's turned into the ct1_ack acknowledgement flag defined [here].
HeaderSentRecvChunk::Done(Ct1Received { uc, sending_ek })
} else {
HeaderSentRecvChunk::StillReceiving(HeaderSent {
uc,
sending_ek,
receiving_ct1,
})
}
}
2.2.5.4. Ct1Received
Represents an agent that has completely received ct1 and is still sending chunks of ek_vector. Additional state includeschunked/send_ek.rs:35-38, unchunked/send_ek.rs:65-72:
-
dk: a KEM decapsulation key
-
ct1: The compressed public key part of a KEM ciphertext
-
ek_encoder
In the Ct1Received state an agent sends chunks of the ek_vector until it receives a chunk of ct2. At that point it knows ek_vector has been received so it transitions into the EkSentCt1Received statechunked/send_ek.rs:168-180:
// UserA switches from "Ct1Received" to "EkSentCt1Received". The function below is the first one called when first receiving ct2.
pub fn recv_ct2_chunk(self, epoch: Epoch, chunk: &Chunk) -> EkSentCt1Received {
assert_eq!(epoch, self.uc.epoch);
let decoder = polynomial::PolyDecoder::new(
incremental_mlkem768::CIPHERTEXT2_SIZE + authenticator::Authenticator::MACSIZE,
);
hax_lib::assume!(decoder.is_ok());
let mut receiving_ct2 = decoder.expect("should be able to decode ct2+mac size");
receiving_ct2.add_chunk(chunk);
EkSentCt1Received {
uc: self.uc,
receiving_ct2,
}
}
2.2.5.5. EkSentCt1Received
Represents an agent that has received ct1, sent ek, and is receiving chunks of ct2. Additional state includesunchunked/send_ek.rs:65-72:
-
dk: a KEM decapsulation key
-
ct1: The compressed public key part of a KEM ciphertext
-
ct2_decoder
In the EkSentCt1Received state an agent doesn't send any data to the other party and it receives chunks of ct2. Once ct2 is received, it verifies the MAC, decapsulates the secret, emits the key, and transitions to the NoHeaderReceived state to wait for the other party to begin sending an encapsulation key for the next epochchunked/send_ek.rs:195-234:
pub fn recv_ct2_chunk(
self,
epoch: Epoch,
chunk: &Chunk,
) -> Result<EkSentCt1ReceivedRecvChunk, Error> {
assert_eq!(epoch, self.uc.epoch);
let Self {
uc,
mut receiving_ct2,
} = self;
receiving_ct2.add_chunk(chunk);
hax_lib::assume!(
receiving_ct2.get_pts_needed() <= polynomial::MAX_STORED_POLYNOMIAL_DEGREE_V1
);
// If ct2 decodes properly and the mac authentication works out
// UserA transitions from "EkSentCt1Received" to "NoHeaderReceived".
// In other words, the users switch roles and UserA now awaits UserB's
// header and ek_vector.
if let Some(mut ct2) = receiving_ct2.decoded_message() {
let mac: authenticator::Mac = ct2
.drain(incremental_mlkem768::CIPHERTEXT2_SIZE..)
.collect();
hax_lib::assume!(
ct2.len() == incremental_mlkem768::CIPHERTEXT2_SIZE
&& mac.len() == authenticator::Authenticator::MACSIZE
);
let (uc, sec) = uc.recv_ct2(ct2, mac)?;
let decoder = polynomial::PolyDecoder::new(
incremental_mlkem768::HEADER_SIZE + authenticator::Authenticator::MACSIZE,
);
hax_lib::assume!(decoder.is_ok());
Ok(EkSentCt1ReceivedRecvChunk::Done((
send_ct::NoHeaderReceived {
uc,
receiving_hdr: decoder.expect("should be able to decode header size"),
},
sec,
)))
} else {
Ok(EkSentCt1ReceivedRecvChunk::StillReceiving(
EkSentCt1Received { uc, receiving_ct2 },
))
}
}
The following describes the state of an agent when they are transmitting a ciphertext in response to an encapsulation key.
2.2.5.6. NoHeaderReceived
Represents an agent that is receiving a header. Additional state includeschunked/send_ct.rs:15-19, unchunked/send_ct.rs:44-47:
-
header_decoder
In the NoHeaderReceived state an agent receives chunks of the header. Once the header has been completely received, it transitions to the HeaderReceived state, but does not sample the ciphertext yetchunked/send_ct.rs:75-105:
// Depending on whether or not UserB can reconstruct the header, UserB either stays in the "NoHeaderReceivedRecvChunk" state or switch to "HeaderReceived" by the end of this function.
pub fn recv_hdr_chunk(
self,
epoch: Epoch,
chunk: &Chunk,
) -> Result<NoHeaderReceivedRecvChunk, Error> {
// Recall that this protocol requires making sure that the users are at the correct epoch and state
assert_eq!(epoch, self.uc.epoch);
let Self {
uc,
mut receiving_hdr,
} = self;
receiving_hdr.add_chunk(chunk);
// The line below ensures that we only receive as many chunks as we need
// to reconstruct the message. Any more would be redundant and would signal that we haven't changed states appropriately.
hax_lib::assume!(
receiving_hdr.get_pts_needed() <= polynomial::MAX_STORED_POLYNOMIAL_DEGREE_V1
);
// If we can reconstruct the header, then we authenticate it and check that its of the appropriate length
if let Some(mut hdr) = receiving_hdr.decoded_message() {
let mac: authenticator::Mac = hdr.drain(incremental_mlkem768::HEADER_SIZE..).collect();
hax_lib::assume!(hdr.len() == 64 && mac.len() == authenticator::Authenticator::MACSIZE);
// We can now start reconstructing the encapsulation key
let receiving_ek = polynomial::PolyDecoder::new(incremental_mlkem768::ENCAPSULATION_KEY_SIZE);
hax_lib::assume!(receiving_ek.is_ok());
// We switch states since receiving the header is complete to "HeaderReceived"
Ok(NoHeaderReceivedRecvChunk::Done(HeaderReceived {
uc: uc.recv_header(epoch, hdr, &mac)?,
receiving_ek: receiving_ek.expect("should be able to decode EncapsulationKey size"),
}))
} else {
// Otherwise the header is still being transmitted and the user remains in the "NoHeaderReceivedRecvChunk" state.
Ok(NoHeaderReceivedRecvChunk::StillReceiving(Self {
uc,
receiving_hdr,
}))
}
}
2.2.5.7. HeaderReceived
Represents an agent that has received a header and is prepared to sample a new ct1 on the next send. Additional state includeschunked/send_ct.rs:22-26, unchunked/send_ct.rs:51-56:
-
ek_seed: seed of a KEM encapsulation key
-
hek: SHA3 hash of ek_seed || ek_vector
-
ek_decoder
In the HeaderReceived state an agent is ready to sample a ciphertext when asked to send. When it does this, it computes the encapsulated shared secret for this epoch and returns it to the caller. While it has an ek_decoder prepared, it will not receive any ek_vector chunks until after it has sent a ct1 message - and then it will have transitioned out of this statechunked/send_ct.rs:118-138. So the Receive function is a no-op:
// Here UserB switches from the "HeaderReceived" state to the "Ct1Sampled" state and will remain in this state until UserA acks receiving all of ct1
pub fn send_ct1_chunk<R: Rng + CryptoRng>(
self,
rng: &mut R,
) -> (Ct1Sampled, Chunk, EpochSecret) {
let Self { uc, receiving_ek } = self;
let (uc, ct1, epoch_secret) = uc.send_ct1(rng);
let encoder = polynomial::PolyEncoder::encode_bytes(&ct1);
let mut sending_ct1 = encoder.expect("should be able to send CTSIZE");
let chunk = sending_ct1.next_chunk();
(
Ct1Sampled {
uc,
sending_ct1,
receiving_ek,
},
chunk,
// Note that the "epoch_secret" is just the reconstructed shared secret and the epoch identifier
epoch_secret,
)
}
2.2.5.8. Ct1Sampled
Represents an agent that has received a header, has sampled ct1, and is sending it in chunks. Additional state includeschunked/send_ct.rs:29-34, unchunked/send_ct.rs:60-69:
-
ek_seed: seed of a KEM encapsulation key
-
hek: SHA3 hash of ek_seed || ek_vector
-
encaps_secret: the secret material used to encapsulate a KEM ciphertext
-
ct1: The compressed public key part of a KEM ciphertext
-
ct1_encoder
-
ek_decoder
The Ct1Sampled state has the most complex transition possibilities. In this state an agent is receiving chunks of ek_vector and sending chunks of ct1. If it receives all of ek_vector before receiving an acknowledgment that ct1 was received, it will transition to EkReceivedCt1Sampled. On the other hand, if it receives an acknowledgment that ct1 was received before ek_vector has been completely received, it will transition to Ct1Acknowledged. If this agent both receives an acknowledgment for Ct1 and receives the last chunk of ek_vector in a single receive call, it will compute ct1 and transition to Ct2Sampledchunked/send_ct.rs:165-206:
pub fn recv_ek_chunk(
self,
epoch: Epoch,
chunk: &Chunk,
ct1_ack: bool,
) -> Result<Ct1SampledRecvChunk, Error> {
//// All the code over the here has the same structure as we've seen so far:
// 1- Combine the chunked pieces of messages
// 2- See if you have enough to reconstruct the msg
// 3- Try to reconstruct it
// 4- Ensure that the reconstructed messages is of the correct size
let Self {
uc,
mut receiving_ek,
sending_ct1,
} = self;
receiving_ek.add_chunk(chunk);
hax_lib::assume!(
receiving_ek.get_pts_needed() <= polynomial::MAX_STORED_POLYNOMIAL_DEGREE_V1
);
// Note that the actual reconstruction of the message happens [here]
Ok(if let Some(decoded) = receiving_ek.decoded_message() {
hax_lib::assume!(decoded.len() == 1152);
let uc = uc.recv_ek(epoch, decoded)?;
///// *********************************************************************
// Assuming that UserB has received an acknowledgement that ct1 was received by UserA
if ct1_ack {
// If Ct1 was acked, then UserB constructs Ct2 prior to sending it in chunks. The line below is where the ML-KEM encapsulation happens [see] for more details.
let (uc, ct2, mac) = uc.send_ct2();
hax_lib::assume!(
[ct2.clone(), mac.clone()].concat().len() % 2 == 0
&& [ct2.clone(), mac.clone()].concat().len()
<= (1 << 16) * crate::encoding::polynomial::NUM_POLYS
);
// UserB then switches states to "Ct2Sampled". The only way to reach "Ct2Sampled" is for UserB to receive ek_vector and UserA to receive ct1
Ct1SampledRecvChunk::Done(Ct2Sampled {
uc,
sending_ct2: send_ct2_encoder(&ct2, &mac),
})
} else { // If ct1 was not acked and UserB can still reconstruct ek_vector, then UserB keeps sending chunks of Ct1 and switches over to the "EkReceivedCt1Sampled" state from the "Ct1Sampled" state. In this case, [this] is where ct1_ack is eventually received and ct2 sampled.
Ct1SampledRecvChunk::StillSending(EkReceivedCt1Sampled { uc, sending_ct1 })
}
} else if ct1_ack { // If ct1 was acked but UserB could not reconstruct ek_vector, then they stop sending chunks of ct1 and still expect chunks of ek. In this case, they switched over to the state "Ct1Acknowledged".
//
// Note that in this case, UserB samples ct2 over [here] but does not send it until ek is received.
Ct1SampledRecvChunk::StillReceiving(Ct1Acknowledged { uc, receiving_ek })
} else { // If neither ct1 was acked nor ek_vector was receiver, UserB remains in their current state and sends over chunks of ct1 while expecting more pieces of ek_vector
Ct1SampledRecvChunk::StillReceivingStillSending(Self {
uc,
receiving_ek,
sending_ct1,
})
})
}
2.2.5.9. EkReceivedCt1Sampled
Represents an agent that has received an encapsulation key and is still sending ct1 in chunks. Additional state includeschunked/send_ct.rs:37-40, unchunked/send_ct.rs:73-82:
-
encaps_secret: the secret material used to encapsulate a KEM ciphertext
-
ct1: The compressed public key part of a KEM ciphertext
-
ek_seed
-
ek_vector
-
ct1_encoder
In the EkReceivedCt1Sampled state an agent sends chunks of ct1 and awaits an acknowledgment that it has been received. When that acknowledgment comes, it computes ct2 and transitions to the Ct2Sampled statechunked/send_ct.rs:241-249:
// Once in the "EkSentCt1Received" state, UserA receives chunks of ct2 using the function below.
pub fn recv_ct2_chunk(
self,
epoch: Epoch,
chunk: &Chunk,
) -> Result<EkSentCt1ReceivedRecvChunk, Error> {
assert_eq!(epoch, self.uc.epoch);
let Self {
uc,
mut receiving_ct2,
} = self;
receiving_ct2.add_chunk(chunk);
hax_lib::assume!(
receiving_ct2.get_pts_needed() <= polynomial::MAX_STORED_POLYNOMIAL_DEGREE_V1
);
// Again, this is where UserA attempts to decode Ct2
// For more information on the decapsulation
// and the mac authentication [see]
if let Some(mut ct2) = receiving_ct2.decoded_message() {
let mac: authenticator::Mac = ct2
.drain(incremental_mlkem768::CIPHERTEXT2_SIZE..)
.collect();
hax_lib::assume!(
ct2.len() == incremental_mlkem768::CIPHERTEXT2_SIZE
// The Mac size is 32 byte long [see]
&& mac.len() == authenticator::Authenticator::MACSIZE
);
let (uc, sec) = uc.recv_ct2(ct2, mac)?;
let decoder = polynomial::PolyDecoder::new(
incremental_mlkem768::HEADER_SIZE + authenticator::Authenticator::MACSIZE,
);
hax_lib::assume!(decoder.is_ok());
// Once Ct2 has been received, the users switch roles and UserA now expects a header from UserB
// Note that "uc" contains UserA's current epoch and is serialized and sent over to UserB to signal that they've moved on to the next epoch. The epoch itself is incremented over [here].
Ok(EkSentCt1ReceivedRecvChunk::Done((
send_ct::NoHeaderReceived {
uc,
receiving_hdr: decoder.expect("should be able to decode header size"),
},
sec,
)))
} else {
// If Ct2 has not been completely received yet, then UserA remains in the current state
Ok(EkSentCt1ReceivedRecvChunk::StillReceiving(
EkSentCt1Received { uc, receiving_ct2 },
))
}
}
2.2.5.10. Ct1Acknowledged
Represents an agent that has completed sending ct1 but is still receiving chunks of ek_vector. Additional state includes:
-
ek_seed: seed of a KEM encapsulation key
-
hek: SHA3 hash of ek_seed || ek_vector
-
encaps_secret: the secret material used to encapsulate a KEM ciphertext
-
ct1: The compressed public key part of a KEM ciphertext
-
ek_decoder
In the Ct1Acknowledged state an agent receives chunks of an incoming ek_vector. Once this has been completely received, it can compute ct2 and transition to the Ct2Sampled statechunked/send_ct.rs:265-289, unchunked/send_ct.rs:60-69:
pub fn recv_ek_chunk(
self,
epoch: Epoch,
chunk: &Chunk,
) -> Result<Ct1AcknowledgedRecvChunk, Error> {
let Self {
uc,
mut receiving_ek,
} = self;
receiving_ek.add_chunk(chunk);
hax_lib::assume!(
receiving_ek.get_pts_needed() <= polynomial::MAX_STORED_POLYNOMIAL_DEGREE_V1
);
Ok(if let Some(decoded) = receiving_ek.decoded_message() {
hax_lib::assume!(decoded.len() == 1152);
let uc = uc.recv_ek(epoch, decoded)?;
let (uc, ct2, mac) = uc.send_ct2();
Ct1AcknowledgedRecvChunk::Done(Ct2Sampled {
uc,
sending_ct2: send_ct2_encoder(&ct2, &mac),
})
} else {
Ct1AcknowledgedRecvChunk::StillReceiving(Self { uc, receiving_ek })
})
}
2.2.5.11. Ct2Sampled
Represents an agent that has completed sending ct1, received ek_vector, and is sending ct2. Additional state includeschunked/send_ct.rs:50-53, unchunked/send_ct.rs:85-88:
-
ct2_encoder
In the Ct2Sampled state an agent sends chunks of ct2 and waits for a message from the next epochchunked/send_ct.rs:298-305. Once a message from the next epoch is received, it transitions to the KeysUnsampled state and prepares to start sending a new encapsulation keychunked/send_ct.rs:308-311:
// UserB will only call this function once they've reached the "Ct2Sampled" state, meaning that they've received ek_vector and UserA received ct1. We emphasize this point because, as we will see next, the first chunks of ct2 will act as an ek_vector_ack for UserA.
pub fn send_ct2_chunk(self) -> (Ct2Sampled, Chunk) {
let Self {
uc,
mut sending_ct2,
} = self;
let chunk = sending_ct2.next_chunk();
(Self { uc, sending_ct2 }, chunk)
}
// Once UserB received the next epoch identifier from UserA, they switch to the "KeysUnsampled" state
pub fn recv_next_epoch(self, epoch: Epoch) -> send_ek::KeysUnsampled {
let uc = self.uc.recv_next_epoch(epoch);
send_ek::KeysUnsampled { uc }
}