mirror of
https://github.com/logos-storage/circom-witnessgen.git
synced 2026-01-02 13:03:09 +00:00
update the Haskell parser to the format change in 94fc740080
This commit is contained in:
parent
2420269dc4
commit
1289d9b7ad
@ -58,7 +58,7 @@ I haven't yet done any proper testing, apart from "works for our purposes".
|
|||||||
Known bugs:
|
Known bugs:
|
||||||
|
|
||||||
- comparison ignores the "signed" semantics of circom
|
- comparison ignores the "signed" semantics of circom
|
||||||
- integer division and modulo is not implemented
|
- integer division and modulo is not implemented (in the Nim version)
|
||||||
|
|
||||||
### Circuit optimizations
|
### Circuit optimizations
|
||||||
|
|
||||||
|
|||||||
@ -59,6 +59,9 @@ data TresOp
|
|||||||
newtype BigUInt
|
newtype BigUInt
|
||||||
= BigUInt [Word8] -- ^ little endian
|
= BigUInt [Word8] -- ^ little endian
|
||||||
|
|
||||||
|
dummyBigUInt :: BigUInt
|
||||||
|
dummyBigUInt = BigUInt [0]
|
||||||
|
|
||||||
showBigUInt :: BigUInt -> String
|
showBigUInt :: BigUInt -> String
|
||||||
showBigUInt (BigUInt bytes) = "0x" ++ concatMap f (reverse bytes) where
|
showBigUInt (BigUInt bytes) = "0x" ++ concatMap f (reverse bytes) where
|
||||||
f :: Word8 -> String
|
f :: Word8 -> String
|
||||||
@ -98,9 +101,16 @@ newtype WitnessMapping
|
|||||||
|
|
||||||
type CircuitInputs = [(String, SignalDescription)]
|
type CircuitInputs = [(String, SignalDescription)]
|
||||||
|
|
||||||
|
data Prime = Prime
|
||||||
|
{ primeNumber :: !BigUInt
|
||||||
|
, primeName :: !String
|
||||||
|
}
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
data GraphMetaData = GraphMetaData
|
data GraphMetaData = GraphMetaData
|
||||||
{ witnessMapping :: WitnessMapping
|
{ witnessMapping :: WitnessMapping
|
||||||
, inputSignals :: CircuitInputs
|
, inputSignals :: CircuitInputs
|
||||||
|
, prime :: Prime
|
||||||
}
|
}
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
|
|||||||
@ -15,11 +15,12 @@ import Control.Monad
|
|||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import System.IO
|
import System.IO
|
||||||
|
|
||||||
import Data.ByteString.Lazy (ByteString)
|
import Data.ByteString.Lazy ( ByteString )
|
||||||
import qualified Data.ByteString.Lazy as L
|
import qualified Data.ByteString.Lazy as L
|
||||||
import qualified Data.ByteString.Lazy.Char8 as LC
|
import qualified Data.ByteString.Lazy.Char8 as LC
|
||||||
|
|
||||||
import "binary" Data.Binary.Get
|
import "binary" Data.Binary.Get
|
||||||
|
import "binary" Data.Binary.Get.Internal ( lookAhead )
|
||||||
import "binary" Data.Binary.Builder as Builder
|
import "binary" Data.Binary.Builder as Builder
|
||||||
|
|
||||||
import Graph
|
import Graph
|
||||||
@ -109,6 +110,10 @@ varUInt = fromIntegral <$> varInt'
|
|||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
expectingError :: Int -> String -> Int -> Get a
|
||||||
|
expectingError actual what shouldbe = do
|
||||||
|
error $ what ++ ": expecting field " ++ show shouldbe ++ "; got " ++ show actual ++ " instead"
|
||||||
|
|
||||||
getNodes :: Get [Node]
|
getNodes :: Get [Node]
|
||||||
getNodes = do
|
getNodes = do
|
||||||
n <- getWord64le
|
n <- getWord64le
|
||||||
@ -144,26 +149,39 @@ getConstantNode = do
|
|||||||
bs <- getLazyByteString (fromIntegral len)
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
return $ ConstantNode (runGet getBigUInt bs)
|
return $ ConstantNode (runGet getBigUInt bs)
|
||||||
|
|
||||||
getBigUInt :: Get BigUInt
|
getBigUInt' :: FieldId -> Get BigUInt
|
||||||
getBigUInt = do
|
getBigUInt' expectedId = do
|
||||||
fld <- getFieldId LEN
|
fld <- getFieldId LEN
|
||||||
if fld /= 1
|
if fld /= expectedId
|
||||||
then error "getBigUInt"
|
then expectingError fld "getBigUInt" expectedId
|
||||||
else do
|
else do
|
||||||
len <- varInt
|
len <- varInt
|
||||||
bs <- getLazyByteString (fromIntegral len)
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
return $ BigUInt (runGet getByteList bs)
|
return $ BigUInt (runGet getByteList bs)
|
||||||
|
|
||||||
|
getBigUInt ::Get BigUInt
|
||||||
|
getBigUInt = getBigUInt' 1
|
||||||
|
|
||||||
getByteList :: Get [Word8]
|
getByteList :: Get [Word8]
|
||||||
getByteList = do
|
getByteList = do
|
||||||
fld <- getFieldId LEN
|
fld <- getFieldId LEN
|
||||||
if fld /= 1
|
if fld /= 1
|
||||||
then error "getByteList"
|
then expectingError fld "getByteList" 1
|
||||||
else do
|
else do
|
||||||
len <- varInt
|
len <- varInt
|
||||||
bs <- getLazyByteString (fromIntegral len)
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
return (L.unpack bs)
|
return (L.unpack bs)
|
||||||
|
|
||||||
|
getString' :: FieldId -> Get String
|
||||||
|
getString' expectedId = do
|
||||||
|
fld <- getFieldId LEN
|
||||||
|
if fld /= expectedId
|
||||||
|
then expectingError fld "getString" expectedId
|
||||||
|
else do
|
||||||
|
len <- varInt
|
||||||
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
|
return (LC.unpack bs)
|
||||||
|
|
||||||
getUnoOpNode :: Get UnoOpNode
|
getUnoOpNode :: Get UnoOpNode
|
||||||
getUnoOpNode = do
|
getUnoOpNode = do
|
||||||
SomeNode op arg1 _ _ <- getSomeNode
|
SomeNode op arg1 _ _ <- getSomeNode
|
||||||
@ -220,13 +238,23 @@ getMetaData = do
|
|||||||
len <- varInt
|
len <- varInt
|
||||||
mapping <- getWitnessMapping
|
mapping <- getWitnessMapping
|
||||||
inputs <- getCircuitInputs
|
inputs <- getCircuitInputs
|
||||||
return $ GraphMetaData mapping inputs
|
prime <- getPrime
|
||||||
|
return $ GraphMetaData mapping inputs prime
|
||||||
|
|
||||||
|
getPrime :: Get Prime
|
||||||
|
getPrime = do
|
||||||
|
number <- getBigUInt' 3
|
||||||
|
name <- getString' 4
|
||||||
|
return $ Prime
|
||||||
|
{ primeNumber = number
|
||||||
|
, primeName = name
|
||||||
|
}
|
||||||
|
|
||||||
getWitnessMapping :: Get WitnessMapping
|
getWitnessMapping :: Get WitnessMapping
|
||||||
getWitnessMapping = do
|
getWitnessMapping = do
|
||||||
fld <- getFieldId LEN
|
fld <- getFieldId LEN
|
||||||
if fld /= 1
|
if fld /= 1
|
||||||
then error "getWitnessMapping: expecting field 1"
|
then expectingError fld "getWitnessMapping" 1
|
||||||
else do
|
else do
|
||||||
len <- varInt
|
len <- varInt
|
||||||
bs <- getLazyByteString (fromIntegral len)
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
@ -240,20 +268,30 @@ getWitnessMapping = do
|
|||||||
getCircuitInputs :: Get CircuitInputs
|
getCircuitInputs :: Get CircuitInputs
|
||||||
getCircuitInputs = worker where
|
getCircuitInputs = worker where
|
||||||
|
|
||||||
|
{-
|
||||||
worker :: Get [(String, SignalDescription)]
|
worker :: Get [(String, SignalDescription)]
|
||||||
worker = isEmpty >>= \b -> if b
|
worker = isEmpty >>= \b -> if b
|
||||||
then return []
|
then return []
|
||||||
else (:) <$> getSingleInput <*> worker
|
else (:) <$> getSingleInput <*> worker
|
||||||
|
-}
|
||||||
|
|
||||||
getSingleInput :: Get (String, SignalDescription)
|
worker :: Get [(String, SignalDescription)]
|
||||||
|
worker = do
|
||||||
|
mb <- getSingleInput
|
||||||
|
case mb of
|
||||||
|
Nothing -> return []
|
||||||
|
Just this -> (this:) <$> worker
|
||||||
|
|
||||||
|
getSingleInput :: Get (Maybe (String, SignalDescription))
|
||||||
getSingleInput = do
|
getSingleInput = do
|
||||||
fld <- getFieldId LEN
|
fld <- lookAhead (getFieldId LEN)
|
||||||
if fld /= 2
|
if fld /= 2
|
||||||
then error "getCircuitInputs: expecting field 2"
|
then return Nothing -- expectingError fld "getSingleInput" 2
|
||||||
else do
|
else do
|
||||||
len <- varInt
|
_fld <- getFieldId LEN
|
||||||
bs <- getLazyByteString (fromIntegral len)
|
len <- varInt
|
||||||
return $ runGet inputHelper bs
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
|
return $ Just $ runGet inputHelper bs
|
||||||
|
|
||||||
inputHelper = do
|
inputHelper = do
|
||||||
name <- getName
|
name <- getName
|
||||||
@ -264,7 +302,7 @@ getCircuitInputs = worker where
|
|||||||
getName = do
|
getName = do
|
||||||
fld <- getFieldId LEN
|
fld <- getFieldId LEN
|
||||||
if fld /= 1
|
if fld /= 1
|
||||||
then error "getCircuitInputs/getName: expecting field 1"
|
then expectingError fld "getCircuitInputs/getName" 1
|
||||||
else do
|
else do
|
||||||
len <- varInt
|
len <- varInt
|
||||||
bs <- getLazyByteString (fromIntegral len)
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
@ -274,7 +312,7 @@ getCircuitInputs = worker where
|
|||||||
getSignal = do
|
getSignal = do
|
||||||
fld <- getFieldId LEN
|
fld <- getFieldId LEN
|
||||||
if fld /= 2
|
if fld /= 2
|
||||||
then error "getCircuitInputs/getSignal: expecting field 2"
|
then expectingError fld "getCircuitInputs/getSignal" 2
|
||||||
else do
|
else do
|
||||||
len <- varInt
|
len <- varInt
|
||||||
bs <- getLazyByteString (fromIntegral len)
|
bs <- getLazyByteString (fromIntegral len)
|
||||||
@ -288,13 +326,13 @@ getCircuitInputs = worker where
|
|||||||
getSignalOffset = do
|
getSignalOffset = do
|
||||||
fld <- getFieldId VARINT
|
fld <- getFieldId VARINT
|
||||||
if fld /= 1
|
if fld /= 1
|
||||||
then error "getCircuitInputs/getSignalOffset: expecting field 1"
|
then expectingError fld "getCircuitInputs/getSignalOffset" 1
|
||||||
else varUInt
|
else varUInt
|
||||||
|
|
||||||
getSignalLength = do
|
getSignalLength = do
|
||||||
fld <- getFieldId VARINT
|
fld <- getFieldId VARINT
|
||||||
if fld /= 2
|
if fld /= 2
|
||||||
then error "getCircuitInputs/getSignalLength: expecting field 2"
|
then expectingError fld "getCircuitInputs/getSignalLength" 2
|
||||||
else varUInt
|
else varUInt
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|||||||
@ -84,4 +84,6 @@ message SignalDescription {
|
|||||||
message GraphMetadata {
|
message GraphMetadata {
|
||||||
repeated uint32 witnessSignals = 1;
|
repeated uint32 witnessSignals = 1;
|
||||||
map<string, SignalDescription> inputs = 2;
|
map<string, SignalDescription> inputs = 2;
|
||||||
|
BigUInt prime = 3;
|
||||||
|
string prime_str = 4;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user