stoffel-vm 0.1.3

Register-based virtual machine runtime and MPC execution engine for Stoffel programs
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use super::{share_fields, MpcValueError, MpcValueResult};
use crate::value_conversions::{usize_to_vm_i64, value_to_i64, value_to_usize};
use stoffel_vm_types::core_types::{
    ArrayRef, ObjectRef, ShareData, ShareType, TableMemory, TableRef, Value,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MatchingSharePair {
    pub share_type: ShareType,
    pub left_data: ShareData,
    pub right_data: ShareData,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ShareObjectRef(ObjectRef);

impl ShareObjectRef {
    pub const fn new_unchecked(object_ref: ObjectRef) -> Self {
        Self(object_ref)
    }

    pub fn validate<M: TableMemory + ?Sized>(
        store: &mut M,
        object_ref: ObjectRef,
    ) -> MpcValueResult<Self> {
        let share_ref = Self::new_unchecked(object_ref);
        share_ref.require_type_tag(store)?;
        Ok(share_ref)
    }

    pub const fn object_ref(self) -> ObjectRef {
        self.0
    }

    pub const fn table_ref(self) -> TableRef {
        TableRef::Object(self.0)
    }

    pub const fn into_value(self) -> Value {
        self.0.into_value()
    }

    pub fn is_share_object<M: TableMemory + ?Sized>(self, store: &mut M) -> bool {
        matches!(
            store.read_table_field(self.table_ref(), &field(share_fields::TYPE)),
            Ok(Some(Value::String(s))) if s == share_fields::TYPE_VALUE
        )
    }

    pub fn share_data<M: TableMemory + ?Sized>(
        self,
        store: &mut M,
    ) -> MpcValueResult<(ShareType, ShareData)> {
        self.require_type_tag(store)?;
        let metadata_type = self.decode_metadata_share_type(store)?;

        let data_field = store
            .read_table_field(self.table_ref(), &field(share_fields::DATA))
            .map_err(|e| {
                MpcValueError::table_memory_context("failed to read Share __data field", e)
            })?
            .ok_or_else(|| MpcValueError::message("Share object missing __data field"))?;

        match data_field {
            Value::Share(ty, data) if ty == metadata_type => Ok((ty, data)),
            Value::Share(ty, _) => Err(MpcValueError::message(format!(
                "Share metadata type {:?} does not match __data type {:?}",
                metadata_type, ty
            ))),
            _ => Err(MpcValueError::message(
                "Share __data field is not a Share value",
            )),
        }
    }

    pub fn share_type<M: TableMemory + ?Sized>(self, store: &mut M) -> MpcValueResult<ShareType> {
        self.share_data(store).map(|(ty, _)| ty)
    }

    pub fn party_id<M: TableMemory + ?Sized>(self, store: &mut M) -> MpcValueResult<usize> {
        self.require_type_tag(store)?;
        let party_id = store
            .read_table_field(self.table_ref(), &field(share_fields::PARTY_ID))
            .map_err(|e| {
                MpcValueError::table_memory_context("failed to read Share __party_id field", e)
            })?
            .ok_or_else(|| MpcValueError::missing_field("Share object", share_fields::PARTY_ID))?;

        Ok(value_to_usize(&party_id, "party_id")?)
    }

    fn require_type_tag<M: TableMemory + ?Sized>(self, store: &mut M) -> MpcValueResult<()> {
        let type_field = store
            .read_table_field(self.table_ref(), &field(share_fields::TYPE))
            .map_err(|e| {
                MpcValueError::table_memory_context("failed to read Share __type field", e)
            })?
            .ok_or_else(|| MpcValueError::message("Object is not a Share: missing __type field"))?;

        if type_field != Value::String(share_fields::TYPE_VALUE.to_string()) {
            return Err(MpcValueError::message(format!(
                "Object is not a Share: __type is {:?}, expected {:?}",
                type_field,
                share_fields::TYPE_VALUE
            )));
        }

        Ok(())
    }

    fn decode_metadata_share_type<M: TableMemory + ?Sized>(
        self,
        store: &mut M,
    ) -> MpcValueResult<ShareType> {
        let share_type_field = store
            .read_table_field(self.table_ref(), &field(share_fields::SHARE_TYPE))
            .map_err(|e| {
                MpcValueError::table_memory_context("failed to read Share __share_type field", e)
            })?
            .ok_or_else(|| MpcValueError::message("Share object missing __share_type field"))?;

        match share_type_field {
            Value::String(s) if s == share_fields::SECRET_INT => {
                let bit_length = match store
                    .read_table_field(self.table_ref(), &field(share_fields::BIT_LENGTH))
                    .map_err(|e| {
                        MpcValueError::table_memory_context("failed to read Share bit length", e)
                    })? {
                    Some(value) => value_to_usize(&value, "bit_length")?,
                    _ => 64,
                };
                Ok(ShareType::try_secret_int(bit_length)?)
            }
            Value::String(s) if s == share_fields::SECRET_UINT => {
                let bit_length = match store
                    .read_table_field(self.table_ref(), &field(share_fields::BIT_LENGTH))
                    .map_err(|e| {
                        MpcValueError::table_memory_context("failed to read Share bit length", e)
                    })? {
                    Some(value) => value_to_usize(&value, "bit_length")?,
                    _ => 64,
                };
                Ok(ShareType::try_secret_uint(bit_length)?)
            }
            Value::String(s) if s == share_fields::SECRET_FIXED_POINT => {
                let k = match store
                    .read_table_field(self.table_ref(), &field(share_fields::PRECISION_K))
                    .map_err(|e| {
                        MpcValueError::table_memory_context("failed to read Share precision k", e)
                    })? {
                    Some(value) => value_to_usize(&value, "precision k")?,
                    _ => 64,
                };
                let f = match store
                    .read_table_field(self.table_ref(), &field(share_fields::PRECISION_F))
                    .map_err(|e| {
                        MpcValueError::table_memory_context("failed to read Share precision f", e)
                    })? {
                    Some(value) => value_to_usize(&value, "precision f")?,
                    _ => 16,
                };
                Ok(ShareType::try_secret_fixed_point_from_bits(k, f)?)
            }
            _ => Err(MpcValueError::message(format!(
                "Unknown share type: {:?}",
                share_type_field
            ))),
        }
    }
}

impl From<ShareObjectRef> for ObjectRef {
    fn from(share_ref: ShareObjectRef) -> Self {
        share_ref.object_ref()
    }
}

impl From<ShareObjectRef> for TableRef {
    fn from(share_ref: ShareObjectRef) -> Self {
        share_ref.table_ref()
    }
}

impl From<ShareObjectRef> for Value {
    fn from(share_ref: ShareObjectRef) -> Self {
        share_ref.into_value()
    }
}

fn field(name: &str) -> Value {
    Value::String(name.to_string())
}

/// Create a new Share object in the object store.
pub fn create_share_object_ref<M: TableMemory + ?Sized>(
    store: &mut M,
    share_type: ShareType,
    data: ShareData,
    party_id: usize,
) -> MpcValueResult<ShareObjectRef> {
    let object_ref = store.create_object_ref()?;
    let obj = TableRef::from(object_ref);

    store
        .set_table_field(
            obj,
            field(share_fields::TYPE),
            Value::String(share_fields::TYPE_VALUE.to_string()),
        )
        .map_err(|e| MpcValueError::table_memory_context("failed to set Share type tag", e))?;

    match share_type {
        ShareType::SecretInt { bit_length } => {
            store
                .set_table_field(
                    obj,
                    field(share_fields::SHARE_TYPE),
                    Value::String(share_fields::SECRET_INT.to_string()),
                )
                .map_err(|e| MpcValueError::table_memory_context("failed to set Share type", e))?;
            store
                .set_table_field(
                    obj,
                    field(share_fields::BIT_LENGTH),
                    Value::I64(usize_to_vm_i64(bit_length, "bit_length")?),
                )
                .map_err(|e| {
                    MpcValueError::table_memory_context("failed to set Share bit length", e)
                })?;
        }
        ShareType::SecretUInt { bit_length } => {
            store
                .set_table_field(
                    obj,
                    field(share_fields::SHARE_TYPE),
                    Value::String(share_fields::SECRET_UINT.to_string()),
                )
                .map_err(|e| MpcValueError::table_memory_context("failed to set Share type", e))?;
            store
                .set_table_field(
                    obj,
                    field(share_fields::BIT_LENGTH),
                    Value::I64(usize_to_vm_i64(bit_length, "bit_length")?),
                )
                .map_err(|e| {
                    MpcValueError::table_memory_context("failed to set Share bit length", e)
                })?;
        }
        ShareType::SecretFixedPoint { precision } => {
            store
                .set_table_field(
                    obj,
                    field(share_fields::SHARE_TYPE),
                    Value::String(share_fields::SECRET_FIXED_POINT.to_string()),
                )
                .map_err(|e| MpcValueError::table_memory_context("failed to set Share type", e))?;
            store
                .set_table_field(
                    obj,
                    field(share_fields::PRECISION_K),
                    Value::I64(usize_to_vm_i64(precision.k(), "precision k")?),
                )
                .map_err(|e| {
                    MpcValueError::table_memory_context("failed to set Share precision k", e)
                })?;
            store
                .set_table_field(
                    obj,
                    field(share_fields::PRECISION_F),
                    Value::I64(usize_to_vm_i64(precision.f(), "precision f")?),
                )
                .map_err(|e| {
                    MpcValueError::table_memory_context("failed to set Share precision f", e)
                })?;
        }
    }

    store
        .set_table_field(
            obj,
            field(share_fields::DATA),
            Value::Share(share_type, data),
        )
        .map_err(|e| MpcValueError::table_memory_context("failed to set Share data", e))?;

    store
        .set_table_field(
            obj,
            field(share_fields::PARTY_ID),
            Value::I64(usize_to_vm_i64(party_id, "party_id")?),
        )
        .map_err(|e| MpcValueError::table_memory_context("failed to set Share party ID", e))?;

    Ok(ShareObjectRef::new_unchecked(object_ref))
}

/// Extract share data from a Share object or raw `Value::Share`.
pub fn extract_share_data<M: TableMemory + ?Sized>(
    store: &mut M,
    value: &Value,
) -> MpcValueResult<(ShareType, ShareData)> {
    if let Value::Share(ty, data) = value {
        return Ok((*ty, data.clone()));
    }

    let Some(object_ref) = ObjectRef::from_value(value) else {
        return Err(MpcValueError::message(format!(
            "Expected Share object or Share value, got {:?}",
            value
        )));
    };

    extract_share_data_ref(store, ShareObjectRef::new_unchecked(object_ref))
}

/// Extract share data from a typed Share object handle.
pub fn extract_share_data_ref<M: TableMemory + ?Sized>(
    store: &mut M,
    share_ref: ShareObjectRef,
) -> MpcValueResult<(ShareType, ShareData)> {
    share_ref.share_data(store)
}

pub fn extract_matching_share_pair<M: TableMemory + ?Sized>(
    store: &mut M,
    left: &Value,
    right: &Value,
    context: impl Into<String>,
) -> MpcValueResult<MatchingSharePair> {
    let (left_type, left_data) = extract_share_data(store, left)?;
    let (right_type, right_data) = extract_share_data(store, right)?;

    if left_type != right_type {
        return Err(MpcValueError::ShareTypeMismatch {
            context: context.into(),
            left: left_type,
            right: right_type,
        });
    }

    Ok(MatchingSharePair {
        share_type: left_type,
        left_data,
        right_data,
    })
}

/// Extract Share values from a VM array.
pub fn extract_share_array<M: TableMemory + ?Sized>(
    store: &mut M,
    value: &Value,
    context: &str,
) -> MpcValueResult<Vec<(ShareType, ShareData)>> {
    let array_ref = match ArrayRef::from_value(value) {
        Some(array_ref) => array_ref,
        None => {
            return Err(MpcValueError::unexpected_value(
                context,
                "array of Share values",
                value.clone(),
            ));
        }
    };

    extract_share_array_ref(store, array_ref, context)
}

/// Extract Share values from a typed VM array handle.
pub fn extract_share_array_ref<M: TableMemory + ?Sized>(
    store: &mut M,
    array_ref: ArrayRef,
    context: &str,
) -> MpcValueResult<Vec<(ShareType, ShareData)>> {
    let array = TableRef::from(array_ref);
    let len = store.read_array_ref_len(array_ref).map_err(|e| {
        MpcValueError::table_memory_context(format!("failed to read {context} length"), e)
    })?;
    let mut shares = Vec::with_capacity(len);

    for i in 0..len {
        let idx = Value::I64(usize_to_vm_i64(i, "array index")?);
        let element = store
            .read_table_field(array, &idx)
            .map_err(|e| {
                MpcValueError::table_memory_context(
                    format!("failed to read {context} element {i}"),
                    e,
                )
            })?
            .ok_or_else(|| MpcValueError::missing_array_element(context, i))?;
        if element == Value::Unit {
            return Err(MpcValueError::missing_array_element(context, i));
        }

        shares.push(extract_share_data(store, &element)?);
    }

    Ok(shares)
}

/// Extract Share values from a VM array and require one common share type.
pub fn extract_homogeneous_share_array<M: TableMemory + ?Sized>(
    store: &mut M,
    value: &Value,
    context: &str,
) -> MpcValueResult<Option<(ShareType, Vec<ShareData>)>> {
    let array_ref = match ArrayRef::from_value(value) {
        Some(array_ref) => array_ref,
        None => {
            return Err(MpcValueError::unexpected_value(
                context,
                "array of Share values",
                value.clone(),
            ));
        }
    };

    extract_homogeneous_share_array_ref(store, array_ref, context)
}

/// Extract Share values from a typed VM array handle and require one common share type.
pub fn extract_homogeneous_share_array_ref<M: TableMemory + ?Sized>(
    store: &mut M,
    array_ref: ArrayRef,
    context: &str,
) -> MpcValueResult<Option<(ShareType, Vec<ShareData>)>> {
    let shares = extract_share_array_ref(store, array_ref, context)?;
    let Some((first_ty, _)) = shares.first() else {
        return Ok(None);
    };
    let first_ty = *first_ty;
    let mut data = Vec::with_capacity(shares.len());

    for (i, (ty, share_data)) in shares.into_iter().enumerate() {
        if ty != first_ty {
            return Err(MpcValueError::message(format!(
                "All shares must have the same type. Element 0 has {:?} but element {} has {:?}",
                first_ty, i, ty
            )));
        }

        data.push(share_data);
    }

    Ok(Some((first_ty, data)))
}

/// One element of a multiplication batch: a real share, or a clear integer
/// scalar that can be combined with shares locally (no MPC interaction).
#[derive(Debug, Clone)]
pub enum ShareOrScalar {
    Share(ShareType, ShareData),
    Scalar(i64),
}

/// Extract a VM array whose elements are Share values or clear integers.
/// Used by `Share.batch_mul` so share-by-public products can be computed
/// locally instead of being rejected.
pub fn extract_share_or_scalar_array<M: TableMemory + ?Sized>(
    store: &mut M,
    value: &Value,
    context: &str,
) -> MpcValueResult<Vec<ShareOrScalar>> {
    let array_ref = match ArrayRef::from_value(value) {
        Some(array_ref) => array_ref,
        None => {
            return Err(MpcValueError::unexpected_value(
                context,
                "array of Share or integer values",
                value.clone(),
            ));
        }
    };

    let array = TableRef::from(array_ref);
    let len = store.read_array_ref_len(array_ref).map_err(|e| {
        MpcValueError::table_memory_context(format!("failed to read {context} length"), e)
    })?;
    let mut elements = Vec::with_capacity(len);

    for i in 0..len {
        let idx = Value::I64(usize_to_vm_i64(i, "array index")?);
        let element = store
            .read_table_field(array, &idx)
            .map_err(|e| {
                MpcValueError::table_memory_context(
                    format!("failed to read {context} element {i}"),
                    e,
                )
            })?
            .ok_or_else(|| MpcValueError::missing_array_element(context, i))?;
        if element == Value::Unit {
            return Err(MpcValueError::missing_array_element(context, i));
        }

        if matches!(
            element,
            Value::I64(_)
                | Value::I32(_)
                | Value::I16(_)
                | Value::I8(_)
                | Value::U8(_)
                | Value::U16(_)
                | Value::U32(_)
                | Value::U64(_)
        ) {
            let scalar = value_to_i64(&element, "share scalar")
                .map_err(|e| MpcValueError::message(e.to_string()))?;
            elements.push(ShareOrScalar::Scalar(scalar));
        } else {
            let (share_type, share_data) = extract_share_data(store, &element)?;
            elements.push(ShareOrScalar::Share(share_type, share_data));
        }
    }

    Ok(elements)
}

/// Check if a value is a Share object or raw `Value::Share`.
pub fn is_share_object<M: TableMemory + ?Sized>(store: &mut M, value: &Value) -> bool {
    if matches!(value, Value::Share(_, _)) {
        return true;
    }

    let Some(object_ref) = ObjectRef::from_value(value) else {
        return false;
    };

    is_share_object_ref(store, ShareObjectRef::new_unchecked(object_ref))
}

/// Check if a typed object handle references a Share object.
pub fn is_share_object_ref<M: TableMemory + ?Sized>(
    store: &mut M,
    share_ref: ShareObjectRef,
) -> bool {
    share_ref.is_share_object(store)
}

/// Get the `ShareType` from a Share object or raw `Value::Share`.
pub fn get_share_type<M: TableMemory + ?Sized>(
    store: &mut M,
    value: &Value,
) -> MpcValueResult<ShareType> {
    if let Value::Share(ty, _) = value {
        return Ok(*ty);
    }

    let Some(object_ref) = ObjectRef::from_value(value) else {
        return Err(MpcValueError::message(format!(
            "Not a Share object: {:?}",
            value
        )));
    };

    get_share_type_ref(store, ShareObjectRef::new_unchecked(object_ref))
}

/// Get the `ShareType` from a typed Share object handle.
pub fn get_share_type_ref<M: TableMemory + ?Sized>(
    store: &mut M,
    share_ref: ShareObjectRef,
) -> MpcValueResult<ShareType> {
    share_ref.share_type(store)
}

/// Get the creator party ID from a Share object.
///
/// Raw `Value::Share` values do not carry object metadata, so callers that
/// still accept raw shares can substitute the current backend party ID.
pub fn get_party_id<M: TableMemory + ?Sized>(
    store: &mut M,
    value: &Value,
) -> MpcValueResult<Option<usize>> {
    if matches!(value, Value::Share(_, _)) {
        return Ok(None);
    }

    let Some(object_ref) = ObjectRef::from_value(value) else {
        return Err(MpcValueError::message(format!(
            "Not a Share object: {:?}",
            value
        )));
    };

    get_party_id_ref(store, ShareObjectRef::new_unchecked(object_ref)).map(Some)
}

/// Get the creator party ID from a typed Share object handle.
pub fn get_party_id_ref<M: TableMemory + ?Sized>(
    store: &mut M,
    share_ref: ShareObjectRef,
) -> MpcValueResult<usize> {
    share_ref.party_id(store)
}