2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
-- | 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)
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
2026-04-22 17:05:00 +02:00
|
|
|
allTestVectors :: IO ()
|
2023-11-16 14:01:22 +01:00
|
|
|
allTestVectors = do
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn "\nTEST VECTORS FOR *OLD* ROUND CONSTANTS"
|
|
|
|
|
putStrLn "======================================"
|
|
|
|
|
allTestVectors' HorizenLabsOld
|
|
|
|
|
putStrLn "\nTEST VECTORS FOR *NEW* ROUND CONSTANTS"
|
|
|
|
|
putStrLn "======================================"
|
|
|
|
|
allTestVectors' HorizenLabsNew
|
|
|
|
|
|
|
|
|
|
allTestVectors' :: Flavour -> IO ()
|
|
|
|
|
allTestVectors' flavour = do
|
|
|
|
|
testVectorsSponge flavour
|
|
|
|
|
testVectorsHash flavour
|
|
|
|
|
testVectorsMerkle flavour
|
2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
2026-04-22 17:05:00 +02:00
|
|
|
testVectorsSponge :: Flavour -> IO ()
|
|
|
|
|
testVectorsSponge flavour = do
|
2023-11-16 14:01:22 +01:00
|
|
|
putStrLn ""
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "test vectors for sponge of field elements with rate=1 | " ++ show flavour
|
|
|
|
|
putStrLn "-------------------------------------------------------------------"
|
2023-11-16 14:01:22 +01:00
|
|
|
forM_ [0..8] $ \n -> do
|
|
|
|
|
let input = map fromIntegral [1..n] :: [Fr]
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "hash of [1.." ++ show n ++ "] :: [Fr] = " ++ show (sponge1 flavour input)
|
2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
putStrLn ""
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "test vectors for sponge of field elements with rate=2 | " ++ show flavour
|
|
|
|
|
putStrLn "-------------------------------------------------------------------"
|
2023-11-16 14:01:22 +01:00
|
|
|
forM_ [0..8] $ \n -> do
|
|
|
|
|
let input = map fromIntegral [1..n] :: [Fr]
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "hash of [1.." ++ show n ++ "] :: [Fr] = " ++ show (sponge2 flavour input)
|
2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
2026-04-22 17:05:00 +02:00
|
|
|
testVectorsHash :: Flavour -> IO ()
|
|
|
|
|
testVectorsHash flavour = do
|
2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
putStrLn ""
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "test vectors for hash (padded sponge with rate=2) of bytes | " ++ show flavour
|
2023-11-16 14:01:22 +01:00
|
|
|
putStrLn "----------------------------------------------------------"
|
|
|
|
|
forM_ [0..80] $ \n -> do
|
|
|
|
|
let input = map fromIntegral [1..n] :: [Word8]
|
|
|
|
|
let bs = B.pack input
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "hash of [1.." ++ show n ++ "] :: [Byte] = " ++ show (hashCell_ flavour bs)
|
2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
2026-04-22 17:05:00 +02:00
|
|
|
testVectorsMerkle :: Flavour -> IO ()
|
|
|
|
|
testVectorsMerkle flavour = do
|
2023-11-16 14:01:22 +01:00
|
|
|
putStrLn ""
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "test vectors for Merkle roots of field elements | " ++ show flavour
|
2023-11-16 14:01:22 +01:00
|
|
|
putStrLn "-----------------------------------------------"
|
|
|
|
|
forM_ [1..40] $ \n -> do
|
|
|
|
|
let input = map fromIntegral [1..n] :: [Fr]
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "Merkle root of [1.." ++ show n ++ "] :: [Fr] = " ++ show (calcMerkleRoot flavour input)
|
2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
putStrLn ""
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "test vectors for Merkle roots of sequence of bytes | " ++ show flavour
|
2023-11-16 14:01:22 +01:00
|
|
|
putStrLn "--------------------------------------------------"
|
|
|
|
|
forM_ [0..80] $ \n -> do
|
|
|
|
|
let input = map fromIntegral [1..n] :: [Word8]
|
|
|
|
|
let bs = B.pack input
|
2023-11-23 14:39:20 +01:00
|
|
|
let flds = cellDataToFieldElements (CellData bs)
|
2026-04-22 17:05:00 +02:00
|
|
|
putStrLn $ "Merkle root of [1.." ++ show n ++ "] :: [Byte] = " ++ show (calcMerkleRoot flavour flds)
|
2023-11-16 14:01:22 +01:00
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|