Skip to content

Commit 3c12828

Browse files
committed
Merge #22: Simplicity bindings
66135ce Add bindings for DAGs and tests (Christian Lewe) 33994ea Update .gitignore (Christian Lewe) 2f0d52c Add simplicity-sys (Christian Lewe) Pull request description: Exposes part of [Simplicity's C API](https://github.com/ElementsProject/simplicity/tree/master/C) in Rust. At the moment, one can decode Simplicity DAGs from bytes, and one can access test programs with hardcoded bytes and their correct Merkle roots. # Todo - [x] Discussion: use minimal set of necessary C files instead of entire library? - [x] Discussion: use C test files? - [x] ~~test CMRs and IMRs against C~~ [postponed to later PR due to outdated jets] - [x] Make sure that FFI is safe and that pointers are freed ACKs for top commit: apoelstra: ACK 66135ce sanket1729: utACK 66135ce. The build script seems fragile. We should have some vendoring-like script that updates the underlying library when anything changes. Tree-SHA512: 6a0308cb9c4b79fd8274dd0d4bc318e917de0644c25f6d008c5a9ca6a579561de28c8b07b40e9ecf274ed36f867b0c1833a1e3a537b9a04b6545671fe785306d
2 parents b16f170 + 66135ce commit 3c12828

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+33713
-2
lines changed

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/target
2-
/fuzz/target
1+
target
2+
fuzz/target
33
**/*.rs.bk
44
*~
55
Cargo.lock
@@ -17,3 +17,8 @@ fuzz/hfuzz_workspace
1717

1818
#Vscode project files
1919
.vscode
20+
21+
#Compiled C
22+
*.o
23+
*.a
24+
*.lib

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ bitcoin_hashes = "0.10"
1919
byteorder = "1.3"
2020
elements = { version = "0.19", optional = true }
2121
miniscript = "7.0"
22+
23+
[dev-dependencies]
24+
simplicity_sys = { version = "0.1.0", path = "./simplicity-sys" }

simplicity-sys/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "simplicity_sys"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[build-dependencies]
9+
cc = "1.0"
10+
11+
[dependencies]
12+
libc = "0.2"

simplicity-sys/build.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
extern crate cc;
2+
3+
use std::path::Path;
4+
5+
fn main() {
6+
let simplicity_path = Path::new("depend/simplicity");
7+
let files: Vec<_> = vec![
8+
"bitstream.c",
9+
"dag.c",
10+
"deserialize.c",
11+
"eval.c",
12+
"frame.c",
13+
"jets.c",
14+
"jets-secp256k1.c",
15+
"rsort.c",
16+
"sha256.c",
17+
"type.c",
18+
"typeInference.c",
19+
"primitive/elements/env.c",
20+
"primitive/elements/exec.c",
21+
"primitive/elements/jets.c",
22+
"primitive/elements/primitive.c",
23+
]
24+
.into_iter()
25+
.map(|x| simplicity_path.join(x))
26+
.collect();
27+
let test_files: Vec<_> = vec![
28+
// "test.c",
29+
"hashBlock.c",
30+
"schnorr0.c",
31+
"schnorr6.c",
32+
"primitive/elements/checkSigHashAllTx1.c",
33+
]
34+
.into_iter()
35+
.map(|x| simplicity_path.join(x))
36+
.collect();
37+
let include = simplicity_path.join("include");
38+
39+
cc::Build::new()
40+
.files(files)
41+
.files(test_files)
42+
.include(include)
43+
.compile("simplicity");
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
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
2+
TEST_OBJS := test.o hashBlock.o schnorr0.o schnorr6.o primitive/elements/checkSigHashAllTx1.o
3+
4+
# From https://fastcompression.blogspot.com/2019/01/compiler-warnings.html
5+
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
6+
7+
ifneq ($(doCheck), 1)
8+
CPPFLAGS := $(CPPFLAGS) -DNDEBUG
9+
endif
10+
11+
ifneq ($(strip $(SINGLE_THREADED)),)
12+
# SINGLE_THREADED is non-empty
13+
CPPFLAGS := $(CPPFLAGS) -DSINGLE_THREADED
14+
endif
15+
16+
CFLAGS := -I include
17+
18+
ifeq ($(strip $(SINGLE_THREADED)),)
19+
# SINGLE_THREADED is empty
20+
LDFLAGS := -pthread
21+
endif
22+
23+
# libsecp256k1 is full of conversion warnings, so we compile jets-secp256k1.c separately.
24+
jets-secp256k1.o: jets-secp256k1.c
25+
$(CC) -c $(CFLAGS) $(CWARN) -Wno-conversion $(CPPFLAGS) -o $@ $<
26+
27+
primitive/elements/jets.o: primitive/elements/jets.c
28+
$(CC) -c $(CFLAGS) $(CWARN) -Wno-switch-enum -Wswitch $(CPPFLAGS) -o $@ $<
29+
30+
%.o: %.c
31+
$(CC) -c $(CFLAGS) $(CWARN) $(CPPFLAGS) -o $@ $<
32+
33+
libElementsSimplicity.a: $(OBJS)
34+
ar rcs $@ $^
35+
36+
test: $(TEST_OBJS) libElementsSimplicity.a
37+
$(CC) $^ -o $@ $(LDFLAGS)
38+
39+
install: libElementsSimplicity.a
40+
mkdir -p $(out)/lib
41+
cp $^ $(out)/lib/
42+
cp -R include $(out)/include
43+
44+
check: test
45+
./test
46+
47+
clean:
48+
-rm -f test libElementsSimplicity.a $(TEST_OBJS) $(OBJS)
49+
50+
.PHONY: install check clean
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
_Static_assert(
2+
0x20 == ' ' && 0x40 == '@' && 0x60 == '`' &&
3+
0x21 == '!' && 0x41 == 'A' && 0x61 == 'a' &&
4+
0x22 == '"' && 0x42 == 'B' && 0x62 == 'b' &&
5+
0x23 == '#' && 0x43 == 'C' && 0x63 == 'c' &&
6+
0x24 == '$' && 0x44 == 'D' && 0x64 == 'd' &&
7+
0x25 == '%' && 0x45 == 'E' && 0x65 == 'e' &&
8+
0x26 == '&' && 0x46 == 'F' && 0x66 == 'f' &&
9+
0x27 == '\'' && 0x47 == 'G' && 0x67 == 'g' &&
10+
0x28 == '(' && 0x48 == 'H' && 0x68 == 'h' &&
11+
0x29 == ')' && 0x49 == 'I' && 0x69 == 'i' &&
12+
0x2a == '*' && 0x4a == 'J' && 0x6a == 'j' &&
13+
0x2b == '+' && 0x4b == 'K' && 0x6b == 'k' &&
14+
0x2c == ',' && 0x4c == 'L' && 0x6c == 'l' &&
15+
0x2d == '-' && 0x4d == 'M' && 0x6d == 'm' &&
16+
0x2e == '.' && 0x4e == 'N' && 0x6e == 'n' &&
17+
0x2f == '/' && 0x4f == 'O' && 0x6f == 'o' &&
18+
0x30 == '0' && 0x50 == 'P' && 0x70 == 'p' &&
19+
0x31 == '1' && 0x51 == 'Q' && 0x71 == 'q' &&
20+
0x32 == '2' && 0x52 == 'R' && 0x72 == 'r' &&
21+
0x33 == '3' && 0x53 == 'S' && 0x73 == 's' &&
22+
0x34 == '4' && 0x54 == 'T' && 0x74 == 't' &&
23+
0x35 == '5' && 0x55 == 'U' && 0x75 == 'u' &&
24+
0x36 == '6' && 0x56 == 'V' && 0x76 == 'v' &&
25+
0x37 == '7' && 0x57 == 'W' && 0x77 == 'w' &&
26+
0x38 == '8' && 0x58 == 'X' && 0x78 == 'x' &&
27+
0x39 == '9' && 0x59 == 'Y' && 0x79 == 'y' &&
28+
0x3a == ':' && 0x5a == 'Z' && 0x7a == 'z' &&
29+
0x3b == ';' && 0x5b == '[' && 0x7b == '{' &&
30+
0x3c == '<' && 0x5c == '\\' && 0x7c == '|' &&
31+
0x3d == '=' && 0x5d == ']' && 0x7d == '}' &&
32+
0x3e == '>' && 0x5e == '^' && 0x7e == '~' &&
33+
0x3f == '?' && 0x5f == '_'
34+
, "ASCII character set required");

0 commit comments

Comments
 (0)