From 91dd9943f176bc0be068ae7c2feec5f983ac91d9 Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Tue, 30 Apr 2024 16:50:15 +0300 Subject: [PATCH] test: draft abi transform test for stdlib blake3 hash function --- codegen/masm/src/codegen/emitter.rs | 13 +- sdk/prelude/src/stdlib/crypto/hashes.rs | 3 +- .../abi_transform_stdlib_blake3_hash.hir | 395 ++++++++++++++++++ .../abi_transform_stdlib_blake3_hash.wat | 295 +++++++++++++ .../miden_sdk_account_test.wat | 6 +- tests/integration/src/compiler_test.rs | 5 +- .../src/rust_masm_tests/abi_transform/mod.rs | 1 + .../rust_masm_tests/abi_transform/stdlib.rs | 38 ++ .../src/rust_masm_tests/intrinsics.rs | 4 +- tests/integration/src/rust_masm_tests/mod.rs | 1 + 10 files changed, 748 insertions(+), 13 deletions(-) create mode 100644 tests/integration/expected/abi_transform_stdlib_blake3_hash.hir create mode 100644 tests/integration/expected/abi_transform_stdlib_blake3_hash.wat create mode 100644 tests/integration/src/rust_masm_tests/abi_transform/mod.rs create mode 100644 tests/integration/src/rust_masm_tests/abi_transform/stdlib.rs diff --git a/codegen/masm/src/codegen/emitter.rs b/codegen/masm/src/codegen/emitter.rs index 25b79a882..376f5b333 100644 --- a/codegen/masm/src/codegen/emitter.rs +++ b/codegen/masm/src/codegen/emitter.rs @@ -419,11 +419,14 @@ impl<'b, 'f: 'b> BlockEmitter<'b, 'f> { fn emit_global_value(&mut self, inst_info: &InstInfo, op: &hir::GlobalValueOp) { assert_eq!(op.op, hir::Opcode::GlobalValue); - let addr = self - .function - .globals - .get_computed_addr(&self.function.f.id, op.global) - .expect("expected linker to identify all undefined symbols"); + let addr = self.function.globals.get_computed_addr(&self.function.f.id, op.global).expect( + format!( + "expected linker to identify all undefined symbols, but failed on func id: {}, \ + gv: {}", + self.function.f.id, op.global + ) + .as_str(), + ); match self.function.f.dfg.global_value(op.global) { hir::GlobalValueData::Load { ref ty, .. } => { let mut emitter = self.inst_emitter(inst_info.inst); diff --git a/sdk/prelude/src/stdlib/crypto/hashes.rs b/sdk/prelude/src/stdlib/crypto/hashes.rs index baadf1f2d..fa17d4ed4 100644 --- a/sdk/prelude/src/stdlib/crypto/hashes.rs +++ b/sdk/prelude/src/stdlib/crypto/hashes.rs @@ -3,7 +3,8 @@ //! value per element. use crate::Felt; -#[link(wasm_import_module = "miden:prelude/std_crypto_hashes")] +// #[link(wasm_import_module = "miden:prelude/std_crypto_hashes")] +#[link(wasm_import_module = "std::crypto_hashes")] extern "C" { /// Computes BLAKE3 1-to-1 hash. /// diff --git a/tests/integration/expected/abi_transform_stdlib_blake3_hash.hir b/tests/integration/expected/abi_transform_stdlib_blake3_hash.hir new file mode 100644 index 000000000..fff614a03 --- /dev/null +++ b/tests/integration/expected/abi_transform_stdlib_blake3_hash.hir @@ -0,0 +1,395 @@ +(component + ;; Modules + (module #abi_transform_stdlib_blake3_hash + ;; Data Segments + (data (mut) (offset 1048576) 0x7e2f73646b2f7072656c7564652f7372632f7374646c69622f63727970746f2f6861736865732e72730000000000100029000000d100000028000000) + + ;; Constants + (const (id 0) 0x00100000) + + ;; Global Variables + (global (export #__stack_pointer) (id 0) (type i32) (const 0)) + + ;; Functions + (func (export #entrypoint) (param i32) (param i32) (param i32) + (block 0 (param v0 i32) (param v1 i32) (param v2 i32) + (let (v3 i32) (const.i32 0)) + (let (v4 felt) (const.felt 0)) + (let (v5 i32) (global.load i32 (global.symbol #__stack_pointer))) + (let (v6 i32) (const.i32 432)) + (let (v7 i32) (sub.wrapping v5 v6)) + (let (v8 (ptr i32)) (global.symbol #__stack_pointer)) + (store v8 v7) + (let (v9 i32) (const.i32 0)) + (let (v10 i64) (const.i64 0)) + (let (v11 felt) (cast v10)) + (br (block 2 v9 v7 v2 v0 v1 v11))) + + (block 1) + + (block 2 + (param v12 i32) + (param v251 i32) + (param v252 i32) + (param v253 i32) + (param v254 i32) + (param v258 felt) + (let (v13 i32) (const.i32 64)) + (let (v14 i1) (neq v12 v13)) + (let (v15 i32) (cast v14)) + (let (v16 i1) (neq v15 0)) + (condbr v16 (block 4) (block 5))) + + (block 3) + + (block 4 + (let (v255 i32) (const.i32 8)) + (let (v256 i32) (add.wrapping v251 v255)) + (let (v257 i32) (add.wrapping v256 v12)) + (let (v259 u32) (cast v257)) + (let (v260 (ptr felt)) (inttoptr v259)) + (store v260 v258) + (let (v261 i32) (const.i32 8)) + (let (v262 i32) (add.wrapping v12 v261)) + (br (block 2 v262 v251 v252 v253 v254 v258))) + + (block 5 + (let (v17 i32) (const.i32 0)) + (br (block 6 v17 v251 v252 v253 v254))) + + (block 6 + (param v18 i32) + (param v233 i32) + (param v234 i32) + (param v235 i32) + (param v239 i32) + (let (v19 i32) (const.i32 64)) + (let (v20 i1) (neq v18 v19)) + (let (v21 i32) (cast v20)) + (let (v22 i1) (neq v21 0)) + (condbr v22 (block 8) (block 9))) + + (block 7) + + (block 8 + (let (v236 i32) (const.i32 8)) + (let (v237 i32) (add.wrapping v233 v236)) + (let (v238 i32) (add.wrapping v237 v18)) + (let (v240 u32) (cast v239)) + (let (v241 (ptr u32)) (inttoptr v240)) + (let (v242 u32) (load v241)) + (let (v243 i64) (zext v242)) + (let (v244 felt) (cast v243)) + (let (v245 u32) (cast v238)) + (let (v246 (ptr felt)) (inttoptr v245)) + (store v246 v244) + (let (v247 i32) (const.i32 8)) + (let (v248 i32) (add.wrapping v18 v247)) + (let (v249 i32) (const.i32 4)) + (let (v250 i32) (add.wrapping v239 v249)) + (br (block 6 v248 v233 v234 v235 v250))) + + (block 9 + (let (v23 i32) (const.i32 0)) + (let (v24 i64) (const.i64 0)) + (let (v25 felt) (cast v24)) + (br (block 10 v23 v233 v234 v235 v25))) + + (block 10 + (param v26 i32) + (param v178 i32) + (param v179 i32) + (param v182 i32) + (param v228 felt) + (let (v27 i32) (const.i32 64)) + (let (v28 i1) (neq v26 v27)) + (let (v29 i32) (cast v28)) + (let (v30 i1) (neq v29 0)) + (condbr v30 (block 12) (block 13))) + + (block 11) + + (block 12 + (let (v225 i32) (const.i32 72)) + (let (v226 i32) (add.wrapping v178 v225)) + (let (v227 i32) (add.wrapping v226 v26)) + (let (v229 u32) (cast v227)) + (let (v230 (ptr felt)) (inttoptr v229)) + (store v230 v228) + (let (v231 i32) (const.i32 8)) + (let (v232 i32) (add.wrapping v26 v231)) + (br (block 10 v232 v178 v179 v182 v228))) + + (block 13 + (let (v31 i32) (const.i32 0)) + (br (block 15 v31 v178 v179 v182))) + + (block 14 + (let (v183 u32) (cast v142)) + (let (v184 u32) (add.checked v183 392)) + (let (v185 (ptr i64)) (inttoptr v184)) + (let (v186 i64) (load v185)) + (let (v187 u32) (cast v180)) + (let (v188 (ptr i64)) (inttoptr v187)) + (store v188 v186) + (let (v189 i32) (const.i32 24)) + (let (v190 i32) (add.wrapping v180 v189)) + (let (v191 i32) (const.i32 392)) + (let (v192 i32) (add.wrapping v142 v191)) + (let (v193 i32) (const.i32 24)) + (let (v194 i32) (add.wrapping v192 v193)) + (let (v195 u32) (cast v194)) + (let (v196 (ptr i64)) (inttoptr v195)) + (let (v197 i64) (load v196)) + (let (v198 u32) (cast v190)) + (let (v199 (ptr i64)) (inttoptr v198)) + (store v199 v197) + (let (v200 i32) (const.i32 16)) + (let (v201 i32) (add.wrapping v180 v200)) + (let (v202 i32) (const.i32 392)) + (let (v203 i32) (add.wrapping v142 v202)) + (let (v204 i32) (const.i32 16)) + (let (v205 i32) (add.wrapping v203 v204)) + (let (v206 u32) (cast v205)) + (let (v207 (ptr i64)) (inttoptr v206)) + (let (v208 i64) (load v207)) + (let (v209 u32) (cast v201)) + (let (v210 (ptr i64)) (inttoptr v209)) + (store v210 v208) + (let (v211 i32) (const.i32 8)) + (let (v212 i32) (add.wrapping v180 v211)) + (let (v213 i32) (const.i32 392)) + (let (v214 i32) (add.wrapping v142 v213)) + (let (v215 i32) (const.i32 8)) + (let (v216 i32) (add.wrapping v214 v215)) + (let (v217 u32) (cast v216)) + (let (v218 (ptr i64)) (inttoptr v217)) + (let (v219 i64) (load v218)) + (let (v220 u32) (cast v212)) + (let (v221 (ptr i64)) (inttoptr v220)) + (store v221 v219) + (let (v222 i32) (const.i32 432)) + (let (v223 i32) (add.wrapping v142 v222)) + (let (v224 (ptr i32)) (global.symbol #__stack_pointer)) + (store v224 v223) + (ret)) + + (block 15 + (param v32 i32) + (param v37 i32) + (param v166 i32) + (param v181 i32) + (let (v33 i32) (const.i32 64)) + (let (v34 i1) (neq v32 v33)) + (let (v35 i32) (cast v34)) + (let (v36 i1) (neq v35 0)) + (condbr v36 (block 17) (block 18))) + + (block 16) + + (block 17 + (let (v163 i32) (const.i32 72)) + (let (v164 i32) (add.wrapping v37 v163)) + (let (v165 i32) (add.wrapping v164 v32)) + (let (v167 u32) (cast v166)) + (let (v168 (ptr u32)) (inttoptr v167)) + (let (v169 u32) (load v168)) + (let (v170 i64) (zext v169)) + (let (v171 felt) (cast v170)) + (let (v172 u32) (cast v165)) + (let (v173 (ptr felt)) (inttoptr v172)) + (store v173 v171) + (let (v174 i32) (const.i32 8)) + (let (v175 i32) (add.wrapping v32 v174)) + (let (v176 i32) (const.i32 4)) + (let (v177 i32) (add.wrapping v166 v176)) + (br (block 15 v175 v37 v177 v181))) + + (block 18 + (let (v38 u32) (cast v37)) + (let (v39 u32) (add.checked v38 8)) + (let (v40 (ptr felt)) (inttoptr v39)) + (let (v41 felt) (load v40)) + (let (v42 u32) (cast v37)) + (let (v43 u32) (add.checked v42 16)) + (let (v44 (ptr felt)) (inttoptr v43)) + (let (v45 felt) (load v44)) + (let (v46 u32) (cast v37)) + (let (v47 u32) (add.checked v46 24)) + (let (v48 (ptr felt)) (inttoptr v47)) + (let (v49 felt) (load v48)) + (let (v50 u32) (cast v37)) + (let (v51 u32) (add.checked v50 32)) + (let (v52 (ptr felt)) (inttoptr v51)) + (let (v53 felt) (load v52)) + (let (v54 u32) (cast v37)) + (let (v55 u32) (add.checked v54 40)) + (let (v56 (ptr felt)) (inttoptr v55)) + (let (v57 felt) (load v56)) + (let (v58 u32) (cast v37)) + (let (v59 u32) (add.checked v58 48)) + (let (v60 (ptr felt)) (inttoptr v59)) + (let (v61 felt) (load v60)) + (let (v62 u32) (cast v37)) + (let (v63 u32) (add.checked v62 56)) + (let (v64 (ptr felt)) (inttoptr v63)) + (let (v65 felt) (load v64)) + (let (v66 u32) (cast v37)) + (let (v67 u32) (add.checked v66 64)) + (let (v68 (ptr felt)) (inttoptr v67)) + (let (v69 felt) (load v68)) + (let (v70 u32) (cast v37)) + (let (v71 u32) (add.checked v70 72)) + (let (v72 (ptr felt)) (inttoptr v71)) + (let (v73 felt) (load v72)) + (let (v74 u32) (cast v37)) + (let (v75 u32) (add.checked v74 80)) + (let (v76 (ptr felt)) (inttoptr v75)) + (let (v77 felt) (load v76)) + (let (v78 u32) (cast v37)) + (let (v79 u32) (add.checked v78 88)) + (let (v80 (ptr felt)) (inttoptr v79)) + (let (v81 felt) (load v80)) + (let (v82 u32) (cast v37)) + (let (v83 u32) (add.checked v82 96)) + (let (v84 (ptr felt)) (inttoptr v83)) + (let (v85 felt) (load v84)) + (let (v86 u32) (cast v37)) + (let (v87 u32) (add.checked v86 104)) + (let (v88 (ptr felt)) (inttoptr v87)) + (let (v89 felt) (load v88)) + (let (v90 u32) (cast v37)) + (let (v91 u32) (add.checked v90 112)) + (let (v92 (ptr felt)) (inttoptr v91)) + (let (v93 felt) (load v92)) + (let (v94 u32) (cast v37)) + (let (v95 u32) (add.checked v94 120)) + (let (v96 (ptr felt)) (inttoptr v95)) + (let (v97 felt) (load v96)) + (let (v98 u32) (cast v37)) + (let (v99 u32) (add.checked v98 128)) + (let (v100 (ptr felt)) (inttoptr v99)) + (let (v101 felt) (load v100)) + (let (v102 i32) (const.i32 136)) + (let (v103 i32) (add.wrapping v37 v102)) + (call (#std::crypto_hashes #blake3_hash_2to1) v41 v45 v49 v53 v57 v61 v65 v69 v73 v77 v81 v85 v89 v93 v97 v101 v103) + (let (v104 i32) (const.i32 264)) + (let (v105 i32) (add.wrapping v37 v104)) + (let (v106 i32) (const.i32 136)) + (let (v107 i32) (add.wrapping v37 v106)) + (let (v108 i32) (const.i32 128)) + (let (v109 u32) (cast v105)) + (let (v110 (ptr u8)) (inttoptr v109)) + (let (v111 u32) (cast v107)) + (let (v112 (ptr u8)) (inttoptr v111)) + (memcpy v112 v110 v108) + (let (v113 i32) (const.i32 416)) + (let (v114 i32) (add.wrapping v37 v113)) + (let (v115 i64) (const.i64 0)) + (let (v116 u32) (cast v114)) + (let (v117 (ptr i64)) (inttoptr v116)) + (store v117 v115) + (let (v118 i32) (const.i32 408)) + (let (v119 i32) (add.wrapping v37 v118)) + (let (v120 i64) (const.i64 0)) + (let (v121 u32) (cast v119)) + (let (v122 (ptr i64)) (inttoptr v121)) + (store v122 v120) + (let (v123 i32) (const.i32 392)) + (let (v124 i32) (add.wrapping v37 v123)) + (let (v125 i32) (const.i32 8)) + (let (v126 i32) (add.wrapping v124 v125)) + (let (v127 i64) (const.i64 0)) + (let (v128 u32) (cast v126)) + (let (v129 (ptr i64)) (inttoptr v128)) + (store v129 v127) + (let (v130 i64) (const.i64 0)) + (let (v131 u32) (cast v37)) + (let (v132 u32) (add.checked v131 392)) + (let (v133 (ptr i64)) (inttoptr v132)) + (store v133 v130) + (let (v134 i32) (const.i32 264)) + (let (v135 i32) (add.wrapping v37 v134)) + (let (v136 i32) (const.i32 0)) + (br (block 19 v136 v37 v135 v181))) + + (block 19 + (param v137 i32) + (param v142 i32) + (param v143 i32) + (param v180 i32) + (let (v138 i32) (const.i32 32)) + (let (v139 i1) (eq v137 v138)) + (let (v140 i32) (cast v139)) + (let (v141 i1) (neq v140 0)) + (condbr v141 (block 14) (block 21))) + + (block 20) + + (block 21 + (let (v144 u32) (cast v143)) + (let (v145 (ptr felt)) (inttoptr v144)) + (let (v146 felt) (load v145)) + (let (v147 i64) (cast v146)) + (let (v148 u32) (cast v142)) + (let (v149 u32) (add.checked v148 424)) + (let (v150 (ptr i64)) (inttoptr v149)) + (store v150 v147) + (let (v151 i32) (const.i32 392)) + (let (v152 i32) (add.wrapping v142 v151)) + (let (v153 i32) (add.wrapping v152 v137)) + (let (v154 i32) (const.i32 4)) + (let (v155 i32) (const.i32 424)) + (let (v156 i32) (add.wrapping v142 v155)) + (let (v157 i32) (const.i32 4)) + (let (v158 i32) (const.i32 1048620)) + (call #core::slice::::copy_from_slice v153 v154 v156 v157 v158) + (let (v159 i32) (const.i32 4)) + (let (v160 i32) (add.wrapping v137 v159)) + (let (v161 i32) (const.i32 8)) + (let (v162 i32) (add.wrapping v143 v161)) + (br (block 19 v160 v142 v162 v180))) + ) + + (func (export #core::slice::::copy_from_slice::len_mismatch_fail) + (param i32) (param i32) (param i32) + (block 0 (param v0 i32) (param v1 i32) (param v2 i32) + (unreachable)) + + (block 1) + ) + + (func (export #core::slice::::copy_from_slice) + (param i32) (param i32) (param i32) (param i32) (param i32) + (block 0 + (param v0 i32) + (param v1 i32) + (param v2 i32) + (param v3 i32) + (param v4 i32) + (let (v5 i1) (neq v1 v3)) + (let (v6 i32) (cast v5)) + (let (v7 i1) (neq v6 0)) + (condbr v7 (block 2) (block 3))) + + (block 1) + + (block 2 + (call #core::slice::::copy_from_slice::len_mismatch_fail v1 v1 v1) + (unreachable)) + + (block 3 + (let (v8 u32) (cast v0)) + (let (v9 (ptr u8)) (inttoptr v8)) + (let (v10 u32) (cast v2)) + (let (v11 (ptr u8)) (inttoptr v10)) + (memcpy v11 v9 v1) + (ret)) + ) + + ;; Imports + (func (import #std::crypto_hashes #blake3_hash_2to1) + (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param felt) (param i32)) + ) + +) diff --git a/tests/integration/expected/abi_transform_stdlib_blake3_hash.wat b/tests/integration/expected/abi_transform_stdlib_blake3_hash.wat new file mode 100644 index 000000000..4427e5441 --- /dev/null +++ b/tests/integration/expected/abi_transform_stdlib_blake3_hash.wat @@ -0,0 +1,295 @@ +(module $abi_transform_stdlib_blake3_hash.wasm + (type (;0;) (func (param i64) (result f64))) + (type (;1;) (func (param f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 i32))) + (type (;2;) (func (param f64) (result i64))) + (type (;3;) (func (param i32 i32 i32))) + (type (;4;) (func (param i32 i32 i32 i32 i32))) + (import "miden:prelude/intrinsics_felt" "from_u64_unchecked" (func $miden_prelude::intrinsics::felt::extern_from_u64_unchecked (;0;) (type 0))) + (import "std::crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;1;) (type 1))) + (import "miden:prelude/intrinsics_felt" "as_u64" (func $miden_prelude::intrinsics::felt::extern_as_u64 (;2;) (type 2))) + (func $entrypoint (;3;) (type 3) (param i32 i32 i32) + (local i32 i32 f64) + global.get $__stack_pointer + i32.const 432 + i32.sub + local.tee 3 + global.set $__stack_pointer + i32.const 0 + local.set 4 + i64.const 0 + call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked + local.set 5 + loop ;; label = @1 + block ;; label = @2 + local.get 4 + i32.const 64 + i32.ne + br_if 0 (;@2;) + i32.const 0 + local.set 4 + loop ;; label = @3 + block ;; label = @4 + local.get 4 + i32.const 64 + i32.ne + br_if 0 (;@4;) + i32.const 0 + local.set 4 + i64.const 0 + call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked + local.set 5 + loop ;; label = @5 + block ;; label = @6 + local.get 4 + i32.const 64 + i32.ne + br_if 0 (;@6;) + i32.const 0 + local.set 4 + block ;; label = @7 + loop ;; label = @8 + block ;; label = @9 + local.get 4 + i32.const 64 + i32.ne + br_if 0 (;@9;) + local.get 3 + f64.load offset=8 + local.get 3 + f64.load offset=16 + local.get 3 + f64.load offset=24 + local.get 3 + f64.load offset=32 + local.get 3 + f64.load offset=40 + local.get 3 + f64.load offset=48 + local.get 3 + f64.load offset=56 + local.get 3 + f64.load offset=64 + local.get 3 + f64.load offset=72 + local.get 3 + f64.load offset=80 + local.get 3 + f64.load offset=88 + local.get 3 + f64.load offset=96 + local.get 3 + f64.load offset=104 + local.get 3 + f64.load offset=112 + local.get 3 + f64.load offset=120 + local.get 3 + f64.load offset=128 + local.get 3 + i32.const 136 + i32.add + call $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 + local.get 3 + i32.const 264 + i32.add + local.get 3 + i32.const 136 + i32.add + i32.const 128 + memory.copy + local.get 3 + i32.const 416 + i32.add + i64.const 0 + i64.store + local.get 3 + i32.const 408 + i32.add + i64.const 0 + i64.store + local.get 3 + i32.const 392 + i32.add + i32.const 8 + i32.add + i64.const 0 + i64.store + local.get 3 + i64.const 0 + i64.store offset=392 + local.get 3 + i32.const 264 + i32.add + local.set 2 + i32.const 0 + local.set 4 + loop ;; label = @10 + local.get 4 + i32.const 32 + i32.eq + br_if 3 (;@7;) + local.get 3 + local.get 2 + f64.load + call $miden_prelude::intrinsics::felt::extern_as_u64 + i64.store offset=424 + local.get 3 + i32.const 392 + i32.add + local.get 4 + i32.add + i32.const 4 + local.get 3 + i32.const 424 + i32.add + i32.const 4 + i32.const 1048620 + call $core::slice::::copy_from_slice + local.get 4 + i32.const 4 + i32.add + local.set 4 + local.get 2 + i32.const 8 + i32.add + local.set 2 + br 0 (;@10;) + end + end + local.get 3 + i32.const 72 + i32.add + local.get 4 + i32.add + local.get 2 + i64.load32_u align=1 + call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked + f64.store + local.get 4 + i32.const 8 + i32.add + local.set 4 + local.get 2 + i32.const 4 + i32.add + local.set 2 + br 0 (;@8;) + end + end + local.get 0 + local.get 3 + i64.load offset=392 + i64.store align=1 + local.get 0 + i32.const 24 + i32.add + local.get 3 + i32.const 392 + i32.add + i32.const 24 + i32.add + i64.load + i64.store align=1 + local.get 0 + i32.const 16 + i32.add + local.get 3 + i32.const 392 + i32.add + i32.const 16 + i32.add + i64.load + i64.store align=1 + local.get 0 + i32.const 8 + i32.add + local.get 3 + i32.const 392 + i32.add + i32.const 8 + i32.add + i64.load + i64.store align=1 + local.get 3 + i32.const 432 + i32.add + global.set $__stack_pointer + return + end + local.get 3 + i32.const 72 + i32.add + local.get 4 + i32.add + local.get 5 + f64.store + local.get 4 + i32.const 8 + i32.add + local.set 4 + br 0 (;@5;) + end + end + local.get 3 + i32.const 8 + i32.add + local.get 4 + i32.add + local.get 1 + i64.load32_u align=1 + call $miden_prelude::intrinsics::felt::extern_from_u64_unchecked + f64.store + local.get 4 + i32.const 8 + i32.add + local.set 4 + local.get 1 + i32.const 4 + i32.add + local.set 1 + br 0 (;@3;) + end + end + local.get 3 + i32.const 8 + i32.add + local.get 4 + i32.add + local.get 5 + f64.store + local.get 4 + i32.const 8 + i32.add + local.set 4 + br 0 (;@1;) + end + ) + (func $core::slice::::copy_from_slice::len_mismatch_fail (;4;) (type 3) (param i32 i32 i32) + unreachable + unreachable + ) + (func $core::slice::::copy_from_slice (;5;) (type 4) (param i32 i32 i32 i32 i32) + block ;; label = @1 + local.get 1 + local.get 3 + i32.ne + br_if 0 (;@1;) + local.get 0 + local.get 2 + local.get 1 + memory.copy + return + end + local.get 1 + local.get 1 + local.get 1 + call $core::slice::::copy_from_slice::len_mismatch_fail + unreachable + ) + (table (;0;) 1 1 funcref) + (memory (;0;) 17) + (global $__stack_pointer (;0;) (mut i32) i32.const 1048576) + (export "memory" (memory 0)) + (export "entrypoint" (func $entrypoint)) + (data $.rodata (;0;) (i32.const 1048576) "~/sdk/prelude/src/stdlib/crypto/hashes.rs\00\00\00\00\00\10\00)\00\00\00\d1\00\00\00(\00\00\00") +) \ No newline at end of file diff --git a/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat b/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat index 08edfd54a..2495d0592 100644 --- a/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat +++ b/tests/integration/expected/rust_sdk_account_test/miden_sdk_account_test.wat @@ -46,8 +46,8 @@ (import "miden:prelude/intrinsics_felt" "div" (func $miden_prelude::intrinsics::felt::extern_div (;16;) (type 1))) (import "miden:prelude/intrinsics_felt" "assert_eq" (func $miden_prelude::intrinsics::felt::extern_assert_eq (;17;) (type 7))) (import "miden:prelude/intrinsics_felt" "neg" (func $miden_prelude::intrinsics::felt::extern_neg (;18;) (type 6))) - (import "miden:prelude/std_crypto_hashes" "blake3_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;19;) (type 8))) - (import "miden:prelude/std_crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;20;) (type 9))) + (import "std::crypto_hashes" "blake3_hash_1to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_1to1 (;19;) (type 8))) + (import "std::crypto_hashes" "blake3_hash_2to1<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::hashes::extern_blake3_hash_2to1 (;20;) (type 9))) (import "miden:prelude/std_crypto_dsa" "rpo_falcon512_verify<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_prelude::stdlib::crypto::dsa::extern_rpo_falcon512_verify (;21;) (type 10))) (import "miden:tx_kernel/account" "get_id<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_account_get_id (;22;) (type 11))) (import "miden:tx_kernel/note" "get_inputs<0x0000000000000000000000000000000000000000000000000000000000000000>" (func $miden_sdk_tx_kernel::externs::extern_note_get_inputs (;23;) (type 12))) @@ -1900,5 +1900,5 @@ (export "test_pipe_double_words_to_memory" (func $test_pipe_double_words_to_memory)) (export "test_remove_asset" (func $test_remove_asset)) (export "test_create_note" (func $test_create_note)) - (data $.rodata (;0;) (i32.const 1048576) "~/sdk/prelude/src/stdlib/crypto/hashes.rs\00\00\00\00\00\10\00)\00\00\00\8a\00\00\00(\00\00\00\00\00\10\00)\00\00\00\d0\00\00\00(\00\00\00") + (data $.rodata (;0;) (i32.const 1048576) "~/sdk/prelude/src/stdlib/crypto/hashes.rs\00\00\00\00\00\10\00)\00\00\00\8b\00\00\00(\00\00\00\00\00\10\00)\00\00\00\d1\00\00\00(\00\00\00") ) \ No newline at end of file diff --git a/tests/integration/src/compiler_test.rs b/tests/integration/src/compiler_test.rs index 0d272c074..c2971e193 100644 --- a/tests/integration/src/compiler_test.rs +++ b/tests/integration/src/compiler_test.rs @@ -357,7 +357,7 @@ impl CompilerTest { } /// Set the Rust source code to compile with `miden-prelude` (stdlib + intrinsics) - pub fn rust_fn_body_with_prelude(name: &str, rust_source: &str) -> Self { + pub fn rust_fn_body_with_prelude(name: &str, rust_source: &str, is_build_std: bool) -> Self { let cwd = std::env::current_dir().unwrap(); let miden_prelude_path = cwd.parent().unwrap().parent().unwrap().join("sdk").join("prelude"); @@ -416,7 +416,8 @@ impl CompilerTest { .as_str(), ) .build(); - let test = Self::rust_source_cargo_lib(proj.root(), false, Some("entrypoint".to_string())); + let test = + Self::rust_source_cargo_lib(proj.root(), is_build_std, Some("entrypoint".to_string())); test } diff --git a/tests/integration/src/rust_masm_tests/abi_transform/mod.rs b/tests/integration/src/rust_masm_tests/abi_transform/mod.rs new file mode 100644 index 000000000..d9366b9fb --- /dev/null +++ b/tests/integration/src/rust_masm_tests/abi_transform/mod.rs @@ -0,0 +1 @@ +mod stdlib; diff --git a/tests/integration/src/rust_masm_tests/abi_transform/stdlib.rs b/tests/integration/src/rust_masm_tests/abi_transform/stdlib.rs new file mode 100644 index 000000000..fb3285002 --- /dev/null +++ b/tests/integration/src/rust_masm_tests/abi_transform/stdlib.rs @@ -0,0 +1,38 @@ +use core::panic; + +use expect_test::expect_file; +use proptest::{ + arbitrary::any, + test_runner::{TestError, TestRunner}, +}; + +use crate::CompilerTest; + +#[test] +fn test_blake3_hash() { + let main_fn = format!( + "(a: [u8; 32], b: [u8; 32]) -> [u8; 32] {{ miden_prelude::blake3_hash_2to1(a, b) }}" + ); + let artifact_name = "abi_transform_stdlib_blake3_hash"; + let mut test = CompilerTest::rust_fn_body_with_prelude(&artifact_name, &main_fn, true); + // Test expected compilation artifacts + test.expect_wasm(expect_file![format!("../../../expected/{artifact_name}.wat")]); + test.expect_ir(expect_file![format!("../../../expected/{artifact_name}.hir")]); + test.expect_masm(expect_file![format!("../../../expected/{artifact_name}.masm")]); + // let ir_masm = test.ir_masm_program(); + // let vm_program = test.vm_masm_program(); + + // Run the Rust and compiled MASM code against a bunch of random inputs and compare the results + let res = + TestRunner::default().run(&(any::<[u8; 32]>(), any::<[u8; 32]>()), move |(_a, _b)| { + todo!("test against rust"); + // run_masm_vs_rust(rs_out, &vm_program, ir_masm.clone(), &args) + }); + match res { + Err(TestError::Fail(_, value)) => { + panic!("Found minimal(shrinked) failing case: {:?}", value); + } + Ok(_) => (), + _ => panic!("Unexpected test result: {:?}", res), + } +} diff --git a/tests/integration/src/rust_masm_tests/intrinsics.rs b/tests/integration/src/rust_masm_tests/intrinsics.rs index aa7a76a85..d3b631e42 100644 --- a/tests/integration/src/rust_masm_tests/intrinsics.rs +++ b/tests/integration/src/rust_masm_tests/intrinsics.rs @@ -20,7 +20,7 @@ macro_rules! test_bin_op { let res_ty_str = stringify!($res_ty); let main_fn = format!("(a: {op_ty_str}, b: {op_ty_str}) -> {res_ty_str} {{ a {op_str} b }}"); let artifact_name = format!("{}_{}", stringify!($name), stringify!($op_ty).to_lowercase()); - let mut test = CompilerTest::rust_fn_body_with_prelude(&artifact_name, &main_fn); + let mut test = CompilerTest::rust_fn_body_with_prelude(&artifact_name, &main_fn, false); // Test expected compilation artifacts test.expect_wasm(expect_file![format!("../../expected/{artifact_name}.wat")]); test.expect_ir(expect_file![format!("../../expected/{artifact_name}.hir")]); @@ -60,7 +60,7 @@ macro_rules! test_compile_comparison_op { let op_str = stringify!($op); let main_fn = format!("(a: Felt, b: Felt) -> bool {{ a {op_str} b }}"); let artifact_name = format!("{}_felt", stringify!($name)); - let mut test = CompilerTest::rust_fn_body_with_prelude(&artifact_name, &main_fn); + let mut test = CompilerTest::rust_fn_body_with_prelude(&artifact_name, &main_fn, false); // Test expected compilation artifacts test.expect_wasm(expect_file![format!("../../expected/{artifact_name}.wat")]); test.expect_ir(expect_file![format!("../../expected/{artifact_name}.hir")]); diff --git a/tests/integration/src/rust_masm_tests/mod.rs b/tests/integration/src/rust_masm_tests/mod.rs index 1ecfae127..fbb64eb82 100644 --- a/tests/integration/src/rust_masm_tests/mod.rs +++ b/tests/integration/src/rust_masm_tests/mod.rs @@ -5,6 +5,7 @@ use proptest::{prop_assert_eq, test_runner::TestCaseError}; use crate::{execute_vm, felt_conversion::TestFelt}; +mod abi_transform; mod apps; mod components; mod instructions;