/ reference / haskell / src / DataSet.hs
DataSet.hs
 1  
 2  {-# LANGUAGE StrictData #-}
 3  module DataSet where
 4  
 5  --------------------------------------------------------------------------------
 6  
 7  import Data.List
 8  import System.FilePath
 9  
10  import Slot hiding ( MkSlotCfg(..) )
11  import qualified Slot as Slot
12  
13  import Misc
14  
15  --------------------------------------------------------------------------------
16  
17  data DataSetCfg = MkDataSetCfg 
18    { _maxDepth      :: Int       -- ^ @nCells@ must fit into this many bits
19    , _maxLog2NSlots :: Int       -- ^ @nSlots@ must fit into this many bits
20    , _nSlots    :: Int           -- ^ number of slots per dataset
21    , _cellSize  :: Int           -- ^ cell size in bytes
22    , _blockSize :: Int           -- ^ slot size in bytes
23    , _nCells    :: Int           -- ^ number of cells per slot
24    , _nSamples  :: Int           -- ^ number of cells we sample in a proof
25    , _dataSrc   :: DataSource
26    }
27    deriving Show
28  
29  fieldElemsPerCell :: DataSetCfg -> Int
30  fieldElemsPerCell cfg = (DataSet._cellSize cfg + 30) `div` 31
31  
32  dataSetSlotCfg :: DataSetCfg -> SlotIdx -> SlotConfig
33  dataSetSlotCfg dsetCfg idx = Slot.MkSlotCfg
34    { Slot._cellSize  = DataSet._cellSize  dsetCfg
35    , Slot._blockSize = DataSet._blockSize dsetCfg
36    , Slot._nCells    = DataSet._nCells    dsetCfg
37    , Slot._nSamples  = DataSet._nSamples  dsetCfg
38    , Slot._dataSrc   = parametricSlotDataSource (DataSet._dataSrc dsetCfg) idx
39    }
40  
41  --------------------------------------------------------------------------------
42  
43  loadDataSetCell :: DataSetCfg -> SlotIdx -> CellIdx -> IO CellData
44  loadDataSetCell dsetCfg slotIdx@(SlotIdx idx) cellidx
45    | idx < 0                 = error "loadDataSetCell: negative slot index"
46    | idx >= _nSlots dsetCfg  = error "loadDataSetCell: slot index out of range"
47    | otherwise               = loadCellData (dataSetSlotCfg dsetCfg slotIdx) cellidx
48    
49  
50  loadDataSetBlock :: DataSetCfg -> SlotIdx -> BlockIdx -> IO BlockData
51  loadDataSetBlock dsetCfg slotIdx@(SlotIdx idx) blockidx
52    | idx < 0                 = error "loadDataSetBlock: negative slot index"
53    | idx >= _nSlots dsetCfg  = error "loadDataSetBlock: slot index out of range"
54    | otherwise               = loadBlockData (dataSetSlotCfg dsetCfg slotIdx) blockidx
55  
56  --------------------------------------------------------------------------------
57  
58  -- | Writes a @circom@ main component with the given parameters
59  --
60  -- > template SampleAndProve( nFieldElemsPerCell, nSamples ) { ... }
61  --
62  circomMainComponent :: DataSetCfg -> FilePath -> IO ()
63  circomMainComponent dsetCfg circomFile = do
64  
65    let cellsPerBlock = (DataSet._blockSize dsetCfg) `div` (DataSet._cellSize dsetCfg)
66    let blockDepth    = ceilingLog2 (fromIntegral cellsPerBlock)
67  
68    let params = intercalate ", " $ map show
69           [ DataSet._maxDepth         dsetCfg
70           , DataSet._maxLog2NSlots    dsetCfg
71           , blockDepth
72           , DataSet.fieldElemsPerCell dsetCfg
73           , DataSet._nSamples         dsetCfg
74           ]
75    writeFile circomFile $ unlines
76      [ "pragma circom 2.0.0;"
77      , "include \"sample_cells.circom\";"
78      , "// SampleAndProven( maxDepth, maxLog2NSlots, blockTreeDepth, nFieldElemsPerCell, nSamples ) "
79      , "component main {public [entropy,dataSetRoot,slotIndex]} = SampleAndProve(" ++ params ++ ");"
80      ]
81  
82  --------------------------------------------------------------------------------
83  
84  parametricSlotSeed :: Seed -> SlotIdx -> Seed
85  parametricSlotSeed (Seed seed) (SlotIdx k) = Seed (seed + 72 + 1001*k)
86  
87  -- | From @dir/dset.dat@ we make @dir/dset5.dat@ for the 5-th slot
88  parametricSlotFileName :: FilePath -> SlotIdx -> FilePath
89  parametricSlotFileName basefile (SlotIdx k) =
90    (dropExtension basefile ++ show k) <.> (takeExtension basefile)
91  
92  parametricSlotDataSource :: DataSource -> SlotIdx -> DataSource
93  parametricSlotDataSource src idx = case src of
94    FakeData seed  -> FakeData (parametricSlotSeed     seed  idx)
95    SlotFile fpath -> SlotFile (parametricSlotFileName fpath idx)
96  
97  --------------------------------------------------------------------------------