From 4cfb707180ffabaaf047b8afab16f7de2fa2aa09 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 6 Feb 2024 10:21:31 -0600 Subject: [PATCH] add test using real samples --- .../codex/slots/backends/testcircomcompat.nim | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/codex/slots/backends/testcircomcompat.nim b/tests/codex/slots/backends/testcircomcompat.nim index e6a31c15..04a010dd 100644 --- a/tests/codex/slots/backends/testcircomcompat.nim +++ b/tests/codex/slots/backends/testcircomcompat.nim @@ -135,3 +135,74 @@ suite "Test Circom Compat Backend": proof = circom.prove(proofInput).tryGet check circom.verify(proof, publicInputs, verifyingKeyPtr[]).tryGet == false + +suite "Test Circom Compat Backend": + let + slotId = 3 + samples = 5 + blockSize = DefaultBlockSize + cellSize = DefaultCellSize + ecK = 2 + ecM = 2 + numDatasetBlocks = 8 + + r1cs = "tests/circuits/fixtures/proof_main.r1cs" + wasm = "tests/circuits/fixtures/proof_main.wasm" + + var + store: BlockStore + manifest: Manifest + protected: Manifest + verifiable: Manifest + circom: CircomCompat + verifyingKeyPtr: ptr CircomKey + proofInput: ProofInput[Poseidon2Hash] + publicInputs: CircomInputs + challenge: array[32, byte] + builder: Poseidon2Builder + sampler: Poseidon2Sampler + + setup: + let + repoDs = SQLiteDatastore.new(Memory).tryGet() + metaDs = SQLiteDatastore.new(Memory).tryGet() + + store = RepoStore.new(repoDs, metaDs) + + (manifest, protected, verifiable) = + await createVerifiableManifest( + store, + numDatasetBlocks, + ecK, ecM, + blockSize, + cellSize) + + builder = Poseidon2Builder.new(store, verifiable).tryGet + sampler = Poseidon2Sampler.new(slotId, store, builder).tryGet + + # circom = CircomCompat.init(r1cs, wasm, zkey) + circom = CircomCompat.init(r1cs, wasm) + verifyingKeyPtr = circom.getVerifyingKey().tryGet + challenge = 1234567.toF.toBytes.toArray32 + + proofInput = (await sampler.getProofInput(challenge, samples)).tryGet + publicInputs = proofInput.toPublicInputs.toCircomInputs + + teardown: + publicInputs.releaseNimInputs() # this is allocated by nim + verifyingKeyPtr.addr.releaseKey() # this comes from the rust FFI + circom.release() # this comes from the rust FFI + + test "Should verify with correct input": + var + proof = circom.prove(proofInput).tryGet + + check circom.verify(proof, publicInputs, verifyingKeyPtr[]).tryGet + + test "Should not verify with incorrect input": + proofInput.slotIndex = 1 # change slot index + + let + proof = circom.prove(proofInput).tryGet + + check circom.verify(proof, publicInputs, verifyingKeyPtr[]).tryGet == false