From a2ecd61c7f1e555a784b3d0aa5952a5349536d07 Mon Sep 17 00:00:00 2001 From: gerceboss Date: Sun, 25 Aug 2024 14:22:28 +0530 Subject: [PATCH] Updated format --- constantine/ethereum_eip4844_kzg.nim | 2 +- constantine/ethereum_eip7594_kzg.nim | 102 ++++++--------------------- 2 files changed, 24 insertions(+), 80 deletions(-) diff --git a/constantine/ethereum_eip4844_kzg.nim b/constantine/ethereum_eip4844_kzg.nim index 99aa70a6..55e6644f 100644 --- a/constantine/ethereum_eip4844_kzg.nim +++ b/constantine/ethereum_eip4844_kzg.nim @@ -151,7 +151,7 @@ func bytes_to_bls_bigint(dst: var Fr[BLS12_381].getBigInt(), src: array[32, byte return status return cttCodecScalar_Success -func bytes_to_bls_field(dst: var Fr[BLS12_381], src: array[32, byte]): CttCodecScalarStatus = +func bytes_to_bls_field*(dst: var Fr[BLS12_381], src: array[32, byte]): CttCodecScalarStatus = ## Convert untrusted bytes to a trusted and validated BLS scalar field element. ## This function does not accept inputs greater than the BLS modulus. var scalar {.noInit.}: Fr[BLS12_381].getBigInt() diff --git a/constantine/ethereum_eip7594_kzg.nim b/constantine/ethereum_eip7594_kzg.nim index 201971aa..efd7598b 100644 --- a/constantine/ethereum_eip7594_kzg.nim +++ b/constantine/ethereum_eip7594_kzg.nim @@ -1,4 +1,4 @@ -import +import std/typetraits, constantine/named/algebras, @@ -11,11 +11,8 @@ import ./hashes, ./platforms/[abstractions, allocs], ./serialization/[codecs_status_codes, codecs_bls12_381, endians], - ./commitments_setups/ethereum_kzg_srs - # ./ethereum_eip4844_kzg - -export trusted_setup_load, trusted_setup_delete, TrustedSetupFormat, TrustedSetupStatus, EthereumKZGContext - + ./ethereum_eip4844_kzg + const RANDOM_CHALLENGE_KZG_CELL_BATCH_DOMAIN=asBytes"RCKZGCBATCH__V1_" const FIELD_ELEMENTS_PER_BLOB=128 const FIELD_ELEMENTS_PER_CELL=64 @@ -34,97 +31,43 @@ type # A polynomial in coefficient form PolyCoeff = PolynomialCoef[FIELD_ELEMENTS_PER_EXT_BLOB,Fr[BLS12_381]] # The evaluation domain of a cell - Coset* = distinct array[FIELD_ELEMENTS_PER_CELL, Fr[BLS12_381]] + Coset* = array[FIELD_ELEMENTS_PER_CELL, Fr[BLS12_381]] # The internal representation of a cell (the evaluations over its Coset) - CosetEvals* = distinct array[FIELD_ELEMENTS_PER_CELL, Fr[BLS12_381]] + CosetEvals* = array[FIELD_ELEMENTS_PER_CELL, Fr[BLS12_381]] # The unit of blob data that can come with its own KZG proof Cell* = array[BYTES_PER_CELL, byte] # Validation: x < CELLS_PER_EXT_BLOB CellIndex* = uint64 -# useful for detecting if memory cleanup us necessary , and check the status of the execution of the function -template checkReturn(evalExpr: CttCodecScalarStatus): untyped {.dirty.} = - # Translate codec status code to KZG status code - # Beware of resource cleanup like heap allocation, this can early exit the caller. - block: - let status = evalExpr # Ensure single evaluation - case status - of cttCodecScalar_Success: discard - of cttCodecScalar_Zero: discard - of cttCodecScalar_ScalarLargerThanCurveOrder: return cttEthKzg_ScalarLargerThanCurveOrder - -template checkReturn(evalExpr: CttCodecEccStatus): untyped {.dirty.} = - # Translate codec status code to KZG status code - # Beware of resource cleanup like heap allocation, this can early exit the caller. - block: - let status = evalExpr # Ensure single evaluation - case status - of cttCodecEcc_Success: discard - of cttCodecEcc_InvalidEncoding: return cttEthKzg_EccInvalidEncoding - of cttCodecEcc_CoordinateGreaterThanOrEqualModulus: return cttEthKzg_EccCoordinateGreaterThanOrEqualModulus - of cttCodecEcc_PointNotOnCurve: return cttEthKzg_EccPointNotOnCurve - of cttCodecEcc_PointNotInSubgroup: return cttEthKzg_EccPointNotInSubGroup - of cttCodecEcc_PointAtInfinity: discard - -template check(Section: untyped, evalExpr: CttCodecScalarStatus): untyped {.dirty.} = - # Translate codec status code to KZG status code - # Exit current code block - block: - let status = evalExpr # Ensure single evaluation - case status - of cttCodecScalar_Success: discard - of cttCodecScalar_Zero: discard - of cttCodecScalar_ScalarLargerThanCurveOrder: result = cttEthKzg_ScalarLargerThanCurveOrder; break Section - -template check(Section: untyped, evalExpr: CttCodecEccStatus): untyped {.dirty.} = - # Translate codec status code to KZG status code - # Exit current code block - block: - let status = evalExpr # Ensure single evaluation - case status - of cttCodecEcc_Success: discard - of cttCodecEcc_InvalidEncoding: result = cttEthKzg_EccInvalidEncoding; break Section - of cttCodecEcc_CoordinateGreaterThanOrEqualModulus: result = cttEthKzg_EccCoordinateGreaterThanOrEqualModulus; break Section - of cttCodecEcc_PointNotOnCurve: result = cttEthKzg_EccPointNotOnCurve; break Section - of cttCodecEcc_PointNotInSubgroup: result = cttEthKzg_EccPointNotInSubGroup; break Section - of cttCodecEcc_PointAtInfinity: discard - - # from eip4844, import # bytes_to_bls_field # bls_field_to_bytes -func cell_to_coset_evals(evals: var CosetEvals,cell: Cell): CttCodecEccStatus= +func cell_to_coset_evals(evals:var CosetEvals,cell: Cell): CttCodecEccStatus= # Convert an untrusted ``Cell`` into a trusted ``CosetEvals`` - var - start: int - ending: int - value:Fr[BLS12_381] - - let view = cast[ptr array[FIELD_ELEMENTS_PER_CELL, array[32, byte]]](cell.unsafeAddr) - for i in 0..FIELD_ELEMENTS_PER_CELL: - start = i*BYTES_PER_FIELD_ELEMENT - ending = (i+1)*BYTES_PER_FIELD_ELEMENT - var status= value.bytes_to_bls_field(view[i]) + var temp: Fr[BLS12_381] + let view = cast[array[FIELD_ELEMENTS_PER_CELL, array[32, byte]]](cell.unsafeAddr) + for i in 0..