2024-02-09 21:40:30 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/sugar
|
|
|
|
import std/options
|
|
|
|
|
|
|
|
import ../../../asynctest
|
2024-02-19 18:58:39 +00:00
|
|
|
|
|
|
|
import pkg/chronos
|
2024-02-09 21:40:30 +00:00
|
|
|
import pkg/poseidon2
|
|
|
|
import pkg/datastore
|
2024-03-19 03:25:13 +00:00
|
|
|
import pkg/serde/json
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
import pkg/codex/slots {.all.}
|
|
|
|
import pkg/codex/slots/types {.all.}
|
|
|
|
import pkg/codex/merkletree
|
|
|
|
import pkg/codex/codextypes
|
|
|
|
import pkg/codex/manifest
|
|
|
|
import pkg/codex/stores
|
|
|
|
|
|
|
|
import ./helpers
|
|
|
|
import ../helpers
|
2024-05-30 06:57:10 +00:00
|
|
|
import ../../helpers
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
suite "Test Circom Compat Backend - control inputs":
|
|
|
|
let
|
|
|
|
r1cs = "tests/circuits/fixtures/proof_main.r1cs"
|
|
|
|
wasm = "tests/circuits/fixtures/proof_main.wasm"
|
2024-02-19 18:58:39 +00:00
|
|
|
zkey = "tests/circuits/fixtures/proof_main.zkey"
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
circom: CircomCompat
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInputs: ProofInputs[Poseidon2Hash]
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
let
|
|
|
|
inputData = readFile("tests/circuits/fixtures/input.json")
|
2024-03-19 03:25:13 +00:00
|
|
|
inputJson = !JsonNode.parse(inputData)
|
2024-02-09 21:40:30 +00:00
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInputs = Poseidon2Hash.jsonToProofInput(inputJson)
|
|
|
|
circom = CircomCompat.init(r1cs, wasm, zkey)
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
teardown:
|
2024-02-19 18:58:39 +00:00
|
|
|
circom.release() # this comes from the rust FFI
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
test "Should verify with correct inputs":
|
|
|
|
let
|
2024-02-19 18:58:39 +00:00
|
|
|
proof = circom.prove(proofInputs).tryGet
|
2024-02-09 21:40:30 +00:00
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
check circom.verify(proof, proofInputs).tryGet
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
test "Should not verify with incorrect inputs":
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInputs.slotIndex = 1 # change slot index
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
let
|
2024-02-19 18:58:39 +00:00
|
|
|
proof = circom.prove(proofInputs).tryGet
|
2024-02-09 21:40:30 +00:00
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
check circom.verify(proof, proofInputs).tryGet == false
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
suite "Test Circom Compat Backend":
|
|
|
|
let
|
|
|
|
ecK = 2
|
|
|
|
ecM = 2
|
2024-02-19 18:58:39 +00:00
|
|
|
slotId = 3
|
|
|
|
samples = 5
|
2024-02-09 21:40:30 +00:00
|
|
|
numDatasetBlocks = 8
|
|
|
|
blockSize = DefaultBlockSize
|
|
|
|
cellSize = DefaultCellSize
|
|
|
|
|
|
|
|
r1cs = "tests/circuits/fixtures/proof_main.r1cs"
|
|
|
|
wasm = "tests/circuits/fixtures/proof_main.wasm"
|
2024-02-19 18:58:39 +00:00
|
|
|
zkey = "tests/circuits/fixtures/proof_main.zkey"
|
2024-02-09 21:40:30 +00:00
|
|
|
|
2024-05-30 06:57:10 +00:00
|
|
|
repoTmp = TempLevelDb.new()
|
|
|
|
metaTmp = TempLevelDb.new()
|
|
|
|
|
2024-02-09 21:40:30 +00:00
|
|
|
var
|
|
|
|
store: BlockStore
|
|
|
|
manifest: Manifest
|
|
|
|
protected: Manifest
|
|
|
|
verifiable: Manifest
|
|
|
|
circom: CircomCompat
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInputs: ProofInputs[Poseidon2Hash]
|
2024-02-09 21:40:30 +00:00
|
|
|
challenge: array[32, byte]
|
|
|
|
builder: Poseidon2Builder
|
|
|
|
sampler: Poseidon2Sampler
|
|
|
|
|
|
|
|
setup:
|
|
|
|
let
|
2024-05-30 06:57:10 +00:00
|
|
|
repoDs = repoTmp.newDb()
|
|
|
|
metaDs = metaTmp.newDb()
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
store = RepoStore.new(repoDs, metaDs)
|
|
|
|
|
|
|
|
(manifest, protected, verifiable) =
|
|
|
|
await createVerifiableManifest(
|
|
|
|
store,
|
|
|
|
numDatasetBlocks,
|
|
|
|
ecK, ecM,
|
|
|
|
blockSize,
|
|
|
|
cellSize)
|
|
|
|
|
|
|
|
builder = Poseidon2Builder.new(store, verifiable).tryGet
|
2024-10-29 10:11:48 +00:00
|
|
|
(await builder.init()).tryGet()
|
2024-02-09 21:40:30 +00:00
|
|
|
sampler = Poseidon2Sampler.new(slotId, store, builder).tryGet
|
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
circom = CircomCompat.init(r1cs, wasm, zkey)
|
2024-02-09 21:40:30 +00:00
|
|
|
challenge = 1234567.toF.toBytes.toArray32
|
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInputs = (await sampler.getProofInput(challenge, samples)).tryGet
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
teardown:
|
2024-02-19 18:58:39 +00:00
|
|
|
circom.release() # this comes from the rust FFI
|
2024-05-30 06:57:10 +00:00
|
|
|
await repoTmp.destroyDb()
|
|
|
|
await metaTmp.destroyDb()
|
|
|
|
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
test "Should verify with correct input":
|
|
|
|
var
|
2024-02-19 18:58:39 +00:00
|
|
|
proof = circom.prove(proofInputs).tryGet
|
2024-02-09 21:40:30 +00:00
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
check circom.verify(proof, proofInputs).tryGet
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
test "Should not verify with incorrect input":
|
2024-02-19 18:58:39 +00:00
|
|
|
proofInputs.slotIndex = 1 # change slot index
|
2024-02-09 21:40:30 +00:00
|
|
|
|
|
|
|
let
|
2024-02-19 18:58:39 +00:00
|
|
|
proof = circom.prove(proofInputs).tryGet
|
2024-02-09 21:40:30 +00:00
|
|
|
|
2024-02-19 18:58:39 +00:00
|
|
|
check circom.verify(proof, proofInputs).tryGet == false
|