/ reference / nim / testvectors / src / testvectors.nim
testvectors.nim
 1  
 2  import sugar
 3  
 4  import std/math
 5  import std/sequtils
 6  
 7  #import constantine/math/arithmetic
 8  import constantine/math/io/io_fields
 9  import constantine/math/io/io_bigints
10  #import constantine/serialization/codecs
11  
12  import poseidon2/types
13  import poseidon2/io
14  #import poseidon2/compress
15  import poseidon2/merkle
16  import poseidon2/sponge
17  
18  #-------------------------------------------------------------------------------
19  
20  proc testVectorsSponge() = 
21    echo( "" )
22    echo( "NIM | test vectors for sponge of field elements with rate=1" )
23    echo( "-----------------------------------------------------------" )
24    for n in 0..8:
25      let input : seq[F] = collect( newSeq , (for i in 1..n: toF(i)) )
26      let hash = Sponge.digest(input, rate = 1)
27      echo( "hash of [1.." & ($n) & "] : seq[F] =  " & toDecimal(hash) )
28  
29    echo( "" )
30    echo( "NIM | test vectors for sponge of field elements with rate=2" )
31    echo( "-----------------------------------------------------------" )
32    for n in 0..8:
33      let input : seq[F] = collect( newSeq , (for i in 1..n: toF(i)) )
34      let hash = Sponge.digest(input, rate = 2)
35      echo( "hash of [1.." & ($n) & "] : seq[F] =  " & toDecimal(hash) )
36  
37  #-------------------------------------------------------------------------------
38  
39  proc testVectorsHash() = 
40    echo ""
41    echo "NIM | test vectors for hash (padded sponge with rate=2) of bytes"
42    echo "----------------------------------------------------------------"
43    for n in 0..80:
44      let input : seq[byte] = collect( newSeq , (for i in 1..n: byte(i)) )
45      let hash = Sponge.digest(input, rate=2)
46      echo( "hash of [1.." & ($n) & "] : seq[byte] =  " & toDecimal(hash) )
47  
48  #-------------------------------------------------------------------------------
49  
50  proc testVectorsMerkle() =
51    echo ""
52    echo "NIM | test vectors for Merkle roots of field elements"
53    echo "-----------------------------------------------------"
54    for n in 1..40:
55      let input : seq[F] = collect( newSeq , (for i in 1..n: toF(i)) )
56      let root = Merkle.digest(input)
57      echo( "Merkle root of [1.." & ($n) & "] : seq[F] =  " & toDecimal(root) )
58  
59    echo ""
60    echo "NIM | test vectors for Merkle roots of sequence of bytes"
61    echo "--------------------------------------------------------"
62    for n in 0..80:
63      let input : seq[byte] = collect( newSeq , (for i in 1..n: byte(i)) )
64      let root = Merkle.digest(input)
65      echo( "Merkle root of [1.." & ($n) & "] : seq[byte] =  " & toDecimal(root) )
66  
67  #-------------------------------------------------------------------------------
68  
69  when isMainModule:
70    testVectorsSponge()
71    testVectorsHash()
72    testVectorsMerkle()
73  
74  #-------------------------------------------------------------------------------