Add ffi smoke test.

This commit is contained in:
Alejandro Cabeza Romero 2026-04-22 12:05:32 +02:00
parent bc58054585
commit fdd1305c0c
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
2 changed files with 86 additions and 0 deletions

64
blend/test_ffi.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#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;
}

View File

@ -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