fix the JSON does not handle large numbers problem

This commit is contained in:
Balazs Komuves 2023-10-26 18:03:24 +02:00
parent 1f4ae5874d
commit 5581ba56f5
No known key found for this signature in database
GPG Key ID: 52130F4FE3E2C2BE
4 changed files with 25 additions and 14 deletions

View File

@ -1,3 +1,3 @@
pragma circom 2.0.0;
include "sample_cells.circom";
component main {public [entropy,slotRoot]} = SampleAndProveV1(1024, 9, 20);
component main {public [entropy,slotRoot]} = SampleAndProveV1(1024, 5, 10);

View File

@ -13,10 +13,10 @@ import qualified ZK.Algebra.Curves.BN128.Fr.Mont as Fr
--------------------------------------------------------------------------------
samplingTest :: FilePath -> IO ()
samplingTest fpath = do
samplingTest :: SlotConfig -> FilePath -> IO ()
samplingTest slotCfg fpath = do
let entropy = 123456789 :: Fr
input <- calculateCircuitInput exSlotCfg entropy
input <- calculateCircuitInput slotCfg entropy
exportCircuitInput fpath input
--------------------------------------------------------------------------------
@ -59,15 +59,18 @@ calculateCircuitInput slotCfg entropy = do
-- | Export the inputs of the storage proof circuits in JSON format,
-- which @circom@ can consume.
--
-- NOTE: large numbers (field elements) must be encoded as JSON strings,
-- not numbers, as Javascript cannot handle large numbers!
--
exportCircuitInput :: FilePath -> CircuitInput -> IO ()
exportCircuitInput fpath input = do
h <- openFile fpath WriteMode
hPutStrLn h $ "{ \"entropy\": " ++ show (_entropy input)
hPutStrLn h $ ", \"slotRoot\": " ++ show (_slotRoot input)
hPutStrLn h $ "{ \"entropy\": " ++ show (show (_entropy input))
hPutStrLn h $ ", \"slotRoot\": " ++ show (show (_slotRoot input))
hPutStrLn h $ ", \"cellData\": "
hPrintListOfLists h (_cellData input)
hPrintListOfLists h ((map.map) show $ _cellData input)
hPutStrLn h $ ", \"merklePaths\": "
hPrintListOfLists h (_merklePaths input)
hPrintListOfLists h ((map.map) show $ _merklePaths input)
hPutStrLn h $ "}"
hClose h
@ -86,7 +89,7 @@ hPrintList' h indentation xs = do
hPutStrLn h (indentation False ++ "]")
hPrintList :: Show a => Handle -> Int -> [a] -> IO ()
hPrintList h indentBy xs = hPrintList' h (\_ -> indent indentBy) xs
hPrintList h indentBy xs = hPrintList' h (\_ -> indent indentBy) $ xs
hPrintListOfLists :: Show a => Handle -> [[a]] -> IO ()
hPrintListOfLists h xss =

View File

@ -52,8 +52,8 @@ fieldElemsPerCell cfg = (_cellSize cfg + 30) `div` 31
--
-- > template SampleAndProveV1( nCells, nFieldElemsPerCell, nSamples ) { ... }
--
circomMainComponentV1 :: FilePath -> SlotConfig -> IO ()
circomMainComponentV1 circomFile slotCfg = do
circomMainComponentV1 :: SlotConfig -> FilePath -> IO ()
circomMainComponentV1 slotCfg circomFile = do
let params = show (_nCells slotCfg)
++ ", " ++ show (fieldElemsPerCell slotCfg)

View File

@ -4,8 +4,16 @@ module Main where
import Slot
import Sampling
mySlotCfg :: SlotConfig
mySlotCfg = MkSlotCfg
{ _cellSize = 128
, _nCells = 1024
, _nSamples = 10
, _dataSrc = FakeData 12345
}
main :: IO ()
main = do
let slotCfg = exSlotCfg
circomMainComponentV1 "slot_main.circom" slotCfg
samplingTest "input_example.json"
let slotCfg = mySlotCfg
circomMainComponentV1 slotCfg "slot_main.circom"
samplingTest slotCfg "input_example.json"