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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use super::*;
use crate::core_vm::VirtualMachine;
use crate::mpc_values::byte_arrays::{create_byte_array, extract_byte_array};
use crate::net::curve::MpcFieldKind;
use crate::net::mpc_engine::{
    MpcCapabilities, MpcEngine, MpcEngineError, MpcEngineOpenInExponent, MpcEngineResult,
    MpcExponentGroup, MpcSessionTopology,
};
use crate::VirtualMachineErrorKind;
use std::collections::HashMap;
use std::error::Error;
use std::sync::{Arc, Mutex};
use stoffel_vm_types::core_types::{
    ArrayRef, ClearShareInput, ClearShareValue, ObjectRef, ObjectStore, ShareData, ShareType,
    TableMemory, TableRef, Value, DEFAULT_FIXED_POINT_FRACTIONAL_BITS, F64,
};
use stoffel_vm_types::functions::VMFunction;
use stoffel_vm_types::instructions::Instruction;

fn callback_runtime_kind(error: &crate::VirtualMachineError) -> Option<VirtualMachineErrorKind> {
    let mut source = error.source();
    while let Some(error) = source {
        if let Some(callback_error) =
            error.downcast_ref::<crate::foreign_functions::ForeignFunctionCallbackError>()
        {
            return callback_error.runtime_kind();
        }
        if let Some(crate::foreign_functions::ForeignFunctionError::CallbackFailed {
            source, ..
        }) = error.downcast_ref::<crate::foreign_functions::ForeignFunctionError>()
        {
            return source.runtime_kind();
        }
        source = error.source();
    }

    None
}

struct NoOpenExpEngine;

impl MpcEngine for NoOpenExpEngine {
    fn protocol_name(&self) -> &'static str {
        "no-open-exp"
    }
    fn topology(&self) -> MpcSessionTopology {
        MpcSessionTopology::try_new(0, 0, 3, 1).expect("test topology should be valid")
    }
    fn is_ready(&self) -> bool {
        true
    }
    fn start(&self) -> crate::net::mpc_engine::MpcEngineResult<()> {
        Ok(())
    }
    fn input_share(
        &self,
        _clear: ClearShareInput,
    ) -> crate::net::mpc_engine::MpcEngineResult<ShareData> {
        Err(crate::net::mpc_engine::MpcEngineError::operation_failed(
            "test_mpc_engine",
            "not implemented",
        ))
    }
    fn open_share(
        &self,
        _ty: ShareType,
        _share_bytes: &[u8],
    ) -> crate::net::mpc_engine::MpcEngineResult<ClearShareValue> {
        Err(crate::net::mpc_engine::MpcEngineError::operation_failed(
            "test_mpc_engine",
            "not implemented",
        ))
    }
    fn shutdown(&self) {}
    fn field_kind(&self) -> MpcFieldKind {
        MpcFieldKind::Bls12_381Fr
    }
}

struct RecordingOpenExpEngine {
    group_seen: Arc<Mutex<Option<MpcExponentGroup>>>,
}

impl MpcEngine for RecordingOpenExpEngine {
    fn protocol_name(&self) -> &'static str {
        "recording-open-exp"
    }
    fn topology(&self) -> MpcSessionTopology {
        MpcSessionTopology::try_new(0, 0, 3, 1).expect("test topology should be valid")
    }
    fn is_ready(&self) -> bool {
        true
    }
    fn start(&self) -> crate::net::mpc_engine::MpcEngineResult<()> {
        Ok(())
    }
    fn input_share(
        &self,
        _clear: ClearShareInput,
    ) -> crate::net::mpc_engine::MpcEngineResult<ShareData> {
        Err(crate::net::mpc_engine::MpcEngineError::operation_failed(
            "test_mpc_engine",
            "not implemented",
        ))
    }
    fn open_share(
        &self,
        _ty: ShareType,
        _share_bytes: &[u8],
    ) -> crate::net::mpc_engine::MpcEngineResult<ClearShareValue> {
        Err(crate::net::mpc_engine::MpcEngineError::operation_failed(
            "test_mpc_engine",
            "not implemented",
        ))
    }
    fn capabilities(&self) -> MpcCapabilities {
        MpcCapabilities::OPEN_IN_EXP
    }
    fn as_open_in_exp(&self) -> Option<&dyn MpcEngineOpenInExponent> {
        Some(self)
    }
    fn shutdown(&self) {}
    fn field_kind(&self) -> MpcFieldKind {
        MpcFieldKind::Bls12_381Fr
    }
}

