Skip to content

Commit

Permalink
BatchSerialization for Uncompressed Points (#365)
Browse files Browse the repository at this point in the history
* feat: BatchSerialization for Uncompressed Points + tests

* fix: comments
  • Loading branch information
advaita-saha authored Feb 23, 2024
1 parent 2080d27 commit c7979b0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
41 changes: 41 additions & 0 deletions constantine/serialization/codecs_banderwagon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.} =
Expand Down
25 changes: 24 additions & 1 deletion tests/t_ethereum_verkle_primitives.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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()

0 comments on commit c7979b0

Please sign in to comment.