mirror of
https://github.com/logos-storage/nim-poseidon2.git
synced 2026-01-02 13:43:08 +00:00
Handle merkleRoot of empty sequence
Co-Authored-By: Balazs Komuves <bkomuves@gmail.com>
This commit is contained in:
parent
cce9c13a04
commit
c4b4936e26
@ -15,6 +15,9 @@ func merkleRoot(xs: openArray[F], isBottomLayer: static bool) : F =
|
||||
let b = high(xs)
|
||||
let m = b-a+1
|
||||
|
||||
when isBottomLayer:
|
||||
assert m > 0, "merkle root of empty sequence is not defined"
|
||||
|
||||
when not isBottomLayer:
|
||||
if m==1:
|
||||
return xs[a]
|
||||
|
||||
@ -32,6 +32,14 @@ suite "conversion to/from bytes":
|
||||
let elements = toSeq bytes.elements(F)
|
||||
check bool(elements[^1] == expected)
|
||||
|
||||
test "converts empty sequence of bytes to single field element":
|
||||
let bytes = seq[byte].default
|
||||
let marker = @[1'u8]
|
||||
let expected = F.fromBytes(marker.toArray)
|
||||
let elements = toSeq bytes.elements(F)
|
||||
check elements.len == 1
|
||||
check bool(elements[0] == expected)
|
||||
|
||||
test "conversion from bytes pads the last chunk when it's less than 31 bytes":
|
||||
let bytes = toSeq 1'u8..80'u8
|
||||
let marker = @[1'u8]
|
||||
|
||||
@ -65,3 +65,13 @@ suite "merkle root":
|
||||
let bytes = toSeq 1'u8..80'u8
|
||||
let rootAsBytes = merkleRoot(bytes).toBytes()
|
||||
check rootAsBytes.toHex == "0x40989b63104f39e3331767883381085bcfc46e2202679123371f1ffe53521b16"
|
||||
|
||||
test "merkle root of empty sequence of elements":
|
||||
let empty = seq[F].default
|
||||
expect Exception:
|
||||
discard merkleRoot(empty)
|
||||
|
||||
test "merkle root of empty sequency of bytes":
|
||||
# merkle root of empty sequence of bytes is uniquely defined through padding
|
||||
let empty = seq[byte].default
|
||||
check merkleRoot(empty).toBytes.toHex == "0xcc8da1d157900e611b89e258d95450e707f4f9eec169422d7c26aba54f803c08"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user