/ reference / nim / proof_input / src / types.nim
types.nim
  1  
  2  import std/strutils
  3  import std/sequtils
  4  
  5  #-------------------------------------------------------------------------------
  6  
  7  type Cell*  = seq[byte]
  8  type Block* = seq[byte]
  9  
 10  #-------------------------------------------------------------------------------
 11  
 12  type 
 13  
 14    MerkleProof*[H] = object
 15      leafIndex*      : int             # linear index of the leaf, starting from 0
 16      leafValue*      : H               # value of the leaf 
 17      merklePath*     : seq[H]          # order: from the bottom to the top
 18      numberOfLeaves* : int             # number of leaves in the tree (=size of input)
 19  
 20    MerkleTree*[H] = object
 21      layers*: seq[seq[H]]        
 22      # ^^^ note: the first layer is the bottom layer, and the last layer is the root
 23  
 24  #-------------------------------------------------------------------------------
 25  
 26  # the circuit expect merkle path of statically known length, so we need to pad them
 27  func padMerkleProof*[H]( old: MerkleProof[H], newlen: int ): MerkleProof[H] = 
 28    let pad = newlen - old.merklePath.len
 29    assert( pad >= 0 )
 30  
 31    var zero : H       # hackety hack hack, it should be initialized to zero
 32  
 33    return MerkleProof[H]( leafIndex:       old.leafIndex       
 34                         , leafValue:       old.leafValue      
 35                         , merklePath:      old.merklePath & repeat(zero,pad)
 36                         , numberOfLeaves:  old.numberOfLeaves
 37                         )
 38  
 39  #-------------------------------------------------------------------------------
 40  
 41  type 
 42  
 43    Seed*     = uint64
 44    CellIdx*  = int
 45    BlockIdx* = int
 46    SlotIdx*  = int
 47  
 48    CellProofInput*[H] = object
 49      cellData*:    Cell
 50      merkleProof*: MerkleProof[H]
 51  
 52    SlotProofInput*[H] = object
 53      dataSetRoot*:    H        # Root
 54      entropy*:        H        # Entropy
 55      nSlots*:         int
 56      nCells*:         int
 57      slotRoot*:       H        # Root
 58      slotIndex*:      SlotIdx
 59      slotProof*:      MerkleProof[H]
 60      proofInputs*:    seq[CellProofInput[H]]
 61  
 62  #-------------------------------------------------------------------------------
 63  
 64  type 
 65    DataSourceKind* = enum 
 66      SlotFile,
 67      FakeData
 68  
 69    DataSource* = object
 70      case kind*: DataSourceKind
 71        of SlotFile: 
 72          filename*: string
 73        of FakeData: 
 74          seed*:     Seed
 75  
 76    SlotConfig* = object
 77      nCells*   : int           # number of cells per slot (should be power of two)
 78      nSamples* : int           # how many cells we sample
 79      dataSrc*  : DataSource    # slot data source
 80  
 81    DataSetConfig* = object
 82      nSlots*   : int           # number of slots in the dataset
 83      nCells*   : int           # number of cells per slot (should be power of two)
 84      nSamples* : int           # how many cells we sample
 85      dataSrc*  : DataSource    # slot data source
 86  
 87    GlobalConfig* = object
 88      maxDepth*      : int      # maximum depth of the big merkle tree (log2 of maximum numbers of cells per slot)
 89      maxLog2NSlots* : int      # log2 of maximum number of slots per dataset
 90      cellSize*      : int      # size of the cells we prove (2048)
 91      blockSize*     : int      # size of the network block (65536)
 92  
 93    HashConfig* = object
 94      field*   : FieldSelect
 95      hashFun* : HashSelect
 96      combo*   : FieldHashCombo
 97      
 98    FieldSelect* = enum
 99      BN254,
100      Goldilocks
101  
102    HashSelect* = enum
103      Poseidon2,
104      Monolith
105  
106    FieldHashCombo* = enum
107      BN254_Poseidon2,
108      Goldilocks_Poseidon2,
109      Goldilocks_Monolith
110  
111  #-------------------------------------------------------------------------------
112  
113  func cellsPerBlock*(glob: GlobalConfig): int = 
114    let k = (glob.blockSize div glob.cellSize)
115    assert( k * glob.cellSize == glob.blockSize , "block size is not divisible by cell size" )
116    return k
117  
118  #-------------------------------------------------------------------------------
119  
120  func parseField*(str0: string): FieldSelect =
121    let str = strutils.toLowerAscii(str0)
122    case str:
123      of "bn254":      return BN254
124      of "goldilocks": return Goldilocks
125      else: raiseAssert("parsefield: unrecognized field `" & str0 & "`")
126  
127  func parseHashFun*(str0: string): HashSelect =
128    let str = strutils.toLowerAscii(str0)
129    case str:
130      of "poseidon2":  return Poseidon2
131      of "monolith":   return Monolith
132      else: raiseAssert("parsefield: unrecognized hash function `" & str0 & "`")
133  
134  #-------------------------------------------------------------------------------
135  
136  {. warning[UnreachableElse]:off .}
137  func toFieldHashCombo*( field: FieldSelect, hash: HashSelect ): FieldHashCombo = 
138    let msg = "invalid hash function `" & ($hash) & "` choice for field `" & ($field) & "`"
139    case field:
140      of BN254: 
141        case hash:
142          of Poseidon2: return BN254_Poseidon2
143          else: raiseAssert(msg)
144      of Goldilocks: 
145        case hash:
146          of Poseidon2: return Goldilocks_Poseidon2
147          of Monolith:  return Goldilocks_Monolith
148          else: raiseAssert(msg)
149  
150  #-------------------------------------------------------------------------------