snarkvm-synthesizer-process 4.7.3

A process for a decentralized virtual machine
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl<N: Network> CallTrait<N> for CallDynamic<N> {
    /// Evaluates the instruction.
    #[inline]
    fn evaluate<A: circuit::Aleo<Network = N>, R: CryptoRng + Rng>(
        &self,
        stack: &Stack<N>,
        registers: &mut Registers<N, A>,
        rng: &mut R,
    ) -> Result<(), CallEvalError> {
        let timer = timer!("CallDynamic::evaluate");

        // Load the operands values.
        let inputs: Vec<_> = self.operands().iter().map(|operand| registers.load(stack, operand)).try_collect()?;

        // Helper: extract a field from a field or identifier literal value.
        let value_to_field = |value: &Value<N>, position: &str| -> Result<Field<N>, CallEvalError> {
            match value {
                Value::Plaintext(Plaintext::Literal(Literal::Field(field), _)) => Ok(*field),
                Value::Plaintext(Plaintext::Literal(Literal::Identifier(id_lit), _)) => id_lit
                    .to_field()
                    .map_err(|e| anyhow!("Failed to convert identifier literal to field ({position}): {e}").into()),
                _ => Err(anyhow!(
                    "Expected the {position} operand of `call.dynamic` to be a field or identifier literal."
                )
                .into()),
            }
        };

        // Get the program name.
        let program_name_as_field = value_to_field(&inputs[0], "first")?;

        // Get the program network.
        let program_network_id = value_to_field(&inputs[1], "second")?;

        // Get the function name.
        let function_name_as_field = value_to_field(&inputs[2], "third")?;

        // Separate the remaining inputs as the function inputs.
        let inputs = &inputs[3..];

        // Resolve the program and function.
        let target = resolve_dynamic_target(
            registers.call_stack_ref(),
            stack,
            &program_name_as_field,
            &program_network_id,
            &function_name_as_field,
        )?;

        // Get the target (in evaluate mode, we must have a valid target).
        let Some(target) = target else {
            return Err(anyhow!("Failed to resolve the target of the dynamic call in 'evaluate' mode.").into());
        };

        // Retrieve the program ID, function name, and substack from the resolved target.
        let program_id = target.program_id();
        let function_name = target.function_name();
        let substack = target.substack();
        lap!(timer, "Retrieved the substack");

        // If the target is a closure, reject it — closures cannot be dynamically called.
        let outputs = if substack.program().get_closure(function_name).is_ok() {
            return Err(anyhow!("Cannot dynamically evaluate a closure: {function_name}").into());
        }
        // If the operator is a function, retrieve the function and compute the output.
        else if let Ok(function) = substack.program().get_function(function_name) {
            // Ensure the number of inputs matches the number of input statements.
            if function.inputs().len() != inputs.len() {
                return Err(anyhow!("Expected {} inputs, found {}", function.inputs().len(), inputs.len()).into());
            }

            // Get the 'root_tvk'.
            let root_tvk = Some(registers.root_tvk()?);

            // Get the call stack.
            let mut call_stack = registers.call_stack();

            // In Authorize mode, we need to compute the new request and add it to the authorization.
            if let CallStack::Authorize(requests, private_key, authorization) = &mut call_stack {
                // Set 'is_root'.
                let is_root = false;
                // Ensure that we have a private key to sign the new request.
                let Some(private_key) = private_key else {
                    return Err(anyhow!("Cannot authorize a new function call without a private key.").into());
                };
                // Retrieve the program checksum, if the program has a constructor.
                let program_checksum = match substack.program().contains_constructor() {
                    true => Some(substack.program_checksum_as_field()?),
                    false => None,
                };

                // Get the input types of the callee.
                let input_types = substack.program().get_function_ref(function_name)?.input_types();
                // Ensure the number of inputs matches the number of input types.
                if input_types.len() != inputs.len() {
                    return Err(anyhow!("Expected {} inputs, found {}", input_types.len(), inputs.len()).into());
                }

                // Convert the caller's inputs to the callee's context.
                let callee_inputs = convert_caller_inputs_to_callee_inputs(inputs, &input_types, substack)?;

                // Compute the request.
                let request = Request::sign(
                    private_key,
                    *substack.program_id(),
                    *function.name(),
                    callee_inputs.iter(),
                    &function.input_types(),
                    root_tvk,
                    is_root,
                    program_checksum,
                    true,
                    rng,
                )?;

                // Add the request to the requests.
                requests.push(request.clone());
                // Add the request to the authorization.
                authorization.push(request)?;
            };

            // In AuthorizeMocked mode, we operate similarly to the Authorize mode but mock the request.
            if let CallStack::AuthorizeMocked(requests, address, authorization) = &mut call_stack {
                // Get the input types of the callee.
                let input_types = substack.program().get_function_ref(function_name)?.input_types();
                // Ensure the number of inputs matches the number of input types.
                if input_types.len() != inputs.len() {
                    return Err(anyhow!("Expected {} inputs, found {}", input_types.len(), inputs.len()).into());
                }

                // Convert the caller's inputs to the callee's context.
                let callee_inputs = convert_caller_inputs_to_callee_inputs(inputs, &input_types, substack)?;

                // Mock the request.
                let request = Request::sample(
                    *address,
                    *substack.program_id(),
                    *function.name(),
                    callee_inputs.iter(),
                    &function.input_types(),
                    true,
                    rng,
                )?;

                // Add the request to the requests.
                requests.push(request.clone());
                // Add the request to the authorization.
                authorization.push(request)?;
            };

            // Set the (console) caller.
            let console_caller = Some(*stack.program_id());
            // Evaluate the function.
            let response = substack.evaluate_function::<A, R>(call_stack, console_caller, root_tvk, rng)?;
            // Convert the callee's outputs to the caller's context.
            response.to_dynamic_outputs()?
        }
        // Else, throw an error.
        else {
            return Err(anyhow!("Dynamic call to '{program_id}/{function_name}' is invalid or unsupported.").into());
        };
        lap!(timer, "Computed outputs");

        // Assign the outputs to the destination registers.
        if outputs.len() != self.destinations().len() {
            return Err(anyhow!(
                "Expected {} outputs, but {} were provided.",
                self.destinations().len(),
                outputs.len()
            )
            .into());
        }
        for (output, register) in outputs.into_iter().zip_eq(&self.destinations()) {
            // Assign the output to the register.
            registers.store(stack, register, output)?;
        }
        finish!(timer);

        Ok(())
    }

    /// Executes the instruction.
    #[inline]
    fn execute<A: circuit::Aleo<Network = N>, R: CryptoRng + Rng>(
        &self,
        stack: &Stack<N>,
        registers: &mut Registers<N, A>,
        rng: &mut R,
    ) -> Result<(), CallExecError> {
        use circuit::{Eject, environment::ToField as _};

        let timer = timer!("CallDynamic::execute");

        // Load the operands values.
        let inputs: Vec<_> =
            self.operands().iter().map(|operand| registers.load_circuit(stack, operand)).try_collect()?;

        // Helper: extract a circuit field from a circuit Field or Identifier literal value.
        // Identifier literals are converted to their field representation via `to_field()`,
        // which adds zero circuit constraints.
        let circuit_value_to_circuit_field =
            |value: &circuit::Value<A>, position: &str| -> Result<circuit::Field<A>, CallExecError> {
                match value {
                    circuit::Value::Plaintext(circuit::Plaintext::Literal(circuit::Literal::Field(field), _)) => {
                        Ok(field.clone())
                    }
                    circuit::Value::Plaintext(circuit::Plaintext::Literal(circuit::Literal::Identifier(id_lit), _)) => {
                        Ok(id_lit.to_field())
                    }
                    _ => Err(anyhow!(
                        "Expected the {position} operand of `call.dynamic` to be a field or identifier literal."
                    )
                    .into()),
                }
            };

        // Get the program name as a circuit field.
        let program_name_as_field = circuit_value_to_circuit_field(&inputs[0], "first")?;
        // Get the program network as a circuit field.
        let program_network_as_field = circuit_value_to_circuit_field(&inputs[1], "second")?;
        // Get the function name as a circuit field.
        let function_name_as_field = circuit_value_to_circuit_field(&inputs[2], "third")?;

        // Separate the remaining inputs as the function inputs.
        let inputs = &inputs[3..];

        // Retrieve the root request's tvk, if available (None if this is the root call).
        let root_tvk = registers.root_tvk().ok();

        // Execute the function.
        let outputs = {
            lap!(timer, "Execute the function");

            // Retrieve the number of public variables in the circuit.
            let num_public = A::num_public();

            // Indicate that external calls are never a root request.
            let is_root = false;

            // Eject the existing circuit.
            let r1cs = A::eject_r1cs_and_reset();
            let (request, caller_response_outputs) = {
                // Resolve the program and function.
                let target = resolve_dynamic_target(
                    registers.call_stack_ref(),
                    stack,
                    &program_name_as_field.eject_value(),
                    &program_network_as_field.eject_value(),
                    &function_name_as_field.eject_value(),
                )?;

                // Eject the circuit inputs.
                let inputs = inputs.eject_value();

                // Set the (console) caller.
                let console_caller = Some(*stack.program_id());

                match registers.call_stack_ref() {
                    // In `Authorize` mode, add any external calls to the stack.
                    CallStack::Authorize(_, private_key, authorization) => {
                        // Get the target.
                        let Some(target) = target else {
                            return Err(anyhow!(
                                "Failed to resolve the target of the dynamic call in 'Authorize' mode."
                            )
                            .into());
                        };
                        // Get the function.
                        let function = target.substack().program().get_function_ref(target.function_name())?;
                        // Retrieve the number of inputs.
                        let num_inputs = function.inputs().len();
                        // Ensure the number of inputs matches the number of input statements.
                        if num_inputs != inputs.len() {
                            return Err(anyhow!("Expected {} inputs, found {}", num_inputs, inputs.len()).into());
                        }
                        // Ensure that we have a private key to sign the new request.
                        let Some(private_key) = private_key else {
                            return Err(anyhow!("Cannot authorize a new function call without a private key.").into());
                        };
                        // Retrieve the program checksum, if the program has a constructor.
                        let program_checksum = match target.substack().program().contains_constructor() {
                            true => Some(target.substack().program_checksum_as_field()?),
                            false => None,
                        };

                        // Get the input types of the callee.
                        let input_types =
                            &target.substack().program().get_function_ref(target.function_name())?.input_types();
                        // Ensure the number of inputs matches the number of input types.
                        if input_types.len() != inputs.len() {
                            return Err(anyhow!("Expected {} inputs, found {}", input_types.len(), inputs.len()).into());
                        }

                        // Convert the caller's inputs to the callee's context.
                        let callee_inputs =
                            convert_caller_inputs_to_callee_inputs(&inputs, input_types, target.substack())?;

                        // Construct the callee's version of the request.
                        let callee_request = Request::sign(
                            private_key,
                            *target.substack().program_id(),
                            *function.name(),
                            callee_inputs.iter(),
                            input_types,
                            root_tvk,
                            is_root,
                            program_checksum,
                            true,
                            rng,
                        )?;

                        // Construct the request verification inputs.
                        let request_verification_inputs = CalleeDynamicRequest::from(&callee_request)?;
                        // Retrieve the call stack.
                        let mut call_stack = registers.call_stack();
                        // Push the callee's request onto the call stack.
                        call_stack.push(callee_request.clone())?;

                        // Add the callee's request to the authorization.
                        authorization.push(callee_request.clone())?;

                        // Execute the callee's request.
                        let callee_response =
                            target.substack().execute_function::<A, R>(call_stack, console_caller, root_tvk, rng)?;

                        // Convert the callee's outputs to the caller's context.
                        let caller_response_outputs = callee_response.to_dynamic_outputs()?;

                        // Return the request verification inputs and response.
                        (request_verification_inputs, caller_response_outputs)
                    }
                    // In `AuthorizeMocked` mode, throw an error.
                    CallStack::AuthorizeMocked(..) => {
                        return Err(anyhow!("Cannot 'execute' a function in 'AuthorizeMocked' mode.").into());
                    }
                    // In `Synthesize` or `CheckDeployment` mode, we use dummy inputs and outputs to avoid building a full sub-circuit.
                    CallStack::Synthesize(_, private_key, ..) | CallStack::CheckDeployment(_, private_key, ..) => {
                        // Note that it does not matter what program ID we use here, since we are only synthesizing dummy outputs.
                        let program_id = ProgramID::from_str("a.aleo")?;
                        // Note that it does not matter what function name we use here, since we are only synthesizing dummy outputs.
                        let function_name = Identifier::<N>::from_str("a")?;

                        // Compute the address.
                        let address = Address::try_from(private_key)?;

                        // Construct the request verification inputs.
                        let request_verification_inputs = CalleeDynamicRequest {
                            network_id: U16::new(N::ID),
                            program_id,
                            function_name,
                            signer: Address::rand(rng),
                            sk_tag: Field::rand(rng),
                            tvk: Field::rand(rng),
                            tcm: Field::rand(rng),
                            caller_input_ids: self
                                .operand_types()
                                .iter()
                                .map(|type_| match type_ {
                                    ValueType::Constant(..) => Ok(InputID::Constant(Field::rand(rng))),
                                    ValueType::Public(..) => Ok(InputID::Public(Field::rand(rng))),
                                    ValueType::Private(..) => Ok(InputID::Private(Field::rand(rng))),
                                    ValueType::Record(..) => Ok(InputID::Record(
                                        Field::rand(rng),
                                        Group::rand(rng),
                                        Field::rand(rng),
                                        Field::rand(rng),
                                        Field::rand(rng),
                                    )),
                                    ValueType::ExternalRecord(..) => Ok(InputID::ExternalRecord(Field::rand(rng))),
                                    ValueType::Future(..) => Err(anyhow!("A future cannot be input directly")),
                                    ValueType::DynamicRecord => Ok(InputID::DynamicRecord(Field::rand(rng))),
                                    ValueType::DynamicFuture => {
                                        Err(anyhow!("A dynamic future cannot be input directly"))
                                    }
                                })
                                .collect::<Result<Vec<_>>>()?,
                        };

                        // Sample the outputs.
                        let callee_response_outputs = self
                            .destination_types()
                            .iter()
                            .map(|output_type| match output_type {
                                ValueType::Record(_) => Err(anyhow!("A dynamic call cannot return a record.")),
                                ValueType::ExternalRecord(_) => {
                                    Err(anyhow!("A dynamic call cannot return an external record."))
                                }
                                ValueType::Future(_) => Err(anyhow!("A dynamic call cannot return a future.")),
                                // Sample the value.
                                _ => stack.sample_value(&address, &output_type.into(), rng),
                            })
                            .collect::<Result<Vec<_>>>()?;

                        // Return the request verification inputs and response.
                        (request_verification_inputs, callee_response_outputs)
                    }
                    // In PackageRun mode, we sign and execute the request once.
                    CallStack::PackageRun(_, private_key, ..) => {
                        // Get the target.
                        let Some(target) = target else {
                            return Err(anyhow!(
                                "Failed to resolve the target of the dynamic call in 'PackageRun' mode."
                            )
                            .into());
                        };
                        // Get the function.
                        let function = target.substack().program().get_function_ref(target.function_name())?;
                        // Retrieve the number of inputs.
                        let num_inputs = function.inputs().len();
                        // Ensure the number of inputs matches the number of input statements.
                        if num_inputs != inputs.len() {
                            return Err(anyhow!("Expected {} inputs, found {}", num_inputs, inputs.len()).into());
                        }
                        // Retrieve the program checksum, if the program has a constructor.
                        let program_checksum = match target.substack().program().contains_constructor() {
                            true => Some(target.substack().program_checksum_as_field()?),
                            false => None,
                        };

                        // Get the input types of the callee.
                        let input_types =
                            &target.substack().program().get_function_ref(target.function_name())?.input_types();
                        // Ensure that the number of inputs match.
                        if input_types.len() != inputs.len() {
                            return Err(anyhow!("Expected {} inputs, found {}", input_types.len(), inputs.len()).into());
                        }
                        // Convert the inputs to the callee's context.
                        let callee_inputs =
                            convert_caller_inputs_to_callee_inputs(&inputs, input_types, target.substack())?;
                        // Construct the callee's version of the request.
                        let callee_request = Request::sign(
                            private_key,
                            *target.substack().program_id(),
                            *function.name(),
                            callee_inputs.iter(),
                            input_types,
                            root_tvk,
                            is_root,
                            program_checksum,
                            true,
                            rng,
                        )?;

                        // Construct the request verification inputs.
                        let request_verification_inputs = CalleeDynamicRequest::from(&callee_request)?;

                        // Retrieve the call stack.
                        let mut call_stack = registers.call_stack();
                        // Push the callee's request onto the call stack.
                        call_stack.push(callee_request.clone())?;

                        // Evaluate the callee's request.
                        let callee_response =
                            target.substack().execute_function::<A, _>(call_stack, console_caller, root_tvk, rng)?;

                        // Convert the callee's outputs to the caller's context.
                        let caller_response_outputs = callee_response.to_dynamic_outputs()?;

                        // Return the request verification inputs and response.
                        (request_verification_inputs, caller_response_outputs)
                    }
                    // In `Evaluate` mode, throw an error.
                    CallStack::Evaluate(..) => {
                        return Err(anyhow!("Cannot 'execute' a function in 'evaluate' mode.").into());
                    }
                    // In `Execute` mode, evaluate and execute the instructions.
                    CallStack::Execute(authorization, _, translations) => {
                        // Get the target.
                        let Some(target) = target else {
                            return Err(
                                anyhow!("Failed to resolve the target of the dynamic call in 'Execute' mode.").into()
                            );
                        };
                        // Get the function.
                        let callee_function = target.substack().program().get_function_ref(target.function_name())?;
                        // Retrieve the number of inputs.
                        let num_inputs = callee_function.inputs().len();
                        // Ensure the number of inputs matches the number of input statements.
                        if num_inputs != inputs.len() {
                            return Err(anyhow!("Expected {} inputs, found {}", num_inputs, inputs.len()).into());
                        }

                        // Retrieve the callee's request (without popping it).
                        let callee_request = authorization.peek_next()?;

                        // Construct the request verification inputs.
                        let callee_request_verification_inputs = CalleeDynamicRequest::from(&callee_request)?;

                        // Evaluate the function, and load the outputs.
                        let console_callee_response = target.substack().evaluate_function::<A, R>(
                            registers.call_stack(),
                            console_caller,
                            root_tvk,
                            rng,
                        )?;
                        // Execute the request.
                        let callee_response = target.substack().execute_function::<A, R>(
                            registers.call_stack(),
                            console_caller,
                            root_tvk,
                            rng,
                        )?;

                        // Ensure the values are equal.
                        if console_callee_response.outputs() != callee_response.outputs() {
                            dev_eprintln!(
                                "\n{:#?} != {:#?}\n",
                                console_callee_response.outputs(),
                                callee_response.outputs()
                            );
                            return Err(anyhow!(
                                "Function '{}' outputs do not match in a 'call.dynamic' instruction.",
                                callee_function.name()
                            )
                            .into());
                        }

                        // Synthesizes the translation proving key for the given program and record
                        // (if not already synthesized), caches it in the stack, and returns it.
                        let get_translation_proving_key = |program_id: &ProgramID<N>,
                                                           record_name: &Identifier<N>,
                                                           rng: &mut R|
                         -> Result<ProvingKey<N>> {
                            let record_stack = match program_id == stack.program_id() {
                                true => stack,
                                false => &stack.get_stack_global(program_id)?,
                            };

                            record_stack.synthesize_translation_key::<A, R>(record_name, rng)?;
                            record_stack.get_proving_key(record_name)
                        };

                        let caller_console_input_ids = callee_request.to_dynamic_input_ids()?;
                        let callee_console_input_ids = callee_request.input_ids();
                        let callee_console_function_id = compute_function_id(
                            &U16::<N>::new(N::ID),
                            callee_request.program_id(),
                            callee_request.function_name(),
                        )?;

                        // Collect input record translations.
                        let input_translations = collect_input_translations(
                            &inputs,
                            &caller_console_input_ids,
                            self.operand_types(),
                            callee_request.inputs(),
                            callee_console_input_ids,
                            &callee_function.input_types(),
                            callee_request.program_id(),
                            callee_console_function_id,
                            *callee_request.tvk(),
                        )?;

                        // Synthesize translation proving keys and store input translations.
                        // Push to the top group of the translation stack (the caller's level).
                        for translation in input_translations {
                            let proving_key =
                                get_translation_proving_key(&translation.program_id, &translation.record_name, rng)?;
                            translations
                                .write()
                                .last_mut()
                                .ok_or_else(|| anyhow!("Translation stack is empty"))?
                                .push((translation, proving_key));
                        }

                        // Collect output record translations.
                        let caller_console_outputs = callee_response.to_dynamic_outputs()?;
                        let caller_console_output_ids = callee_response.to_dynamic_output_ids(
                            callee_request.network_id(),
                            callee_request.program_id(),
                            callee_request.function_name(),
                            callee_request.inputs().len(),
                            callee_request.tvk(),
                            callee_request.tcm(),
                        )?;

                        // Closure to compute record view key for non-external output records.
                        let compute_record_view_key = |operand_index: usize,
                                                       record_static: &console::program::Record<N, Plaintext<N>>|
                         -> Result<Option<Field<N>>> {
                            // Get the output index.
                            let Some(Operand::Register(register)) = target
                                .substack()
                                .get_function_ref(target.function_name())?
                                .outputs()
                                .get_index(operand_index)
                                .map(|op| op.operand())
                            else {
                                return Err(anyhow!("Expected output to be a register"));
                            };
                            // Prepare the index as a field element.
                            let index = Field::from_u64(register.locator());
                            // Compute the randomizer as `HashToScalar(tvk || index)`.
                            let randomizer = N::hash_to_scalar_psd2(&[*callee_request.tvk(), index])?;
                            // Compute the record view key.
                            let rvk = (*record_static.owner().to_group() * randomizer).to_x_coordinate();
                            Ok(Some(rvk))
                        };

                        let output_translations = collect_output_translations(
                            &caller_console_outputs,
                            &caller_console_output_ids,
                            self.destination_types(),
                            callee_response.outputs(),
                            callee_response.output_ids(),
                            &callee_function.output_types(),
                            callee_request.program_id(),
                            callee_console_function_id,
                            *callee_request.tvk(),
                            num_inputs,
                            compute_record_view_key,
                        )?;

                        // Synthesize translation proving keys and store output translations.
                        // Push to the top group of the translation stack (the caller's level).
                        for translation in output_translations {
                            let proving_key =
                                get_translation_proving_key(&translation.program_id, &translation.record_name, rng)?;
                            translations
                                .write()
                                .last_mut()
                                .ok_or_else(|| anyhow!("Translation stack is empty"))?
                                .push((translation, proving_key));
                        }

                        // Return the caller's request and response.
                        (callee_request_verification_inputs, caller_console_outputs)
                    }
                }
            };
            lap!(timer, "Computed the request and response");

            // Restore the caller's circuit, which was saved before the callee was synthesized.
            A::inject_r1cs(r1cs);

            use circuit::Inject;

            // Inject the network ID as `Mode::Constant`.
            let network_id = circuit::U16::constant(request.network_id);
            // Inject the program ID name as `Mode::Public`.
            let program_id = circuit::ProgramID::public(request.program_id);
            // Inject the function name as `Mode::Public`.
            let function_name = circuit::Identifier::public(request.function_name);
            // Inject the function ID as `Mode::Public`.
            let function_id = circuit::Field::new(
                circuit::Mode::Public,
                compute_function_id(&request.network_id, &request.program_id, &request.function_name)?,
            );

            // Ensure that the program and function names in the registers match the witnessed values.
            A::assert_eq(program_id.name(), program_name_as_field)?;
            A::assert_eq(program_id.network(), program_network_as_field)?;
            A::assert_eq(&function_name, function_name_as_field)?;

            // Ensure exactly 4 public variables were added: program name, program network,
            // function name, and function ID. This guards against spurious public injections.
            if A::num_public() != num_public + 4 {
                return Err(anyhow!("Forbidden: 'call.dynamic' injected excess public variables").into());
            }

            // Inject the `signer` (from the request) as `Mode::Private`.
            let signer = circuit::Address::new(circuit::Mode::Private, request.signer);
            // Inject the `sk_tag` (from the request) as `Mode::Private`.
            let sk_tag = circuit::Field::new(circuit::Mode::Private, request.sk_tag);
            // Inject the `tvk` (from the request) as `Mode::Private`.
            let tvk = circuit::Field::new(circuit::Mode::Private, request.tvk);
            // Inject the `tcm` (from the request) as `Mode::Public`.
            let tcm = circuit::Field::new(circuit::Mode::Public, request.tcm);
            // Compute the transition commitment as `Hash(tvk)`.
            let candidate_tcm = A::hash_psd2(&[tvk.clone()]);
            // Ensure the transition commitment matches the computed transition commitment.
            A::assert_eq(&tcm, candidate_tcm)?;

            // Inject the caller input IDs as `Mode::Public`.
            let input_ids = request
                .caller_input_ids
                .iter()
                .map(|input_id| circuit::InputID::new(circuit::Mode::Public, *input_id))
                .collect::<Vec<_>>();

            // Ensure the candidate input IDs match their computed inputs.
            let (check_input_ids, _) = circuit::Request::check_input_ids::<false>(
                &network_id,
                &program_id,
                &function_name,
                &input_ids,
                inputs,
                self.operand_types(),
                &signer,
                &sk_tag,
                &tvk,
                &tcm,
                None,
                Some(function_id.clone()),
            );
            A::assert(check_input_ids)?;
            lap!(timer, "Checked the input ids");

            // Checking that none of the outputs in the caller's context are records or futures.
            for output in caller_response_outputs.iter() {
                match output {
                    Value::Record(_) => return Err(anyhow!("A dynamic call cannot return a record.").into()),
                    Value::Future(_) => return Err(anyhow!("A dynamic call cannot return a future.").into()),
                    Value::Plaintext(_) | Value::DynamicRecord(_) | Value::DynamicFuture(_) => {} // Do nothing.
                }
            }

            // Use `None` for the output registers. This is safe since an output of a dynamic call cannot be a record.
            let output_registers = vec![None; caller_response_outputs.len()];

            // Inject the outputs as `Mode::Private` (with the 'tcm' and output IDs as `Mode::Public`).
            let outputs = circuit::Response::process_outputs_from_callback(
                &network_id,
                &program_id,
                &function_name,
                inputs.len(),
                &tvk,
                &tcm,
                caller_response_outputs.clone(),
                self.destination_types(),
                &output_registers,
                Some(function_id),
            );
            lap!(timer, "Checked the outputs");

            // Return the circuit outputs.
            outputs
        };

        // Assign the outputs to the destination registers.
        if outputs.len() != self.destinations().len() {
            return Err(anyhow!(
                "[execute Dynamic] Expected {} outputs, but {} were provided.",
                self.destinations().len(),
                outputs.len()
            )
            .into());
        }
        for (output, register) in outputs.into_iter().zip_eq(&self.destinations()) {
            // Assign the output to the register.
            registers.store_circuit(stack, register, output)?;
        }
        lap!(timer, "Assigned the outputs to registers");

        finish!(timer);

        Ok(())
    }
}