impl MpcEngineOpenInExponent for RecordingOpenExpEngine {
    fn open_share_in_exp(
        &self,
        _ty: ShareType,
        _share_bytes: &[u8],
        _generator_bytes: &[u8],
    ) -> MpcEngineResult<Vec<u8>> {
        Err(MpcEngineError::operation_failed(
            "open_share_in_exp",
            "legacy exponent-open path should not be used",
        ))
    }

    fn open_share_in_exp_group(
        &self,
        group: MpcExponentGroup,
        _ty: ShareType,
        share_bytes: &[u8],
        generator_bytes: &[u8],
    ) -> MpcEngineResult<Vec<u8>> {
        if share_bytes != [1, 2, 3, 4] {
            return Err(MpcEngineError::operation_failed(
                "open_share_in_exp_group",
                format!("unexpected share bytes: {:?}", share_bytes),
            ));
        }
        if generator_bytes.is_empty() {
            return Err(MpcEngineError::operation_failed(
                "open_share_in_exp_group",
                "generator bytes should not be empty",
            ));
        }

        *self
            .group_seen
            .lock()
            .expect("group recording lock should not be poisoned") = Some(group);

        Ok(vec![5, 6, 7, 8])
    }
}

/// Test engine that implements `input_share` as a trivial public-constant
/// sharing: a degree-1 BLS `RobustShare` whose evaluation at this party equals
/// the scaled clear value (`round(value * 2^f)` for fixed-point). All other
/// local share algebra falls through to the real `share_algebra` default impls,
/// so add/scalar-mul over these shares is genuine field arithmetic.
struct ConstantInputEngine;

fn encode_robust_share_bls(value: i64) -> ShareData {
    use ark_serialize::CanonicalSerialize;
    use stoffelmpc_mpc::honeybadger::robust_interpolate::robust_interpolate::RobustShare;
    let share = RobustShare::new(ark_bls12_381::Fr::from(value as u64), 1, 1);
    let mut out = Vec::new();
    share
        .serialize_compressed(&mut out)
        .expect("serialize test share");
    ShareData::Opaque(out.into())
}

impl MpcEngine for ConstantInputEngine {
    fn protocol_name(&self) -> &'static str {
        "constant-input"
    }
    fn topology(&self) -> MpcSessionTopology {
        MpcSessionTopology::try_new(0, 0, 3, 1).expect("test topology should be valid")
    }
    fn is_ready(&self) -> bool {
        true
    }
    fn start(&self) -> MpcEngineResult<()> {
        Ok(())
    }
    fn input_share(&self, clear: ClearShareInput) -> MpcEngineResult<ShareData> {
        let scaled = match clear.value() {
            ClearShareValue::FixedPoint(F64(v)) => {
                let f = match clear.share_type() {
                    ShareType::SecretFixedPoint { precision } => precision.f(),
                    _ => 0,
                };
                (v * 2f64.powi(f as i32)).round() as i64
            }
            ClearShareValue::Integer(v) => v,
            ClearShareValue::UnsignedInteger(v) => v as i64,
            ClearShareValue::Boolean(b) => b as i64,
        };
        Ok(encode_robust_share_bls(scaled))
    }
    fn open_share(&self, _ty: ShareType, _share_bytes: &[u8]) -> MpcEngineResult<ClearShareValue> {
        Err(MpcEngineError::operation_failed(
            "constant-input",
            "not implemented",
        ))
    }
    fn shutdown(&self) {}
    fn field_kind(&self) -> MpcFieldKind {
        MpcFieldKind::Bls12_381Fr
    }
}

