stoffel-rust-sdk 0.1.3

Rust SDK for building Stoffel MPC applications
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Built runtime for a compiled or loaded Stoffel program.
//!
//! A runtime owns program metadata, validated MPC/network settings, and local
//! execution inputs. It can construct participant builders or execute supported
//! local development paths.

use std::path::{Path, PathBuf};
use std::time::Duration;

use crate::client::{ClientBuilder, OffChainClientConfigBuilder};
use crate::config::{
    MpcConfig, MpcConfigSummary, NetworkConfig, NetworkConfigSummary, NetworkDeployment,
};
use crate::error::{Error, Result};
use crate::program::{BytecodeSummary, Program, ProgramSummary};
use crate::server::ServerBuilder;
use crate::types::{ProgramArgs, Value};
use crate::vm;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct RuntimeSummary {
    pub program: ProgramSummary,
    pub mpc: Option<MpcConfigSummary>,
    pub network: Option<NetworkConfigSummary>,
    pub named_input_count: usize,
    pub client_input_count: usize,
    pub expected_clients: Option<usize>,
    pub local_runner_configured: bool,
}

#[derive(Debug, Clone)]
pub struct StoffelRuntime {
    program: Program,
    mpc_config: Option<MpcConfig>,
    network_config: Option<NetworkConfig>,
    local_runner_path: Option<PathBuf>,
    inputs: Vec<(String, Value)>,
    client_inputs: Vec<(u64, Vec<Value>)>,
    expected_clients: Option<usize>,
    /// Developer-supplied per-client output counts, used by the local runner as
    /// a fallback only when the program does not statically declare outputs for
    /// a client (otherwise the manifest count wins).
    client_output_counts: std::collections::HashMap<u64, u64>,
}

impl StoffelRuntime {
    pub(crate) fn new(
        program: Program,
        mpc_config: Option<MpcConfig>,
        network_config: Option<NetworkConfig>,
        local_runner_path: Option<PathBuf>,
        inputs: Vec<(String, Value)>,
        client_inputs: Vec<(u64, Vec<Value>)>,
        expected_clients: Option<usize>,
    ) -> Self {
        Self {
            program,
            mpc_config,
            network_config,
            local_runner_path,
            inputs,
            client_inputs,
            expected_clients,
            client_output_counts: std::collections::HashMap::new(),
        }
    }

    /// Set developer-supplied per-client output counts (CLI / Stoffel.toml).
    pub(crate) fn set_client_output_counts(&mut self, counts: std::collections::HashMap<u64, u64>) {
        self.client_output_counts = counts;
    }

    /// Developer-supplied per-client output counts (local-runner fallback).
    pub(crate) fn client_output_counts(&self) -> &std::collections::HashMap<u64, u64> {
        &self.client_output_counts
    }

    pub fn program(&self) -> &Program {
        &self.program
    }

    pub(crate) fn with_program_and_client_inputs(
        mut self,
        program: Program,
        client_inputs: Vec<(u64, Vec<Value>)>,
    ) -> Self {
        self.program = program;
        self.inputs.clear();
        self.client_inputs = client_inputs;
        self
    }

    /// Serialize the compiled program as CLI-compatible Stoffel bytecode.
    pub fn to_bytecode(&self) -> Result<Vec<u8>> {
        self.program.to_bytecode()
    }

    /// Save the compiled program as CLI-compatible Stoffel bytecode.
    pub fn save_bytecode(&self, path: impl AsRef<Path>) -> Result<()> {
        self.program.save_bytecode(path)
    }

    /// Summarize the CLI-compatible bytecode artifact without changing it.
    pub fn bytecode_summary(&self) -> Result<BytecodeSummary> {
        self.program.bytecode_summary()
    }

    pub fn client(&self) -> ClientBuilder {
        let builder = ClientBuilder::new().with_program(self.program.clone());
        match &self.network_config {
            Some(config) => match config.server_addresses() {
                Ok(addresses) => builder.servers(addresses),
                Err(error) => builder.configuration_error(error.to_string()),
            },
            None => builder,
        }
    }

    /// Create a client builder with this runtime's program and all deployment servers.
    pub fn client_for_deployment(&self, deployment: &NetworkDeployment) -> ClientBuilder {
        ClientBuilder::new()
            .with_program(self.program.clone())
            .network_deployment(deployment)
    }

    /// Create an off-chain client IO config builder from this runtime's typed program metadata.
    ///
    /// The runtime supplies the MPC backend, topology, and output count for
    /// `client_slot`. Callers still provide coordinator/node endpoints,
    /// timestamp, and client identity explicitly.
    pub fn offchain_client_config(&self, client_slot: u64) -> Result<OffChainClientConfigBuilder> {
        let client = self.program.client(client_slot).ok_or_else(|| {
            Error::Configuration(format!(
                "program does not declare ClientStore metadata for client slot {client_slot}"
            ))
        })?;
        let input_start_index = self
            .program
            .clients()
            .take_while(|schema| schema.client_slot() != client_slot)
            .map(|schema| schema.input_count() as u64)
            .sum();
        let mpc_config = self
            .mpc_config
            .as_ref()
            .ok_or_else(|| Error::Configuration("MPC configuration is required".to_owned()))?;
        Ok(OffChainClientConfigBuilder::default()
            .client_slot(client_slot)
            .input_start_index(input_start_index)
            .parties(mpc_config.parties)
            .threshold(mpc_config.threshold)
            .backend(mpc_config.backend)
            .input_types(client.inputs().iter().copied())
            .output_types(client.outputs().iter().copied()))
    }

