Objectifying some stuff

This commit is contained in:
Dankrad Feist 2022-10-01 16:39:46 +01:00
parent 7dcd70b95e
commit 4fdabd2b4e
No known key found for this signature in database
GPG Key ID: 6815E6A20BEBBABA
2 changed files with 23 additions and 10 deletions

View File

@ -80,7 +80,7 @@
%pythoncode %{
# Helper functions
def fr_from_int(x):
def _fr_from_int(x):
r = []
while x > 0:
r.append(x % 2**64)
@ -90,7 +90,9 @@ def fr_from_int(x):
r.append(0)
return BLSFieldElement_from_uint64s(tuple(r))
def int_from_fr(fr):
blst_fr.from_int = _fr_from_int
def _int_from_fr(fr):
digits = uint64s_from_BLSFieldElement(fr)
res, mult = 0, 1
for x in digits:
@ -98,7 +100,9 @@ def int_from_fr(fr):
mult *= 2**64
return res
def poly_from_values(values):
blst_fr.__int__ = _int_from_fr
def _poly_from_values(values):
ret, pptr = alloc_polynomial(len(values))
assert ret == 0
p = PolynomialEvalFormPtr_frompointer(pptr).value()
@ -106,4 +110,14 @@ def poly_from_values(values):
for i, c in enumerate(values):
pvalues[i] = fr_from_int(c)
return p
PolynomialEvalForm.from_values = _poly_from_values
def _frompybytes(b):
h = bytes(len(b))
for i, byte in enumerate(b):
h[i] = byte
return h
bytes.frompybytes = _frompybytes
%}

View File

@ -10,10 +10,10 @@ n = 11
p = 1
powers = ckzg.BLSFieldElements(n)
ckzg.compute_powers(powers.cast(), ckzg.fr_from_int(x), n)
ckzg.compute_powers(powers.cast(), ckzg.blst_fr.from_int(x), n)
for i in range(n):
assert p == ckzg.int_from_fr(powers[i])
assert p == int(powers[i])
p *= x
p %= 2**256
@ -33,13 +33,13 @@ num_blobs = 3
blobs = [ckzg.BLSFieldElements(BLOB_SIZE) for _ in range(num_blobs)]
for i in range(num_blobs):
for j in range(BLOB_SIZE):
blobs[i][j] = ckzg.fr_from_int(random.randrange(0, 2**256))
blobs[i][j] = ckzg.blst_fr.from_int(random.randrange(0, 2**256))
kzg_commitments = [ckzg.blob_to_kzg_commitment(blob.cast(), ts) for blob in 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
blobs_as_ints = [[ckzg.int_from_fr(frs[i]) for i in range(BLOB_SIZE)] for frs in blobs]
blobs_as_ints = [[int(frs[i]) for i in range(BLOB_SIZE)] for frs in blobs]
kzg_commitments_as_bytes = []
for c in kzg_commitments:
a = ckzg.bytes(48)
@ -50,9 +50,8 @@ for c in kzg_commitments:
encoded_blobs = ssz.encode(blobs_as_ints, blobs_sedes)
encoded_commitments = ssz.encode(kzg_commitments_as_bytes, kzg_commitments_sedes)
hashed = ssz.hash.hashlib.sha256(encoded_blobs + encoded_commitments).digest()
h = ckzg.bytes(len(hashed))
for i, byte in enumerate(hashed):
h[i] = byte
h = ckzg.bytes.frompybytes(hashed)
r = ckzg.bytes_to_bls_field(h.cast())
r_powers = ckzg.BLSFieldElements(len(kzg_commitments))