2024-01-17 19:24:34 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/sugar
|
|
|
|
import std/random
|
|
|
|
import std/strutils
|
2024-02-08 02:27:11 +00:00
|
|
|
|
|
|
|
import ../../../asynctest
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/constantine/math/arithmetic
|
|
|
|
import pkg/constantine/math/io/io_fields
|
|
|
|
import pkg/poseidon2/io
|
|
|
|
import pkg/poseidon2
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/contracts/requests
|
|
|
|
import pkg/codex/contracts
|
|
|
|
import pkg/codex/merkletree
|
|
|
|
import pkg/codex/stores/cachestore
|
2024-02-08 02:27:11 +00:00
|
|
|
import pkg/codex/slots/types
|
2024-01-17 19:24:34 +00:00
|
|
|
import pkg/codex/slots/sampler/utils
|
2024-03-19 03:25:13 +00:00
|
|
|
import pkg/codex/utils/json
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
import ../backends/helpers
|
|
|
|
import ../../helpers
|
|
|
|
import ../../examples
|
|
|
|
import ../../merkletree/helpers
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
asyncchecksuite "Test proof sampler utils":
|
2024-02-08 02:27:11 +00:00
|
|
|
let
|
|
|
|
cellsPerBlock = DefaultBlockSize div DefaultCellSize
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
var
|
2024-02-08 02:27:11 +00:00
|
|
|
inputData: string
|
|
|
|
inputJson: JsonNode
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInput: ProofInputs[Poseidon2Hash]
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
setup:
|
2024-02-08 02:27:11 +00:00
|
|
|
inputData = readFile("tests/circuits/fixtures/input.json")
|
2024-03-19 03:25:13 +00:00
|
|
|
inputJson = !JsonNode.parse(inputData)
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInput = Poseidon2Hash.jsonToProofInput(inputJson)
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
test "Extract low bits":
|
|
|
|
proc extract(value: uint64, nBits: int): uint64 =
|
|
|
|
let big = toF(value).toBig()
|
|
|
|
return extractLowBits(big, nBits)
|
|
|
|
|
|
|
|
check:
|
|
|
|
extract(0x88, 4) == 0x8.uint64
|
|
|
|
extract(0x88, 7) == 0x8.uint64
|
|
|
|
extract(0x9A, 5) == 0x1A.uint64
|
|
|
|
extract(0x9A, 7) == 0x1A.uint64
|
|
|
|
extract(0x1248, 10) == 0x248.uint64
|
|
|
|
extract(0x1248, 12) == 0x248.uint64
|
|
|
|
extract(0x1248306A560C9AC0.uint64, 10) == 0x2C0.uint64
|
|
|
|
extract(0x1248306A560C9AC0.uint64, 12) == 0xAC0.uint64
|
|
|
|
extract(0x1248306A560C9AC0.uint64, 50) == 0x306A560C9AC0.uint64
|
|
|
|
extract(0x1248306A560C9AC0.uint64, 52) == 0x8306A560C9AC0.uint64
|
|
|
|
|
|
|
|
test "Can find single slot-cell index":
|
|
|
|
proc slotCellIndex(i: Natural): Natural =
|
2024-02-08 02:27:11 +00:00
|
|
|
return cellIndex(proofInput.entropy, proofInput.slotRoot, proofInput.nCellsPerSlot, i)
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
proc getExpectedIndex(i: int): Natural =
|
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
hash = Sponge.digest(@[proofInput.entropy, proofInput.slotRoot, toF(i)], rate = 2)
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
return int(extractLowBits(hash.toBig(), ceilingLog2(proofInput.nCellsPerSlot)))
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
slotCellIndex(1) == getExpectedIndex(1)
|
|
|
|
slotCellIndex(2) == getExpectedIndex(2)
|
|
|
|
slotCellIndex(3) == getExpectedIndex(3)
|
|
|
|
|
|
|
|
test "Can find sequence of slot-cell indices":
|
|
|
|
proc slotCellIndices(n: int): seq[Natural] =
|
2024-02-08 02:27:11 +00:00
|
|
|
cellIndices(proofInput.entropy, proofInput.slotRoot, numCells = proofInput.nCellsPerSlot, n)
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
proc getExpectedIndices(n: int): seq[Natural] =
|
2024-02-08 02:27:11 +00:00
|
|
|
return collect(newSeq, (for i in 1..n: cellIndex(proofInput.entropy, proofInput.slotRoot, proofInput.nCellsPerSlot, i)))
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
slotCellIndices(3) == getExpectedIndices(3)
|
|
|
|
|
|
|
|
for (input, expected) in [(10, 0), (31, 0), (32, 1), (63, 1), (64, 2)]:
|
|
|
|
test "Can get slotBlockIndex from slotCellIndex (" & $input & " -> " & $expected & ")":
|
2024-02-08 02:27:11 +00:00
|
|
|
let slotBlockIndex = toBlkInSlot(input, numCells = cellsPerBlock)
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
slotBlockIndex == expected
|
|
|
|
|
|
|
|
for (input, expected) in [(10, 10), (31, 31), (32, 0), (63, 31), (64, 0)]:
|
|
|
|
test "Can get blockCellIndex from slotCellIndex (" & $input & " -> " & $expected & ")":
|
2024-02-08 02:27:11 +00:00
|
|
|
let blockCellIndex = toCellInBlk(input, numCells = cellsPerBlock)
|
2024-01-17 19:24:34 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
blockCellIndex == expected
|