Skip to content

Commit

Permalink
Merge #205: WASM FFI 2
Browse files Browse the repository at this point in the history
2b95398 Cargo: Fix genrandom in WASM (Christian Lewe)
ba4c0fa Cargo: Remove libc (Christian Lewe)
e77330c FFI: Allocate in Rust (Christian Lewe)
fec1835 FFI: Use local types (Christian Lewe)
c7f8a08 FFI: Define uint_fast*_t types (Christian Lewe)
3fd64d3 FFI: Update build script (Christian Lewe)
63522a7 FFI: Split jet and test bindings (Christian Lewe)
cfb9a33 Update libsimplicity (Christian Lewe)

Pull request description:

  Make rust-simplicity compile in WASM.

  Move test-only FFI bindings into separate module, separating them from jet-only FFI bindings. The latter compile in WASM while the former don't.

  Define more C types (checked in WASM) and use these local types. Remove libc dependency entirely.

  Redefine allocation in Rust.

ACKs for top commit:
  apoelstra:
    ACK 2b95398

Tree-SHA512: 05eb734dcd902e742ce20ca7cdb7b661f9558d66f26d28e927b21304581c0d4eb763cffb611a4335c378de55d07498d265bd224ffe320a10b3725cf78ad16df7
  • Loading branch information
apoelstra committed Mar 17, 2024
2 parents 25d8dcc + 2b95398 commit e8cfeeb
Show file tree
Hide file tree
Showing 43 changed files with 1,248 additions and 663 deletions.
85 changes: 84 additions & 1 deletion Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ dependencies = [
"serde",
]

[[package]]
name = "bumpalo"
version = "3.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"

[[package]]
name = "byteorder"
version = "1.4.3"
Expand Down Expand Up @@ -92,8 +98,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
dependencies = [
"cfg-if",
"js-sys",
"libc",
"wasi",
"wasm-bindgen",
]

[[package]]
Expand Down Expand Up @@ -125,6 +133,15 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"

[[package]]
name = "js-sys"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [
"wasm-bindgen",
]

[[package]]
name = "lazy_static"
version = "1.4.0"
Expand All @@ -137,6 +154,12 @@ version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"

[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"

[[package]]
name = "memmap2"
version = "0.5.10"
Expand All @@ -157,6 +180,12 @@ dependencies = [
"bitcoin-internals",
]

[[package]]
name = "once_cell"
version = "1.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9670a07f94779e00908f3e686eab508878ebb390ba6e604d3a284c00e8d0487b"

[[package]]
name = "ppv-lite86"
version = "0.2.17"
Expand Down Expand Up @@ -355,6 +384,7 @@ dependencies = [
"bitcoin_hashes",
"byteorder",
"elements",
"getrandom",
"hex-conservative",
"miniscript",
"santiago",
Expand All @@ -368,7 +398,6 @@ version = "0.2.0"
dependencies = [
"bitcoin_hashes",
"cc",
"libc",
]

[[package]]
Expand All @@ -393,3 +422,57 @@ name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

[[package]]
name = "wasm-bindgen"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]

[[package]]
name = "wasm-bindgen-backend"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]

[[package]]
name = "wasm-bindgen-macro"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]

[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]

[[package]]
name = "wasm-bindgen-shared"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ actual-serde = { package = "serde", version = "1.0.103", features = [
"derive",
], optional = true }

[target.wasm32-unknown-unknown.dependencies]
getrandom = { version = "0.2", features = ["js"] }

