mirror of
https://github.com/status-im/c-kzg-4844.git
synced 2025-02-03 05:44:52 +00:00
Add python helper code to swig interface
This commit is contained in:
parent
459209375c
commit
e33064cf28
@ -76,3 +76,34 @@
|
|||||||
|
|
||||||
%include "../min-src/c_kzg_4844.h"
|
%include "../min-src/c_kzg_4844.h"
|
||||||
%include "../inc/blst.h"
|
%include "../inc/blst.h"
|
||||||
|
|
||||||
|
%pythoncode %{
|
||||||
|
# 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 BLSFieldElement_from_uint64s(tuple(r))
|
||||||
|
|
||||||
|
def int_from_fr(fr):
|
||||||
|
digits = 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 = alloc_polynomial(len(values))
|
||||||
|
assert ret == 0
|
||||||
|
p = PolynomialEvalFormPtr_frompointer(pptr).value()
|
||||||
|
pvalues = BLSFieldElements_frompointer(p.values)
|
||||||
|
for i, c in enumerate(values):
|
||||||
|
pvalues[i] = fr_from_int(c)
|
||||||
|
return p
|
||||||
|
%}
|
@ -3,35 +3,6 @@ import ckzg
|
|||||||
import random
|
import random
|
||||||
import ssz
|
import ssz
|
||||||
|
|
||||||
# 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
|
# Simple test of compute_powers
|
||||||
|
|
||||||
x = 32930439
|
x = 32930439
|
||||||
@ -39,10 +10,10 @@ n = 11
|
|||||||
p = 1
|
p = 1
|
||||||
|
|
||||||
powers = ckzg.BLSFieldElements(n)
|
powers = ckzg.BLSFieldElements(n)
|
||||||
ckzg.compute_powers(powers.cast(), fr_from_int(x), n)
|
ckzg.compute_powers(powers.cast(), ckzg.fr_from_int(x), n)
|
||||||
|
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
assert p == int_from_fr(powers[i])
|
assert p == ckzg.int_from_fr(powers[i])
|
||||||
p *= x
|
p *= x
|
||||||
p %= 2**256
|
p %= 2**256
|
||||||
|
|
||||||
@ -62,13 +33,13 @@ num_blobs = 3
|
|||||||
blobs = [ckzg.BLSFieldElements(BLOB_SIZE) for _ in range(num_blobs)]
|
blobs = [ckzg.BLSFieldElements(BLOB_SIZE) for _ in range(num_blobs)]
|
||||||
for i in range(num_blobs):
|
for i in range(num_blobs):
|
||||||
for j in range(BLOB_SIZE):
|
for j in range(BLOB_SIZE):
|
||||||
blobs[i][j] = fr_from_int(random.randrange(0, 2**256))
|
blobs[i][j] = ckzg.fr_from_int(random.randrange(0, 2**256))
|
||||||
kzg_commitments = [ckzg.blob_to_kzg_commitment(blob.cast(), ts) for blob in blobs]
|
kzg_commitments = [ckzg.blob_to_kzg_commitment(blob.cast(), ts) for blob in blobs]
|
||||||
|
|
||||||
# Compute polynomial commitments for these blobs
|
# Compute polynomial commitments for these blobs
|
||||||
# We don't follow the spec exactly to get the hash, but it shouldn't matter since it's random data
|
# We don't follow the spec exactly to get the hash, but it shouldn't matter since it's random data
|
||||||
|
|
||||||
blobs_as_ints = [[int_from_fr(frs[i]) for i in range(BLOB_SIZE)] for frs in blobs]
|
blobs_as_ints = [[ckzg.int_from_fr(frs[i]) for i in range(BLOB_SIZE)] for frs in blobs]
|
||||||
kzg_commitments_as_bytes = []
|
kzg_commitments_as_bytes = []
|
||||||
for c in kzg_commitments:
|
for c in kzg_commitments:
|
||||||
a = ckzg.bytes(48)
|
a = ckzg.bytes(48)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user