import constantine/math/arithmetic import poseidon2/types import poseidon2/roundfun import poseidon2/io export toBytes #------------------------------------------------------------------------------- # the Poseidon2 permutation (mutable, in-place version) proc permInplace*(x, y, z : var F) = linearLayer(x, y, z); for j in 0..3: externalRound(j, x, y, z) for j in 0..55: internalRound(j, x, y, z) for j in 4..7: externalRound(j, x, y, z) # the Poseidon2 permutation func perm*(xyz: S) : S = var (x,y,z) = xyz permInplace(x, y, z) return (x,y,z) #------------------------------------------------------------------------------- # sponge with rate=1 (capacity=2) func spongeWithRate1*(xs: openArray[F]) : F = let a = low(xs) let b = high(xs) let n = b-a+1 var s0 : F = zero var s1 : F = zero var s2 : F = toF(0x0301) ; s2 += twoToThe64 # domain separation IV := (2^64 + 256*t + r) for x in xs: s0 += x permInplace(s0,s1,s2) # padding s0 += one; permInplace(s0,s1,s2) return s0 # sponge with rate=2 (capacity=1) func spongeWithRate2*(xs: openArray[F]) : F = let a = low(xs) let b = high(xs) let n = b-a+1 let halfn : int = n div 2 var s0 : F = zero var s1 : F = zero var s2 : F = toF(0x0302) ; s2 += twoToThe64 # domain separation IV := (2^64 + 256*t + r) for i in 0..