Play around with some testing of the min interface

This commit is contained in:
Ramana Kumar 2022-09-30 07:21:48 +01:00
parent cfa9675512
commit be3f0c22cf
No known key found for this signature in database
GPG Key ID: ED471C788B900433
3 changed files with 81 additions and 3 deletions

6
.gitignore vendored
View File

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

View File

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

View File

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