From be3f0c22cf43c55e8ac43cf853c4cd89c7c6959a Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Fri, 30 Sep 2022 07:21:48 +0100 Subject: [PATCH] Play around with some testing of the min interface --- .gitignore | 6 ++-- min-bindings/python/Makefile | 11 ++++++ min-bindings/python/tests.py | 67 ++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 min-bindings/python/Makefile create mode 100644 min-bindings/python/tests.py diff --git a/.gitignore b/.gitignore index 239cc39..b117d3d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,7 @@ inc/blst_aux.h* .vscode/ *.json .clang-format -bindings/*/_*.so -bindings/python/ckzg.py -bindings/python/c_kzg_wrap.c +*bindings/*/_*.so +*bindings/python/ckzg.py +*bindings/python/*_wrap.c __pycache__ diff --git a/min-bindings/python/Makefile b/min-bindings/python/Makefile new file mode 100644 index 0000000..4cb6362 --- /dev/null +++ b/min-bindings/python/Makefile @@ -0,0 +1,11 @@ +INCLUDE_DIRS = .. ../../min-src ../../inc +INCLUDE_PY = $(shell python -c 'import sysconfig; print(sysconfig.get_config_var("INCLUDEPY"))') + +test: tests.py _ckzg.so + python $< + +_ckzg.so: c_kzg_4844_wrap.c ../../min-src/c_kzg_4844.o ../../lib/libblst.a + clang -O -Wall -shared -fPIC -Wl,-Bsymbolic -I${INCLUDE_PY} ${addprefix -I,${INCLUDE_DIRS}} -o $@ $^ + +c_kzg_4844_wrap.c ckzg.py: ../c_kzg_4844.swg + swig -DSWIGWORDSIZE64 -O -Wall -python -outcurrentdir $< diff --git a/min-bindings/python/tests.py b/min-bindings/python/tests.py new file mode 100644 index 0000000..441df17 --- /dev/null +++ b/min-bindings/python/tests.py @@ -0,0 +1,67 @@ +import atexit +import ckzg +import random + +# Helper functions + +def fr_from_int(x): + r = [] + while x > 0: + r.append(x % 2**64) + x //= 2**64 + assert len(r) <= 4 + while len(r) < 4: + r.append(0) + return ckzg.BLSFieldElement_from_uint64s(tuple(r)) + +def int_from_fr(fr): + digits = ckzg.uint64s_from_BLSFieldElement(fr) + res, mult = 0, 1 + for x in digits: + res += mult * x + mult *= 2**64 + return res + +def poly_from_values(values): + ret, pptr = ckzg.alloc_polynomial(len(values)) + assert ret == 0 + p = ckzg.PolynomialEvalFormPtr_frompointer(pptr).value() + pvalues = ckzg.BLSFieldElements_frompointer(p.values) + for i, c in enumerate(values): + pvalues[i] = fr_from_int(c) + return p + +# Simple test of compute_powers + +x = 32930439 +n = 11 +p = 1 + +powers = ckzg.BLSFieldElements(n) +ckzg.compute_powers(powers.cast(), fr_from_int(x), n) + +for i in range(n): + assert p == int_from_fr(powers[i]) + p *= x + p %= 2**256 + +# Load a trusted setup + +ret, ts = ckzg.load_trusted_setup("../../src/trusted_setup.txt") +assert ret == 0 + +BLOB_SIZE = 4096 + +# Commit to a blob + +blob = ckzg.BLSFieldElements(BLOB_SIZE) +for i in range(BLOB_SIZE): + blob[i] = fr_from_int(random.randrange(0, 2**256)) +commitment = ckzg.blob_to_kzg_commitment(blob.cast(), ts) + +print('Tests passed') + +def cleanup(): + ckzg.free_trusted_setup(ts) + +atexit.register(cleanup)