2026-04-22 22:44:09 +02:00

127 lines
5.7 KiB
Nim

import std/unittest
import std/sequtils
import constantine/math/io/io_fields
import constantine/math/arithmetic
import poseidon2/types
import poseidon2/io
import poseidon2
#-------------------------------------------------------------------------------
const expectedSpongeResultsRate1_oldConstants : array[9, string] =
[ "09602279551593072781767869672876731992056744563146041630517050886365970636003"
, "10482658356029234675406545538312309989559675144291371554217154670816627355386"
, "21219451497454286976687787075722272763501660786972150636742566108535080181479"
, "08722715919324543554361251138508029319575030629222089689820594626285008029447"
, "05252479416071756925432047522706920823732701994843337274608383487213952562652"
, "14335681867091508859155432377770517006032816456578203813846625263518832139083"
, "01787518659902041499279810468457113017929918842893543992004146450251877175230"
, "05951872852437676666746911120894618677241724527492839948381478122628367478399"
, "01483169051171494649812096066993363178940115888705003734441901747432523068356"
]
const expectedSpongeResultsRate2_oldConstants : array[9, string] =
[ "19499082269592267598445447545057958665614609297846978854367973542689225942769"
, "18575012789231469229884764324941297325518518125159947154666143736134627612926"
, "08497041170554791294461959662381834821683188757759653310126334787903454881833"
, "07477361136953606818741895260704612015724121297282898803513690475311354933324"
, "00124468679175306239235509344657309567209200730228442938605071013597332255858"
, "16623067489256565233778087665060282683386099247772442960143578746217625299219"
, "11486960019850145257815352297225482939271961443661416961989480881863168607026"
, "20420541301992412878354329495337915388337723490334029715201499395107517967097"
, "15368493599988308785714434050658408098972196808672741268698522157366881904768"
]
#-------------------------------------------------------------------------------
const expectedSpongeResultsRate1_newConstants : array[9, string] =
[ "04377424091368814254584985932000325369434186561094943049988002427001371028682"
, "04882927658549556007721001327257767042687783024950239124517957916798755877693"
, "04780075560415732571153009499315189673966439308277303342424971291506882586108"
, "11474073528049602460823047474981999023141929996686953106018399735733847148245"
, "18497777614932774211335329486842841597372903251455898848830055848165198236001"
, "10604030222565816223616875651989127064440338459350084151593575075022111417457"
, "21776923324072869685005088338231207859554473518124587106175254723327312624225"
, "02792992288869135760313579543774377265869215242633606960230371229360776626275"
, "20693007341698461222515050179991203000899947120254813866359977866790502772966"
]
const expectedSpongeResultsRate2_newConstants : array[9, string] =
[ "11798481567564189003554007054356793867771045155136537398977037610842664973086"
, "04295345858975036877726752063699022226529926252001084742973667726185976172546"
, "04843018033972723164735619122919964846723654438742840794484490585074757880377"
, "14366573672249189186063883832534066613008473033573013521169876759944167284847"
, "18980797754459442521759183174826646847662801133806115709836254071366539109721"
, "20835927296130700305485703890299389670765608010514411209585750532357495956217"
, "00376136331714847730896275946478129283551272685413282664899718356562373464640"
, "06676481722159775414384926904273367103332158165985849413600251374153631877410"
, "14702393876084904104384222772945859101087277678512774408869360595483438759502"
]
#-------------------------------------------------------------------------------
suite "sponge (old round constants)":
test "sponge with rate=1":
for n in 0..8:
var xs: seq[F]
for i in 1..n:
xs.add( toF(i) )
let h = Sponge.digest(xs, rate = 1, which = HorizenLabsOld)
check toDecimal(h) == expectedSpongeResultsRate1_oldConstants[n]
test "sponge with rate=2":
for n in 0..8:
var xs: seq[F]
for i in 1..n:
xs.add( toF(i) )
let h = Sponge.digest(xs, rate = 2, which = HorizenLabsOld)
check toDecimal(h) == expectedSpongeResultsRate2_oldConstants[n]
test "sponge with byte array as input":
let bytes = toSeq 1'u8..80'u8
var sponge = Sponge.initWithDomSep(rate = 2, domSep = DOMSEP_IV_RATE2_BYTES, which = HorizenLabsOld)
let elements = toSeq bytes.elements(F)
for element in bytes.elements(F):
sponge.update(element)
let expected = sponge.finish()
check bool(Sponge.digest(bytes, rate = 2, which = HorizenLabsOld) == expected)
#-------------------------------------------------------------------------------
suite "sponge (new round constants)":
test "sponge with rate=1":
for n in 0..8:
var xs: seq[F]
for i in 1..n:
xs.add( toF(i) )
let h = Sponge.digest(xs, rate = 1, which = HorizenLabsNew)
check toDecimal(h) == expectedSpongeResultsRate1_newConstants[n]
test "sponge with rate=2":
for n in 0..8:
var xs: seq[F]
for i in 1..n:
xs.add( toF(i) )
let h = Sponge.digest(xs, rate = 2, which = HorizenLabsNew)
check toDecimal(h) == expectedSpongeResultsRate2_newConstants[n]
test "sponge with byte array as input":
let bytes = toSeq 1'u8..80'u8
var sponge = Sponge.initWithDomSep(rate = 2, domSep = DOMSEP_IV_RATE2_BYTES, which = HorizenLabsNew)
let elements = toSeq bytes.elements(F)
for element in bytes.elements(F):
sponge.update(element)
let expected = sponge.finish()
check bool(Sponge.digest(bytes, rate = 2, which = HorizenLabsNew) == expected)
#-------------------------------------------------------------------------------