mirror of
https://github.com/status-im/c-kzg-4844.git
synced 2025-01-23 08:29:17 +00:00
Add some comments to Python file, rename to tests
This commit is contained in:
parent
9aa1dc289f
commit
43d54b8154
@ -1,43 +1,58 @@
|
|||||||
import atexit
|
import atexit
|
||||||
import ckzg
|
import ckzg
|
||||||
|
|
||||||
def int_from_uint64s(tup):
|
def int_from_uint64s(digits):
|
||||||
|
"""Convert a 4-tuple of base64 digits to the int it denotes"""
|
||||||
res, mult = 0, 1
|
res, mult = 0, 1
|
||||||
for x in tup:
|
for x in digits:
|
||||||
res += mult * x
|
res += mult * x
|
||||||
mult *= 2 ** 64
|
mult *= 2 ** 64
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def eval_poly(coeffs, x):
|
def eval_poly(coeffs, x):
|
||||||
|
"""Evaluate a polynomial represented by a sequence of coefficients"""
|
||||||
res, mult = 0, 1
|
res, mult = 0, 1
|
||||||
for c in coeffs:
|
for c in coeffs:
|
||||||
res += mult * c
|
res += mult * c
|
||||||
mult *= x
|
mult *= x
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
# Make some elements to be used as coefficients
|
||||||
|
|
||||||
c1 = ckzg.fr_from_uint64s((12,13,0,0))
|
c1 = ckzg.fr_from_uint64s((12,13,0,0))
|
||||||
c2 = ckzg.fr_from_uint64(2)
|
c2 = ckzg.fr_from_uint64(2)
|
||||||
c3 = ckzg.fr_from_uint64s((1,0,0,0))
|
c3 = ckzg.fr_from_uint64s((1,0,0,0))
|
||||||
c4 = ckzg.fr_sub(c2, c3)
|
c4 = ckzg.fr_sub(c2, c3)
|
||||||
|
|
||||||
|
# A few sanity checks
|
||||||
|
|
||||||
assert ckzg.fr_is_one(c4)
|
assert ckzg.fr_is_one(c4)
|
||||||
|
|
||||||
assert ckzg.fr_equal(c3, c4)
|
assert ckzg.fr_equal(c3, c4)
|
||||||
|
|
||||||
|
# Create an array of the coefficients
|
||||||
|
|
||||||
coeffs = [c1, c2, c3, c4]
|
coeffs = [c1, c2, c3, c4]
|
||||||
cfa = ckzg.frArray(len(coeffs))
|
cfa = ckzg.frArray(len(coeffs))
|
||||||
for i, c in enumerate(coeffs):
|
for i, c in enumerate(coeffs):
|
||||||
cfa[i] = c
|
cfa[i] = c
|
||||||
|
|
||||||
|
# Build the polynomial
|
||||||
|
|
||||||
ret, pptr = ckzg.new_poly_with_coeffs(cfa.cast(), len(coeffs))
|
ret, pptr = ckzg.new_poly_with_coeffs(cfa.cast(), len(coeffs))
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
# Check one of its coefficients is as expected
|
||||||
|
|
||||||
p = ckzg.polyp_frompointer(pptr).value()
|
p = ckzg.polyp_frompointer(pptr).value()
|
||||||
assert p.length == 4
|
assert p.length == 4
|
||||||
|
|
||||||
pcoeffs = ckzg.frArray_frompointer(p.coeffs)
|
pcoeffs = ckzg.frArray_frompointer(p.coeffs)
|
||||||
assert ckzg.fr_to_uint64s(pcoeffs[1]) == (2, 0, 0, 0)
|
assert ckzg.fr_to_uint64s(pcoeffs[1]) == (2, 0, 0, 0)
|
||||||
|
|
||||||
|
# Build a trusted setup with an arbitrary secret s
|
||||||
|
# and max scale 4 (so 16 secret values)
|
||||||
|
|
||||||
max_scale = 4
|
max_scale = 4
|
||||||
|
|
||||||
ret, fs = ckzg.new_fft_settings(max_scale)
|
ret, fs = ckzg.new_fft_settings(max_scale)
|
||||||
@ -55,20 +70,31 @@ ckzg.generate_trusted_setup(g1s.cast(), g2s.cast(), secret_s, num_secrets)
|
|||||||
ret, ks = ckzg.new_kzg_settings(g1s.cast(), g2s.cast(), num_secrets, fs)
|
ret, ks = ckzg.new_kzg_settings(g1s.cast(), g2s.cast(), num_secrets, fs)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
# Compute the Lagrange form of our polynomial in this setup
|
||||||
|
|
||||||
ret, p_l = ckzg.new_poly_l_from_poly(p, ks)
|
ret, p_l = ckzg.new_poly_l_from_poly(p, ks)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
# Check some evaluations at the point 2
|
||||||
|
# First, that Lagrange and coefficient form evaluations agree
|
||||||
|
|
||||||
ret, y_l = ckzg.eval_poly_l(p_l, c2, fs)
|
ret, y_l = ckzg.eval_poly_l(p_l, c2, fs)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
y = ckzg.eval_poly(p, c2)
|
y = ckzg.eval_poly(p, c2)
|
||||||
assert ckzg.fr_equal(y, y_l)
|
assert ckzg.fr_equal(y, y_l)
|
||||||
|
|
||||||
|
# And that this agrees with a naive Python evaluation
|
||||||
|
|
||||||
py_coeffs = [int_from_uint64s(ckzg.fr_to_uint64s(c)) for c in coeffs]
|
py_coeffs = [int_from_uint64s(ckzg.fr_to_uint64s(c)) for c in coeffs]
|
||||||
|
|
||||||
y_p = eval_poly(py_coeffs, int_from_uint64s(ckzg.fr_to_uint64s(c2)))
|
y_p = eval_poly(py_coeffs, int_from_uint64s(ckzg.fr_to_uint64s(c2)))
|
||||||
assert int_from_uint64s(ckzg.fr_to_uint64s(y)) == y_p
|
assert int_from_uint64s(ckzg.fr_to_uint64s(y)) == y_p
|
||||||
|
|
||||||
|
print("All tests passed.")
|
||||||
|
|
||||||
|
# We need to manually free the C allocated arrays
|
||||||
|
# Use atexit so this file can be loaded interactively before freeing
|
||||||
def cleanup():
|
def cleanup():
|
||||||
ckzg.free_poly(pptr)
|
ckzg.free_poly(pptr)
|
||||||
ckzg.free_poly_l(p_l)
|
ckzg.free_poly_l(p_l)
|
Loading…
x
Reference in New Issue
Block a user