#[test]
fn share_add_constant_and_alias_are_both_registered() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);

    assert!(vm.has_function("Share.add_constant"));
    assert!(
        vm.has_function("Share.add_scalar"),
        "deprecated Share.add_scalar alias must stay registered for old bytecode"
    );
}

#[test]
fn share_add_constant_adds_fixed_point_float_in_scaled_domain() {
    use ark_serialize::CanonicalDeserialize;
    use stoffelmpc_mpc::honeybadger::robust_interpolate::robust_interpolate::RobustShare;

    let scale = 2f64.powi(DEFAULT_FIXED_POINT_FRACTIONAL_BITS as i32);

    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);
    vm.set_mpc_engine(Arc::new(ConstantInputEngine));

    // Fixed-point share holding 2.5, plus the fixed-point constant 1.25 -> 3.75.
    let fixed_ty = ShareType::default_secret_fixed_point();
    let base = vm
        .create_share_object(
            fixed_ty,
            encode_robust_share_bls((2.5 * scale).round() as i64),
            0,
        )
        .expect("create fixed-point share object");
    let result = vm
        .execute_with_args("Share.add_constant", &[base, Value::Float(F64(1.25))])
        .expect("Share.add_constant on a fixed-point share with a float constant");
    let (_ty, data) = vm
        .read_share_object(&result)
        .expect("result is a Share object");
    let share = RobustShare::<ark_bls12_381::Fr>::deserialize_compressed(data.as_bytes())
        .expect("deserialize result share");
    assert_eq!(
        share.share[0],
        ark_bls12_381::Fr::from((3.75 * scale).round() as u64),
        "fixed + float must add in the 2^f-scaled fixed-point domain"
    );

    // Integer share holding 3, plus the float constant 1.25 -> fixed-point 4.25:
    // the integer share is promoted into the fixed-point domain before adding.
    let int_ty = ShareType::secret_int(64);
    let base_int = vm
        .create_share_object(int_ty, encode_robust_share_bls(3), 0)
        .expect("create integer share object");
    let result_int = vm
        .execute_with_args("Share.add_constant", &[base_int, Value::Float(F64(1.25))])
        .expect("Share.add_constant promotes an integer share to fixed-point");
    let (result_int_ty, data_int) = vm
        .read_share_object(&result_int)
        .expect("result is a Share object");
    assert!(
        matches!(result_int_ty, ShareType::SecretFixedPoint { .. }),
        "integer + float must yield a fixed-point share, got {result_int_ty:?}"
    );
    let share_int = RobustShare::<ark_bls12_381::Fr>::deserialize_compressed(data_int.as_bytes())
        .expect("deserialize promoted result share");
    assert_eq!(
        share_int.share[0],
        ark_bls12_381::Fr::from((4.25 * scale).round() as u64),
        "integer + float must promote the share to the fixed-point domain before adding"
    );
}

#[test]
fn try_register_mpc_builtins_rejects_duplicate_registration() {
    let mut vm = VirtualMachine::builder()
        .with_standard_library(false)
        .with_mpc_builtins(false)
        .build();

    try_register_mpc_builtins(&mut vm).expect("first MPC builtin registration should succeed");
    let err = try_register_mpc_builtins(&mut vm)
        .expect_err("second MPC builtin registration must be rejected");
    assert_eq!(err.kind(), VirtualMachineErrorKind::Registration);
    let err = err.to_string();

    assert!(
        err.contains("Share.from_clear") && err.contains("already registered"),
        "unexpected error: {err}"
    );
    assert!(vm.has_function("Share.open"));
}