// Information needed to verify the callee's request in a dynamic call.
struct CalleeDynamicRequest<N: Network> {
    // The network ID.
    network_id: U16<N>,
    // The program ID.
    program_id: ProgramID<N>,
    // The function name.
    function_name: Identifier<N>,
    // The signer.
    signer: Address<N>,
    // The sk_tag.
    sk_tag: Field<N>,
    // The tvk.
    tvk: Field<N>,
    // The tcm.
    tcm: Field<N>,
    // The caller input IDs.
    caller_input_ids: Vec<InputID<N>>,
}

impl<N: Network> CalleeDynamicRequest<N> {
    /// Constructs the request verification inputs from a request and caller input IDs.
    #[inline]
    pub fn from(request: &Request<N>) -> Result<Self> {
        Ok(Self {
            network_id: *request.network_id(),
            program_id: *request.program_id(),
            function_name: *request.function_name(),
            signer: *request.signer(),
            sk_tag: *request.sk_tag(),
            tvk: *request.tvk(),
            tcm: *request.tcm(),
            caller_input_ids: request.to_dynamic_input_ids()?,
        })
    }
}

/// Converts caller inputs to callee inputs for a dynamic call.
///
/// In a dynamic call, the caller provides inputs in its own context (e.g., `DynamicRecord`),
/// but the callee expects inputs in its context (e.g., `Record` with a specific type).
/// This function performs the necessary conversions:
///
/// - `DynamicRecord` → `Record`: When the callee expects a `Record` or `ExternalRecord`,
///   the dynamic record is converted to a concrete record by looking up the owner visibility
///   from the callee's program.
///
/// - `Future` / `DynamicFuture`: These are not allowed as inputs to dynamic calls and will
///   cause this function to return an error.
///
/// - All other types (`Plaintext`, `Record`, `DynamicRecord` not matching the above): Passed through unchanged.
///
/// # Arguments
/// * `inputs` - The caller's input values
/// * `input_types` - The callee's expected input types
/// * `stack` - The callee's stack, used to look up record type information
///
/// # Returns
/// A vector of converted values suitable for the callee's context.
///
/// # Errors
/// Returns an error if:
/// - A `Future` or `DynamicFuture` is provided as input
/// - Record type lookup fails
/// - Dynamic record conversion fails
fn convert_caller_inputs_to_callee_inputs<N: Network>(
    inputs: &[Value<N>],
    input_types: &[ValueType<N>],
    stack: &Stack<N>,
) -> Result<Vec<Value<N>>> {
    inputs
        .iter()
        .zip_eq(input_types.iter())
        .map(|(input, input_type)| {
            match (input, input_type) {
                // Convert DynamicRecord to Record when callee expects a Record.
                (Value::DynamicRecord(dynamic_record), ValueType::Record(record_name)) => {
                    // Look up the owner visibility from the callee's program.
                    let owner_is_private = stack.program().get_record(record_name)?.owner().is_private();
                    Ok(Value::Record(dynamic_record.to_record(owner_is_private)?))
                }
                // Convert DynamicRecord to Record when callee expects an ExternalRecord.
                (Value::DynamicRecord(dynamic_record), ValueType::ExternalRecord(locator)) => {
                    let record_program_id = locator.program_id();
                    let record_name = locator.resource();

                    // Obtain the program where the external record is defined.
                    let external_record_stack = stack.get_external_stack(record_program_id)?;

                    // Look up the owner visibility from the external program.
                    let owner_is_private =
                        external_record_stack.program().get_record(record_name)?.owner().is_private();

                    Ok(Value::Record(dynamic_record.to_record(owner_is_private)?))
                }
                // Futures are not allowed as inputs to dynamic calls.
                (Value::Future(_), _) => Err(anyhow!("A future cannot be an input to a dynamic call.")),
                // Dynamic futures are not allowed as inputs to dynamic calls.
                (Value::DynamicFuture(_), _) => Err(anyhow!("A dynamic future cannot be an input to a dynamic call.")),
                // Plaintext values pass through unchanged.
                (Value::Plaintext(_), _) => Ok(input.clone()),
                // Record values pass through unchanged.
                (Value::Record(_), _) => Ok(input.clone()),
                // DynamicRecord values that don't match the above patterns pass through unchanged.
                (Value::DynamicRecord(_), _) => Ok(input.clone()),
            }
        })
        .collect()
}

