From fdd1305c0c3cf59f9afa06834f11447bb731957c Mon Sep 17 00:00:00 2001 From: Alejandro Cabeza Romero Date: Wed, 22 Apr 2026 12:05:32 +0200 Subject: [PATCH] Add ffi smoke test. --- blend/test_ffi.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++ justfile | 22 ++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 blend/test_ffi.cpp diff --git a/blend/test_ffi.cpp b/blend/test_ffi.cpp new file mode 100644 index 0000000..16755a0 --- /dev/null +++ b/blend/test_ffi.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include "poq/ffi.hpp" + +static uint8_t* read_file(const char* path, size_t* out_size) { + FILE* f = fopen(path, "rb"); + if (!f) return nullptr; + fseek(f, 0, SEEK_END); + *out_size = ftell(f); + rewind(f); + uint8_t* buf = (uint8_t*)malloc(*out_size + 1); + fread(buf, 1, *out_size, f); + buf[*out_size] = '\0'; + fclose(f); + return buf; +} + +int main() { + Status status = poq_generate_witness_from_files( + "poq", + "../input.json", + "witness.wtns" + ); + + if (status_is_error(status)) { + fprintf(stderr, "Error [%d]: %s\n", status.code, status.message); + return 1; + } + + printf("generate_witness_from_files: OK\n"); + + size_t dat_size, json_size; + uint8_t* dat_data = read_file("poq.dat", &dat_size); + uint8_t* json_data = read_file("../input.json", &json_size); + + WitnessInput input = { + {dat_data, dat_size}, + (const char*)json_data + }; + Bytes output = {nullptr, 0}; + + status = poq_generate_witness(&input, &output); + + free(dat_data); + free(json_data); + + if (status_is_error(status)) { + fprintf(stderr, "Error [%d]: %s\n", status.code, status.message); + return 1; + } + + size_t wtns_size; + uint8_t* wtns_data = read_file("witness.wtns", &wtns_size); + assert(wtns_data != nullptr); + assert(output.size == wtns_size); + assert(memcmp(output.data, wtns_data, output.size) == 0); + free(wtns_data); + + printf("generate_witness: OK (%zu bytes, matches witness.wtns)\n", output.size); + free_bytes(&output); + return 0; +} diff --git a/justfile b/justfile index 85f4744..7c105f6 100644 --- a/justfile +++ b/justfile @@ -1,3 +1,6 @@ +src := justfile_directory() + "/src" +ci_makefile := justfile_directory() + "/.github/resources/witness-generator/Makefile" + prettify: nix shell nixpkgs#clang-tools -c clang-format -i src/**.cpp src/**.hpp @@ -10,3 +13,22 @@ sage-run script +args='': -v "{{justfile_directory()}}:/work" \ -w "/work/$(dirname '{{script}}')" \ sagemath/sagemath sage "$(basename '{{script}}')" {{args}} + +# Build the PoQ circuit and its C++ witness generator, equivalent to the CI build. +poq: + circom blend/poq.circom --c --r1cs --no_asm --O2 --output blend + # circom-generated main() has no return on the success path; patch it before -O3 turns it into an infinite loop + sed -i ':a;N;$!ba;s/\n}\n\n*$/\n return 0;\n}/' blend/poq_cpp/main.cpp + cp -r {{src}}/poq blend/poq_cpp/poq + cp {{src}}/circom_adapter.cpp {{src}}/circom_adapter.hpp {{src}}/circom_fwd.hpp {{src}}/types.hpp blend/poq_cpp/ + cp {{ci_makefile}} blend/poq_cpp/Makefile + cp blend/test_ffi.cpp blend/poq_cpp/test_ffi.cpp + make -C blend/poq_cpp PROJECT=poq linux-lib + +# Run a simple smoke test of the PoQ witness generator. +test-poq: poq + g++ -std=c++11 -O3 -I blend/poq_cpp blend/poq_cpp/test_ffi.cpp -L blend/poq_cpp -lwitness_poq -lgmp -o blend/poq_cpp/test_ffi + cd blend/poq_cpp && ./test_ffi + +clean: + rm -rf blend/poq_cpp