#[test]
fn mpc_info_builtins_expose_backend_identity_and_capabilities() {
    let group_seen = Arc::new(Mutex::new(None));
    let mut vm = VirtualMachine::builder()
        .with_standard_library(false)
        .with_mpc_engine(Arc::new(RecordingOpenExpEngine {
            group_seen: Arc::clone(&group_seen),
        }))
        .build();

    assert_eq!(
        vm.execute_with_args("Mpc.protocol_name", &[]).unwrap(),
        Value::String("recording-open-exp".to_string())
    );
    assert_eq!(
        vm.execute_with_args("Mpc.curve", &[]).unwrap(),
        Value::String("bls12-381".to_string())
    );
    assert_eq!(
        vm.execute_with_args("Mpc.field", &[]).unwrap(),
        Value::String("bls12-381-fr".to_string())
    );
    assert_eq!(
        vm.execute_with_args("Mpc.instance_id", &[]).unwrap(),
        Value::U64(0)
    );
    assert_eq!(
        vm.execute_with_args(
            "Mpc.has_capability",
            &[Value::String("open_in_exp".to_string())],
        )
        .unwrap(),
        Value::Bool(true)
    );
    assert_eq!(
        vm.execute_with_args(
            "Mpc.has_capability",
            &[Value::String("multiplication".to_string())],
        )
        .unwrap(),
        Value::Bool(false)
    );

    let capabilities = vm.execute_with_args("Mpc.capabilities", &[]).unwrap();
    let Value::Array(capabilities_ref) = capabilities else {
        panic!("Mpc.capabilities should return an array");
    };
    assert_eq!(vm.read_array_len(capabilities_ref).unwrap(), 1);
    assert_eq!(
        vm.read_table_field(TableRef::from(capabilities_ref), &Value::I64(0),)
            .unwrap(),
        Some(Value::String("open-in-exponent".to_string()))
    );
}

#[test]
fn mpc_has_capability_returns_false_for_unknown_capability_names() {
    let mut vm = VirtualMachine::builder()
        .with_standard_library(false)
        .with_mpc_engine(Arc::new(NoOpenExpEngine))
        .build();

    assert_eq!(
        vm.execute_with_args(
            "Mpc.has_capability",
            &[Value::String("not-a-capability".to_string())],
        )
        .unwrap(),
        Value::Bool(false)
    );
}

#[test]
fn test_share_object_creation() {
    let mut store = ObjectStore::new();
    let share_type = ShareType::default_secret_int();
    let data = vec![1, 2, 3, 4];
    let party_id = 0;

    let share_ref = share_object::create_share_object_ref(
        &mut store,
        share_type,
        ShareData::Opaque(data.clone().into()),
        party_id,
    )
    .expect("share object creation should succeed");

    // Verify we can extract the share data
    let result = share_object::extract_share_data_ref(&mut store, share_ref);
    assert!(result.is_ok());
    let (ty, extracted_data) = result.unwrap();
    assert_eq!(ty, share_type);
    assert_eq!(extracted_data, ShareData::Opaque(data.into()));
}

#[test]
fn share_object_type_metadata_rejects_invalid_integer_conversions() {
    let mut store = ObjectStore::new();
    let object_ref = store
        .create_object_ref()
        .expect("create malformed share object");
    let table_ref = TableRef::from(object_ref);
    store
        .set_table_field(
            table_ref,
            Value::String(share_fields::TYPE.to_string()),
            Value::String(share_fields::TYPE_VALUE.to_string()),
        )
        .expect("set share type tag");
    store
        .set_table_field(
            table_ref,
            Value::String(share_fields::SHARE_TYPE.to_string()),
            Value::String(share_fields::SECRET_INT.to_string()),
        )
        .expect("set share type field");
    store
        .set_table_field(
            table_ref,
            Value::String(share_fields::BIT_LENGTH.to_string()),
            Value::I64(-1),
        )
        .expect("set invalid bit length field");
    store
        .set_table_field(
            table_ref,
            Value::String(share_fields::DATA.to_string()),
            Value::Share(
                ShareType::default_secret_int(),
                ShareData::Opaque(vec![].into()),
            ),
        )
        .expect("set share data field");

    let object = Value::from(object_ref);
    let err = share_object::get_share_type(&mut store, &object)
        .expect_err("negative bit length must be rejected");
    let err = err.to_string();

    assert!(
        err.contains("bit_length") && err.contains("non-negative integer"),
        "unexpected error: {err}"
    );
}

