From ae03690d51d65a6b15093cf5c8eaa422f4a1e926 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 18 Jan 2024 13:37:33 +0100 Subject: [PATCH] Submit proof as field elements, not bytes --- contracts/Marketplace.sol | 2 +- contracts/Proofs.sol | 19 +++++++++---------- test/Marketplace.test.js | 9 ++++++--- test/examples.js | 4 +++- test/proof.js | 38 ++++++++++++++++---------------------- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index d96ebb7..54ecc08 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -100,7 +100,7 @@ contract Marketplace is Proofs, StateRetrieval { function fillSlot( RequestId requestId, uint256 slotIndex, - bytes calldata proof + uint256[8] calldata proof ) public requestIsKnown(requestId) { Request storage request = _requests[requestId]; require(slotIndex < request.ask.slots, "Invalid slot"); diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index 76948a3..3c466bd 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -108,20 +108,19 @@ abstract contract Proofs is Periods { return isRequired && pointer < _config.downtime; } - function submitProof(SlotId id, bytes calldata proof) public { + function submitProof(SlotId id, uint256[8] calldata proof) public { require(!_received[id][_blockPeriod()], "Proof already submitted"); - require(proof.length == 256, "invalid proof length"); uint256[2] memory a; uint256[2][2] memory b; uint256[2] memory c; - a[0] = uint256(bytes32(proof[0:32])); - a[1] = uint256(bytes32(proof[32:64])); - b[0][0] = uint256(bytes32(proof[64:96])); - b[0][1] = uint256(bytes32(proof[96:128])); - b[1][0] = uint256(bytes32(proof[128:160])); - b[1][1] = uint256(bytes32(proof[160:192])); - c[0] = uint256(bytes32(proof[192:224])); - c[1] = uint256(bytes32(proof[224:256])); + a[0] = proof[0]; + a[1] = proof[1]; + b[0][0] = proof[2]; + b[0][1] = proof[3]; + b[1][0] = proof[4]; + b[1][1] = proof[5]; + c[0] = proof[6]; + c[1] = proof[7]; // TODO: The `pubSignals` should be constructed from information that we already know: // - external entropy (for example some fresh ethereum block header) - this gives us the unbiased randomness we use to sample which cells to prove diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index 5244602..4fb879b 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -1,9 +1,12 @@ const { ethers } = require("hardhat") -const { hexlify, randomBytes } = ethers.utils const { AddressZero } = ethers.constants const { BigNumber } = ethers const { expect } = require("chai") -const { exampleConfiguration, exampleRequest } = require("./examples") +const { + exampleConfiguration, + exampleRequest, + exampleProof, +} = require("./examples") const { periodic, hours } = require("./time") const { requestId, slotId, askToArray } = require("./ids") const { @@ -76,7 +79,7 @@ describe("Marketplace constructor", function () { }) describe("Marketplace", function () { - const proof = hexlify(randomBytes(256)) + const proof = exampleProof() const config = exampleConfiguration() let marketplace diff --git a/test/examples.js b/test/examples.js index c6ea8f2..b3e4a02 100644 --- a/test/examples.js +++ b/test/examples.js @@ -39,4 +39,6 @@ const exampleRequest = async () => { } } -module.exports = { exampleConfiguration, exampleRequest } +const exampleProof = () => ([1, 2, 3, 4, 5, 6, 7, 8]) + +module.exports = { exampleConfiguration, exampleRequest, exampleProof } diff --git a/test/proof.js b/test/proof.js index fc71ba1..7b4fd59 100644 --- a/test/proof.js +++ b/test/proof.js @@ -1,40 +1,34 @@ const fs = require("fs") const ethers = require("ethers") -const { arrayify, concat } = ethers.utils const { BigNumber } = ethers const BASE_PATH = __dirname + "/../verifier/networks" const PROOF_FILE_NAME = "example-proof/proof.json" -function decimalToBytes(decimal) { - return arrayify(BigNumber.from(decimal).toHexString()) +function G1ToUInts(point) { + return [ + point[0], + point[1] + ] } -function G1ToBytes(point) { - return concat([ - decimalToBytes(point[0]), - decimalToBytes(point[1]) - ]) -} - -function G2ToBytes(point) { - return concat([ - decimalToBytes(point[0][1]), - decimalToBytes(point[0][0]), - decimalToBytes(point[1][1]), - decimalToBytes(point[1][0]) - ]) +function G2ToUInts(point) { + return [ + point[0][1], + point[0][0], + point[1][1], + point[1][0] + ] } function loadProof(name) { const proof = JSON.parse( fs.readFileSync(`${BASE_PATH}/${name}/${PROOF_FILE_NAME}`) ) - return concat([ - G1ToBytes(proof['pi_a']), - G2ToBytes(proof['pi_b']), - G1ToBytes(proof['pi_c']) - ]) + return [] + .concat(G1ToUInts(proof['pi_a'])) + .concat(G2ToUInts(proof['pi_b'])) + .concat(G1ToUInts(proof['pi_c'])) } module.exports = { loadProof }