Objectifying some stuff
This commit is contained in:
parent
7dcd70b95e
commit
4fdabd2b4e
|
@ -80,7 +80,7 @@
|
||||||
%pythoncode %{
|
%pythoncode %{
|
||||||
# Helper functions
|
# Helper functions
|
||||||
|
|
||||||
def fr_from_int(x):
|
def _fr_from_int(x):
|
||||||
r = []
|
r = []
|
||||||
while x > 0:
|
while x > 0:
|
||||||
r.append(x % 2**64)
|
r.append(x % 2**64)
|
||||||
|
@ -90,7 +90,9 @@ def fr_from_int(x):
|
||||||
r.append(0)
|
r.append(0)
|
||||||
return BLSFieldElement_from_uint64s(tuple(r))
|
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)
|
digits = uint64s_from_BLSFieldElement(fr)
|
||||||
res, mult = 0, 1
|
res, mult = 0, 1
|
||||||
for x in digits:
|
for x in digits:
|
||||||
|
@ -98,7 +100,9 @@ def int_from_fr(fr):
|
||||||
mult *= 2**64
|
mult *= 2**64
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def poly_from_values(values):
|
blst_fr.__int__ = _int_from_fr
|
||||||
|
|
||||||
|
def _poly_from_values(values):
|
||||||
ret, pptr = alloc_polynomial(len(values))
|
ret, pptr = alloc_polynomial(len(values))
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
p = PolynomialEvalFormPtr_frompointer(pptr).value()
|
p = PolynomialEvalFormPtr_frompointer(pptr).value()
|
||||||
|
@ -106,4 +110,14 @@ def poly_from_values(values):
|
||||||
for i, c in enumerate(values):
|
for i, c in enumerate(values):
|
||||||
pvalues[i] = fr_from_int(c)
|
pvalues[i] = fr_from_int(c)
|
||||||
return p
|
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
|
||||||
%}
|
%}
|
|
@ -10,10 +10,10 @@ n = 11
|
||||||
p = 1
|
p = 1
|
||||||
|
|
||||||
powers = ckzg.BLSFieldElements(n)
|
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):
|
for i in range(n):
|
||||||
assert p == ckzg.int_from_fr(powers[i])
|
assert p == int(powers[i])
|
||||||
p *= x
|
p *= x
|
||||||
p %= 2**256
|
p %= 2**256
|
||||||
|
|
||||||
|
@ -33,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] = 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]
|
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 = [[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 = []
|
kzg_commitments_as_bytes = []
|
||||||
for c in kzg_commitments:
|
for c in kzg_commitments:
|
||||||
a = ckzg.bytes(48)
|
a = ckzg.bytes(48)
|
||||||
|
@ -50,9 +50,8 @@ for c in kzg_commitments:
|
||||||
encoded_blobs = ssz.encode(blobs_as_ints, blobs_sedes)
|
encoded_blobs = ssz.encode(blobs_as_ints, blobs_sedes)
|
||||||
encoded_commitments = ssz.encode(kzg_commitments_as_bytes, kzg_commitments_sedes)
|
encoded_commitments = ssz.encode(kzg_commitments_as_bytes, kzg_commitments_sedes)
|
||||||
hashed = ssz.hash.hashlib.sha256(encoded_blobs + encoded_commitments).digest()
|
hashed = ssz.hash.hashlib.sha256(encoded_blobs + encoded_commitments).digest()
|
||||||
h = ckzg.bytes(len(hashed))
|
|
||||||
for i, byte in enumerate(hashed):
|
h = ckzg.bytes.frompybytes(hashed)
|
||||||
h[i] = byte
|
|
||||||
|
|
||||||
r = ckzg.bytes_to_bls_field(h.cast())
|
r = ckzg.bytes_to_bls_field(h.cast())
|
||||||
r_powers = ckzg.BLSFieldElements(len(kzg_commitments))
|
r_powers = ckzg.BLSFieldElements(len(kzg_commitments))
|
||||||
|
|
Loading…
Reference in New Issue