// A reference to a stack, either local or external.
enum StackRef<'a, N: Network> {
    Local(&'a Stack<N>),
    External(Arc<Stack<N>>),
}

impl<'a, N: Network> Deref for StackRef<'a, N> {
    type Target = Stack<N>;

    fn deref(&self) -> &Self::Target {
        match self {
            StackRef::Local(stack) => stack,
            StackRef::External(stack) => stack.as_ref(),
        }
    }
}

// A resolved target of a dynamic call.
struct ResolvedTarget<'a, N: Network> {
    // The program ID.
    program_id: ProgramID<N>,
    // The function name.
    function_name: Identifier<N>,
    // The stack.
    substack: StackRef<'a, N>,
}

impl<'a, N: Network> ResolvedTarget<'a, N> {
    /// Returns the program ID.
    #[inline]
    pub fn program_id(&self) -> &ProgramID<N> {
        &self.program_id
    }

    /// Returns the function name.
    #[inline]
    pub fn function_name(&self) -> &Identifier<N> {
        &self.function_name
    }

    /// Returns the stack.
    #[inline]
    pub fn substack(&self) -> &Stack<N> {
        &self.substack
    }
}

// A helper function that attempts to resolve the target of a dynamic call.
// This function returns:
// - Ok(Some(ResolvedTarget)) if the target is successfully resolved.
// - Ok(None) in `Synthesize` or `CheckDeployment` mode when the target cannot be resolved.
// - Err(_) in other modes when the target cannot be resolved.
fn resolve_dynamic_target<'a, N: Network>(
    call_stack: &'a CallStack<N>,
    stack: &'a Stack<N>,
    program_name_as_field: &Field<N>,
    program_network_as_field: &Field<N>,
    function_name_as_field: &Field<N>,
) -> Result<Option<ResolvedTarget<'a, N>>> {
    // Determine whether we are in "dummy" (`Synthesize` or `CheckDeployment`) mode.
    let in_dummy_mode = matches!(call_stack, CallStack::Synthesize(..) | CallStack::CheckDeployment(..));

    // Decode the program name, exiting gracefully in dummy mode if it fails.
    let program_name = match Identifier::from_field(program_name_as_field) {
        Ok(id) => id,
        Err(_) if in_dummy_mode => {
            return Ok(None);
        }
        Err(e) => return Err(anyhow!("Failed to decode the program name in a dynamic call: {e}")),
    };

    // Decode the program network, exiting gracefully in dummy mode if it fails.
    let program_network = match Identifier::from_field(program_network_as_field) {
        Ok(id) => id,
        Err(_) if in_dummy_mode => {
            return Ok(None);
        }
        Err(e) => return Err(anyhow!("Failed to decode the program network in a dynamic call: {e}")),
    };

    // Decode the function name, exiting gracefully in dummy mode if it fails.
    let function_name = match Identifier::from_field(function_name_as_field) {
        Ok(id) => id,
        Err(_) if in_dummy_mode => {
            return Ok(None);
        }
        Err(e) => return Err(anyhow!("Failed to decode the function name in a dynamic call: {e}")),
    };

    // Construct the program ID.
    let program_id = match ProgramID::try_from((program_name, program_network)) {
        Ok(id) => id,
        Err(_) if in_dummy_mode => {
            return Ok(None);
        }
        Err(e) => return Err(anyhow!("Failed to construct the program ID in a dynamic call: {e}")),
    };

    // Verify that the call is not to `credits.aleo/fee_private` or `credits.aleo/fee_public`.
    // Safe: "fee_private" and "fee_public" are hardcoded valid identifiers.
    let fee_private = Identifier::from_str("fee_private").expect("'fee_private' is a valid identifier");
    let fee_public = Identifier::from_str("fee_public").expect("'fee_public' is a valid identifier");
    if program_id == ProgramID::credits() && (function_name == fee_private || function_name == fee_public) {
        return Err(anyhow!(
            "Cannot perform an external call to 'credits.aleo/fee_private' or 'credits.aleo/fee_public'."
        ));
    }

    // Retrieve the optional external stack.
    let external_stack = match stack.program().id() == &program_id {
        false => match stack.get_stack_global(&program_id) {
            Ok(ext_stack) => Some(ext_stack),
            Err(_) if in_dummy_mode => {
                return Ok(None);
            }
            Err(e) => return Err(anyhow!("Failed to retrieve the external stack in a dynamic call: {e}")),
        },
        true => None,
    };

    // Retrieve the substack.
    let substack = match &external_stack {
        Some(external_stack) => StackRef::External(external_stack.clone()),
        None => StackRef::Local(stack),
    };

    // Verify that the function is not a closure.
    if substack.program().get_closure(&function_name).is_ok() {
        Err(anyhow!("Cannot dynamically evaluate a closure: {function_name}"))
    } else if substack.program().contains_function(&function_name) {
        Ok(Some(ResolvedTarget { program_id, function_name, substack }))
    } else if in_dummy_mode {
        Ok(None)
    } else {
        Err(anyhow!("Dynamic call to '{program_id}/{function_name}' is invalid or unsupported."))
    }
}

