mirror of
https://github.com/logos-storage/nim-poseidon2.git
synced 2026-01-02 13:43:08 +00:00
Rename marshal -> toBytes, unmarshal -> fromBytes
For two reasons: - to distinguish them from the marshalling functions from constantine - they do not follow the convention that something that is marshalled can be unmarshalled, because they take in 31 bytes but produce 32 bytes
This commit is contained in:
parent
1c7c5d4ec4
commit
dbe3d8a692
@ -98,4 +98,4 @@ func merkleRoot*(xs: openArray[F]) : F =
|
||||
return merkleRoot(ys)
|
||||
|
||||
func merkleRoot*(bytes: openArray[byte]): F =
|
||||
merkleRoot(seq[F].unmarshal(bytes))
|
||||
merkleRoot(seq[F].fromBytes(bytes))
|
||||
|
||||
@ -3,7 +3,7 @@ import constantine/math/arithmetic
|
||||
import constantine/math/io/io_bigints
|
||||
import constantine/math/config/curves
|
||||
|
||||
func unmarshal*(_: type F, bytes: openArray[byte]): F =
|
||||
func fromBytes*(_: type F, bytes: openArray[byte]): F =
|
||||
## Converts bytes into a field element. The byte array is interpreted as a
|
||||
## canonical little-endian big integer. The array should be of length 31 bytes
|
||||
## or less to ensure that it fits in a field of 254 bits. The remaining
|
||||
@ -14,20 +14,20 @@ func unmarshal*(_: type F, bytes: openArray[byte]): F =
|
||||
let bigint = B.unmarshal(padded, littleEndian)
|
||||
return F.fromBig(bigint)
|
||||
|
||||
func unmarshal*(_: type seq[F], bytes: openArray[byte]): seq[F] =
|
||||
func fromBytes*(_: type seq[F], bytes: openArray[byte]): seq[F] =
|
||||
## Converts bytes into field elements. The byte array is converted 31 bytes at
|
||||
## a time with the `F.unmarshal()` function.
|
||||
## a time with the `F.fromBytes()` function.
|
||||
const chunkLen = 31
|
||||
var elements: seq[F]
|
||||
var chunkStart = 0
|
||||
while chunkStart < bytes.len:
|
||||
let chunkEnd = min(chunkStart + 31, bytes.len)
|
||||
let element = F.unmarshal(bytes.toOpenArray(chunkStart, chunkEnd - 1))
|
||||
let element = F.fromBytes(bytes.toOpenArray(chunkStart, chunkEnd - 1))
|
||||
elements.add(element)
|
||||
chunkStart += chunkLen
|
||||
return elements
|
||||
|
||||
func marshal*(element: F): array[32, byte] =
|
||||
func toBytes*(element: F): array[32, byte] =
|
||||
## Converts a field element into its canonical representation in little-endian
|
||||
## byte order. Uses at most 254 bits, the remaining 6 most-significant bits
|
||||
## are set to 0.
|
||||
|
||||
@ -5,20 +5,20 @@ import constantine/math/arithmetic
|
||||
import poseidon2/types
|
||||
import poseidon2/io
|
||||
|
||||
suite "unmarshalling":
|
||||
suite "conversion to/from bytes":
|
||||
|
||||
test "converts little endian bytes into field elements":
|
||||
let bytes = toSeq 1'u8..31'u8
|
||||
let paddedTo32 = bytes & @[0'u8] # most significant byte is not used
|
||||
let expected = F.fromBig(B.unmarshal(paddedTo32, littleEndian))
|
||||
let unmarshalled = F.unmarshal(bytes)
|
||||
let unmarshalled = F.fromBytes(bytes)
|
||||
check bool(unmarshalled == expected)
|
||||
|
||||
test "pads little endian bytes to the right with 0's":
|
||||
let bytes = @[0x56'u8, 0x34, 0x12]
|
||||
let paddedTo32 = bytes & 0'u8.repeat(32 - bytes.len)
|
||||
let expected = F.fromBig(B.unmarshal(paddedTo32, littleEndian))
|
||||
let unmarshalled = F.unmarshal(bytes)
|
||||
let unmarshalled = F.fromBytes(bytes)
|
||||
check bool(unmarshalled == expected)
|
||||
|
||||
test "converts every 31 bytes into a field element":
|
||||
@ -27,7 +27,7 @@ suite "unmarshalling":
|
||||
let expected1 = F.fromBig(B.unmarshal(padded[ 0..<31] & @[0'u8], littleEndian))
|
||||
let expected2 = F.fromBig(B.unmarshal(padded[31..<62] & @[0'u8], littleEndian))
|
||||
let expected3 = F.fromBig(B.unmarshal(padded[62..<93] & @[0'u8], littleEndian))
|
||||
let elements = seq[F].unmarshal(bytes)
|
||||
let elements = seq[F].fromBytes(bytes)
|
||||
check elements.len == 3
|
||||
check bool(elements[0] == expected1)
|
||||
check bool(elements[1] == expected2)
|
||||
@ -38,4 +38,4 @@ suite "unmarshalling":
|
||||
setMinusOne(element) # largest element in the field
|
||||
var expected: array[32, byte]
|
||||
marshal(expected, element.toBig(), littleEndian)
|
||||
check element.marshal() == expected
|
||||
check element.toBytes() == expected
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user