#[test]
fn share_object_extraction_rejects_metadata_type_mismatch() {
    let mut store = ObjectStore::new();
    let share_ref = share_object::create_share_object_ref(
        &mut store,
        ShareType::secret_int(64),
        ShareData::Opaque(vec![1, 2, 3, 4].into()),
        0,
    )
    .expect("share object creation should succeed");
    let object = Value::from(share_ref);

    store
        .set_table_field(
            TableRef::from(share_ref),
            Value::String(share_fields::SHARE_TYPE.to_string()),
            Value::String(share_fields::SECRET_FIXED_POINT.to_string()),
        )
        .expect("set conflicting share type");
    store
        .set_table_field(
            TableRef::from(share_ref),
            Value::String(share_fields::PRECISION_K.to_string()),
            Value::I64(64),
        )
        .expect("set precision k");
    store
        .set_table_field(
            TableRef::from(share_ref),
            Value::String(share_fields::PRECISION_F.to_string()),
            Value::I64(16),
        )
        .expect("set precision f");

    let err = share_object::extract_share_data(&mut store, &object)
        .expect_err("metadata must agree with the embedded share data type");
    let err = err.to_string();

    assert!(
        err.contains("metadata type") && err.contains("__data type"),
        "unexpected error: {err}"
    );
}

#[test]
fn share_mul_field_preserves_feldman_share_data() {
    use ark_bls12_381::{Fr, G1Projective};
    use ark_ec::PrimeGroup;
    use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
    use stoffelmpc_mpc::common::share::feldman::FeldmanShamirShare;

    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);
    vm.set_mpc_engine(Arc::new(NoOpenExpEngine));

    let share_value = Fr::from(7u64);
    let commitment = G1Projective::generator() * share_value;
    let share =
        FeldmanShamirShare::new(share_value, 1, 0, vec![commitment]).expect("create Feldman share");
    let mut share_bytes = Vec::new();
    share
        .serialize_compressed(&mut share_bytes)
        .expect("serialize Feldman share");

    let share_value = vm
        .create_share_object(
            ShareType::secret_int(64),
            ShareData::Feldman {
                data: share_bytes.into(),
                commitments: Vec::new().into(),
            },
            0,
        )
        .expect("share object creation should succeed");

    let mut field_bytes = Vec::new();
    Fr::from(3u64)
        .serialize_compressed(&mut field_bytes)
        .expect("serialize field scalar");
    let field_array = vm
        .create_byte_array(&field_bytes)
        .expect("field byte array");

    let fn_call = VMFunction::new(
        "test_share_mul_field".to_string(),
        vec![],
        Vec::new(),
        None,
        4,
        vec![
            Instruction::LDI(0, share_value),
            Instruction::LDI(1, field_array),
            Instruction::PUSHARG(0),
            Instruction::PUSHARG(1),
            Instruction::CALL("Share.mul_field".to_string()),
            Instruction::RET(0),
        ],
        HashMap::new(),
    );
    vm.register_function(fn_call);

    let result = vm
        .execute("test_share_mul_field")
        .expect("Share.mul_field should execute");
    let (_ty, result_data) = vm
        .read_share_object(&result)
        .expect("result should be a valid Share object");
    let commitments = result_data
        .commitments()
        .expect("Feldman data should remain Feldman after Share.mul_field");
    assert_eq!(commitments.len(), 1);

    let decoded =
        FeldmanShamirShare::<Fr, G1Projective>::deserialize_compressed(result_data.as_bytes())
            .expect("decode multiplied Feldman share");
    assert_eq!(decoded.feldmanshare.share[0], Fr::from(21u64));
    assert_eq!(
        decoded.commitments[0],
        G1Projective::generator() * Fr::from(21u64)
    );
}