// Checks that all translation arrays have the same length.
fn check_translation_array_lengths(
    context: &str,
    caller_values_len: usize,
    caller_ids_len: usize,
    caller_types_len: usize,
    callee_values_len: usize,
    callee_ids_len: usize,
    callee_types_len: usize,
) -> Result<()> {
    // Ensure caller and callee have the same number of values.
    ensure!(
        caller_values_len == callee_values_len,
        "{context}: caller and callee values should have the same length ({caller_values_len} vs. {callee_values_len})"
    );
    // Ensure caller values and IDs have the same length.
    ensure!(
        caller_values_len == caller_ids_len,
        "{context}: caller values and IDs should have the same length ({caller_values_len} vs. {caller_ids_len})"
    );
    // Ensure caller values and types have the same length.
    ensure!(
        caller_values_len == caller_types_len,
        "{context}: caller values and types should have the same length ({caller_values_len} vs. {caller_types_len})"
    );
    // Ensure callee values and IDs have the same length.
    ensure!(
        callee_values_len == callee_ids_len,
        "{context}: callee values and IDs should have the same length ({callee_values_len} vs. {callee_ids_len})"
    );
    // Ensure callee values and types have the same length.
    ensure!(
        callee_values_len == callee_types_len,
        "{context}: callee values and types should have the same length ({callee_values_len} vs. {callee_types_len})"
    );
    Ok(())
}

