/ reference / haskell / src / TestVectors.hs
TestVectors.hs
 1  
 2  -- | Generate test vectors to compare with other implementations
 3  
 4  module TestVectors where
 5  
 6  --------------------------------------------------------------------------------
 7  
 8  import Control.Monad
 9  
10  import Data.Word
11  import qualified Data.ByteString as B
12  
13  import Poseidon2.Merkle
14  import Poseidon2.Sponge
15  import Slot
16  
17  import ZK.Algebra.Curves.BN128.Fr.Mont (Fr)
18  
19  --------------------------------------------------------------------------------
20  
21  allTestVectors = do
22    testVectorsSponge 
23    testVectorsHash
24    testVectorsMerkle
25  
26  --------------------------------------------------------------------------------
27  
28  testVectorsSponge :: IO ()
29  testVectorsSponge = do
30    putStrLn ""
31    putStrLn "test vectors for sponge of field elements with rate=1"
32    putStrLn "-----------------------------------------------------"
33    forM_ [0..8] $ \n -> do
34      let input = map fromIntegral [1..n] :: [Fr]
35      putStrLn $ "hash of [1.." ++ show n ++ "] :: [Fr] =  " ++ show (sponge1 input)
36  
37    putStrLn ""
38    putStrLn "test vectors for sponge of field elements with rate=2"
39    putStrLn "-----------------------------------------------------"
40    forM_ [0..8] $ \n -> do
41      let input = map fromIntegral [1..n] :: [Fr]
42      putStrLn $ "hash of [1.." ++ show n ++ "] :: [Fr] =  " ++ show (sponge2 input)
43  
44  --------------------------------------------------------------------------------
45  
46  testVectorsHash :: IO ()
47  testVectorsHash = do
48  
49    putStrLn ""
50    putStrLn "test vectors for hash (padded sponge with rate=2) of bytes"
51    putStrLn "----------------------------------------------------------"
52    forM_ [0..80] $ \n -> do
53      let input = map fromIntegral [1..n] :: [Word8]
54      let bs    = B.pack input
55      putStrLn $ "hash of [1.." ++ show n ++ "] :: [Byte] =  " ++ show (hashCell_ bs)
56  
57  --------------------------------------------------------------------------------
58  
59  testVectorsMerkle :: IO ()
60  testVectorsMerkle = do
61    putStrLn ""
62    putStrLn "test vectors for Merkle roots of field elements"
63    putStrLn "-----------------------------------------------"
64    forM_ [1..40] $ \n -> do
65      let input = map fromIntegral [1..n] :: [Fr]
66      putStrLn $ "Merkle root of [1.." ++ show n ++ "] :: [Fr]  =  " ++ show (calcMerkleRoot input)
67  
68    putStrLn ""
69    putStrLn "test vectors for Merkle roots of sequence of bytes"
70    putStrLn "--------------------------------------------------"
71    forM_ [0..80] $ \n -> do
72      let input = map fromIntegral [1..n] :: [Word8]
73      let bs    = B.pack input
74      let flds  = cellDataToFieldElements (CellData bs)
75      putStrLn $ "Merkle root of [1.." ++ show n ++ "] :: [Byte]  =  " ++ show (calcMerkleRoot flds)
76  
77  --------------------------------------------------------------------------------