Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/target
/fuzz/target
target
fuzz/target
**/*.rs.bk
*~
Cargo.lock
Expand All @@ -17,3 +17,8 @@ fuzz/hfuzz_workspace

#Vscode project files
.vscode

#Compiled C
*.o
*.a
*.lib
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ bitcoin_hashes = "0.10"
byteorder = "1.3"
elements = { version = "0.19", optional = true }
miniscript = "7.0"

[dev-dependencies]
simplicity_sys = { version = "0.1.0", path = "./simplicity-sys" }
12 changes: 12 additions & 0 deletions simplicity-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "simplicity_sys"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
cc = "1.0"

[dependencies]
libc = "0.2"
44 changes: 44 additions & 0 deletions simplicity-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
extern crate cc;

use std::path::Path;

fn main() {
let simplicity_path = Path::new("depend/simplicity");
let files: Vec<_> = vec![
"bitstream.c",
"dag.c",
"deserialize.c",
"eval.c",
"frame.c",
"jets.c",
"jets-secp256k1.c",
"rsort.c",
"sha256.c",
"type.c",
"typeInference.c",
"primitive/elements/env.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![
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cdee8cb:

I don't think we need this array (or these files, for that matter).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah -- you have unit tests in -sys that use them. In this case I think you could gate this array with #[cfg(test)] so that it's not built when simplicity-sys is used as a dependency.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could reduce the built files to a minimum. For instance, in order to construct C-DAGs from binary, we definitely need decodeMallocDag() from dag.c.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this PR is to expose (1) a way to decode DAGs from bytes in C, and (2) test programs with hardcoded bytes and Merkle roots. This is why we need test_files. My original plan was to test Rust against C, but this has to be done in a future PR due to outdated jets.

// "test.c",
"hashBlock.c",
"schnorr0.c",
"schnorr6.c",
"primitive/elements/checkSigHashAllTx1.c",
]
.into_iter()
.map(|x| simplicity_path.join(x))
.collect();
let include = simplicity_path.join("include");

cc::Build::new()
.files(files)
.files(test_files)
.include(include)
.compile("simplicity");
}
50 changes: 50 additions & 0 deletions simplicity-sys/depend/simplicity/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
OBJS := bitstream.o dag.o deserialize.o eval.o frame.o jets.o jets-secp256k1.o rsort.o sha256.o type.o typeInference.o primitive/elements/env.o primitive/elements/exec.o primitive/elements/jets.o primitive/elements/primitive.o
TEST_OBJS := test.o hashBlock.o schnorr0.o schnorr6.o primitive/elements/checkSigHashAllTx1.o

# From https://fastcompression.blogspot.com/2019/01/compiler-warnings.html
CWARN := -Werror -Wall -Wextra -Wcast-qual -Wcast-align -Wstrict-aliasing -Wpointer-arith -Winit-self -Wshadow -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -Wfloat-equal -Wundef -Wconversion

ifneq ($(doCheck), 1)
CPPFLAGS := $(CPPFLAGS) -DNDEBUG
endif

ifneq ($(strip $(SINGLE_THREADED)),)
# SINGLE_THREADED is non-empty
CPPFLAGS := $(CPPFLAGS) -DSINGLE_THREADED
endif

CFLAGS := -I include

ifeq ($(strip $(SINGLE_THREADED)),)
# SINGLE_THREADED is empty
LDFLAGS := -pthread
endif

# libsecp256k1 is full of conversion warnings, so we compile jets-secp256k1.c separately.
jets-secp256k1.o: jets-secp256k1.c
$(CC) -c $(CFLAGS) $(CWARN) -Wno-conversion $(CPPFLAGS) -o $@ $<

primitive/elements/jets.o: primitive/elements/jets.c
$(CC) -c $(CFLAGS) $(CWARN) -Wno-switch-enum -Wswitch $(CPPFLAGS) -o $@ $<

%.o: %.c
$(CC) -c $(CFLAGS) $(CWARN) $(CPPFLAGS) -o $@ $<

libElementsSimplicity.a: $(OBJS)
ar rcs $@ $^

test: $(TEST_OBJS) libElementsSimplicity.a
$(CC) $^ -o $@ $(LDFLAGS)

install: libElementsSimplicity.a
mkdir -p $(out)/lib
cp $^ $(out)/lib/
cp -R include $(out)/include

check: test
./test

clean:
-rm -f test libElementsSimplicity.a $(TEST_OBJS) $(OBJS)

.PHONY: install check clean
34 changes: 34 additions & 0 deletions simplicity-sys/depend/simplicity/ascii.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
_Static_assert(
0x20 == ' ' && 0x40 == '@' && 0x60 == '`' &&
0x21 == '!' && 0x41 == 'A' && 0x61 == 'a' &&
0x22 == '"' && 0x42 == 'B' && 0x62 == 'b' &&
0x23 == '#' && 0x43 == 'C' && 0x63 == 'c' &&
0x24 == '$' && 0x44 == 'D' && 0x64 == 'd' &&
0x25 == '%' && 0x45 == 'E' && 0x65 == 'e' &&
0x26 == '&' && 0x46 == 'F' && 0x66 == 'f' &&
0x27 == '\'' && 0x47 == 'G' && 0x67 == 'g' &&
0x28 == '(' && 0x48 == 'H' && 0x68 == 'h' &&
0x29 == ')' && 0x49 == 'I' && 0x69 == 'i' &&
0x2a == '*' && 0x4a == 'J' && 0x6a == 'j' &&
0x2b == '+' && 0x4b == 'K' && 0x6b == 'k' &&
0x2c == ',' && 0x4c == 'L' && 0x6c == 'l' &&
0x2d == '-' && 0x4d == 'M' && 0x6d == 'm' &&
0x2e == '.' && 0x4e == 'N' && 0x6e == 'n' &&
0x2f == '/' && 0x4f == 'O' && 0x6f == 'o' &&
0x30 == '0' && 0x50 == 'P' && 0x70 == 'p' &&
0x31 == '1' && 0x51 == 'Q' && 0x71 == 'q' &&
0x32 == '2' && 0x52 == 'R' && 0x72 == 'r' &&
0x33 == '3' && 0x53 == 'S' && 0x73 == 's' &&
0x34 == '4' && 0x54 == 'T' && 0x74 == 't' &&
0x35 == '5' && 0x55 == 'U' && 0x75 == 'u' &&
0x36 == '6' && 0x56 == 'V' && 0x76 == 'v' &&
0x37 == '7' && 0x57 == 'W' && 0x77 == 'w' &&
0x38 == '8' && 0x58 == 'X' && 0x78 == 'x' &&
0x39 == '9' && 0x59 == 'Y' && 0x79 == 'y' &&
0x3a == ':' && 0x5a == 'Z' && 0x7a == 'z' &&
0x3b == ';' && 0x5b == '[' && 0x7b == '{' &&
0x3c == '<' && 0x5c == '\\' && 0x7c == '|' &&
0x3d == '=' && 0x5d == ']' && 0x7d == '}' &&
0x3e == '>' && 0x5e == '^' && 0x7e == '~' &&
0x3f == '?' && 0x5f == '_'
, "ASCII character set required");
Loading