// Collects input record translations from dynamic to static records.
// The caller is responsible for synthesizing translation proving keys.
fn collect_input_translations<N: Network>(
    caller_values: &[Value<N>],
    caller_ids: &[InputID<N>],
    caller_types: &[ValueType<N>],
    callee_values: &[Value<N>],
    callee_ids: &[InputID<N>],
    callee_types: &[ValueType<N>],
    callee_program_id: &ProgramID<N>,
    function_id: Field<N>,
    tvk: Field<N>,
) -> Result<Vec<TranslationAssignment<N>>> {
    // Validate that all arrays have the same length.
    check_translation_array_lengths(
        "Inputs",
        caller_values.len(),
        caller_ids.len(),
        caller_types.len(),
        callee_values.len(),
        callee_ids.len(),
        callee_types.len(),
    )?;

    // Initialize the translations vector.
    let mut translations = Vec::new();

    // Iterate over all inputs, matching caller and callee data.
    for (operand_index, (caller_value, caller_id, caller_type, callee_value, callee_id, callee_type)) in
        itertools::izip!(caller_values, caller_ids, caller_types, callee_values, callee_ids, callee_types).enumerate()
    {
        match (caller_value, caller_id, caller_type, callee_value, callee_id, callee_type) {
            // Case: DynamicRecord translates to a non-external Record.
            (
                Value::DynamicRecord(record_dynamic),
                InputID::DynamicRecord(id_dynamic),
                ValueType::DynamicRecord,
                Value::Record(record_static),
                InputID::Record(_record_commitment, gamma, record_view_key, serial_number, _tag),
                ValueType::Record(record_name),
            ) => {
                // Add the translation data for this non-external record.
                translations.push(TranslationAssignment {
                    record_static: record_static.clone(),
                    record_dynamic: record_dynamic.clone(),
                    program_id: *callee_program_id,
                    function_id,
                    record_name: *record_name,
                    is_to_static: true,
                    is_external_record: false,
                    tvk,
                    record_view_key: Some(*record_view_key),
                    gamma: Some(*gamma),
                    id_static: *serial_number,
                    id_dynamic: *id_dynamic,
                    record_register_index: u16::try_from(operand_index)
                        .map_err(|_| anyhow!("Input operand index {operand_index} exceeds u16"))?,
                });
            }
            // Case: DynamicRecord translates to an ExternalRecord.
            (
                Value::DynamicRecord(record_dynamic),
                InputID::DynamicRecord(id_dynamic),
                ValueType::DynamicRecord,
                Value::Record(record_static),
                InputID::ExternalRecord(id_static),
                ValueType::ExternalRecord(record_locator),
            ) => {
                // Add the translation data for this external record.
                translations.push(TranslationAssignment {
                    record_static: record_static.clone(),
                    record_dynamic: record_dynamic.clone(),
                    program_id: *record_locator.program_id(),
                    function_id,
                    record_name: *record_locator.resource(),
                    is_to_static: true,
                    is_external_record: true,
                    tvk,
                    record_view_key: None,
                    gamma: None,
                    id_static: *id_static,
                    id_dynamic: *id_dynamic,
                    record_register_index: u16::try_from(operand_index)
                        .map_err(|_| anyhow!("Input operand index {operand_index} exceeds u16"))?,
                });
            }
            // Plaintext values do not require translation.
            (Value::Plaintext(..), ..) => {}
            // Record values that don't match the above patterns do not require translation.
            (Value::Record(..), ..) => {}
            // Future values do not require translation.
            (Value::Future(..), ..) => {}
            // DynamicFuture values do not require translation.
            (Value::DynamicFuture(..), ..) => {}
            // DynamicRecord values that don't match the above patterns do not require translation.
            (Value::DynamicRecord(..), ..) => {}
        }
    }

    Ok(translations)
}

