Haskell: minor refactoring to help debugging (by which I mean debugging the Nim one...)

This commit is contained in:
Balazs Komuves 2025-03-16 20:10:19 +01:00
parent 4651602cb6
commit dfe5239fcb
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
5 changed files with 55 additions and 15 deletions

View File

@ -21,10 +21,15 @@ import Witness
--------------------------------------------------------------------------------
exportWitness :: FilePath -> Witness -> IO ()
exportWitness fpath ws = do
exportWitness fpath (MkWitness ws) = do
let bs = runPut (putWitness ws)
L.writeFile fpath bs
exportFeltSequence :: FilePath -> Array Int F -> IO ()
exportFeltSequence fpath arr = do
let bs = runPut $ putRawWitness (elems arr)
L.writeFile fpath bs
--------------------------------------------------------------------------------
putHeader :: Int -> Put

View File

@ -4,6 +4,7 @@ module Graph where
--------------------------------------------------------------------------------
import Control.Monad
import Text.Printf
--------------------------------------------------------------------------------
@ -104,3 +105,21 @@ data GraphMetaData = GraphMetaData
deriving (Show)
--------------------------------------------------------------------------------
debugPrintGraph :: Graph -> IO ()
debugPrintGraph (Graph nodes meta) = do
forM_ (zip [0..] nodes) $ \(i,node) -> do
putStrLn $ show i ++ " -> " ++ showNode node
showNode :: Node -> String
showNode node = case node of
AnInputNode node -> show node
AConstantNode node -> show node
AnUnoOpNode node -> show node
ADuoOpNode node -> show node
ATresOpNode node -> show node
printNode :: Node -> IO ()
printNode = putStrLn . showNode
--------------------------------------------------------------------------------

View File

@ -32,14 +32,19 @@ testInputs = Map.fromList
graphFile = "../../tmp/graph4.bin"
inputFile = "../../tmp/input4.json"
wtnsFile = "../../tmp/my4.wtns"
fullFile = "../../tmp/my4_full.bin"
main :: IO ()
main = do
Right graph <- parseGraphFile graphFile
putStrLn ""
inputs <- loadInputJsonFile inputFile
print (inputSignals $ graphMeta graph)
let wtns = witnessCalc inputs graph
Right graph <- parseGraphFile graphFile
inputs <- loadInputJsonFile inputFile
-- putStrLn ""
-- print wtns
-- print (inputSignals $ graphMeta graph)
-- let full = fullComputation graph inputs
-- exportFeltSequence fullFile full
let wtns = witnessCalc graph inputs
exportWitness wtnsFile wtns

View File

@ -70,7 +70,7 @@ readGraphFile h = do
else do
hSeekInt h (flen - 8)
offset <- (fromIntegral . runGet getWord64le) <$> hGetBytes h 8
putStrLn $ "metadata offset = " ++ show offset
-- putStrLn $ "metadata offset = " ++ show offset
if (offset >= flen) || (offset <= 18)
then return $ Left "invalid final `graphMetaData` offset bytes"
else do

View File

@ -15,31 +15,42 @@ import BN254
import qualified Semantics as S ; import Semantics ( PrimOp , evalPrimOp )
import qualified Graph as G ; import Graph ( Graph(..) , Node(..) , UnoOpNode(..) , DuoOpNode(..) , TresOpNode(..) , SignalDescription(..) )
import Debug.Trace
debug msg x y = trace (">>> " ++ msg ++ " ~> " ++ show x) y
--------------------------------------------------------------------------------
type Witness = Array Int F
newtype Witness = MkWitness (Array Int F)
type Inputs = Map String [Integer]
witnessCalc :: Inputs -> Graph -> Witness
witnessCalc inputs (Graph nodes meta) = witness where
-- | This includes all temporary values (one per graph node), not all of which is
-- present in the final witness
fullComputation :: Graph -> Inputs -> Array Int F
fullComputation (Graph nodes meta) inputs = rawWitness where
nodesArr = listArray (0,length nodes-1) nodes
rawWitness = evaluateNodes rawInputs nodesArr
rawInputs = convertInputs (G.inputSignals meta) inputs
fullLogToWitness :: Graph -> Array Int F -> Witness
fullLogToWitness (Graph nodes meta) fullLog = MkWitness witness where
mapping_ = G.fromWitnessMapping (G.witnessMapping meta)
wtnslen = length mapping_
mapping = listArray (0,wtnslen-1) mapping_
witness = listArray (0,wtnslen-1) $ [ rawWitness!(fromIntegral (mapping!i)) | i<-[0..wtnslen-1] ]
witness = listArray (0,wtnslen-1) $ [ fullLog!(fromIntegral (mapping!i)) | i<-[0..wtnslen-1] ]
witnessCalc :: Graph -> Inputs -> Witness
witnessCalc graph inputs = fullLogToWitness graph (fullComputation graph inputs)
convertInputs :: [(String,SignalDescription)] -> Map String [Integer] -> IntMap F
convertInputs descTable inputTable = IntMap.fromList $ concatMap f descTable where
convertInputs descTable inputTable = IntMap.fromList $ (0,1) : concatMap f descTable where
f :: (String,SignalDescription) -> [(Int,F)]
f (name,desc) = case Map.lookup name inputTable of
Nothing -> error $ "input signal `" ++ name ++ "` not found in the given inputs!"
Just values -> if length values /= fromIntegral (signalLength desc)
then error $ "input signal `" ++ name ++ "` has incorrect size"
else let ofs = fromIntegral (signalOffset desc) :: Int
in (0,1) : zip [ofs..] (map toF values)
in zip [ofs..] (map toF values)
--------------------------------------------------------------------------------
@ -79,7 +90,7 @@ duoToPrimOp (DuoOpNode op arg1 arg2) = case op of
G.Mul -> S.Mul arg1 arg2
G.Div -> S.Div arg1 arg2
G.Add -> S.Add arg1 arg2
G.Sub -> S.Sub arg1 arg2
G.Sub -> S.Sub arg1 arg2 -- debug "sub " (op,arg1,arg2) $
G.Pow -> S.Pow arg1 arg2
G.Idiv -> S.Idiv arg1 arg2
G.Mod -> S.Mod arg1 arg2