use crate::SessionInfo;
use super::{state::Verify as VerifyState, Verifier, VerifierError};
use mpz_circuits::types::Value;
use mpz_garble::{Memory, Verify};
use mpz_ot::CommittedOTSender;
use serio::stream::IoStreamExt;
use tlsn_common::msg::ServerIdentityProof;
use tlsn_core::transcript::{get_value_ids, Direction, PartialTranscript};
use tracing::{info, instrument};
impl Verifier<VerifyState> {
#[instrument(parent = &self.span, level = "info", skip_all, err)]
pub async fn receive(&mut self) -> Result<PartialTranscript, VerifierError> {
self.state
.mux_fut
.poll_with(async {
let partial_transcript: PartialTranscript = self.state.io.expect_next().await?;
info!("Received partial transcript from prover");
if partial_transcript.len_sent()
!= self.state.connection_info.transcript_length.sent as usize
|| partial_transcript.len_received()
!= self.state.connection_info.transcript_length.received as usize
{
return Err(VerifierError::verify(
"prover sent transcript with incorrect length",
));
}
let sent_value_ids =
get_value_ids(Direction::Sent, partial_transcript.sent_authed());
let recv_value_ids =
get_value_ids(Direction::Received, partial_transcript.received_authed());
let value_refs = sent_value_ids
.chain(recv_value_ids)
.map(|id| {
self.state
.vm
.get_value(id.as_str())
.expect("Byte should be in VM memory")
})
.collect::<Vec<_>>();
let values = partial_transcript
.iter(Direction::Sent)
.chain(partial_transcript.iter(Direction::Received))
.map(Value::U8)
.collect::<Vec<_>>();
self.state.vm.verify(&value_refs, &values).await?;
info!("Successfully verified purported transcript");
Ok::<_, VerifierError>(partial_transcript)
})
.await
}
#[instrument(parent = &self.span, level = "info", skip_all, err)]
pub async fn finalize(self) -> Result<SessionInfo, VerifierError> {
let VerifyState {
mut io,
mux_ctrl,
mut mux_fut,
mut vm,
mut ot_send,
mut ctx,
server_ephemeral_key,
connection_info,
..
} = self.state;
let ServerIdentityProof {
name: server_name,
data,
} = mux_fut
.poll_with(async {
ot_send.reveal(&mut ctx).await?;
vm.finalize().await?;
info!("Finalized all MPC");
let identity_proof: ServerIdentityProof = io.expect_next().await?;
Ok::<_, VerifierError>(identity_proof)
})
.await?;
data.verify_with_provider(
self.config.crypto_provider(),
connection_info.time,
&server_ephemeral_key,
&server_name,
)
.map_err(VerifierError::verify)?;
info!("Successfully verified session");
if !mux_fut.is_complete() {
mux_ctrl.mux().close();
mux_fut.await?;
}
Ok(SessionInfo {
server_name,
connection_info,
})
}
}