add padding of Merkle paths (to fixed length) to the Nim reference impl

This commit is contained in:
Balazs Komuves 2023-12-14 14:21:10 +01:00
parent 4d101442ca
commit cbcf9b476c
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
6 changed files with 31 additions and 8 deletions

View File

@ -15,8 +15,8 @@ smallDataSetCfg = MkDataSetCfg
, _maxLog2NSlots = 5
, _nSlots = 5
, _cellSize = 128
, _blockSize = 1024
, _nCells = 64
, _blockSize = 4096 -- 1024
, _nCells = 256 -- 64
, _nSamples = 10
, _dataSrc = FakeData (Seed 12345)
}

View File

@ -19,6 +19,11 @@ const exDataSetCfg* =
, dataSrc: DataSource(kind: FakeData, seed: 12345)
)
const exGlobalCfg* =
GlobalConfig( maxDepth: 16
, maxLog2NSlots: 5
)
#-------------------------------------------------------------------------------
{.overflowChecks: off.}

View File

@ -29,7 +29,7 @@ proc buildSlotTreeFull( slotCfg: SlotConfig ): (seq[MerkleTree], MerkleTree) =
proc buildSlotTree( slotCfg: SlotConfig ): MerkleTree =
return buildSlotTreeFull(slotCfg)[1]
proc generateProofInput*( dsetCfg: DataSetConfig, slotIdx: SlotIdx, entropy: Entropy ): SlotProofInput =
proc generateProofInput*( globCfg: GlobalConfig, dsetCfg: DataSetConfig, slotIdx: SlotIdx, entropy: Entropy ): SlotProofInput =
let nslots = dsetCfg.nSlots
let ncells = dsetCfg.nCells
let nblocks = ncells div cellsPerBlock
@ -57,7 +57,7 @@ proc generateProofInput*( dsetCfg: DataSetConfig, slotIdx: SlotIdx, entropy: Ent
let cellData = slotLoadCellData( ourSlotCfg, cellIdx )
let botProof = merkleProof( blockTree , cellIdx mod cellsPerBlock )
let topProof = merkleProof( bigTree , blockIdx )
let prf = mergeMerkleProofs( botProof, topProof )
let prf = padMerkleProof( mergeMerkleProofs( botProof, topProof ), globCfg.maxDepth )
inputs.add( CellProofInput(cellData: cellData, merkleProof: prf) )
return SlotProofInput( dataSetRoot: dsetRoot
@ -66,7 +66,7 @@ proc generateProofInput*( dsetCfg: DataSetConfig, slotIdx: SlotIdx, entropy: Ent
, nSlots: nslots
, slotIndex: slotIdx
, slotRoot: ourSlotRoot
, slotProof: slotProof
, slotProof: padMerkleProof( slotProof, globCfg.maxLog2NSlots )
, proofInputs: inputs
)

View File

@ -44,7 +44,7 @@ genFakeCell cfg (Seed seed) (CellIdx idx) = (mkCellData cfg $ B.pack list) where
go cnt state = fromIntegral state'' : go (cnt-1) state'' where
state' = state*(state + seed1)*(state + seed2) + state*(state `xor` 0x5a5a5a5a) + seed1*state + (seed2 + 17)
state'' = mod state' 1698428844001831
#]
]#
#-------------------------------------------------------------------------------

View File

@ -50,8 +50,9 @@ when isMainModule:
let slotIdx = 3
let fakedata = DataSource(kind: FakeData, seed: 12345)
let dsetcfg = DataSetConfig( nCells: 256, nSlots: 5, nSamples: 5, dataSrc: fakedata)
let globcfg = GlobalConfig( maxDepth: 16, maxLog2NSlots: 5)
let dsetcfg = DataSetConfig( nCells: 256, nSlots: 5, nSamples: 10, dataSrc: fakedata)
let entropy = toF( 1234567 )
let prfInput = generateProofInput(dsetcfg, slotIdx, entropy)
let prfInput = generateProofInput(globcfg, dsetcfg, slotIdx, entropy)
exportProofInput( "json/foo.json" , prfInput )

View File

@ -1,5 +1,6 @@
import std/strutils
import std/sequtils
from constantine/math/io/io_fields import toDecimal
@ -48,6 +49,18 @@ type
#-------------------------------------------------------------------------------
# the circuit expect merkle path of statically known length, so we need to pad them
func padMerkleProof*( old: MerkleProof, newlen: int ): MerkleProof =
let pad = newlen - old.merklePath.len
assert( pad >= 0 )
return MerkleProof( leafIndex: old.leafIndex
, leafValue: old.leafValue
, merklePath: old.merklePath & repeat(zero,pad)
, numberOfLeaves: old.numberOfLeaves
)
#-------------------------------------------------------------------------------
type
Seed* = uint64
@ -94,4 +107,8 @@ type
nSamples* : int # how many cells we sample
dataSrc* : DataSource # slot data source
GlobalConfig* = object
maxDepth* : int # maximum depth of the big merkle tree (log2 of maximum numbers of cells per slot)
maxLog2NSlots* : int # log2 of maximum number of slots per dataset
#-------------------------------------------------------------------------------