    pub fn server(&self, party_id: usize) -> ServerBuilder {
        let mut builder = ServerBuilder::new(party_id).with_program(self.program.clone());
        if let Some(config) = &self.mpc_config {
            builder = builder.mpc_config(config);
        }
        match &self.network_config {
            Some(config) if config.network.party_id == party_id => builder.network_config(config),
            Some(config) => builder.configuration_error(format!(
                "network config party_id {} does not match requested server party_id {party_id}",
                config.network.party_id
            )),
            None => builder,
        }
    }

    /// Create a server builder for one party config while carrying this program.
    pub fn server_for_config(&self, config: &NetworkConfig) -> ServerBuilder {
        let mut builder = ServerBuilder::new(config.party_id()).with_program(self.program.clone());
        if let Some(mpc_config) = &self.mpc_config {
            builder = builder.mpc_config(mpc_config);
        }
        builder.network_config(config)
    }

    /// Create one server builder per party in a validated deployment plan.
    ///
    /// Each builder carries this runtime's compiled program metadata and uses
    /// the corresponding party's network config.
    pub fn servers_for_deployment(&self, deployment: &NetworkDeployment) -> Vec<ServerBuilder> {
        deployment
            .configs()
            .iter()
            .map(|config| self.server_for_config(config))
            .collect()
    }

    pub fn mpc_config(&self) -> Option<&MpcConfig> {
        self.mpc_config.as_ref()
    }

    pub fn network_config(&self) -> Option<&NetworkConfig> {
        self.network_config.as_ref()
    }

    pub fn mpc_summary(&self) -> Result<Option<MpcConfigSummary>> {
        self.mpc_config.as_ref().map(MpcConfig::summary).transpose()
    }

    pub fn network_summary(&self) -> Result<Option<NetworkConfigSummary>> {
        self.network_config
            .as_ref()
            .map(NetworkConfig::summary)
            .transpose()
    }

    pub fn summary(&self) -> Result<RuntimeSummary> {
        Ok(RuntimeSummary {
            program: self.program.summary(),
            mpc: self.mpc_summary()?,
            network: self.network_summary()?,
            named_input_count: self.inputs.len(),
            client_input_count: self.client_inputs.len(),
            expected_clients: self.expected_clients,
            local_runner_configured: self.local_runner_path.is_some(),
        })
    }

    pub fn local_runner_binary_path(&self) -> Option<&Path> {
        self.local_runner_path.as_deref()
    }

    pub fn inputs(&self) -> &[(String, Value)] {
        &self.inputs
    }

    pub fn client_inputs(&self) -> &[(u64, Vec<Value>)] {
        &self.client_inputs
    }

    pub fn configured_expected_clients(&self) -> Option<usize> {
        self.expected_clients
    }

    pub fn validate_client_inputs(&self) -> Result<()> {
        if let Some(expected_clients) = self.expected_clients {
            if expected_clients == 0 {
                return Err(Error::Configuration(
                    "expected_clients must be greater than 0".to_owned(),
                ));
            }
            self.program.validate_expected_clients(expected_clients)?;
        }
        self.program
            .validate_owned_client_inputs(&self.client_inputs)
    }

    /// Replace named function inputs for clear execution.
    pub fn with_inputs<V>(mut self, inputs: &[(&str, V)]) -> Self
    where
        V: Clone + Into<Value>,
    {
        self.inputs = inputs
            .iter()
            .map(|(name, value)| ((*name).to_owned(), value.clone().into()))
            .collect();
        self
    }

    /// Add one named function input for clear execution.
    pub fn with_input<V>(mut self, name: impl Into<String>, value: V) -> Self
    where
        V: Into<Value>,
    {
        self.inputs.push((name.into(), value.into()));
        self
    }

    /// Replace named function inputs with values loaded from a `.json`, `.csv`, or `.txt` file.
    pub fn with_input_file(mut self, path: impl AsRef<Path>) -> Result<Self> {
        self.inputs = crate::input_file::load_named_inputs_file(path)?;
        Ok(self)
    }

    /// Add one coordinator client input set for local networked execution.
    pub fn with_client_input<V>(mut self, client_slot: u64, values: &[V]) -> Self
    where
        V: Clone + Into<Value>,
    {
        self.client_inputs.push((
            client_slot,
            values.iter().cloned().map(Into::into).collect(),
        ));
        self
    }

    /// Replace local ClientStore inputs with values loaded from a `.json`, `.csv`, or `.txt` file.
    pub fn with_client_input_file(mut self, path: impl AsRef<Path>) -> Result<Self> {
        self.client_inputs = crate::input_file::load_client_inputs_file(path)?;
        Ok(self)
    }