#[test]
fn share_from_clear_rejects_unsigned_values_outside_vm_integer_range() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);
    vm.set_mpc_engine(Arc::new(NoOpenExpEngine));

    let err = vm
        .execute_with_args(
            "Share.from_clear",
            &[Value::U64(u64::try_from(i64::MAX).unwrap() + 1)],
        )
        .expect_err("oversized unsigned clear value must not truncate to i64");
    assert_eq!(err.kind(), VirtualMachineErrorKind::ForeignFunction);
    assert_eq!(
        callback_runtime_kind(&err),
        Some(VirtualMachineErrorKind::Mpc)
    );
    let err = err.to_string();

    assert!(
        err.contains("clear integer") && err.contains("exceeds i64 range"),
        "unexpected error: {err}"
    );
}

#[test]
fn share_mul_reports_missing_multiplication_capability_through_vm_runtime() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);
    vm.set_mpc_engine(Arc::new(NoOpenExpEngine));
    let ty = ShareType::default_secret_int();

    let err = vm
        .execute_with_args(
            "Share.mul",
            &[
                Value::Share(ty, ShareData::Opaque(vec![1].into())),
                Value::Share(ty, ShareData::Opaque(vec![2].into())),
            ],
        )
        .expect_err("Share.mul should require the multiplication capability");

    assert_eq!(err.kind(), VirtualMachineErrorKind::ForeignFunction);
    assert_eq!(
        callback_runtime_kind(&err),
        Some(VirtualMachineErrorKind::Mpc)
    );
    let err = err.to_string();
    assert!(
        err.contains("does not support multiplication"),
        "unexpected error: {err}"
    );
}

#[test]
fn share_random_reports_missing_randomness_capability_through_vm_runtime() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);
    vm.set_mpc_engine(Arc::new(NoOpenExpEngine));

    let err = vm
        .execute_with_args("Share.random", &[])
        .expect_err("Share.random should require the randomness capability");

    assert_eq!(err.kind(), VirtualMachineErrorKind::ForeignFunction);
    assert_eq!(
        callback_runtime_kind(&err),
        Some(VirtualMachineErrorKind::Mpc)
    );
    let err = err.to_string();
    assert!(
        err.contains("does not support jointly-random share generation"),
        "unexpected error: {err}"
    );
}

#[test]
fn test_is_share_object() {
    let mut store = ObjectStore::new();
    let share_type = ShareType::default_secret_int();
    let data = vec![1, 2, 3, 4];

    let share_ref = share_object::create_share_object_ref(
        &mut store,
        share_type,
        ShareData::Opaque(data.into()),
        0,
    )
    .expect("share object creation should succeed");
    let non_share_ref = store.create_object_ref().expect("create non-share object");

    assert!(share_object::is_share_object(
        &mut store,
        &Value::from(share_ref)
    ));
    assert!(!share_object::is_share_object(
        &mut store,
        &Value::from(non_share_ref)
    ));
    assert!(share_object::is_share_object(
        &mut store,
        &Value::Share(share_type, ShareData::Opaque(vec![].into()))
    ));
    assert!(!share_object::is_share_object(&mut store, &Value::I64(42)));
}

#[test]
fn byte_array_helpers_work_through_table_memory_trait_object() {
    let mut memory: Box<dyn TableMemory> = Box::new(ObjectStore::new());
    let bytes = vec![0, 1, 2, 254, 255];

    let array_id =
        create_byte_array(&mut *memory, &bytes).expect("byte array creation should succeed");
    let roundtrip = extract_byte_array(&mut *memory, &Value::from(ArrayRef::new(array_id)))
        .expect("byte array extraction should succeed");

    assert_eq!(roundtrip, bytes);
}

#[test]
fn share_get_party_id_rejects_non_share_objects() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);

    let object_id = vm
        .create_object_ref()
        .expect("create non-share object")
        .id();
    let object = Value::from(ObjectRef::new(object_id));
    vm.set_table_field(
        TableRef::object(object_id),
        Value::String(share_fields::PARTY_ID.to_string()),
        Value::I64(12),
    )
    .expect("set misleading party ID field");

    let err = vm
        .execute_with_args("Share.get_party_id", &[object])
        .expect_err("non-share object must not expose Share metadata");
    let err = err.to_string();

    assert!(
        err.contains("Object is not a Share"),
        "unexpected error: {err}"
    );
}

