Skip to content

Commit

Permalink
Merge pull request #15 from codex-storage/integers-to-field
Browse files Browse the repository at this point in the history
integers to field elements
  • Loading branch information
benbierens authored Nov 24, 2023
2 parents b953cde + ddff11d commit cfb4f97
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions poseidon2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export sponge
export compress
export merkle
export toBytes
export toF
export elements
export types
8 changes: 8 additions & 0 deletions poseidon2/io.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ./types
import constantine/math/arithmetic
import constantine/math/io/io_bigints
import constantine/math/io/io_fields
import constantine/math/config/curves

export curves
Expand Down Expand Up @@ -35,3 +36,10 @@ iterator elements*(bytes: openArray[byte], _: type F): F =
let finalChunk = bytes[chunkStart..<bytes.len] & endMarker
let finalElement = F.fromOpenArray(finalChunk)
yield finalElement

# Remark: since `fromInt()` does not work at compile time, this doesn't either
func toF*(a: SomeInteger | SomeUnsignedInt) : F =
when a is SomeInteger:
fromInt(result, a)
else:
fromUInt(result, a)
6 changes: 0 additions & 6 deletions poseidon2/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ func getZero*() : F =
setZero(z)
return z

# Remark: since `fromInt()` does not work at compile time, this doesn't either
func toF*(a: int) : F =
var y : F
y.fromInt(a)
return y

#-------------------------------------------------------------------------------

const zero* : F = getZero()
Expand Down
2 changes: 1 addition & 1 deletion tests/poseidon2/testCompress.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/unittest
import constantine/math/arithmetic
import poseidon2/types
import poseidon2/io
import poseidon2/permutation
import poseidon2/compress

Expand Down
27 changes: 27 additions & 0 deletions tests/poseidon2/testIo.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import std/unittest
import std/sequtils
import constantine/math/io/io_bigints
import constantine/math/io/io_fields
import constantine/math/arithmetic
import poseidon2/types
import poseidon2/io
Expand Down Expand Up @@ -54,3 +55,29 @@ suite "conversion to/from bytes":
var expected: array[32, byte]
marshal(expected, element.toBig(), littleEndian)
check element.toBytes() == expected

suite "conversion from integers":

test "converts unsigned integers to a field element":

proc checkConversion[I](integer: I) =
var expected: F
fromUint(expected, integer)
check bool(integer.toF == expected)

checkConversion(0x11'u8)
checkConversion(0x1122'u16)
checkConversion(0x11223344'u32)
checkConversion(0x1122334455667788'u64)

test "converts signed integers to a field element":

proc checkConversion[I](integer: I) =
var expected: F
fromInt(expected, integer)
check bool(integer.toF == expected)

checkConversion(0x11'i8)
checkConversion(0x1122'i16)
checkConversion(0x11223344'i32)
checkConversion(0x1122334455667788'i64)
2 changes: 1 addition & 1 deletion tests/poseidon2/testPermutation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import std/unittest
import constantine/math/arithmetic
import constantine/math/io/io_fields
import constantine/math/io/io_bigints
import constantine/math/config/curves

import poseidon2/types
import poseidon2/io
import poseidon2/permutation

suite "permutation":
Expand Down
1 change: 1 addition & 0 deletions tests/poseidon2/testSponge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import constantine/math/io/io_fields
import constantine/math/arithmetic

import poseidon2/types
import poseidon2/io
import poseidon2

#-------------------------------------------------------------------------------
Expand Down

0 comments on commit cfb4f97

Please sign in to comment.