Ensure that we don't have any side-effects

Required for integration into libp2p, and a
good idea in general :)
This commit is contained in:
Mark Spanbroek 2023-11-01 10:34:55 +01:00
parent fa7c27a833
commit 505a2a8f08
2 changed files with 11 additions and 11 deletions

View File

@ -8,7 +8,7 @@ import poseidon2/io
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
let zero : F = getZero() const zero : F = getZero()
const externalRoundConst : array[24, F] = arrayFromHex( externalRoundConstStr ) const externalRoundConst : array[24, F] = arrayFromHex( externalRoundConstStr )
const internalRoundConst : array[56, F] = arrayFromHex( internalRoundConstStr ) const internalRoundConst : array[56, F] = arrayFromHex( internalRoundConstStr )
@ -16,19 +16,19 @@ const internalRoundConst : array[56, F] = arrayFromHex( internalRoundConstStr )
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# inplace sbox, x => x^5 # inplace sbox, x => x^5
proc sbox(x: var F) : void = func sbox(x: var F) : void =
var y = x var y = x
square(y) square(y)
square(y) square(y)
x *= y x *= y
proc linearLayer(x, y, z : var F) = func linearLayer(x, y, z : var F) =
var s = x ; s += y ; s += z var s = x ; s += y ; s += z
x += s x += s
y += s y += s
z += s z += s
proc internalRound(j: int; x, y, z: var F) = func internalRound(j: int; x, y, z: var F) =
x += internalRoundConst[j] x += internalRoundConst[j]
sbox(x) sbox(x)
var s = x ; s += y ; s += z var s = x ; s += y ; s += z
@ -37,7 +37,7 @@ proc internalRound(j: int; x, y, z: var F) =
y += s y += s
z += s z += s
proc externalRound(j: int; x, y, z : var F) = func externalRound(j: int; x, y, z : var F) =
x += externalRoundConst[3*j+0] x += externalRoundConst[3*j+0]
y += externalRoundConst[3*j+1] y += externalRoundConst[3*j+1]
z += externalRoundConst[3*j+2] z += externalRoundConst[3*j+2]
@ -47,7 +47,7 @@ proc externalRound(j: int; x, y, z : var F) =
y += s y += s
z += s z += s
proc permInplace*(x, y, z : var F) = func permInplace*(x, y, z : var F) =
linearLayer(x, y, z); linearLayer(x, y, z);
for j in 0..3: for j in 0..3:
externalRound(j, x, y, z) externalRound(j, x, y, z)
@ -56,21 +56,21 @@ proc permInplace*(x, y, z : var F) =
for j in 4..7: for j in 4..7:
externalRound(j, x, y, z) externalRound(j, x, y, z)
proc perm*(xyz: S) : S = func perm*(xyz: S) : S =
var (x,y,z) = xyz var (x,y,z) = xyz
permInplace(x, y, z) permInplace(x, y, z)
return (x,y,z) return (x,y,z)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
proc compress*(a, b : F) : F = func compress*(a, b : F) : F =
var x = a var x = a
var y = b var y = b
var z : F ; setZero(z) var z : F ; setZero(z)
permInplace(x, y, z) permInplace(x, y, z)
return x return x
proc merkleRoot*(xs: openArray[F]) : F = func merkleRoot*(xs: openArray[F]) : F =
let a = low(xs) let a = low(xs)
let b = high(xs) let b = high(xs)
let m = b-a+1 let m = b-a+1
@ -97,5 +97,5 @@ proc merkleRoot*(xs: openArray[F]) : F =
return merkleRoot(ys) return merkleRoot(ys)
proc merkleRoot*(bytes: openArray[byte]): F = func merkleRoot*(bytes: openArray[byte]): F =
merkleRoot(F.unmarshal(bytes, littleEndian)) merkleRoot(F.unmarshal(bytes, littleEndian))

View File

@ -2,7 +2,7 @@ import ./types
import constantine/math/arithmetic import constantine/math/arithmetic
import constantine/math/io/io_bigints import constantine/math/io/io_bigints
proc unmarshal*( func unmarshal*(
_: type F, _: type F,
bytes: openArray[byte], bytes: openArray[byte],
endian: static Endianness): seq[F] = endian: static Endianness): seq[F] =