From c7979b003372b329dd450ff152bb5945cafb0db5 Mon Sep 17 00:00:00 2001 From: Advaita Saha Date: Fri, 23 Feb 2024 13:03:37 +0530 Subject: [PATCH] BatchSerialization for Uncompressed Points (#365) * feat: BatchSerialization for Uncompressed Points + tests * fix: comments --- .../serialization/codecs_banderwagon.nim | 41 +++++++++++++++++++ tests/t_ethereum_verkle_primitives.nim | 25 ++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/constantine/serialization/codecs_banderwagon.nim b/constantine/serialization/codecs_banderwagon.nim index 541abf74..32c9be39 100644 --- a/constantine/serialization/codecs_banderwagon.nim +++ b/constantine/serialization/codecs_banderwagon.nim @@ -281,6 +281,47 @@ func serializeBatch*( return cttCodecEcc_Success +func serializeBatchUncompressed*( + dst: ptr UncheckedArray[array[64, byte]], + points: ptr UncheckedArray[EC_Prj], + N: int, + ) : CttCodecEccStatus {.noInline.} = + ## Batch Serialization of Banderwagon Points + ## In uncompressed format + ## serialize = [ bigEndian( x ) , bigEndian( y ) ] + ## Returns cttCodecEcc_Success if successful + + # collect all the z coordinates + var zs = allocStackArray(Fp[Banderwagon], N) + var zs_inv = allocStackArray(Fp[Banderwagon], N) + for i in 0 ..< N: + zs[i] = points[i].z + + zs_inv.batchInvert(zs, N) + + for i in 0 ..< N: + var X: Fp[Banderwagon] + var Y: Fp[Banderwagon] + + X.prod(points[i].x, zs_inv[i]) + Y.prod(points[i].y, zs_inv[i]) + + var xSerialized: array[32, byte] + xSerialized.marshal(X, bigEndian) + var ySerialized: array[32, byte] + ySerialized.marshal(Y, bigEndian) + + for j in 0 ..< 32: + dst[i][j] = xSerialized[j] + dst[i][j + 32] = ySerialized[j] + + return cttCodecEcc_Success + +func serializeBatchUncompressed*[N: static int]( + dst: var array[N, array[64, byte]], + points: array[N, EC_Prj]): CttCodecEccStatus {.inline.} = + return serializeBatchUncompressed(dst.asUnchecked(), points.asUnchecked(), N) + func serializeBatch*[N: static int]( dst: var array[N, array[32, byte]], points: array[N, EC_Prj]): CttCodecEccStatus {.inline.} = diff --git a/tests/t_ethereum_verkle_primitives.nim b/tests/t_ethereum_verkle_primitives.nim index 24d186ef..d7dbf63a 100644 --- a/tests/t_ethereum_verkle_primitives.nim +++ b/tests/t_ethereum_verkle_primitives.nim @@ -513,4 +513,27 @@ suite "Batch Operations on Banderwagon": for i in 0 ..< len: doAssert expected_bit_strings[i] == arr[i].toHex(), "bit string does not match expected" - testBatchSerialize(expected_bit_strings.len) \ No newline at end of file + testBatchSerialize(expected_bit_strings.len) + + ## Check batch Uncompressed encoding + test "Batch Uncompressed Point Serialization": + proc testBatchUncompressedSerialization() = + var points: array[10, EC] + var point, point_regen {.noInit.}: EC + point.fromAffine(generator) + + for i in 0 ..< 10: + points[i] = point + point.double() + + var arr: array[10, array[64, byte]] + let stat = arr.serializeBatchUncompressed(points) + + doAssert stat == cttCodecEcc_Success, "Uncompressed Serialization Failed" + + for i in 0 ..< 10: + let stat2 = point_regen.deserializeUncompressed(arr[i]) + doAssert stat2 == cttCodecEcc_Success, "Uncompressed Deserialization Failed" + doAssert (points[i] == point_regen).bool(), "Uncompressed SerDe Inconsistent" + + testBatchUncompressedSerialization() \ No newline at end of file