Skip to content

Commit e80be3e

Browse files
committed
proper signal size
1 parent cf6e34f commit e80be3e

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

Diff for: circom-compat/src/Circom/CLI.hs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Circom.CLI (defaultMain) where
22

33
import Circom.R1CS (R1CSHeader (rhPrime), decodeR1CSHeaderFromFile, r1csFromCircomR1CS, r1csToCircomR1CS, witnessFromCircomWitness)
44
import Circom.Solver (CircomProgram (..), mkCircomProgram, nativeGenWitness)
5-
import Circuit.Arithmetic (CircuitVars (..), InputBindings (labelToVar), VarType (..), restrictVars)
5+
import Circuit.Arithmetic (CircuitVars (..), InputBindings (labelToVar), VarType (..), inputSizes, restrictVars)
66
import Circuit.Dataflow qualified as DataFlow
77
import Circuit.Dot (arithCircuitToDot)
88
import Circuit.Language.Compile (BuilderState (..), ExprM, runCircuitBuilder)
@@ -21,7 +21,6 @@ import GHC.TypeNats (SNat, withKnownNat, withSomeSNat)
2121
import Numeric (showHex)
2222
import Options.Applicative (CommandFields, Mod, Parser, ParserInfo, command, eitherReader, execParser, fullDesc, header, help, helper, hsubparser, info, long, option, progDesc, showDefault, showDefaultWith, strOption, switch, value)
2323
import Protolude
24-
import Protolude.Unsafe (unsafeHead)
2524
import R1CS (R1CS, Witness (Witness), isValidWitness, toR1CS)
2625
import Prelude (MonadFail (fail))
2726

@@ -328,11 +327,7 @@ decodeIOVars enc v = do
328327
mkInputsTemplate :: Encoding -> CircuitVars Text -> IOVars
329328
mkInputsTemplate enc vars =
330329
let inputsOnly = cvInputsLabels $ restrictVars vars (cvPrivateInputs vars `IntSet.union` cvPublicInputs vars)
331-
vs =
332-
map (\a -> (fst $ unsafeHead a, length a)) $
333-
groupBy (\a b -> fst a == fst b) $
334-
Map.keys $
335-
labelToVar inputsOnly
330+
vs = Map.toList $ inputSizes inputsOnly
336331
f (label, len) =
337332
if len > 1
338333
then (label, Array (replicate len 0))

Diff for: circom-compat/src/Circom/Solver.hs

+10-4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ data ProgramEnv f = ProgramEnv
7777
peInputsSize :: Int,
7878
peWitnessSize :: Int,
7979
peCircuit :: ArithCircuit f,
80+
peSignalSizes :: Map FNVHash Int,
8081
peCircuitVars :: CircuitVars FNVHash
8182
}
8283

@@ -86,14 +87,16 @@ mkProgramEnv ::
8687
CircomProgram f ->
8788
ProgramEnv f
8889
mkProgramEnv CircomProgram {cpVars = vars, cpCircuit = circ} =
89-
ProgramEnv
90+
let vs = relabel hashText vars
91+
in ProgramEnv
9092
{ peFieldSize = FieldSize 32,
9193
peRawPrime = toInteger $ char (1 :: f),
9294
peVersion = 2,
9395
peInputsSize = IntSet.size $ cvPrivateInputs vars <> cvPublicInputs vars,
9496
peWitnessSize = IntSet.size $ IntSet.insert oneVar $ cvVars vars,
9597
peCircuit = circ,
96-
peCircuitVars = relabel hashText vars
98+
peSignalSizes = panic "",
99+
peCircuitVars = vs
97100
}
98101

99102
data ProgramState f = ProgramState
@@ -150,8 +153,11 @@ _getInputSize :: ProgramEnv f -> Int
150153
_getInputSize = peInputsSize
151154

152155
-- we dont (yet) support multiple values (e.g. arrays) for signal values
153-
_getInputSignalSize :: Word32 -> Word32 -> IO Int
154-
_getInputSignalSize _ _ = pure 1
156+
_getInputSignalSize :: ProgramEnv f -> Word32 -> Word32 -> IO Int
157+
_getInputSignalSize ProgramEnv {peSignalSizes} msb lsb =
158+
let h = mkFNV msb lsb
159+
in pure $ fromMaybe 0 $ Map.lookup h peSignalSizes
160+
155161

156162
-- we ignore the last arugment because our signals don't have indices, only names
157163
_setInputSignal ::

Diff for: circuit/src/Circuit/Arithmetic.hs

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Circuit.Arithmetic
2323
nGates,
2424
InputBindings (..),
2525
insertInputBinding,
26+
inputSizes,
2627
Reindexable (..),
2728
restrictVars,
2829
)
@@ -50,6 +51,7 @@ import Text.PrettyPrint.Leijen.Text as PP
5051
vcat,
5152
(<+>),
5253
)
54+
import Protolude.Unsafe (unsafeHead)
5355

5456
data InputType = Public | Private deriving (Show, Eq, Ord, Generic, NFData)
5557

@@ -489,6 +491,13 @@ restrictInputBindings s InputBindings {..} =
489491
varToLabel = IntMap.restrictKeys varToLabel s
490492
}
491493

494+
inputSizes :: (Ord label) => InputBindings label -> Map label Int
495+
inputSizes InputBindings {..} =
496+
Map.fromList $
497+
map (\a -> (fst $ unsafeHead a, length a)) $
498+
groupBy (\a b -> fst a == fst b) $
499+
Map.keys labelToVar
500+
492501
--------------------------------------------------------------------------------
493502

494503
class Reindexable a where

0 commit comments

Comments
 (0)