From 4dc261055716972b4f9816e71bbcc1df0af2850a Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Wed, 1 Mar 2023 12:59:06 +0100 Subject: [PATCH] Bindings "filesystem" (#225) * bindings structure * missed some renaming * add back the headers * path fixes * need to sleep at night * windows path mystery is unfathomable --- bindings/README.md | 7 ++++ constantine.nimble | 38 +++++++++---------- examples_c/README.md | 5 +++ .../t_libctt_bls12_381.c | 11 ++++-- include/README.md | 5 +++ .../constantine_bls12_381.h | 0 .../generated => include}/constantine_pasta.h | 0 lib/README.md | 5 +++ 8 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 bindings/README.md create mode 100644 examples_c/README.md rename {tests/bindings => examples_c}/t_libctt_bls12_381.c (97%) create mode 100644 include/README.md rename {bindings/generated => include}/constantine_bls12_381.h (100%) rename {bindings/generated => include}/constantine_pasta.h (100%) create mode 100644 lib/README.md diff --git a/bindings/README.md b/bindings/README.md new file mode 100644 index 00000000..fe61b443 --- /dev/null +++ b/bindings/README.md @@ -0,0 +1,7 @@ +# Bindings generator + +This folder holds the bindings generators for Constantine. + +To create a new build, download install the [Nim programming language](https://nim-lang.org/install.html), navigate to Constantine's root folder and call `nimble bindings`. + +Headers will be placed in "include" folder and static and dynamic libraries in "lib" \ No newline at end of file diff --git a/constantine.nimble b/constantine.nimble index 41199e76..e690b7ec 100644 --- a/constantine.nimble +++ b/constantine.nimble @@ -468,12 +468,12 @@ proc genDynamicBindings(bindingsName, prefixNimMain: string) = # In the future, Constantine might use: # - heap-allocated sequences and objects manually managed or managed by destructors for multithreading. # - heap-allocated strings for hex-string or decimal strings - echo "Compiling dynamic library: bindings/generated/" & libName + echo "Compiling dynamic library: lib/" & libName exec "nim c -f " & flags & " --noMain -d:danger --app:lib --gc:arc " & " --panics:on " & # Defects are not catchable " --verbosity:0 --hints:off --warnings:off " & " --nimMainPrefix:" & prefixNimMain & - " --out:" & libName & " --outdir:bindings/generated " & + " --out:" & libName & " --outdir:lib " & " --nimcache:nimcache/bindings/" & bindingsName & " bindings/" & bindingsName & ".nim" @@ -483,9 +483,9 @@ proc genDynamicBindings(bindingsName, prefixNimMain: string) = elif defined(macosx): compile "lib" & bindingsName & ".dylib.arm", "--cpu:arm64 -l:'-target arm64-apple-macos11' -t:'-target arm64-apple-macos11'" compile "lib" & bindingsName & ".dylib.x64", "--cpu:amd64 -l:'-target x86_64-apple-macos10.12' -t:'-target x86_64-apple-macos10.12'" - exec "lipo bindings/generated/lib" & bindingsName & ".dylib.arm " & - " bindings/generated/lib" & bindingsName & ".dylib.x64 " & - " -output bindings/generated/lib" & bindingsName & ".dylib -create" + exec "lipo lib/lib" & bindingsName & ".dylib.arm " & + " lib/lib" & bindingsName & ".dylib.x64 " & + " -output lib/lib" & bindingsName & ".dylib -create" else: compile "lib" & bindingsName & ".so" @@ -499,12 +499,12 @@ proc genStaticBindings(bindingsName, prefixNimMain: string) = # In the future, Constantine might use: # - heap-allocated sequences and objects manually managed or managed by destructors for multithreading. # - heap-allocated strings for hex-string or decimal strings - echo "Compiling static library: bindings/generated/" & libName + echo "Compiling static library: lib/" & libName exec "nim c -f " & flags & " --noMain -d:danger --app:staticLib --gc:arc " & " --panics:on " & # Defects are not catchable " --verbosity:0 --hints:off --warnings:off " & " --nimMainPrefix:" & prefixNimMain & - " --out:" & libName & " --outdir:bindings/generated " & + " --out:" & libName & " --outdir:lib " & " --nimcache:nimcache/bindings/" & bindingsName & " bindings/" & bindingsName & ".nim" @@ -514,21 +514,21 @@ proc genStaticBindings(bindingsName, prefixNimMain: string) = elif defined(macosx): compile "lib" & bindingsName & ".a.arm", "--cpu:arm64 -l:'-target arm64-apple-macos11' -t:'-target arm64-apple-macos11'" compile "lib" & bindingsName & ".a.x64", "--cpu:amd64 -l:'-target x86_64-apple-macos10.12' -t:'-target x86_64-apple-macos10.12'" - exec "lipo bindings/generated/lib" & bindingsName & ".a.arm " & - " bindings/generated/lib" & bindingsName & ".a.x64 " & - " -output bindings/generated/lib" & bindingsName & ".a -create" + exec "lipo lib/lib" & bindingsName & ".a.arm " & + " lib/lib" & bindingsName & ".a.x64 " & + " -output lib/lib" & bindingsName & ".a -create" else: compile "lib" & bindingsName & ".a" proc genHeaders(bindingsName: string) = - echo "Generating header: bindings/generated/" & bindingsName & ".h" + echo "Generating header: include/" & bindingsName & ".h" exec "nim c -d:release -d:CttGenerateHeaders " & " --verbosity:0 --hints:off --warnings:off " & - " --out:" & bindingsName & "_gen_header.exe --outdir:bindings/generated " & + " --out:" & bindingsName & "_gen_header.exe --outdir:build " & " --nimcache:nimcache/bindings/" & bindingsName & "_header" & " bindings/" & bindingsName & ".nim" - exec "bindings/generated/" & bindingsName & "_gen_header.exe bindings/generated" + exec "build/" & bindingsName & "_gen_header.exe include" proc genParallelCmdRunner() = exec "nim c --verbosity:0 --hints:off --warnings:off -d:release --out:build/pararun --nimcache:nimcache/pararun helpers/pararun.nim" @@ -549,23 +549,23 @@ task test_bindings, "Test C bindings": exec "mkdir -p build/testsuite" echo "--> Testing dynamically linked library" when not defined(windows): - exec "gcc -Ibindings/generated -Lbindings/generated -o build/testsuite/t_libctt_bls12_381_dl tests/bindings/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381" - exec "LD_LIBRARY_PATH=bindings/generated ./build/testsuite/t_libctt_bls12_381_dl" + exec "gcc -Iinclude -Llib -o build/testsuite/t_libctt_bls12_381_dl examples_c/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381" + exec "LD_LIBRARY_PATH=lib ./build/testsuite/t_libctt_bls12_381_dl" else: # Put DLL near the exe as LD_LIBRARY_PATH doesn't work even in an POSIX compatible shell - exec "gcc -Ibindings/generated -Lbindings/generated -o build/testsuite/t_libctt_bls12_381_dl.exe tests/bindings/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381" + exec "gcc -Iinclude -Llib -o build/testsuite/t_libctt_bls12_381_dl.exe examples_c/t_libctt_bls12_381.c -lgmp -lconstantine_bls12_381" exec "./build/testsuite/t_libctt_bls12_381_dl.exe" echo "--> Testing statically linked library" when not defined(windows): # Beware MacOS annoying linker with regards to static libraries # The following standard way cannot be used on MacOS - # exec "gcc -Ibindings/generated -Lbindings/generated -o build/t_libctt_bls12_381_sl.exe tests/bindings/t_libctt_bls12_381.c -lgmp -Wl,-Bstatic -lconstantine_bls12_381 -Wl,-Bdynamic" + # exec "gcc -Iinclude -Llib -o build/t_libctt_bls12_381_sl.exe examples_c/t_libctt_bls12_381.c -lgmp -Wl,-Bstatic -lconstantine_bls12_381 -Wl,-Bdynamic" - exec "gcc -Ibindings/generated -o build/testsuite/t_libctt_bls12_381_sl tests/bindings/t_libctt_bls12_381.c bindings/generated/libconstantine_bls12_381.a -lgmp" + exec "gcc -Iinclude -o build/testsuite/t_libctt_bls12_381_sl examples_c/t_libctt_bls12_381.c lib/libconstantine_bls12_381.a -lgmp" exec "./build/testsuite/t_libctt_bls12_381_sl" else: - exec "gcc -Ibindings/generated -o build/testsuite/t_libctt_bls12_381_sl.exe tests/bindings/t_libctt_bls12_381.c bindings/generated/constantine_bls12_381.lib -lgmp" + exec "gcc -Iinclude -o build/testsuite/t_libctt_bls12_381_sl.exe examples_c/t_libctt_bls12_381.c lib/constantine_bls12_381.lib -lgmp" exec "./build/testsuite/t_libctt_bls12_381_sl.exe" task test, "Run all tests": diff --git a/examples_c/README.md b/examples_c/README.md new file mode 100644 index 00000000..21bb2057 --- /dev/null +++ b/examples_c/README.md @@ -0,0 +1,5 @@ +# C bindings examples + +This folder holds tests (prefixed with `t_`) and examples for the C bindings. + +Headers are located in `include/` and the static and dynamic libraries in `lib/` \ No newline at end of file diff --git a/tests/bindings/t_libctt_bls12_381.c b/examples_c/t_libctt_bls12_381.c similarity index 97% rename from tests/bindings/t_libctt_bls12_381.c rename to examples_c/t_libctt_bls12_381.c index 8f9692dd..ecd61c65 100644 --- a/tests/bindings/t_libctt_bls12_381.c +++ b/examples_c/t_libctt_bls12_381.c @@ -6,6 +6,9 @@ // * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). // at your option. This file may not be copied, modified, or distributed except according to those terms. +// This is a test to ensure Constantine's modular arithmetic is consistent with GMP. +// While not intended as a tutorial, it showcases serialization, deserialization and computation. + #include #include #include @@ -34,7 +37,7 @@ void prologue( mpz_ptr p, bls12381_fp* a_ctt, bls12381_fp* b_ctt, byte a_buf[ByteLength], byte b_buf[ByteLength]) { - + // Generate random value in the range 0 ..< 2^(bits-1) mpz_urandomb(a, gmp_rng, BitLength); mpz_urandomb(b, gmp_rng, BitLength); @@ -65,7 +68,7 @@ void epilogue( mpz_ptr r, mpz_ptr a, mpz_ptr b, bls12381_fp* r_ctt, bls12381_fp* a_ctt, bls12381_fp* b_ctt, char* operation) { - + byte r_raw_gmp[ByteLength]; byte r_raw_ctt[ByteLength]; @@ -98,10 +101,10 @@ void epilogue( } int main(){ - + // Initialize the runtime. For Constantine, it populates CPU runtime detection dispatch. ctt_bls12381_init_NimMain(); - + gmp_randstate_t gmpRng; gmp_randinit_mt(gmpRng); // The GMP seed varies between run so that diff --git a/include/README.md b/include/README.md new file mode 100644 index 00000000..6c7f44ed --- /dev/null +++ b/include/README.md @@ -0,0 +1,5 @@ +# Headers + +This folder holds the generated C headers for Constantine. + +To create a new build, download install the [Nim programming language](https://nim-lang.org/install.html), navigate to Constantine's root folder and call `nimble bindings`. \ No newline at end of file diff --git a/bindings/generated/constantine_bls12_381.h b/include/constantine_bls12_381.h similarity index 100% rename from bindings/generated/constantine_bls12_381.h rename to include/constantine_bls12_381.h diff --git a/bindings/generated/constantine_pasta.h b/include/constantine_pasta.h similarity index 100% rename from bindings/generated/constantine_pasta.h rename to include/constantine_pasta.h diff --git a/lib/README.md b/lib/README.md new file mode 100644 index 00000000..350c9550 --- /dev/null +++ b/lib/README.md @@ -0,0 +1,5 @@ +# Libraries + +This folder holds the generated dynamic libraries (.so on Linux, .dylib on MacOS, .dll on Windows) and static libraries (.a on Linux and Mac, .dll on Windows). + +To create a new build, download install the [Nim programming language](https://nim-lang.org/install.html), navigate to Constantine's root folder and call `nimble bindings`. \ No newline at end of file