generate test vectors, using Haskell and Nim

This commit is contained in:
Balazs Komuves 2023-11-16 14:01:22 +01:00
parent 868247f64d
commit be5a0ed2a9
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
6 changed files with 171 additions and 3 deletions

View File

@ -1,4 +1,4 @@
DS_Store
.DS_Store
.ghc.environment*
dist
dist-newstyle

View File

@ -0,0 +1,77 @@
-- | Generate test vectors to compare with other implementations
module TestVectors where
--------------------------------------------------------------------------------
import Control.Monad
import Data.Word
import qualified Data.ByteString as B
import Poseidon2.Merkle
import Poseidon2.Sponge
import Slot
import ZK.Algebra.Curves.BN128.Fr.Mont (Fr)
--------------------------------------------------------------------------------
allTestVectors = do
testVectorsSponge
testVectorsHash
testVectorsMerkle
--------------------------------------------------------------------------------
testVectorsSponge :: IO ()
testVectorsSponge = do
putStrLn ""
putStrLn "test vectors for sponge of field elements with rate=1"
putStrLn "-----------------------------------------------------"
forM_ [0..8] $ \n -> do
let input = map fromIntegral [1..n] :: [Fr]
putStrLn $ "hash of [1.." ++ show n ++ "] :: [Fr] = " ++ show (sponge1 input)
putStrLn ""
putStrLn "test vectors for sponge of field elements with rate=2"
putStrLn "-----------------------------------------------------"
forM_ [0..8] $ \n -> do
let input = map fromIntegral [1..n] :: [Fr]
putStrLn $ "hash of [1.." ++ show n ++ "] :: [Fr] = " ++ show (sponge2 input)
--------------------------------------------------------------------------------
testVectorsHash :: IO ()
testVectorsHash = do
putStrLn ""
putStrLn "test vectors for hash (padded sponge with rate=2) of bytes"
putStrLn "----------------------------------------------------------"
forM_ [0..80] $ \n -> do
let input = map fromIntegral [1..n] :: [Word8]
let bs = B.pack input
putStrLn $ "hash of [1.." ++ show n ++ "] :: [Byte] = " ++ show (hashCell bs)
--------------------------------------------------------------------------------
testVectorsMerkle :: IO ()
testVectorsMerkle = do
putStrLn ""
putStrLn "test vectors for Merkle roots of field elements"
putStrLn "-----------------------------------------------"
forM_ [1..40] $ \n -> do
let input = map fromIntegral [1..n] :: [Fr]
putStrLn $ "Merkle root of [1.." ++ show n ++ "] :: [Fr] = " ++ show (calcMerkleRoot input)
putStrLn ""
putStrLn "test vectors for Merkle roots of sequence of bytes"
putStrLn "--------------------------------------------------"
forM_ [0..80] $ \n -> do
let input = map fromIntegral [1..n] :: [Word8]
let bs = B.pack input
let flds = cellDataToFieldElements bs
putStrLn $ "Merkle root of [1.." ++ show n ++ "] :: [Byte] = " ++ show (calcMerkleRoot flds)
--------------------------------------------------------------------------------

View File

@ -34,13 +34,14 @@ Library
zikkurat-algebra == 0.0.1
Exposed-Modules: Poseidon2
Sampling
Slot
TestVectors
Poseidon2.Example
Poseidon2.Merkle
Poseidon2.Permutation
Poseidon2.RoundConsts
Poseidon2.Sponge
Sampling
Slot
Default-Language: Haskell2010
Default-Extensions: CPP, BangPatterns

2
reference/nim/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.DS_Store
testvectors

View File

@ -0,0 +1,74 @@
import sugar
import std/math
import std/sequtils
#import constantine/math/arithmetic
import constantine/math/io/io_fields
import constantine/math/io/io_bigints
#import constantine/serialization/codecs
import poseidon2/types
import poseidon2/io
#import poseidon2/compress
import poseidon2/merkle
import poseidon2/sponge
#-------------------------------------------------------------------------------
proc testVectorsSponge() =
echo( "" )
echo( "NIM | test vectors for sponge of field elements with rate=1" )
echo( "-----------------------------------------------------------" )
for n in 0..8:
let input : seq[F] = collect( newSeq , (for i in 1..n: toF(i)) )
let hash = Sponge.digest(input, rate = 1)
echo( "hash of [1.." & ($n) & "] : seq[F] = " & toDecimal(hash) )
echo( "" )
echo( "NIM | test vectors for sponge of field elements with rate=2" )
echo( "-----------------------------------------------------------" )
for n in 0..8:
let input : seq[F] = collect( newSeq , (for i in 1..n: toF(i)) )
let hash = Sponge.digest(input, rate = 2)
echo( "hash of [1.." & ($n) & "] : seq[F] = " & toDecimal(hash) )
#-------------------------------------------------------------------------------
proc testVectorsHash() =
echo ""
echo "NIM | test vectors for hash (padded sponge with rate=2) of bytes"
echo "----------------------------------------------------------------"
for n in 0..80:
let input : seq[byte] = collect( newSeq , (for i in 1..n: byte(i)) )
let hash = Sponge.digest(input, rate=2)
echo( "hash of [1.." & ($n) & "] : seq[byte] = " & toDecimal(hash) )
#-------------------------------------------------------------------------------
proc testVectorsMerkle() =
echo ""
echo "NIM | test vectors for Merkle roots of field elements"
echo "-----------------------------------------------------"
for n in 1..40:
let input : seq[F] = collect( newSeq , (for i in 1..n: toF(i)) )
let root = merkleRoot(input)
echo( "Merkle root of [1.." & ($n) & "] : seq[F] = " & toDecimal(root) )
echo ""
echo "NIM | test vectors for Merkle roots of sequence of bytes"
echo "--------------------------------------------------------"
for n in 0..80:
let input : seq[byte] = collect( newSeq , (for i in 1..n: byte(i)) )
let root = merkleRoot(input)
echo( "Merkle root of [1.." & ($n) & "] : seq[byte] = " & toDecimal(root) )
#-------------------------------------------------------------------------------
when isMainModule:
testVectorsSponge()
testVectorsHash()
testVectorsMerkle()
#-------------------------------------------------------------------------------

View File

@ -0,0 +1,14 @@
# Package
version = "0.1.0"
author = "Balazs Komuves"
description = "test vectors using the nim-poseidon2 library"
license = "MIT"
srcDir = "src"
bin = @["testvectors"]
# Dependencies
requires "nim >= 1.6.0"
requires "https://github.com/mratsim/constantine"
requires "https://github.com/codex-storage/nim-poseidon2#596f7b18070b44ca0bf305bf9bdf1dc4f6011181"