#[test]
fn share_get_party_id_rejects_raw_shares_without_mpc_engine() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);

    let raw_share = Value::Share(
        ShareType::secret_int(64),
        ShareData::Opaque(vec![1, 2, 3].into()),
    );
    let err = vm
        .execute_with_args("Share.get_party_id", &[raw_share])
        .expect_err("raw share party ID must come from configured MPC engine");

    assert_eq!(err.kind(), VirtualMachineErrorKind::ForeignFunction);
    assert_eq!(
        callback_runtime_kind(&err),
        Some(VirtualMachineErrorKind::Mpc)
    );
    assert!(
        err.to_string().contains("MPC engine not configured"),
        "unexpected error: {err}"
    );
}

#[test]
fn share_open_exp_rejects_engine_without_support() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);
    vm.set_mpc_engine(Arc::new(NoOpenExpEngine));

    let share_value = vm
        .create_share_object(
            ShareType::secret_int(64),
            ShareData::Opaque(vec![1, 2, 3, 4].into()),
            0,
        )
        .expect("share object creation should succeed");

    let fn_call = VMFunction::new(
        "test_share_open_exp".to_string(),
        vec![],
        Vec::new(),
        None,
        4,
        vec![
            Instruction::LDI(0, share_value),
            Instruction::LDI(1, Value::String("bls12-381-g1".to_string())),
            Instruction::PUSHARG(0),
            Instruction::PUSHARG(1),
            Instruction::CALL("Share.open_exp".to_string()),
            Instruction::RET(0),
        ],
        HashMap::new(),
    );
    vm.register_function(fn_call);

    let err = vm
        .execute("test_share_open_exp")
        .expect_err("Share.open_exp should fail for engines without exponent-open support");
    assert_eq!(err.kind(), VirtualMachineErrorKind::ForeignFunction);
    assert_eq!(
        callback_runtime_kind(&err),
        Some(VirtualMachineErrorKind::Mpc)
    );
    let err = err.to_string();

    assert!(
        err.contains("does not support Share.open_exp"),
        "expected capability error, got: {}",
        err
    );
}

#[test]
fn share_open_exp_uses_group_backend_for_bls12381_g2() {
    let mut vm = VirtualMachine::builder().with_mpc_builtins(false).build();
    register_mpc_builtins(&mut vm);

    let group_seen = Arc::new(Mutex::new(None));
    vm.set_mpc_engine(Arc::new(RecordingOpenExpEngine {
        group_seen: Arc::clone(&group_seen),
    }));

    let share_value = vm
        .create_share_object(
            ShareType::secret_int(64),
            ShareData::Opaque(vec![1, 2, 3, 4].into()),
            0,
        )
        .expect("share object creation should succeed");

    let fn_call = VMFunction::new(
        "test_share_open_exp_g2".to_string(),
        vec![],
        Vec::new(),
        None,
        4,
        vec![
            Instruction::LDI(0, share_value),
            Instruction::LDI(1, Value::String("bls12-381-g2".to_string())),
            Instruction::PUSHARG(0),
            Instruction::PUSHARG(1),
            Instruction::CALL("Share.open_exp".to_string()),
            Instruction::RET(0),
        ],
        HashMap::new(),
    );
    vm.register_function(fn_call);

    let result = vm
        .execute("test_share_open_exp_g2")
        .expect("Share.open_exp should use the group-aware backend hook");
    let bytes = vm
        .read_byte_array(&result)
        .expect("Share.open_exp should return a byte array");

    assert_eq!(bytes, vec![5, 6, 7, 8]);
    assert_eq!(
        *group_seen
            .lock()
            .expect("group recording lock should not be poisoned"),
        Some(MpcExponentGroup::Bls12381G2)
    );
}