Skip to content

Commit

Permalink
Replace the dynamic gas calculation with the static one (#17)
Browse files Browse the repository at this point in the history
* Replaced the dynamic gas calculation with the static one

* Updated versions
  • Loading branch information
KyrylR authored Nov 19, 2024
1 parent 4fa5a95 commit dfbfdc6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/zkit",
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
6 changes: 3 additions & 3 deletions src/core/templates/verifier_groth16.sol.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract <%=verifier_id%> {
mstore(add(pointer_, 32), y_)
mstore(add(pointer_, 64), s_)

res_ := staticcall(sub(gas(), 2000), 7, pointer_, 96, pointer_, 64) // ecMul
res_ := staticcall(6000, 7, pointer_, 96, pointer_, 64) // ecMul
res_ := and(res_, gt(returndatasize(), 0)) // check that multiplication succeeded

if iszero(res_) {
Expand All @@ -77,7 +77,7 @@ contract <%=verifier_id%> {
mstore(add(pointer_, 64), mload(pR_))
mstore(add(pointer_, 96), mload(add(pR_, 32)))

res_ := staticcall(sub(gas(), 2000), 6, pointer_, 128, pR_, 64) // ecAdd
res_ := staticcall(150, 6, pointer_, 128, pR_, 64) // ecAdd
res_ := and(res_, gt(returndatasize(), 0)) // check that addition succeeded
}

Expand Down Expand Up @@ -136,7 +136,7 @@ contract <%=verifier_id%> {
mstore(add(pPairing_, 704), DELTA_Y1)
mstore(add(pPairing_, 736), DELTA_Y2)

res_ := staticcall(sub(gas(), 2000), 8, pPairing_, 768, pPairing_, 32) // ecPairing
res_ := staticcall(181000, 8, pPairing_, 768, pPairing_, 32) // ecPairing
res_ := and(res_, mload(pPairing_)) // check that pairing succeeded
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/templates/verifier_groth16.vy.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def _g1MulAdd(pR: uint256[2], pP: uint256[2], s: uint256) -> (bool, uint256[2]):
success, response = raw_call(
EC_MUL_PRECOMPILED_ADDRESS,
abi_encode(pP, s),
gas=6000,
max_outsize=64,
is_static_call=True,
revert_on_failure=False
Expand All @@ -68,6 +69,7 @@ def _g1MulAdd(pR: uint256[2], pP: uint256[2], s: uint256) -> (bool, uint256[2]):
success, response = raw_call(
EC_ADD_PRECOMPILED_ADDRESS,
abi_encode(pR, pS),
gas=150,
max_outsize=64,
is_static_call=True,
revert_on_failure=False
Expand Down Expand Up @@ -107,6 +109,7 @@ def _checkPairing(pA: uint256[2], pB: uint256[2][2], pC: uint256[2], pubSignals:
pC,
DELTA_X1, DELTA_X2, DELTA_Y1, DELTA_Y2
),
gas=181000,
max_outsize=32,
is_static_call=True,
revert_on_failure=False
Expand Down
10 changes: 5 additions & 5 deletions src/core/templates/verifier_plonk.sol.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ contract <%=verifier_id%> {
mstore(add(mIn,64), mload(pP_))
mstore(add(mIn,96), mload(add(pP_, 32)))

res_ := staticcall(sub(gas(), 2000), 6, mIn, 128, pR_, 64)
res_ := staticcall(150, 6, mIn, 128, pR_, 64)
}

function g1_mulAccC(pR_, x_, y_, s_) -> res_ {
Expand All @@ -424,7 +424,7 @@ contract <%=verifier_id%> {
mstore(add(mIn,32), y_)
mstore(add(mIn,64), s_)

res_ := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64)
res_ := staticcall(6000, 7, mIn, 96, mIn, 64)

if iszero(res_) {
leave
Expand All @@ -433,7 +433,7 @@ contract <%=verifier_id%> {
mstore(add(mIn,64), mload(pR_))
mstore(add(mIn,96), mload(add(pR_, 32)))

res_ := staticcall(sub(gas(), 2000), 6, mIn, 128, pR_, 64)
res_ := staticcall(150, 6, mIn, 128, pR_, 64)
}

function g1_mulSetC(pR_, x_, y_, s_) -> res_ {
Expand All @@ -442,7 +442,7 @@ contract <%=verifier_id%> {
mstore(add(mIn,32), y_)
mstore(add(mIn,64), s_)

res_ := staticcall(sub(gas(), 2000), 7, mIn, 96, pR_, 64)
res_ := staticcall(6000, 7, mIn, 96, pR_, 64)
}

function g1_mulSet(pR_, pP_, s_) -> res_ {
Expand Down Expand Up @@ -748,7 +748,7 @@ contract <%=verifier_id%> {
mstore(add(mIn,320), G2_Y2)
mstore(add(mIn,352), G2_Y1)

if iszero(staticcall(sub(gas(), 2000), 8, mIn, 384, mIn, 0x20)) {
if iszero(staticcall(113000, 8, mIn, 384, mIn, 0x20)) {
leave
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/templates/verifier_plonk.vy.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def _ecadd(a: uint256[2], b: uint256[2]) -> (bool, uint256[2]):
success, response = raw_call(
EC_ADD_PRECOMPILED_ADDRESS,
abi_encode(a, b),
gas=150,
max_outsize=64,
is_static_call=True,
revert_on_failure=False
Expand All @@ -171,6 +172,7 @@ def _ecmul(p: uint256[2], s: uint256) -> (bool, uint256[2]):
success, response = raw_call(
EC_MUL_PRECOMPILED_ADDRESS,
abi_encode(p, s),
gas=6000,
max_outsize=64,
is_static_call=True,
revert_on_failure=False
Expand Down Expand Up @@ -639,6 +641,7 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
success, response = raw_call(
EC_PAIRING_PRECOMPILED_ADDRESS,
abi_encode(mIn),
gas=113000,
max_outsize=32,
is_static_call=True,
revert_on_failure=False
Expand Down
33 changes: 33 additions & 0 deletions test/CircuitZKit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,39 @@ describe("CircuitZKit", () => {
expect(await verifier.verifyProof(...data)).to.be.true;
});

it("should correctly create Vyper verifier and verify 'groth16' proof", async function () {
const circuitName = "Multiplier";
const verifierDirPath = getVerifiersDirFullPath();
const artifactsDirFullPath = getArtifactsFullPath(`${circuitName}.circom`);

const multiplierCircuit = getCircuitZKit<"groth16">(circuitName, "groth16", {
circuitName,
circuitArtifactsPath: artifactsDirFullPath,
verifierDirPath,
});

const expectedVerifierFilePath = path.join(verifierDirPath, `${multiplierCircuit.getVerifierName()}.vy`);

await multiplierCircuit.createVerifier("vy");
expect(fs.existsSync(expectedVerifierFilePath)).to.be.true;

await this.hre.run("compile", { quiet: true });

const a = 2;
const b = 3;

const proof: any = await multiplierCircuit.generateProof({ a, b });

expect(await multiplierCircuit.verifyProof(proof)).to.be.true;

let data = await multiplierCircuit.generateCalldata(proof);

const MultiplierVerifierFactory = await this.hre.ethers.getContractFactory("MultiplierGroth16Verifier");
const verifier = await MultiplierVerifierFactory.deploy();

expect(await verifier.verifyProof(...data)).to.be.true;
});

it("should correctly create Vyper verifier and verify 'plonk' proof", async function () {
const circuitName = "MultiDimensionalArray";
const verifierDirPath = getVerifiersDirFullPath();
Expand Down

0 comments on commit dfbfdc6

Please sign in to comment.