// Collects output record translations from static to dynamic records.
// The `compute_record_view_key` closure computes the record view key for non-external records.
fn collect_output_translations<N: Network>(
    caller_values: &[Value<N>],
    caller_ids: &[OutputID<N>],
    caller_types: &[ValueType<N>],
    callee_values: &[Value<N>],
    callee_ids: &[OutputID<N>],
    callee_types: &[ValueType<N>],
    callee_program_id: &ProgramID<N>,
    function_id: Field<N>,
    tvk: Field<N>,
    num_inputs: usize,
    compute_record_view_key: impl Fn(usize, &console::program::Record<N, Plaintext<N>>) -> Result<Option<Field<N>>>,
) -> Result<Vec<TranslationAssignment<N>>> {
    // Validate that all arrays have the same length.
    check_translation_array_lengths(
        "Outputs",
        caller_values.len(),
        caller_ids.len(),
        caller_types.len(),
        callee_values.len(),
        callee_ids.len(),
        callee_types.len(),
    )?;

    // Initialize the translations vector.
    let mut translations = Vec::new();

    // Iterate over all outputs, matching caller and callee data.
    for (operand_index, (caller_value, caller_id, caller_type, callee_value, callee_id, callee_type)) in
        itertools::izip!(caller_values, caller_ids, caller_types, callee_values, callee_ids, callee_types).enumerate()
    {
        match (caller_value, caller_id, caller_type, callee_value, callee_id, callee_type) {
            // Case: DynamicRecord translates to a non-external Record.
            (
                Value::DynamicRecord(record_dynamic),
                OutputID::DynamicRecord(id_dynamic),
                ValueType::DynamicRecord,
                Value::Record(record_static),
                OutputID::Record(id_static, _checksum, _sender_ciphertext),
                ValueType::Record(record_name),
            ) => {
                // Compute the record view key for this non-external record.
                let record_view_key = compute_record_view_key(operand_index, record_static)?;

                // Add the translation data for this non-external record.
                translations.push(TranslationAssignment {
                    record_static: record_static.clone(),
                    record_dynamic: record_dynamic.clone(),
                    program_id: *callee_program_id,
                    function_id,
                    record_name: *record_name,
                    is_to_static: false,
                    is_external_record: false,
                    tvk,
                    record_view_key,
                    gamma: None,
                    id_static: *id_static,
                    id_dynamic: *id_dynamic,
                    record_register_index: u16::try_from(num_inputs + operand_index)
                        .map_err(|_| anyhow!("Output operand index {} exceeds u16", num_inputs + operand_index))?,
                });
            }
            // Case: DynamicRecord translates to an ExternalRecord.
            (
                Value::DynamicRecord(record_dynamic),
                OutputID::DynamicRecord(id_dynamic),
                ValueType::DynamicRecord,
                Value::Record(record_static),
                OutputID::ExternalRecord(id_static),
                ValueType::ExternalRecord(record_locator),
            ) => {
                // Add the translation data for this external record.
                translations.push(TranslationAssignment {
                    record_static: record_static.clone(),
                    record_dynamic: record_dynamic.clone(),
                    program_id: *record_locator.program_id(),
                    function_id,
                    record_name: *record_locator.resource(),
                    is_to_static: false,
                    is_external_record: true,
                    tvk,
                    record_view_key: None,
                    gamma: None,
                    id_static: *id_static,
                    id_dynamic: *id_dynamic,
                    record_register_index: u16::try_from(num_inputs + operand_index)
                        .map_err(|_| anyhow!("Output operand index {} exceeds u16", num_inputs + operand_index))?,
                });
            }
            // Plaintext values do not require translation.
            (Value::Plaintext(..), ..) => {}
            // Record values that don't match the above patterns do not require translation.
            (Value::Record(..), ..) => {}
            // Future values do not require translation.
            (Value::Future(..), ..) => {}
            // DynamicFuture values do not require translation.
            (Value::DynamicFuture(..), ..) => {}
            // DynamicRecord values that don't match the above patterns do not require translation.
            (Value::DynamicRecord(..), ..) => {}
        }
    }

    Ok(translations)
}