Skip to main content

sui_move_natives_latest/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use self::{
5    address::{AddressFromBytesCostParams, AddressFromU256CostParams, AddressToU256CostParams},
6    config::ConfigReadSettingImplCostParams,
7    crypto::{bls12381, ecdsa_k1, ecdsa_r1, ecvrf, ed25519, groth16, hash, hmac},
8    crypto::{
9        bls12381::{Bls12381Bls12381MinPkVerifyCostParams, Bls12381Bls12381MinSigVerifyCostParams},
10        ecdsa_k1::{
11            EcdsaK1DecompressPubkeyCostParams, EcdsaK1EcrecoverCostParams,
12            EcdsaK1Secp256k1VerifyCostParams,
13        },
14        ecdsa_r1::{EcdsaR1EcrecoverCostParams, EcdsaR1Secp256R1VerifyCostParams},
15        ecvrf::EcvrfEcvrfVerifyCostParams,
16        ed25519::Ed25519VerifyCostParams,
17        groth16::{
18            Groth16PrepareVerifyingKeyCostParams, Groth16VerifyGroth16ProofInternalCostParams,
19        },
20        hash::{HashBlake2b256CostParams, HashKeccak256CostParams},
21        hmac::HmacHmacSha3256CostParams,
22        poseidon,
23    },
24    dynamic_field::{
25        DynamicFieldAddChildObjectCostParams, DynamicFieldBorrowChildObjectCostParams,
26        DynamicFieldHasChildObjectCostParams, DynamicFieldHasChildObjectWithTyCostParams,
27        DynamicFieldHashTypeAndKeyCostParams, DynamicFieldRemoveChildObjectCostParams,
28    },
29    event::EventEmitCostParams,
30    object::{BorrowUidCostParams, DeleteImplCostParams, RecordNewIdCostParams},
31    package::PackageVersioningOriginalPackageIdImplCostParams,
32    scratch::{
33        ScratchAddCostParams, ScratchExistsCostParams, ScratchExistsWithTypeCostParams,
34        ScratchReadCostParams, ScratchRemoveCostParams,
35    },
36    transfer::{
37        TransferFreezeObjectCostParams, TransferInternalCostParams, TransferShareObjectCostParams,
38    },
39    tx_context::{
40        TxContextDeriveIdCostParams, TxContextEpochCostParams, TxContextEpochTimestampMsCostParams,
41        TxContextFreshIdCostParams, TxContextGasBudgetCostParams, TxContextGasPriceCostParams,
42        TxContextIdsCreatedCostParams, TxContextRGPCostParams, TxContextReplaceCostParams,
43        TxContextSenderCostParams, TxContextSponsorCostParams,
44    },
45    types::TypesIsOneTimeWitnessCostParams,
46    validator::ValidatorValidateMetadataBcsCostParams,
47};
48use crate::crypto::group_ops::GroupOpsCostParams;
49use crate::crypto::poseidon::PoseidonBN254CostParams;
50use crate::crypto::rangeproofs::{self, BulletproofsCostParams};
51use crate::crypto::zklogin;
52use crate::crypto::zklogin::{CheckZkloginIdCostParams, CheckZkloginIssuerCostParams};
53use crate::{crypto::group_ops, transfer::PartyTransferInternalCostParams};
54use better_any::{Tid, TidAble};
55use crypto::nitro_attestation::{self, NitroAttestationCostParams};
56use crypto::vdf::{self, VDFCostParams};
57use move_binary_format::errors::{PartialVMError, PartialVMResult};
58use move_binary_format::safe_unwrap;
59use move_core_types::{
60    annotated_value as A,
61    gas_algebra::{AbstractMemorySize, InternalGas},
62    identifier::Identifier,
63    language_storage::{StructTag, TypeTag},
64    runtime_value as R,
65    vm_status::StatusCode,
66};
67use move_vm_runtime::natives::{
68    extensions::NativeExtensionMarker,
69    functions::{NativeContext, NativeFunction, NativeFunctionTable},
70    move_stdlib::{self as MSN, GasParameters},
71};
72use move_vm_runtime::{
73    execution::{
74        Type,
75        values::{Struct, Value},
76    },
77    natives::functions::NativeResult,
78    shared::views::{SizeConfig, ValueView},
79};
80use std::sync::Arc;
81use sui_protocol_config::ProtocolConfig;
82use sui_types::{MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS, SUI_SYSTEM_ADDRESS};
83use transfer::TransferReceiveObjectInternalCostParams;
84
85mod accumulator;
86mod address;
87mod config;
88mod crypto;
89mod dynamic_field;
90pub mod event;
91pub mod funds_accumulator;
92mod object;
93pub mod object_runtime;
94mod package;
95mod protocol_config;
96mod random;
97pub mod scratch;
98pub mod test_scenario;
99mod test_utils;
100pub mod transaction_context;
101mod transfer;
102mod tx_context;
103mod types;
104mod validator;
105
106// TODO: remove in later PRs once we define the proper cost of native functions
107const DEFAULT_UNUSED_TX_CONTEXT_ENTRY_COST: u64 = 10;
108#[derive(Tid)]
109pub struct NativesCostTable {
110    // Address natives
111    pub address_from_bytes_cost_params: AddressFromBytesCostParams,
112    pub address_to_u256_cost_params: AddressToU256CostParams,
113    pub address_from_u256_cost_params: AddressFromU256CostParams,
114
115    // Config
116    pub config_read_setting_impl_cost_params: ConfigReadSettingImplCostParams,
117
118    // Package versioning
119    pub package_original_package_id_impl_cost_params:
120        PackageVersioningOriginalPackageIdImplCostParams,
121
122    // Dynamic field natives
123    pub dynamic_field_hash_type_and_key_cost_params: DynamicFieldHashTypeAndKeyCostParams,
124    pub dynamic_field_add_child_object_cost_params: DynamicFieldAddChildObjectCostParams,
125    pub dynamic_field_borrow_child_object_cost_params: DynamicFieldBorrowChildObjectCostParams,
126    pub dynamic_field_remove_child_object_cost_params: DynamicFieldRemoveChildObjectCostParams,
127    pub dynamic_field_has_child_object_cost_params: DynamicFieldHasChildObjectCostParams,
128    pub dynamic_field_has_child_object_with_ty_cost_params:
129        DynamicFieldHasChildObjectWithTyCostParams,
130
131    // Scratch natives
132    pub scratch_add_cost_params: ScratchAddCostParams,
133    pub scratch_read_cost_params: ScratchReadCostParams,
134    pub scratch_remove_cost_params: ScratchRemoveCostParams,
135    pub scratch_exists_cost_params: ScratchExistsCostParams,
136    pub scratch_exists_with_type_cost_params: ScratchExistsWithTypeCostParams,
137
138    // Event natives
139    pub event_emit_cost_params: EventEmitCostParams,
140
141    // Funds accumulator natives
142    pub reserve_object_funds_for_withdrawal_cost_params:
143        funds_accumulator::ReserveObjectFundsForWithdrawalCostParams,
144
145    // Object
146    pub borrow_uid_cost_params: BorrowUidCostParams,
147    pub delete_impl_cost_params: DeleteImplCostParams,
148    pub record_new_id_cost_params: RecordNewIdCostParams,
149
150    // Transfer
151    pub transfer_transfer_internal_cost_params: TransferInternalCostParams,
152    pub transfer_party_transfer_internal_cost_params: PartyTransferInternalCostParams,
153    pub transfer_freeze_object_cost_params: TransferFreezeObjectCostParams,
154    pub transfer_share_object_cost_params: TransferShareObjectCostParams,
155
156    // TxContext
157    pub tx_context_derive_id_cost_params: TxContextDeriveIdCostParams,
158    pub tx_context_fresh_id_cost_params: TxContextFreshIdCostParams,
159    pub tx_context_sender_cost_params: TxContextSenderCostParams,
160    pub tx_context_epoch_cost_params: TxContextEpochCostParams,
161    pub tx_context_epoch_timestamp_ms_cost_params: TxContextEpochTimestampMsCostParams,
162    pub tx_context_sponsor_cost_params: TxContextSponsorCostParams,
163    pub tx_context_rgp_cost_params: TxContextRGPCostParams,
164    pub tx_context_gas_price_cost_params: TxContextGasPriceCostParams,
165    pub tx_context_gas_budget_cost_params: TxContextGasBudgetCostParams,
166    pub tx_context_ids_created_cost_params: TxContextIdsCreatedCostParams,
167    pub tx_context_replace_cost_params: TxContextReplaceCostParams,
168
169    // Type
170    pub type_is_one_time_witness_cost_params: TypesIsOneTimeWitnessCostParams,
171
172    // Validator
173    pub validator_validate_metadata_bcs_cost_params: ValidatorValidateMetadataBcsCostParams,
174
175    // Crypto natives
176    pub crypto_invalid_arguments_cost: InternalGas,
177    // bls12381
178    pub bls12381_bls12381_min_sig_verify_cost_params: Bls12381Bls12381MinSigVerifyCostParams,
179    pub bls12381_bls12381_min_pk_verify_cost_params: Bls12381Bls12381MinPkVerifyCostParams,
180
181    // ecdsak1
182    pub ecdsa_k1_ecrecover_cost_params: EcdsaK1EcrecoverCostParams,
183    pub ecdsa_k1_decompress_pubkey_cost_params: EcdsaK1DecompressPubkeyCostParams,
184    pub ecdsa_k1_secp256k1_verify_cost_params: EcdsaK1Secp256k1VerifyCostParams,
185
186    // ecdsar1
187    pub ecdsa_r1_ecrecover_cost_params: EcdsaR1EcrecoverCostParams,
188    pub ecdsa_r1_secp256_r1_verify_cost_params: EcdsaR1Secp256R1VerifyCostParams,
189
190    // ecvrf
191    pub ecvrf_ecvrf_verify_cost_params: EcvrfEcvrfVerifyCostParams,
192
193    // ed25519
194    pub ed25519_verify_cost_params: Ed25519VerifyCostParams,
195
196    // groth16
197    pub groth16_prepare_verifying_key_cost_params: Groth16PrepareVerifyingKeyCostParams,
198    pub groth16_verify_groth16_proof_internal_cost_params:
199        Groth16VerifyGroth16ProofInternalCostParams,
200
201    // hash
202    pub hash_blake2b256_cost_params: HashBlake2b256CostParams,
203    pub hash_keccak256_cost_params: HashKeccak256CostParams,
204
205    // poseidon
206    pub poseidon_bn254_cost_params: PoseidonBN254CostParams,
207
208    // hmac
209    pub hmac_hmac_sha3_256_cost_params: HmacHmacSha3256CostParams,
210
211    // group ops
212    pub group_ops_cost_params: GroupOpsCostParams,
213
214    // vdf
215    pub vdf_cost_params: VDFCostParams,
216
217    // zklogin
218    pub check_zklogin_id_cost_params: CheckZkloginIdCostParams,
219    pub check_zklogin_issuer_cost_params: CheckZkloginIssuerCostParams,
220
221    // Receive object
222    pub transfer_receive_object_internal_cost_params: TransferReceiveObjectInternalCostParams,
223
224    // nitro attestation
225    pub nitro_attestation_cost_params: NitroAttestationCostParams,
226
227    // bulletproofs range proofs
228    pub bulletproofs_cost_params: BulletproofsCostParams,
229}
230
231impl NativeExtensionMarker<'_> for NativesCostTable {}
232
233impl NativesCostTable {
234    pub fn from_protocol_config(protocol_config: &ProtocolConfig) -> NativesCostTable {
235        Self {
236            address_from_bytes_cost_params: AddressFromBytesCostParams {
237                address_from_bytes_cost_base: protocol_config.address_from_bytes_cost_base().into(),
238            },
239            address_to_u256_cost_params: AddressToU256CostParams {
240                address_to_u256_cost_base: protocol_config.address_to_u256_cost_base().into(),
241            },
242            address_from_u256_cost_params: AddressFromU256CostParams {
243                address_from_u256_cost_base: protocol_config.address_from_u256_cost_base().into(),
244            },
245
246            config_read_setting_impl_cost_params: ConfigReadSettingImplCostParams {
247                config_read_setting_impl_cost_base: protocol_config
248                    .config_read_setting_impl_cost_base_as_option()
249                    .map(Into::into),
250                config_read_setting_impl_cost_per_byte: protocol_config
251                    .config_read_setting_impl_cost_per_byte_as_option()
252                    .map(Into::into),
253            },
254
255            package_original_package_id_impl_cost_params:
256                PackageVersioningOriginalPackageIdImplCostParams {
257                    package_original_package_id_impl_cost_base: protocol_config
258                        .package_original_package_id_impl_cost_base_as_option()
259                        .map(Into::into),
260                    package_original_package_id_impl_cost_per_byte: protocol_config
261                        .package_original_package_id_impl_cost_per_byte_as_option()
262                        .map(Into::into),
263                },
264
265            dynamic_field_hash_type_and_key_cost_params: DynamicFieldHashTypeAndKeyCostParams {
266                dynamic_field_hash_type_and_key_cost_base: protocol_config
267                    .dynamic_field_hash_type_and_key_cost_base()
268                    .into(),
269                dynamic_field_hash_type_and_key_type_cost_per_byte: protocol_config
270                    .dynamic_field_hash_type_and_key_type_cost_per_byte()
271                    .into(),
272                dynamic_field_hash_type_and_key_value_cost_per_byte: protocol_config
273                    .dynamic_field_hash_type_and_key_value_cost_per_byte()
274                    .into(),
275                dynamic_field_hash_type_and_key_type_tag_cost_per_byte: protocol_config
276                    .dynamic_field_hash_type_and_key_type_tag_cost_per_byte()
277                    .into(),
278            },
279            dynamic_field_add_child_object_cost_params: DynamicFieldAddChildObjectCostParams {
280                dynamic_field_add_child_object_cost_base: protocol_config
281                    .dynamic_field_add_child_object_cost_base()
282                    .into(),
283                dynamic_field_add_child_object_type_cost_per_byte: protocol_config
284                    .dynamic_field_add_child_object_type_cost_per_byte()
285                    .into(),
286                dynamic_field_add_child_object_value_cost_per_byte: protocol_config
287                    .dynamic_field_add_child_object_value_cost_per_byte()
288                    .into(),
289                dynamic_field_add_child_object_struct_tag_cost_per_byte: protocol_config
290                    .dynamic_field_add_child_object_struct_tag_cost_per_byte()
291                    .into(),
292            },
293            dynamic_field_borrow_child_object_cost_params:
294                DynamicFieldBorrowChildObjectCostParams {
295                    dynamic_field_borrow_child_object_cost_base: protocol_config
296                        .dynamic_field_borrow_child_object_cost_base()
297                        .into(),
298                    dynamic_field_borrow_child_object_child_ref_cost_per_byte: protocol_config
299                        .dynamic_field_borrow_child_object_child_ref_cost_per_byte()
300                        .into(),
301                    dynamic_field_borrow_child_object_type_cost_per_byte: protocol_config
302                        .dynamic_field_borrow_child_object_type_cost_per_byte()
303                        .into(),
304                },
305            dynamic_field_remove_child_object_cost_params:
306                DynamicFieldRemoveChildObjectCostParams {
307                    dynamic_field_remove_child_object_cost_base: protocol_config
308                        .dynamic_field_remove_child_object_cost_base()
309                        .into(),
310                    dynamic_field_remove_child_object_child_cost_per_byte: protocol_config
311                        .dynamic_field_remove_child_object_child_cost_per_byte()
312                        .into(),
313                    dynamic_field_remove_child_object_type_cost_per_byte: protocol_config
314                        .dynamic_field_remove_child_object_type_cost_per_byte()
315                        .into(),
316                },
317            dynamic_field_has_child_object_cost_params: DynamicFieldHasChildObjectCostParams {
318                dynamic_field_has_child_object_cost_base: protocol_config
319                    .dynamic_field_has_child_object_cost_base()
320                    .into(),
321            },
322            dynamic_field_has_child_object_with_ty_cost_params:
323                DynamicFieldHasChildObjectWithTyCostParams {
324                    dynamic_field_has_child_object_with_ty_cost_base: protocol_config
325                        .dynamic_field_has_child_object_with_ty_cost_base()
326                        .into(),
327                    dynamic_field_has_child_object_with_ty_type_cost_per_byte: protocol_config
328                        .dynamic_field_has_child_object_with_ty_type_cost_per_byte()
329                        .into(),
330                    dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte: protocol_config
331                        .dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte()
332                        .into(),
333                },
334
335            scratch_add_cost_params: ScratchAddCostParams {
336                scratch_add_cost_base: protocol_config
337                    .scratch_add_cost_base_as_option()
338                    .map(Into::into),
339            },
340            scratch_read_cost_params: ScratchReadCostParams {
341                scratch_read_cost_base: protocol_config
342                    .scratch_read_cost_base_as_option()
343                    .map(Into::into),
344                scratch_read_value_cost: protocol_config
345                    .scratch_read_value_cost_as_option()
346                    .map(Into::into),
347            },
348            scratch_remove_cost_params: ScratchRemoveCostParams {
349                scratch_remove_cost_base: protocol_config
350                    .scratch_remove_cost_base_as_option()
351                    .map(Into::into),
352            },
353            scratch_exists_cost_params: ScratchExistsCostParams {
354                scratch_exists_cost_base: protocol_config
355                    .scratch_exists_cost_base_as_option()
356                    .map(Into::into),
357            },
358            scratch_exists_with_type_cost_params: ScratchExistsWithTypeCostParams {
359                scratch_exists_with_type_cost_base: protocol_config
360                    .scratch_exists_with_type_cost_base_as_option()
361                    .map(Into::into),
362                scratch_exists_with_type_type_cost: protocol_config
363                    .scratch_exists_with_type_type_cost_as_option()
364                    .map(Into::into),
365            },
366
367            event_emit_cost_params: EventEmitCostParams {
368                event_emit_value_size_derivation_cost_per_byte: protocol_config
369                    .event_emit_value_size_derivation_cost_per_byte()
370                    .into(),
371                event_emit_tag_size_derivation_cost_per_byte: protocol_config
372                    .event_emit_tag_size_derivation_cost_per_byte()
373                    .into(),
374                event_emit_output_cost_per_byte: protocol_config
375                    .event_emit_output_cost_per_byte()
376                    .into(),
377                event_emit_cost_base: protocol_config.event_emit_cost_base().into(),
378                event_emit_auth_stream_cost: protocol_config
379                    .event_emit_auth_stream_cost_as_option()
380                    .map(Into::into),
381            },
382
383            reserve_object_funds_for_withdrawal_cost_params:
384                funds_accumulator::ReserveObjectFundsForWithdrawalCostParams {
385                    base_cost: protocol_config
386                        .reserve_object_funds_for_withdrawal_cost_base_as_option()
387                        .map(Into::into),
388                    cold_read_cost: protocol_config
389                        .reserve_object_funds_for_withdrawal_cold_read_cost_as_option()
390                        .map(Into::into),
391                },
392
393            borrow_uid_cost_params: BorrowUidCostParams {
394                object_borrow_uid_cost_base: protocol_config.object_borrow_uid_cost_base().into(),
395            },
396            delete_impl_cost_params: DeleteImplCostParams {
397                object_delete_impl_cost_base: protocol_config.object_delete_impl_cost_base().into(),
398            },
399            record_new_id_cost_params: RecordNewIdCostParams {
400                object_record_new_uid_cost_base: protocol_config
401                    .object_record_new_uid_cost_base()
402                    .into(),
403                object_record_new_uid_from_hash_cost_base: protocol_config
404                    .object_record_new_uid_from_hash_cost_base_as_option()
405                    .map(Into::into),
406            },
407
408            // Crypto
409            crypto_invalid_arguments_cost: protocol_config.crypto_invalid_arguments_cost().into(),
410            // ed25519
411            ed25519_verify_cost_params: Ed25519VerifyCostParams {
412                ed25519_ed25519_verify_cost_base: protocol_config
413                    .ed25519_ed25519_verify_cost_base()
414                    .into(),
415                ed25519_ed25519_verify_msg_cost_per_byte: protocol_config
416                    .ed25519_ed25519_verify_msg_cost_per_byte()
417                    .into(),
418                ed25519_ed25519_verify_msg_cost_per_block: protocol_config
419                    .ed25519_ed25519_verify_msg_cost_per_block()
420                    .into(),
421            },
422            // hash
423            hash_blake2b256_cost_params: HashBlake2b256CostParams {
424                hash_blake2b256_cost_base: protocol_config.hash_blake2b256_cost_base().into(),
425                hash_blake2b256_data_cost_per_byte: protocol_config
426                    .hash_blake2b256_data_cost_per_byte()
427                    .into(),
428                hash_blake2b256_data_cost_per_block: protocol_config
429                    .hash_blake2b256_data_cost_per_block()
430                    .into(),
431            },
432            hash_keccak256_cost_params: HashKeccak256CostParams {
433                hash_keccak256_cost_base: protocol_config.hash_keccak256_cost_base().into(),
434                hash_keccak256_data_cost_per_byte: protocol_config
435                    .hash_keccak256_data_cost_per_byte()
436                    .into(),
437                hash_keccak256_data_cost_per_block: protocol_config
438                    .hash_keccak256_data_cost_per_block()
439                    .into(),
440            },
441            transfer_transfer_internal_cost_params: TransferInternalCostParams {
442                transfer_transfer_internal_cost_base: protocol_config
443                    .transfer_transfer_internal_cost_base()
444                    .into(),
445            },
446            transfer_party_transfer_internal_cost_params: PartyTransferInternalCostParams {
447                transfer_party_transfer_internal_cost_base: protocol_config
448                    .transfer_party_transfer_internal_cost_base_as_option()
449                    .map(Into::into),
450            },
451            transfer_freeze_object_cost_params: TransferFreezeObjectCostParams {
452                transfer_freeze_object_cost_base: protocol_config
453                    .transfer_freeze_object_cost_base()
454                    .into(),
455            },
456            transfer_share_object_cost_params: TransferShareObjectCostParams {
457                transfer_share_object_cost_base: protocol_config
458                    .transfer_share_object_cost_base()
459                    .into(),
460            },
461            // tx_context
462            tx_context_derive_id_cost_params: TxContextDeriveIdCostParams {
463                tx_context_derive_id_cost_base: protocol_config
464                    .tx_context_derive_id_cost_base()
465                    .into(),
466            },
467            tx_context_fresh_id_cost_params: TxContextFreshIdCostParams {
468                tx_context_fresh_id_cost_base: protocol_config
469                    .tx_context_fresh_id_cost_base()
470                    .into(),
471            },
472            tx_context_sender_cost_params: TxContextSenderCostParams {
473                tx_context_sender_cost_base: protocol_config.tx_context_sender_cost_base().into(),
474            },
475            tx_context_epoch_cost_params: TxContextEpochCostParams {
476                tx_context_epoch_cost_base: protocol_config.tx_context_epoch_cost_base().into(),
477            },
478            tx_context_epoch_timestamp_ms_cost_params: TxContextEpochTimestampMsCostParams {
479                tx_context_epoch_timestamp_ms_cost_base: protocol_config
480                    .tx_context_epoch_timestamp_ms_cost_base()
481                    .into(),
482            },
483            tx_context_sponsor_cost_params: TxContextSponsorCostParams {
484                tx_context_sponsor_cost_base: protocol_config.tx_context_sponsor_cost_base().into(),
485            },
486            tx_context_rgp_cost_params: TxContextRGPCostParams {
487                tx_context_rgp_cost_base: protocol_config
488                    .tx_context_rgp_cost_base_as_option()
489                    .unwrap_or(DEFAULT_UNUSED_TX_CONTEXT_ENTRY_COST)
490                    .into(),
491            },
492            tx_context_gas_price_cost_params: TxContextGasPriceCostParams {
493                tx_context_gas_price_cost_base: protocol_config
494                    .tx_context_gas_price_cost_base()
495                    .into(),
496            },
497            tx_context_gas_budget_cost_params: TxContextGasBudgetCostParams {
498                tx_context_gas_budget_cost_base: protocol_config
499                    .tx_context_gas_budget_cost_base()
500                    .into(),
501            },
502            tx_context_ids_created_cost_params: TxContextIdsCreatedCostParams {
503                tx_context_ids_created_cost_base: protocol_config
504                    .tx_context_ids_created_cost_base()
505                    .into(),
506            },
507            tx_context_replace_cost_params: TxContextReplaceCostParams {
508                tx_context_replace_cost_base: protocol_config.tx_context_replace_cost_base().into(),
509            },
510            type_is_one_time_witness_cost_params: TypesIsOneTimeWitnessCostParams {
511                types_is_one_time_witness_cost_base: protocol_config
512                    .types_is_one_time_witness_cost_base()
513                    .into(),
514                types_is_one_time_witness_type_tag_cost_per_byte: protocol_config
515                    .types_is_one_time_witness_type_tag_cost_per_byte()
516                    .into(),
517                types_is_one_time_witness_type_cost_per_byte: protocol_config
518                    .types_is_one_time_witness_type_cost_per_byte()
519                    .into(),
520            },
521            validator_validate_metadata_bcs_cost_params: ValidatorValidateMetadataBcsCostParams {
522                validator_validate_metadata_cost_base: protocol_config
523                    .validator_validate_metadata_cost_base()
524                    .into(),
525                validator_validate_metadata_data_cost_per_byte: protocol_config
526                    .validator_validate_metadata_data_cost_per_byte()
527                    .into(),
528            },
529            bls12381_bls12381_min_sig_verify_cost_params: Bls12381Bls12381MinSigVerifyCostParams {
530                bls12381_bls12381_min_sig_verify_cost_base: protocol_config
531                    .bls12381_bls12381_min_sig_verify_cost_base()
532                    .into(),
533                bls12381_bls12381_min_sig_verify_msg_cost_per_byte: protocol_config
534                    .bls12381_bls12381_min_sig_verify_msg_cost_per_byte()
535                    .into(),
536                bls12381_bls12381_min_sig_verify_msg_cost_per_block: protocol_config
537                    .bls12381_bls12381_min_sig_verify_msg_cost_per_block()
538                    .into(),
539            },
540            bls12381_bls12381_min_pk_verify_cost_params: Bls12381Bls12381MinPkVerifyCostParams {
541                bls12381_bls12381_min_pk_verify_cost_base: protocol_config
542                    .bls12381_bls12381_min_pk_verify_cost_base()
543                    .into(),
544                bls12381_bls12381_min_pk_verify_msg_cost_per_byte: protocol_config
545                    .bls12381_bls12381_min_pk_verify_msg_cost_per_byte()
546                    .into(),
547                bls12381_bls12381_min_pk_verify_msg_cost_per_block: protocol_config
548                    .bls12381_bls12381_min_pk_verify_msg_cost_per_block()
549                    .into(),
550            },
551            ecdsa_k1_ecrecover_cost_params: EcdsaK1EcrecoverCostParams {
552                ecdsa_k1_ecrecover_keccak256_cost_base: protocol_config
553                    .ecdsa_k1_ecrecover_keccak256_cost_base()
554                    .into(),
555                ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte: protocol_config
556                    .ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte()
557                    .into(),
558                ecdsa_k1_ecrecover_keccak256_msg_cost_per_block: protocol_config
559                    .ecdsa_k1_ecrecover_keccak256_msg_cost_per_block()
560                    .into(),
561                ecdsa_k1_ecrecover_sha256_cost_base: protocol_config
562                    .ecdsa_k1_ecrecover_sha256_cost_base()
563                    .into(),
564                ecdsa_k1_ecrecover_sha256_msg_cost_per_byte: protocol_config
565                    .ecdsa_k1_ecrecover_sha256_msg_cost_per_byte()
566                    .into(),
567                ecdsa_k1_ecrecover_sha256_msg_cost_per_block: protocol_config
568                    .ecdsa_k1_ecrecover_sha256_msg_cost_per_block()
569                    .into(),
570            },
571            ecdsa_k1_decompress_pubkey_cost_params: EcdsaK1DecompressPubkeyCostParams {
572                ecdsa_k1_decompress_pubkey_cost_base: protocol_config
573                    .ecdsa_k1_decompress_pubkey_cost_base()
574                    .into(),
575            },
576            ecdsa_k1_secp256k1_verify_cost_params: EcdsaK1Secp256k1VerifyCostParams {
577                ecdsa_k1_secp256k1_verify_keccak256_cost_base: protocol_config
578                    .ecdsa_k1_secp256k1_verify_keccak256_cost_base()
579                    .into(),
580                ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte: protocol_config
581                    .ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte()
582                    .into(),
583                ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block: protocol_config
584                    .ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block()
585                    .into(),
586                ecdsa_k1_secp256k1_verify_sha256_cost_base: protocol_config
587                    .ecdsa_k1_secp256k1_verify_sha256_cost_base()
588                    .into(),
589                ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte: protocol_config
590                    .ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte()
591                    .into(),
592                ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block: protocol_config
593                    .ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block()
594                    .into(),
595            },
596            ecdsa_r1_ecrecover_cost_params: EcdsaR1EcrecoverCostParams {
597                ecdsa_r1_ecrecover_keccak256_cost_base: protocol_config
598                    .ecdsa_r1_ecrecover_keccak256_cost_base()
599                    .into(),
600                ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte: protocol_config
601                    .ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte()
602                    .into(),
603                ecdsa_r1_ecrecover_keccak256_msg_cost_per_block: protocol_config
604                    .ecdsa_r1_ecrecover_keccak256_msg_cost_per_block()
605                    .into(),
606                ecdsa_r1_ecrecover_sha256_cost_base: protocol_config
607                    .ecdsa_r1_ecrecover_sha256_cost_base()
608                    .into(),
609                ecdsa_r1_ecrecover_sha256_msg_cost_per_byte: protocol_config
610                    .ecdsa_r1_ecrecover_sha256_msg_cost_per_byte()
611                    .into(),
612                ecdsa_r1_ecrecover_sha256_msg_cost_per_block: protocol_config
613                    .ecdsa_r1_ecrecover_sha256_msg_cost_per_block()
614                    .into(),
615            },
616            ecdsa_r1_secp256_r1_verify_cost_params: EcdsaR1Secp256R1VerifyCostParams {
617                ecdsa_r1_secp256r1_verify_keccak256_cost_base: protocol_config
618                    .ecdsa_r1_secp256r1_verify_keccak256_cost_base()
619                    .into(),
620                ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte: protocol_config
621                    .ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte()
622                    .into(),
623                ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block: protocol_config
624                    .ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block()
625                    .into(),
626                ecdsa_r1_secp256r1_verify_sha256_cost_base: protocol_config
627                    .ecdsa_r1_secp256r1_verify_sha256_cost_base()
628                    .into(),
629                ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte: protocol_config
630                    .ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte()
631                    .into(),
632                ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block: protocol_config
633                    .ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block()
634                    .into(),
635            },
636            ecvrf_ecvrf_verify_cost_params: EcvrfEcvrfVerifyCostParams {
637                ecvrf_ecvrf_verify_cost_base: protocol_config.ecvrf_ecvrf_verify_cost_base().into(),
638                ecvrf_ecvrf_verify_alpha_string_cost_per_byte: protocol_config
639                    .ecvrf_ecvrf_verify_alpha_string_cost_per_byte()
640                    .into(),
641                ecvrf_ecvrf_verify_alpha_string_cost_per_block: protocol_config
642                    .ecvrf_ecvrf_verify_alpha_string_cost_per_block()
643                    .into(),
644            },
645            groth16_prepare_verifying_key_cost_params: Groth16PrepareVerifyingKeyCostParams {
646                groth16_prepare_verifying_key_bls12381_cost_base: protocol_config
647                    .groth16_prepare_verifying_key_bls12381_cost_base()
648                    .into(),
649                groth16_prepare_verifying_key_bn254_cost_base: protocol_config
650                    .groth16_prepare_verifying_key_bn254_cost_base()
651                    .into(),
652            },
653            groth16_verify_groth16_proof_internal_cost_params:
654                Groth16VerifyGroth16ProofInternalCostParams {
655                    groth16_verify_groth16_proof_internal_bls12381_cost_base: protocol_config
656                        .groth16_verify_groth16_proof_internal_bls12381_cost_base()
657                        .into(),
658                    groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input:
659                        protocol_config
660                            .groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input()
661                            .into(),
662                    groth16_verify_groth16_proof_internal_bn254_cost_base: protocol_config
663                        .groth16_verify_groth16_proof_internal_bn254_cost_base()
664                        .into(),
665                    groth16_verify_groth16_proof_internal_bn254_cost_per_public_input:
666                        protocol_config
667                            .groth16_verify_groth16_proof_internal_bn254_cost_per_public_input()
668                            .into(),
669                    groth16_verify_groth16_proof_internal_public_input_cost_per_byte:
670                        protocol_config
671                            .groth16_verify_groth16_proof_internal_public_input_cost_per_byte()
672                            .into(),
673                },
674            hmac_hmac_sha3_256_cost_params: HmacHmacSha3256CostParams {
675                hmac_hmac_sha3_256_cost_base: protocol_config.hmac_hmac_sha3_256_cost_base().into(),
676                hmac_hmac_sha3_256_input_cost_per_byte: protocol_config
677                    .hmac_hmac_sha3_256_input_cost_per_byte()
678                    .into(),
679                hmac_hmac_sha3_256_input_cost_per_block: protocol_config
680                    .hmac_hmac_sha3_256_input_cost_per_block()
681                    .into(),
682            },
683            transfer_receive_object_internal_cost_params: TransferReceiveObjectInternalCostParams {
684                transfer_receive_object_internal_cost_base: protocol_config
685                    .transfer_receive_object_cost_base_as_option()
686                    .unwrap_or(0)
687                    .into(),
688                transfer_receive_object_internal_cost_per_byte: protocol_config
689                    .transfer_receive_object_cost_per_byte_as_option()
690                    .unwrap_or(0)
691                    .into(),
692                transfer_receive_object_internal_type_cost_per_byte: protocol_config
693                    .transfer_receive_object_type_cost_per_byte_as_option()
694                    .unwrap_or(0)
695                    .into(),
696            },
697            check_zklogin_id_cost_params: CheckZkloginIdCostParams {
698                check_zklogin_id_cost_base: protocol_config
699                    .check_zklogin_id_cost_base_as_option()
700                    .map(Into::into),
701            },
702            check_zklogin_issuer_cost_params: CheckZkloginIssuerCostParams {
703                check_zklogin_issuer_cost_base: protocol_config
704                    .check_zklogin_issuer_cost_base_as_option()
705                    .map(Into::into),
706            },
707            poseidon_bn254_cost_params: PoseidonBN254CostParams {
708                poseidon_bn254_cost_base: protocol_config
709                    .poseidon_bn254_cost_base_as_option()
710                    .map(Into::into),
711                poseidon_bn254_data_cost_per_block: protocol_config
712                    .poseidon_bn254_cost_per_block_as_option()
713                    .map(Into::into),
714            },
715            group_ops_cost_params: GroupOpsCostParams {
716                bls12381_decode_scalar_cost: protocol_config
717                    .group_ops_bls12381_decode_scalar_cost_as_option()
718                    .map(Into::into),
719                bls12381_decode_g1_cost: protocol_config
720                    .group_ops_bls12381_decode_g1_cost_as_option()
721                    .map(Into::into),
722                bls12381_decode_g2_cost: protocol_config
723                    .group_ops_bls12381_decode_g2_cost_as_option()
724                    .map(Into::into),
725                bls12381_decode_gt_cost: protocol_config
726                    .group_ops_bls12381_decode_gt_cost_as_option()
727                    .map(Into::into),
728                bls12381_scalar_add_cost: protocol_config
729                    .group_ops_bls12381_scalar_add_cost_as_option()
730                    .map(Into::into),
731                bls12381_g1_add_cost: protocol_config
732                    .group_ops_bls12381_g1_add_cost_as_option()
733                    .map(Into::into),
734                bls12381_g2_add_cost: protocol_config
735                    .group_ops_bls12381_g2_add_cost_as_option()
736                    .map(Into::into),
737                bls12381_gt_add_cost: protocol_config
738                    .group_ops_bls12381_gt_add_cost_as_option()
739                    .map(Into::into),
740                bls12381_scalar_sub_cost: protocol_config
741                    .group_ops_bls12381_scalar_sub_cost_as_option()
742                    .map(Into::into),
743                bls12381_g1_sub_cost: protocol_config
744                    .group_ops_bls12381_g1_sub_cost_as_option()
745                    .map(Into::into),
746                bls12381_g2_sub_cost: protocol_config
747                    .group_ops_bls12381_g2_sub_cost_as_option()
748                    .map(Into::into),
749                bls12381_gt_sub_cost: protocol_config
750                    .group_ops_bls12381_gt_sub_cost_as_option()
751                    .map(Into::into),
752                bls12381_scalar_mul_cost: protocol_config
753                    .group_ops_bls12381_scalar_mul_cost_as_option()
754                    .map(Into::into),
755                bls12381_g1_mul_cost: protocol_config
756                    .group_ops_bls12381_g1_mul_cost_as_option()
757                    .map(Into::into),
758                bls12381_g2_mul_cost: protocol_config
759                    .group_ops_bls12381_g2_mul_cost_as_option()
760                    .map(Into::into),
761                bls12381_gt_mul_cost: protocol_config
762                    .group_ops_bls12381_gt_mul_cost_as_option()
763                    .map(Into::into),
764                bls12381_scalar_div_cost: protocol_config
765                    .group_ops_bls12381_scalar_div_cost_as_option()
766                    .map(Into::into),
767                bls12381_g1_div_cost: protocol_config
768                    .group_ops_bls12381_g1_div_cost_as_option()
769                    .map(Into::into),
770                bls12381_g2_div_cost: protocol_config
771                    .group_ops_bls12381_g2_div_cost_as_option()
772                    .map(Into::into),
773                bls12381_gt_div_cost: protocol_config
774                    .group_ops_bls12381_gt_div_cost_as_option()
775                    .map(Into::into),
776                bls12381_g1_hash_to_base_cost: protocol_config
777                    .group_ops_bls12381_g1_hash_to_base_cost_as_option()
778                    .map(Into::into),
779                bls12381_g2_hash_to_base_cost: protocol_config
780                    .group_ops_bls12381_g2_hash_to_base_cost_as_option()
781                    .map(Into::into),
782                bls12381_g1_hash_to_cost_per_byte: protocol_config
783                    .group_ops_bls12381_g1_hash_to_cost_per_byte_as_option()
784                    .map(Into::into),
785                bls12381_g2_hash_to_cost_per_byte: protocol_config
786                    .group_ops_bls12381_g2_hash_to_cost_per_byte_as_option()
787                    .map(Into::into),
788                bls12381_g1_msm_base_cost: protocol_config
789                    .group_ops_bls12381_g1_msm_base_cost_as_option()
790                    .map(Into::into),
791                bls12381_g2_msm_base_cost: protocol_config
792                    .group_ops_bls12381_g2_msm_base_cost_as_option()
793                    .map(Into::into),
794                bls12381_g1_msm_base_cost_per_input: protocol_config
795                    .group_ops_bls12381_g1_msm_base_cost_per_input_as_option()
796                    .map(Into::into),
797                bls12381_g2_msm_base_cost_per_input: protocol_config
798                    .group_ops_bls12381_g2_msm_base_cost_per_input_as_option()
799                    .map(Into::into),
800                bls12381_msm_max_len: protocol_config.group_ops_bls12381_msm_max_len_as_option(),
801                bls12381_pairing_cost: protocol_config
802                    .group_ops_bls12381_pairing_cost_as_option()
803                    .map(Into::into),
804                bls12381_g1_to_uncompressed_g1_cost: protocol_config
805                    .group_ops_bls12381_g1_to_uncompressed_g1_cost_as_option()
806                    .map(Into::into),
807                bls12381_uncompressed_g1_to_g1_cost: protocol_config
808                    .group_ops_bls12381_uncompressed_g1_to_g1_cost_as_option()
809                    .map(Into::into),
810                bls12381_uncompressed_g1_sum_base_cost: protocol_config
811                    .group_ops_bls12381_uncompressed_g1_sum_base_cost_as_option()
812                    .map(Into::into),
813                bls12381_uncompressed_g1_sum_cost_per_term: protocol_config
814                    .group_ops_bls12381_uncompressed_g1_sum_cost_per_term_as_option()
815                    .map(Into::into),
816                bls12381_uncompressed_g1_sum_max_terms: protocol_config
817                    .group_ops_bls12381_uncompressed_g1_sum_max_terms_as_option(),
818                ristretto_decode_scalar_cost: protocol_config
819                    .group_ops_ristretto_decode_scalar_cost_as_option()
820                    .map(Into::into),
821                ristretto_decode_point_cost: protocol_config
822                    .group_ops_ristretto_decode_point_cost_as_option()
823                    .map(Into::into),
824                ristretto_scalar_add_cost: protocol_config
825                    .group_ops_ristretto_scalar_add_cost_as_option()
826                    .map(Into::into),
827                ristretto_point_add_cost: protocol_config
828                    .group_ops_ristretto_point_add_cost_as_option()
829                    .map(Into::into),
830                ristretto_scalar_sub_cost: protocol_config
831                    .group_ops_ristretto_scalar_sub_cost_as_option()
832                    .map(Into::into),
833                ristretto_point_sub_cost: protocol_config
834                    .group_ops_ristretto_point_sub_cost_as_option()
835                    .map(Into::into),
836                ristretto_scalar_mul_cost: protocol_config
837                    .group_ops_ristretto_scalar_mul_cost_as_option()
838                    .map(Into::into),
839                ristretto_point_mul_cost: protocol_config
840                    .group_ops_ristretto_point_mul_cost_as_option()
841                    .map(Into::into),
842                ristretto_scalar_div_cost: protocol_config
843                    .group_ops_ristretto_scalar_div_cost_as_option()
844                    .map(Into::into),
845                ristretto_point_div_cost: protocol_config
846                    .group_ops_ristretto_point_div_cost_as_option()
847                    .map(Into::into),
848            },
849            vdf_cost_params: VDFCostParams {
850                vdf_verify_cost: protocol_config
851                    .vdf_verify_vdf_cost_as_option()
852                    .map(Into::into),
853                hash_to_input_cost: protocol_config
854                    .vdf_hash_to_input_cost_as_option()
855                    .map(Into::into),
856            },
857            nitro_attestation_cost_params: NitroAttestationCostParams {
858                parse_base_cost: protocol_config
859                    .nitro_attestation_parse_base_cost_as_option()
860                    .map(Into::into),
861                parse_cost_per_byte: protocol_config
862                    .nitro_attestation_parse_cost_per_byte_as_option()
863                    .map(Into::into),
864                verify_base_cost: protocol_config
865                    .nitro_attestation_verify_base_cost_as_option()
866                    .map(Into::into),
867                verify_cost_per_cert: protocol_config
868                    .nitro_attestation_verify_cost_per_cert_as_option()
869                    .map(Into::into),
870            },
871            bulletproofs_cost_params: BulletproofsCostParams {
872                verify_bulletproofs_ristretto255_base_cost: protocol_config
873                    .verify_bulletproofs_ristretto255_base_cost_as_option()
874                    .map(Into::into),
875                verify_bulletproofs_ristretto255_cost_per_bit_and_commitment: protocol_config
876                    .verify_bulletproofs_ristretto255_cost_per_bit_and_commitment_as_option()
877                    .map(Into::into),
878            },
879        }
880    }
881}
882
883pub fn make_stdlib_gas_params_for_protocol_config(
884    protocol_config: &ProtocolConfig,
885) -> GasParameters {
886    macro_rules! get_gas_cost_or_default {
887        ($name: ident) => {{
888            debug_assert!(
889                protocol_config.version.as_u64() < 53 || protocol_config.$name().is_some()
890            );
891            protocol_config.$name().map(Into::into).unwrap_or(0.into())
892        }};
893    }
894    macro_rules! get_gas_cost_or_default_since {
895        ($name: ident, $version: expr) => {{
896            debug_assert!(
897                protocol_config.version.as_u64() < $version || protocol_config.$name().is_some()
898            );
899            protocol_config.$name().map(Into::into).unwrap_or(0.into())
900        }};
901    }
902    GasParameters::new(
903        MSN::bcs::GasParameters {
904            to_bytes: MSN::bcs::ToBytesGasParameters {
905                per_byte_serialized: get_gas_cost_or_default!(
906                    bcs_per_byte_serialized_cost_as_option
907                ),
908                legacy_min_output_size: get_gas_cost_or_default!(
909                    bcs_legacy_min_output_size_cost_as_option
910                ),
911                failure: get_gas_cost_or_default!(bcs_failure_cost_as_option),
912            },
913        },
914        MSN::debug::GasParameters {
915            print: MSN::debug::PrintGasParameters {
916                base_cost: get_gas_cost_or_default!(debug_print_base_cost_as_option),
917            },
918            print_stack_trace: MSN::debug::PrintStackTraceGasParameters {
919                base_cost: get_gas_cost_or_default!(debug_print_stack_trace_base_cost_as_option),
920            },
921        },
922        MSN::hash::GasParameters {
923            sha2_256: MSN::hash::Sha2_256GasParameters {
924                base: get_gas_cost_or_default!(hash_sha2_256_base_cost_as_option),
925                per_byte: get_gas_cost_or_default!(hash_sha2_256_per_byte_cost_as_option),
926                legacy_min_input_len: get_gas_cost_or_default!(
927                    hash_sha2_256_legacy_min_input_len_cost_as_option
928                ),
929            },
930            sha3_256: MSN::hash::Sha3_256GasParameters {
931                base: get_gas_cost_or_default!(hash_sha3_256_base_cost_as_option),
932                per_byte: get_gas_cost_or_default!(hash_sha3_256_per_byte_cost_as_option),
933                legacy_min_input_len: get_gas_cost_or_default!(
934                    hash_sha3_256_legacy_min_input_len_cost_as_option
935                ),
936            },
937        },
938        MSN::string::GasParameters {
939            check_utf8: MSN::string::CheckUtf8GasParameters {
940                base: get_gas_cost_or_default!(string_check_utf8_base_cost_as_option),
941                per_byte: get_gas_cost_or_default!(string_check_utf8_per_byte_cost_as_option),
942            },
943            is_char_boundary: MSN::string::IsCharBoundaryGasParameters {
944                base: get_gas_cost_or_default!(string_is_char_boundary_base_cost_as_option),
945            },
946            sub_string: MSN::string::SubStringGasParameters {
947                base: get_gas_cost_or_default!(string_sub_string_base_cost_as_option),
948                per_byte: get_gas_cost_or_default!(string_sub_string_per_byte_cost_as_option),
949            },
950            index_of: MSN::string::IndexOfGasParameters {
951                base: get_gas_cost_or_default!(string_index_of_base_cost_as_option),
952                per_byte_pattern: get_gas_cost_or_default!(
953                    string_index_of_per_byte_pattern_cost_as_option
954                ),
955                per_byte_searched: get_gas_cost_or_default!(
956                    string_index_of_per_byte_searched_cost_as_option
957                ),
958            },
959        },
960        MSN::type_name::GasParameters {
961            get: MSN::type_name::GetGasParameters {
962                base: get_gas_cost_or_default!(type_name_get_base_cost_as_option),
963                per_byte: get_gas_cost_or_default!(type_name_get_per_byte_cost_as_option),
964            },
965            id: MSN::type_name::IdGasParameters::new(
966                protocol_config.type_name_id_base_cost_as_option(),
967            ),
968        },
969        MSN::vector::GasParameters {
970            empty: MSN::vector::EmptyGasParameters {
971                base: get_gas_cost_or_default!(vector_empty_base_cost_as_option),
972            },
973            length: MSN::vector::LengthGasParameters {
974                base: get_gas_cost_or_default!(vector_length_base_cost_as_option),
975            },
976            push_back: MSN::vector::PushBackGasParameters {
977                base: get_gas_cost_or_default!(vector_push_back_base_cost_as_option),
978                legacy_per_abstract_memory_unit: get_gas_cost_or_default!(
979                    vector_push_back_legacy_per_abstract_memory_unit_cost_as_option
980                ),
981            },
982            borrow: MSN::vector::BorrowGasParameters {
983                base: get_gas_cost_or_default!(vector_borrow_base_cost_as_option),
984            },
985            pop_back: MSN::vector::PopBackGasParameters {
986                base: get_gas_cost_or_default!(vector_pop_back_base_cost_as_option),
987            },
988            destroy_empty: MSN::vector::DestroyEmptyGasParameters {
989                base: get_gas_cost_or_default!(vector_destroy_empty_base_cost_as_option),
990            },
991            swap: MSN::vector::SwapGasParameters {
992                base: get_gas_cost_or_default!(vector_swap_base_cost_as_option),
993            },
994            reverse: MSN::vector::ReverseGasParameters {
995                base: get_gas_cost_or_default_since!(vector_reverse_base_cost_as_option, 140),
996                per_elem: get_gas_cost_or_default_since!(
997                    vector_reverse_per_elem_cost_as_option,
998                    140
999                ),
1000            },
1001            keep_range: MSN::vector::KeepRangeGasParameters {
1002                base: get_gas_cost_or_default_since!(vector_keep_range_base_cost_as_option, 140),
1003                per_dropped_elem: get_gas_cost_or_default_since!(
1004                    vector_keep_range_per_dropped_elem_cost_as_option,
1005                    140
1006                ),
1007                per_moved_elem: get_gas_cost_or_default_since!(
1008                    vector_keep_range_per_moved_elem_cost_as_option,
1009                    140
1010                ),
1011            },
1012            copy_range: MSN::vector::CopyRangeGasParameters {
1013                base: get_gas_cost_or_default_since!(vector_copy_range_base_cost_as_option, 140),
1014            },
1015            replace_range: MSN::vector::ReplaceRangeGasParameters {
1016                base: get_gas_cost_or_default_since!(vector_replace_range_base_cost_as_option, 140),
1017                per_elem: get_gas_cost_or_default_since!(
1018                    vector_replace_range_per_elem_cost_as_option,
1019                    140
1020                ),
1021            },
1022        },
1023    )
1024}
1025
1026pub fn all_natives(silent: bool, protocol_config: &ProtocolConfig) -> NativeFunctionTable {
1027    let sui_framework_natives: &[(&str, &str, NativeFunction)] = &[
1028        (
1029            "accumulator",
1030            "emit_deposit_event",
1031            make_native!(accumulator::emit_deposit_event),
1032        ),
1033        (
1034            "accumulator",
1035            "emit_withdraw_event",
1036            make_native!(accumulator::emit_withdraw_event),
1037        ),
1038        (
1039            "accumulator_settlement",
1040            "record_settlement_sui_conservation",
1041            make_native!(accumulator::record_settlement_sui_conservation),
1042        ),
1043        ("address", "from_bytes", make_native!(address::from_bytes)),
1044        ("address", "to_u256", make_native!(address::to_u256)),
1045        ("address", "from_u256", make_native!(address::from_u256)),
1046        ("hash", "blake2b256", make_native!(hash::blake2b256)),
1047        (
1048            "bls12381",
1049            "bls12381_min_sig_verify",
1050            make_native!(bls12381::bls12381_min_sig_verify),
1051        ),
1052        (
1053            "bls12381",
1054            "bls12381_min_pk_verify",
1055            make_native!(bls12381::bls12381_min_pk_verify),
1056        ),
1057        (
1058            "dynamic_field",
1059            "hash_type_and_key",
1060            make_native!(dynamic_field::hash_type_and_key),
1061        ),
1062        (
1063            "config",
1064            "read_setting_impl",
1065            make_native!(config::read_setting_impl),
1066        ),
1067        (
1068            "package",
1069            "original_package_id_impl",
1070            make_native!(package::original_package_id_impl),
1071        ),
1072        (
1073            "dynamic_field",
1074            "add_child_object",
1075            make_native!(dynamic_field::add_child_object),
1076        ),
1077        (
1078            "dynamic_field",
1079            "borrow_child_object",
1080            make_native!(dynamic_field::borrow_child_object),
1081        ),
1082        (
1083            "dynamic_field",
1084            "borrow_child_object_mut",
1085            make_native!(dynamic_field::borrow_child_object),
1086        ),
1087        (
1088            "dynamic_field",
1089            "remove_child_object",
1090            make_native!(dynamic_field::remove_child_object),
1091        ),
1092        (
1093            "dynamic_field",
1094            "has_child_object",
1095            make_native!(dynamic_field::has_child_object),
1096        ),
1097        (
1098            "dynamic_field",
1099            "has_child_object_with_ty",
1100            make_native!(dynamic_field::has_child_object_with_ty),
1101        ),
1102        ("scratch", "add_impl", make_native!(scratch::add_impl)),
1103        ("scratch", "read_impl", make_native!(scratch::read_impl)),
1104        ("scratch", "remove_impl", make_native!(scratch::remove_impl)),
1105        ("scratch", "exists_impl", make_native!(scratch::exists_impl)),
1106        (
1107            "scratch",
1108            "exists_with_type_impl",
1109            make_native!(scratch::exists_with_type_impl),
1110        ),
1111        (
1112            "ecdsa_k1",
1113            "secp256k1_ecrecover",
1114            make_native!(ecdsa_k1::ecrecover),
1115        ),
1116        (
1117            "ecdsa_k1",
1118            "decompress_pubkey",
1119            make_native!(ecdsa_k1::decompress_pubkey),
1120        ),
1121        (
1122            "ecdsa_k1",
1123            "secp256k1_verify",
1124            make_native!(ecdsa_k1::secp256k1_verify),
1125        ),
1126        ("ecvrf", "ecvrf_verify", make_native!(ecvrf::ecvrf_verify)),
1127        (
1128            "ecdsa_r1",
1129            "secp256r1_ecrecover",
1130            make_native!(ecdsa_r1::ecrecover),
1131        ),
1132        (
1133            "ecdsa_r1",
1134            "secp256r1_verify",
1135            make_native!(ecdsa_r1::secp256r1_verify),
1136        ),
1137        (
1138            "ed25519",
1139            "ed25519_verify",
1140            make_native!(ed25519::ed25519_verify),
1141        ),
1142        ("event", "emit", make_native!(event::emit)),
1143        (
1144            "event",
1145            "emit_authenticated_impl",
1146            make_native!(event::emit_authenticated_impl),
1147        ),
1148        (
1149            "event",
1150            "events_by_type",
1151            make_native!(event::get_events_by_type),
1152        ),
1153        ("event", "num_events", make_native!(event::num_events)),
1154        (
1155            "funds_accumulator",
1156            "add_to_accumulator_address",
1157            make_native!(funds_accumulator::add_to_accumulator_address),
1158        ),
1159        (
1160            "funds_accumulator",
1161            "withdraw_from_accumulator_address",
1162            make_native!(funds_accumulator::withdraw_from_accumulator_address),
1163        ),
1164        (
1165            "funds_accumulator",
1166            "reserve_object_funds_for_withdrawal",
1167            make_native!(funds_accumulator::reserve_object_funds_for_withdrawal),
1168        ),
1169        (
1170            "groth16",
1171            "verify_groth16_proof_internal",
1172            make_native!(groth16::verify_groth16_proof_internal),
1173        ),
1174        (
1175            "groth16",
1176            "prepare_verifying_key_internal",
1177            make_native!(groth16::prepare_verifying_key_internal),
1178        ),
1179        ("hmac", "hmac_sha3_256", make_native!(hmac::hmac_sha3_256)),
1180        ("hash", "keccak256", make_native!(hash::keccak256)),
1181        (
1182            "group_ops",
1183            "internal_validate",
1184            make_native!(group_ops::internal_validate),
1185        ),
1186        (
1187            "group_ops",
1188            "internal_add",
1189            make_native!(group_ops::internal_add),
1190        ),
1191        (
1192            "group_ops",
1193            "internal_sub",
1194            make_native!(group_ops::internal_sub),
1195        ),
1196        (
1197            "group_ops",
1198            "internal_mul",
1199            make_native!(group_ops::internal_mul),
1200        ),
1201        (
1202            "group_ops",
1203            "internal_div",
1204            make_native!(group_ops::internal_div),
1205        ),
1206        (
1207            "group_ops",
1208            "internal_hash_to",
1209            make_native!(group_ops::internal_hash_to),
1210        ),
1211        (
1212            "group_ops",
1213            "internal_multi_scalar_mul",
1214            make_native!(group_ops::internal_multi_scalar_mul),
1215        ),
1216        (
1217            "group_ops",
1218            "internal_pairing",
1219            make_native!(group_ops::internal_pairing),
1220        ),
1221        (
1222            "group_ops",
1223            "internal_convert",
1224            make_native!(group_ops::internal_convert),
1225        ),
1226        (
1227            "group_ops",
1228            "internal_sum",
1229            make_native!(group_ops::internal_sum),
1230        ),
1231        ("object", "delete_impl", make_native!(object::delete_impl)),
1232        ("object", "borrow_uid", make_native!(object::borrow_uid)),
1233        (
1234            "object",
1235            "record_new_uid",
1236            make_native!(object::record_new_uid),
1237        ),
1238        (
1239            "object",
1240            "record_new_uid_from_hash",
1241            make_native!(object::record_new_uid_from_hash),
1242        ),
1243        (
1244            "test_scenario",
1245            "take_from_address_by_id",
1246            make_native!(test_scenario::take_from_address_by_id),
1247        ),
1248        (
1249            "test_scenario",
1250            "most_recent_id_for_address",
1251            make_native!(test_scenario::most_recent_id_for_address),
1252        ),
1253        (
1254            "test_scenario",
1255            "was_taken_from_address",
1256            make_native!(test_scenario::was_taken_from_address),
1257        ),
1258        (
1259            "test_scenario",
1260            "take_immutable_by_id",
1261            make_native!(test_scenario::take_immutable_by_id),
1262        ),
1263        (
1264            "test_scenario",
1265            "most_recent_immutable_id",
1266            make_native!(test_scenario::most_recent_immutable_id),
1267        ),
1268        (
1269            "test_scenario",
1270            "was_taken_immutable",
1271            make_native!(test_scenario::was_taken_immutable),
1272        ),
1273        (
1274            "test_scenario",
1275            "take_shared_by_id",
1276            make_native!(test_scenario::take_shared_by_id),
1277        ),
1278        (
1279            "test_scenario",
1280            "most_recent_id_shared",
1281            make_native!(test_scenario::most_recent_id_shared),
1282        ),
1283        (
1284            "test_scenario",
1285            "was_taken_shared",
1286            make_native!(test_scenario::was_taken_shared),
1287        ),
1288        (
1289            "test_scenario",
1290            "end_transaction",
1291            make_native!(test_scenario::end_transaction),
1292        ),
1293        (
1294            "test_scenario",
1295            "ids_for_address",
1296            make_native!(test_scenario::ids_for_address),
1297        ),
1298        (
1299            "test_scenario",
1300            "allocate_receiving_ticket_for_object",
1301            make_native!(test_scenario::allocate_receiving_ticket_for_object),
1302        ),
1303        (
1304            "test_scenario",
1305            "deallocate_receiving_ticket_for_object",
1306            make_native!(test_scenario::deallocate_receiving_ticket_for_object),
1307        ),
1308        (
1309            "test_scenario",
1310            "settled_funds",
1311            make_native!(test_scenario::settled_funds),
1312        ),
1313        (
1314            "test_scenario",
1315            "reserve_funds_from_address",
1316            make_native!(test_scenario::reserve_funds_from_address),
1317        ),
1318        (
1319            "transfer",
1320            "transfer_impl",
1321            make_native!(transfer::transfer_internal),
1322        ),
1323        (
1324            "transfer",
1325            "party_transfer_impl",
1326            make_native!(transfer::party_transfer_internal),
1327        ),
1328        (
1329            "transfer",
1330            "freeze_object_impl",
1331            make_native!(transfer::freeze_object),
1332        ),
1333        (
1334            "transfer",
1335            "share_object_impl",
1336            make_native!(transfer::share_object),
1337        ),
1338        (
1339            "transfer",
1340            "receive_impl",
1341            make_native!(transfer::receive_object_internal),
1342        ),
1343        (
1344            "tx_context",
1345            "last_created_id",
1346            make_native!(tx_context::last_created_id),
1347        ),
1348        (
1349            "tx_context",
1350            "derive_id",
1351            make_native!(tx_context::derive_id),
1352        ),
1353        ("tx_context", "fresh_id", make_native!(tx_context::fresh_id)),
1354        (
1355            "tx_context",
1356            "native_sender",
1357            make_native!(tx_context::sender),
1358        ),
1359        (
1360            "tx_context",
1361            "native_epoch",
1362            make_native!(tx_context::epoch),
1363        ),
1364        (
1365            "tx_context",
1366            "native_epoch_timestamp_ms",
1367            make_native!(tx_context::epoch_timestamp_ms),
1368        ),
1369        (
1370            "tx_context",
1371            "native_sponsor",
1372            make_native!(tx_context::sponsor),
1373        ),
1374        ("tx_context", "native_rgp", make_native!(tx_context::rgp)),
1375        (
1376            "tx_context",
1377            "native_gas_price",
1378            make_native!(tx_context::gas_price),
1379        ),
1380        (
1381            "tx_context",
1382            "native_gas_budget",
1383            make_native!(tx_context::gas_budget),
1384        ),
1385        (
1386            "tx_context",
1387            "native_ids_created",
1388            make_native!(tx_context::ids_created),
1389        ),
1390        ("tx_context", "replace", make_native!(tx_context::replace)),
1391        (
1392            "types",
1393            "is_one_time_witness",
1394            make_native!(types::is_one_time_witness),
1395        ),
1396        (
1397            "test_utils",
1398            "create_one_time_witness",
1399            make_native!(test_utils::create_one_time_witness),
1400        ),
1401        (
1402            "random",
1403            "generate_rand_seed_for_testing",
1404            make_native!(random::generate_rand_seed_for_testing),
1405        ),
1406        (
1407            "zklogin_verified_id",
1408            "check_zklogin_id_internal",
1409            make_native!(zklogin::check_zklogin_id_internal),
1410        ),
1411        (
1412            "zklogin_verified_issuer",
1413            "check_zklogin_issuer_internal",
1414            make_native!(zklogin::check_zklogin_issuer_internal),
1415        ),
1416        (
1417            "poseidon",
1418            "poseidon_bn254_internal",
1419            make_native!(poseidon::poseidon_bn254_internal),
1420        ),
1421        (
1422            "protocol_config",
1423            "is_feature_enabled",
1424            make_native!(protocol_config::is_feature_enabled),
1425        ),
1426        (
1427            "vdf",
1428            "vdf_verify_internal",
1429            make_native!(vdf::vdf_verify_internal),
1430        ),
1431        (
1432            "vdf",
1433            "hash_to_input_internal",
1434            make_native!(vdf::hash_to_input_internal),
1435        ),
1436        (
1437            "ecdsa_k1",
1438            "secp256k1_sign",
1439            make_native!(ecdsa_k1::secp256k1_sign),
1440        ),
1441        (
1442            "ecdsa_k1",
1443            "secp256k1_keypair_from_seed",
1444            make_native!(ecdsa_k1::secp256k1_keypair_from_seed),
1445        ),
1446        (
1447            "nitro_attestation",
1448            "load_nitro_attestation_internal",
1449            make_native!(nitro_attestation::load_nitro_attestation_internal),
1450        ),
1451        (
1452            "rangeproofs",
1453            "verify_bulletproofs_ristretto255_internal",
1454            make_native!(rangeproofs::verify_bulletproofs_ristretto255),
1455        ),
1456        (
1457            "rangeproofs",
1458            "verify_bulletproofs_with_dst_ristretto255_internal",
1459            make_native!(rangeproofs::verify_bulletproofs_with_dst_ristretto255),
1460        ),
1461    ];
1462    let sui_framework_natives_iter =
1463        sui_framework_natives
1464            .iter()
1465            .cloned()
1466            .map(|(module_name, func_name, func)| {
1467                (
1468                    SUI_FRAMEWORK_ADDRESS,
1469                    // Safe: string literals are always valid identifiers
1470                    Identifier::new(module_name).unwrap(),
1471                    Identifier::new(func_name).unwrap(),
1472                    func,
1473                )
1474            });
1475    let sui_system_natives: &[(&str, &str, NativeFunction)] = &[(
1476        "validator",
1477        "validate_metadata_bcs",
1478        make_native!(validator::validate_metadata_bcs),
1479    )];
1480    sui_system_natives
1481        .iter()
1482        .cloned()
1483        .map(|(module_name, func_name, func)| {
1484            (
1485                SUI_SYSTEM_ADDRESS,
1486                // Safe: string literals are always valid identifiers
1487                Identifier::new(module_name).unwrap(),
1488                Identifier::new(func_name).unwrap(),
1489                func,
1490            )
1491        })
1492        .chain(sui_framework_natives_iter)
1493        .chain(
1494            move_vm_runtime::natives::move_stdlib::stdlib_native_function_table(
1495                MOVE_STDLIB_ADDRESS,
1496                make_stdlib_gas_params_for_protocol_config(protocol_config),
1497                silent,
1498            ),
1499        )
1500        .collect()
1501}
1502
1503// ID { bytes: address }
1504// Extract the first field of the struct to get the address bytes.
1505pub fn get_receiver_object_id(object: Value) -> Result<Value, PartialVMError> {
1506    get_nested_struct_field(object, &[0])
1507}
1508
1509// Object { id: UID { id: ID { bytes: address } } .. }
1510// Extract the first field of the struct 3 times to get the id bytes.
1511pub fn get_object_id(object: Value) -> Result<Value, PartialVMError> {
1512    get_nested_struct_field(object, &[0, 0, 0])
1513}
1514
1515// Extract a field value that's nested inside value `v`. The offset of each nesting
1516// is determined by `offsets`.
1517pub fn get_nested_struct_field(mut v: Value, offsets: &[usize]) -> Result<Value, PartialVMError> {
1518    for offset in offsets {
1519        v = get_nth_struct_field(v, *offset)?;
1520    }
1521    Ok(v)
1522}
1523
1524pub fn get_nth_struct_field(v: Value, n: usize) -> Result<Value, PartialVMError> {
1525    let mut itr = v.value_as::<Struct>()?.unpack();
1526    Ok(safe_unwrap!(itr.nth(n)))
1527}
1528
1529/// Returns the struct tag, non-annotated type layout, and fully annotated type layout of `ty`.
1530pub(crate) fn get_tag_and_layouts(
1531    context: &NativeContext,
1532    ty: &Type,
1533) -> PartialVMResult<Option<(StructTag, R::MoveTypeLayout, A::MoveTypeLayout)>> {
1534    let tag = match context.type_to_type_tag(ty)? {
1535        TypeTag::Struct(s) => s,
1536        _ => {
1537            return Err(
1538                PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
1539                    .with_message("Sui verifier guarantees this is a struct".to_string()),
1540            );
1541        }
1542    };
1543    let Some(layout) = context.type_to_type_layout(ty)? else {
1544        return Ok(None);
1545    };
1546    let Some(annotated_layout) = context.type_to_fully_annotated_layout(ty)? else {
1547        return Ok(None);
1548    };
1549    Ok(Some((*tag, layout, annotated_layout)))
1550}
1551
1552#[macro_export]
1553macro_rules! make_native {
1554    ($native: expr) => {
1555        Arc::new(
1556            move |context, ty_args, args| -> PartialVMResult<NativeResult> {
1557                $native(context, ty_args, args)
1558            },
1559        )
1560    };
1561}
1562
1563#[macro_export]
1564macro_rules! get_extension {
1565    ($context: expr, $ext: ty) => {
1566        $context.extensions().get::<$ext>()
1567    };
1568    ($context: expr) => {
1569        $context.extensions().get()
1570    };
1571}
1572
1573#[macro_export]
1574macro_rules! get_extension_mut {
1575    ($context: expr, $ext: ty) => {
1576        $context.extensions_mut().get_mut::<$ext>()
1577    };
1578    ($context: expr) => {
1579        $context.extensions_mut().get_mut()
1580    };
1581}
1582
1583#[macro_export]
1584macro_rules! charge_cache_or_load_gas {
1585    ($context:ident, $cache_info:expr) => {{
1586        use $crate::object_runtime::object_store::CacheInfo;
1587        match $cache_info {
1588            CacheInfo::CachedObject | CacheInfo::CachedValue => (),
1589            CacheInfo::Loaded(bytes_opt) => {
1590                let config = get_extension!($context, ObjectRuntime)?.protocol_config;
1591                if config.object_runtime_charge_cache_load_gas() {
1592                    let bytes = bytes_opt.unwrap_or(0).max(1);
1593                    native_charge_gas_early_exit!($context, InternalGas::new(bytes as u64));
1594                }
1595            }
1596        }
1597    }};
1598}
1599
1600pub(crate) fn legacy_test_cost() -> InternalGas {
1601    InternalGas::new(0)
1602}
1603
1604pub(crate) fn abstract_size(
1605    _protocol_config: &ProtocolConfig,
1606    v: &Value,
1607) -> PartialVMResult<AbstractMemorySize> {
1608    v.abstract_memory_size(&SizeConfig {
1609        include_vector_size: true,
1610        traverse_references: false,
1611    })
1612}
1613
1614#[cfg(test)]
1615mod tests {
1616    use super::*;
1617    use sui_protocol_config::{Chain, ProtocolVersion};
1618
1619    #[test]
1620    fn vector_bulk_native_gas_mapping_is_protocol_gated() {
1621        let before = ProtocolConfig::get_for_version(ProtocolVersion::new(139), Chain::Unknown);
1622        let before = make_stdlib_gas_params_for_protocol_config(&before);
1623        assert_eq!(before.vector.reverse.base, 0.into());
1624        assert_eq!(before.vector.reverse.per_elem, 0.into());
1625        assert_eq!(before.vector.keep_range.base, 0.into());
1626        assert_eq!(before.vector.keep_range.per_dropped_elem, 0.into());
1627        assert_eq!(before.vector.keep_range.per_moved_elem, 0.into());
1628        assert_eq!(before.vector.copy_range.base, 0.into());
1629        assert_eq!(before.vector.replace_range.base, 0.into());
1630        assert_eq!(before.vector.replace_range.per_elem, 0.into());
1631
1632        let active = ProtocolConfig::get_for_version(ProtocolVersion::new(140), Chain::Unknown);
1633        let active = make_stdlib_gas_params_for_protocol_config(&active);
1634        assert_eq!(active.vector.reverse.base, 52.into());
1635        assert_eq!(active.vector.reverse.per_elem, 8.into());
1636        assert_eq!(active.vector.keep_range.base, 52.into());
1637        assert_eq!(active.vector.keep_range.per_dropped_elem, 1.into());
1638        assert_eq!(active.vector.keep_range.per_moved_elem, 8.into());
1639        assert_eq!(active.vector.copy_range.base, 52.into());
1640        assert_eq!(active.vector.replace_range.base, 52.into());
1641        assert_eq!(active.vector.replace_range.per_elem, 8.into());
1642    }
1643}