[dev-dependencies]
simplicity-sys = { version = "0.2.0", path = "./simplicity-sys", features = [
"test-utils",
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cargo-fuzz = true

[dependencies]
honggfuzz = { version = "0.5.55", default-features = false }
simplicity-lang = { path = ".." }
simplicity-lang = { path = "..", features = ["test-utils"] }

[[bin]]
name = "c_rust_merkle"
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/c_rust_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use honggfuzz::fuzz;

use simplicity::ffi::{self, ffi::SimplicityErr};
use simplicity::ffi::tests::{ffi::SimplicityErr, run_program, TestUpTo};
use simplicity::hashes::sha256::Midstate;
use simplicity::jet::Elements;
use simplicity::{BitIter, RedeemNode};

fn do_test(data: &[u8]) {
let c_result = ffi::tests::run_program(data, ffi::tests::TestUpTo::CheckOneOne);
let c_result = run_program(data, TestUpTo::CheckOneOne);

let mut iter = BitIter::from(data);
let rust_result = RedeemNode::<Elements>::decode(&mut iter);
Expand Down
1 change: 0 additions & 1 deletion simplicity-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ rust-version = "1.58.0"
cc = "1.0.83"

[dependencies]
libc = "0.2"
hashes = { package = "bitcoin_hashes", version = "0.13" }

[features]
Expand Down
49 changes: 26 additions & 23 deletions simplicity-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,15 @@ fn main() {
// rerun if changes to the C code
println!("cargo:rerun-if-changed=depend");
let simplicity_path = Path::new("depend/simplicity");
let files: Vec<_> = vec![
"bitstream.c",
"dag.c",
"deserialize.c",
"eval.c",
let jet_files: Vec<_> = vec![
"frame.c",
"jets.c",
"jets-secp256k1.c",
"rsort.c",
"sha256.c",
"type.c",
"typeInference.c",
"primitive/elements/env.c",
"primitive/elements/ops.c",
"primitive/elements/exec.c",
"primitive/elements/jets.c",
"primitive/elements/primitive.c",
]
.into_iter()
.map(|x| simplicity_path.join(x))
.collect();
let test_files: Vec<_> = vec![
// "test.c",
"ctx8Pruned.c",
"ctx8Unpruned.c",
"hashBlock.c",
"schnorr0.c",
"schnorr6.c",
"primitive/elements/checkSigHashAllTx1.c",
]
.into_iter()
.map(|x| simplicity_path.join(x))
Expand All @@ -46,13 +26,36 @@ fn main() {
.std("c11")
.flag_if_supported("-fno-inline-functions")
.opt_level(2)
.files(files)
.files(test_files)
.files(jet_files)
.file(Path::new("depend/wrapper.c"))
.file(Path::new("depend/env.c"))
.file(Path::new("depend/jets_wrapper.c"))
.include(simplicity_path.join("include"));

if cfg!(feature = "test-utils") {
let test_files: Vec<_> = vec![
"bitstream.c",
"dag.c",
"deserialize.c",
"eval.c",
"type.c",
"typeInference.c",
"primitive/elements/exec.c",
"primitive/elements/primitive.c",
"ctx8Pruned.c",
"ctx8Unpruned.c",
"hashBlock.c",
"schnorr0.c",
"schnorr6.c",
"primitive/elements/checkSigHashAllTx1.c",
]
.into_iter()
.map(|x| simplicity_path.join(x))
.collect();

build.files(test_files);
}

if cfg!(not(fuzzing)) {
build.define("PRODUCTION", None);
}
Expand Down
10 changes: 0 additions & 10 deletions simplicity-sys/depend/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,3 @@ void c_set_txEnv(txEnv *result, const transaction *tx, const tapEnv *taproot, co
sha256_toMidstate(genesis.s, genesisHash);
*result = build_txEnv(tx, taproot, &genesis, ix);
}

void c_free_tapEnv(tapEnv *env)
{
free(env);
}

void c_free_transaction(transaction *tx)
{
free(tx);
}
3 changes: 3 additions & 0 deletions simplicity-sys/depend/jets_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ WRAP_(ge_is_on_curve)
WRAP_(ge_negate)
WRAP_(gej_add)
WRAP_(gej_double)
WRAP_(gej_equiv)
WRAP_(gej_ge_add)
WRAP_(gej_ge_add_ex)
WRAP_(gej_ge_equiv)
WRAP_(gej_infinity)
WRAP_(gej_is_infinity)
WRAP_(gej_is_on_curve)
Expand Down Expand Up @@ -443,6 +445,7 @@ WRAP_(tapleaf_hash)
WRAP_(tapleaf_version)
WRAP_(tappath)
WRAP_(tappath_hash)
WRAP_(total_fee)
WRAP_(tx_hash)
WRAP_(tx_is_final)
WRAP_(tx_lock_distance)
Expand Down
2 changes: 1 addition & 1 deletion simplicity-sys/depend/simplicity-HEAD-revision.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# This file has been automatically generated.
9a1d681427b8b5384bafd758d0160c62c4344a8e
adb7be91acd1e91415b93a5b29c634c4447f7a50
1 change: 0 additions & 1 deletion simplicity-sys/depend/simplicity/bitstream.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "bitstream.h"

#include <limits.h>
#include <stdlib.h>
#include "simplicity_assert.h"

/* Closes a bitstream by consuming all remaining bits.
Expand Down
5 changes: 3 additions & 2 deletions simplicity-sys/depend/simplicity/dag.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "prefix.h"
#include "rsort.h"
#include "sha256.h"
#include "simplicity_alloc.h"
#include "uword.h"

/* Given a tag for a node, return the SHA-256 hash of its associated CMR tag.
Expand Down Expand Up @@ -573,7 +574,7 @@ simplicity_err fillWitnessData(dag_node* dag, type* type_dag, const size_t len,
*/
simplicity_err verifyNoDuplicateIdentityRoots(sha256_midstate* imr, const dag_node* dag, const type* type_dag, const size_t dag_len) {
simplicity_assert(0 < dag_len);
sha256_midstate* imr_buf = malloc((size_t)dag_len * sizeof(sha256_midstate));
sha256_midstate* imr_buf = simplicity_malloc((size_t)dag_len * sizeof(sha256_midstate));
if (!imr_buf) return SIMPLICITY_ERR_MALLOC;

computeIdentityMerkleRoot(imr_buf, dag, type_dag, dag_len);
Expand All @@ -582,7 +583,7 @@ simplicity_err verifyNoDuplicateIdentityRoots(sha256_midstate* imr, const dag_no

int result = hasDuplicates(imr_buf, dag_len);

free(imr_buf);
simplicity_free(imr_buf);

switch (result) {
case -1: return SIMPLICITY_ERR_MALLOC;
Expand Down
6 changes: 3 additions & 3 deletions simplicity-sys/depend/simplicity/deserialize.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "deserialize.h"

#include <limits.h>
#include <stdlib.h>
#include "limitations.h"
#include "primitive.h"
#include "simplicity_alloc.h"
#include "simplicity_assert.h"

/* Fetches 'len' 'uint32_t's from 'stream' into 'result'.
Expand Down Expand Up @@ -195,7 +195,7 @@ int32_t decodeMallocDag(dag_node** dag, combinator_counters* census, bitstream*
static_assert(DAG_LEN_MAX <= SIZE_MAX / sizeof(dag_node), "dag array too large.");
static_assert(1 <= DAG_LEN_MAX, "DAG_LEN_MAX is zero.");
static_assert(DAG_LEN_MAX - 1 <= UINT32_MAX, "dag array index does not fit in uint32_t.");
*dag = malloc((size_t)dagLen * sizeof(dag_node));
*dag = simplicity_malloc((size_t)dagLen * sizeof(dag_node));
if (!*dag) return SIMPLICITY_ERR_MALLOC;

if (census) *census = (combinator_counters){0};
Expand All @@ -210,7 +210,7 @@ int32_t decodeMallocDag(dag_node** dag, combinator_counters* census, bitstream*
if (IS_OK(error)) {
return dagLen;
} else {
free(*dag);
simplicity_free(*dag);
*dag = NULL;
return (int32_t)error;
}
Expand Down
Loading

0 comments on commit e8cfeeb

Please sign in to comment.