diff --git a/poseidon2.nim b/poseidon2.nim index 1e3e568..d68ec1c 100644 --- a/poseidon2.nim +++ b/poseidon2.nim @@ -1,73 +1,13 @@ import constantine/math/arithmetic import poseidon2/types -import poseidon2/roundfun +import poseidon2/permutation import poseidon2/io +import poseidon2/sponge +export sponge 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 = - 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..