tlsn_prover/
state.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! TLS prover states.

use mpz_core::serialize::CanonicalSerialize;
use mpz_garble::protocol::deap::PeerEncodings;
use mpz_garble_core::{encoding_state, EncodedValue};
use std::collections::HashMap;
use tls_mpc::MpcTlsLeader;
use tlsn_common::{
    mux::{MuxControl, MuxFuture},
    Context, DEAPThread, Io, OTReceiver,
};
use tlsn_core::{
    connection::{ConnectionInfo, ServerCertData},
    transcript::{encoding::EncodingProvider, Direction, Idx, Transcript, TranscriptCommitConfig},
};

/// Entry state
pub struct Initialized;

opaque_debug::implement!(Initialized);

/// State after MPC setup has completed.
pub struct Setup {
    pub(crate) io: Io,
    pub(crate) mux_ctrl: MuxControl,
    pub(crate) mux_fut: MuxFuture,

    pub(crate) mpc_tls: MpcTlsLeader,
    pub(crate) vm: DEAPThread,
    pub(crate) ot_recv: OTReceiver,
    pub(crate) ctx: Context,
}

opaque_debug::implement!(Setup);

/// State after the TLS connection has been closed.
pub struct Closed {
    pub(crate) io: Io,
    pub(crate) mux_ctrl: MuxControl,
    pub(crate) mux_fut: MuxFuture,

    pub(crate) vm: DEAPThread,
    pub(crate) ot_recv: OTReceiver,
    pub(crate) ctx: Context,

    pub(crate) connection_info: ConnectionInfo,
    pub(crate) server_cert_data: ServerCertData,

    pub(crate) transcript: Transcript,
}

opaque_debug::implement!(Closed);

/// Notarizing state.
pub struct Notarize {
    pub(crate) io: Io,
    pub(crate) mux_ctrl: MuxControl,
    pub(crate) mux_fut: MuxFuture,

    pub(crate) vm: DEAPThread,
    pub(crate) ot_recv: OTReceiver,
    pub(crate) ctx: Context,

    pub(crate) connection_info: ConnectionInfo,
    pub(crate) server_cert_data: ServerCertData,

    pub(crate) transcript: Transcript,
    pub(crate) encoding_provider: Box<dyn EncodingProvider + Send + Sync>,

    pub(crate) transcript_commit_config: Option<TranscriptCommitConfig>,
}

opaque_debug::implement!(Notarize);

impl From<Closed> for Notarize {
    fn from(state: Closed) -> Self {
        struct HashMapProvider(HashMap<String, EncodedValue<encoding_state::Active>>);

        impl EncodingProvider for HashMapProvider {
            fn provide_encoding(&self, direction: Direction, idx: &Idx) -> Option<Vec<u8>> {
                let mut encoding = Vec::new();
                let prefix = match direction {
                    Direction::Sent => "tx/",
                    Direction::Received => "rx/",
                };
                for i in idx.iter() {
                    encoding
                        .extend_from_slice(&self.0.get(&format!("{}{}", prefix, i))?.to_bytes());
                }

                Some(encoding)
            }
        }

        let encoding_provider = HashMapProvider(collect_encodings(&state.vm, &state.transcript));

        Self {
            io: state.io,
            mux_ctrl: state.mux_ctrl,
            mux_fut: state.mux_fut,
            vm: state.vm,
            ot_recv: state.ot_recv,
            ctx: state.ctx,
            connection_info: state.connection_info,
            server_cert_data: state.server_cert_data,
            transcript: state.transcript,
            encoding_provider: Box::new(encoding_provider),
            transcript_commit_config: None,
        }
    }
}

/// Proving state.
pub struct Prove {
    pub(crate) io: Io,
    pub(crate) mux_ctrl: MuxControl,
    pub(crate) mux_fut: MuxFuture,

    pub(crate) vm: DEAPThread,
    pub(crate) ot_recv: OTReceiver,
    pub(crate) ctx: Context,

    pub(crate) server_cert_data: ServerCertData,

    pub(crate) transcript: Transcript,
}

impl From<Closed> for Prove {
    fn from(state: Closed) -> Self {
        Self {
            io: state.io,
            mux_ctrl: state.mux_ctrl,
            mux_fut: state.mux_fut,
            vm: state.vm,
            ot_recv: state.ot_recv,
            ctx: state.ctx,
            server_cert_data: state.server_cert_data,
            transcript: state.transcript,
        }
    }
}

#[allow(missing_docs)]
pub trait ProverState: sealed::Sealed {}

impl ProverState for Initialized {}
impl ProverState for Setup {}
impl ProverState for Closed {}
impl ProverState for Notarize {}
impl ProverState for Prove {}

mod sealed {
    pub trait Sealed {}
    impl Sealed for super::Initialized {}
    impl Sealed for super::Setup {}
    impl Sealed for super::Closed {}
    impl Sealed for super::Notarize {}
    impl Sealed for super::Prove {}
}

fn collect_encodings(
    vm: &impl PeerEncodings,
    transcript: &Transcript,
) -> HashMap<String, EncodedValue<encoding_state::Active>> {
    let tx_ids = (0..transcript.sent().len()).map(|id| format!("tx/{id}"));
    let rx_ids = (0..transcript.received().len()).map(|id| format!("rx/{id}"));

    let ids = tx_ids.chain(rx_ids).collect::<Vec<_>>();
    let id_refs = ids.iter().map(|id| id.as_ref()).collect::<Vec<_>>();

    vm.get_peer_encodings(&id_refs)
        .expect("encodings for all transcript values should be present")
        .into_iter()
        .zip(ids)
        .map(|(encoding, id)| (id, encoding))
        .collect()
}