    /// Replace all coordinator client input sets for local networked execution.
    pub fn with_client_inputs<V>(mut self, inputs: &[(u64, &[V])]) -> Self
    where
        V: Clone + Into<Value>,
    {
        self.client_inputs = inputs
            .iter()
            .map(|(client_slot, values)| {
                (
                    *client_slot,
                    values.iter().cloned().map(Into::into).collect(),
                )
            })
            .collect();
        self
    }

    /// Declare output-capable client slots `0..n-1` for local execution.
    pub fn expected_output_clients(mut self, n: usize) -> Self {
        self.expected_clients = Some(n);
        self
    }

    /// Set the `stoffel-run` binary path used by local coordinator execution.
    pub fn local_runner_path(mut self, path: impl AsRef<Path>) -> Self {
        self.local_runner_path = Some(path.as_ref().to_path_buf());
        self
    }

    /// Configure a real localhost coordinator-backed MPC run.
    ///
    /// This builder exposes local development controls without replacing the
    /// networking, coordinator, or protocol implementations owned by the lower
    /// crates.
    pub fn local_network(&self) -> LocalNetworkBuilder<'_> {
        LocalNetworkBuilder::new(self)
    }

    pub(crate) fn input_values_for_function(&self, function_name: &str) -> Result<Vec<Value>> {
        let function = self
            .program
            .function(function_name)
            .ok_or_else(|| Error::FunctionNotFound(function_name.to_owned()))?;
        function.validate_inputs(&self.inputs)?;
        let parameters = function.parameters();
        let mut values = Vec::with_capacity(parameters.len());

        for parameter in parameters {
            let value = self
                .inputs
                .iter()
                .find(|(name, _)| name == parameter)
                .map(|(_, value)| value)
                .expect("function input validation should guarantee one value per parameter");
            values.push(value.clone());
        }

        Ok(values)
    }

    pub fn execute_clear(&self) -> Result<Vec<Value>> {
        self.execute_clear_function("main")
    }

    pub fn execute_clear_function(&self, name: &str) -> Result<Vec<Value>> {
        vm::execute_clear(self, name)
    }

    /// Execute a named function with positional SDK values.
    ///
    /// This is the direct, local cleartext execution path for a loaded or
    /// compiled program. Arguments are validated against the function metadata
    /// stored in the binary before the VM is invoked.
    pub fn execute<A>(&self, name: &str, args: A) -> Result<Vec<Value>>
    where
        A: ProgramArgs,
    {
        let args = args.into_values();
        vm::execute_clear_with_sdk_args(self, name, &args)
    }

    /// Execute `main` with positional SDK values.
    pub fn execute_main<A>(&self, args: A) -> Result<Vec<Value>>
    where
        A: ProgramArgs,
    {
        self.execute("main", args)
    }

    /// Execute the runtime's `main` entrypoint using the local coordinator runner.
    ///
    /// This uses the same real localhost party mesh as [`crate::Stoffel::execute_local`].
    /// It is intended for runtimes that were built from no-argument
    /// `ClientStore` entrypoints with client input supplied through the builder.
    pub async fn execute_local(&self) -> Result<Vec<Value>> {
        self.execute_local_function("main").await
    }

    /// Execute a named runtime entrypoint using the local coordinator runner.
    pub async fn execute_local_function(&self, name: &str) -> Result<Vec<Value>> {
        vm::execute_local(self, name).await
    }
}

#[derive(Debug, Clone)]
pub struct LocalNetworkBuilder<'a> {
    runtime: &'a StoffelRuntime,
    entry: String,
    runner_path: Option<PathBuf>,
    timeout: Option<Duration>,
}

impl<'a> LocalNetworkBuilder<'a> {
    fn new(runtime: &'a StoffelRuntime) -> Self {
        Self {
            runtime,
            entry: "main".to_owned(),
            runner_path: None,
            timeout: None,
        }
    }

    /// Select the runtime entrypoint to execute. Default is `main`.
    pub fn entry(mut self, entry: impl Into<String>) -> Self {
        self.entry = entry.into();
        self
    }

    /// Override the `stoffel-run` binary path for this local run.
    pub fn runner_path(mut self, path: impl AsRef<Path>) -> Self {
        self.runner_path = Some(path.as_ref().to_path_buf());
        self
    }

    /// Set the timeout applied to the local coordinator and party processes.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    pub fn configured_entry(&self) -> &str {
        &self.entry
    }

    pub fn configured_runner_path(&self) -> Option<&Path> {
        self.runner_path.as_deref()
    }

    pub fn configured_timeout(&self) -> Option<Duration> {
        self.timeout
    }

    /// Execute the configured local coordinator-backed MPC run.
    pub async fn run(self) -> Result<Vec<Value>> {
        if matches!(self.timeout, Some(timeout) if timeout.is_zero()) {
            return Err(Error::Configuration(
                "local network timeout must be greater than zero".to_owned(),
            ));
        }
        vm::execute_local_with_options(
            self.runtime,
            &self.entry,
            vm::LocalExecutionOptions {
                runner_path: self.runner_path,
                timeout: self.timeout,
            },
        )
        .await
    }
}