Skip to content

Commit

Permalink
Merge pull request #27 from Chengxuan/propose-generate-script
Browse files Browse the repository at this point in the history
add a script for circuit compilation and verifier generation
  • Loading branch information
jimthematrix committed Aug 3, 2024
2 parents 56b4652 + 138a67b commit 39bd365
Show file tree
Hide file tree
Showing 49 changed files with 368 additions and 145 deletions.
2 changes: 1 addition & 1 deletion solidity/contracts/lib/verifier_anon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ contract Groth16Verifier_Anon {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[4] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
2 changes: 1 addition & 1 deletion solidity/contracts/lib/verifier_anon_enc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract Groth16Verifier_AnonEnc {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[7] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
2 changes: 1 addition & 1 deletion solidity/contracts/lib/verifier_anon_enc_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ contract Groth16Verifier_AnonEncNullifier {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[10] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
2 changes: 1 addition & 1 deletion solidity/contracts/lib/verifier_anon_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract Groth16Verifier_AnonNullifier {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[7] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/lib/verifier_check_hashes_value.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

pragma solidity >=0.7.0 <0.9.0;

contract Groth16Verifier_CheckValue {
contract Groth16Verifier_CheckHashesValue {
// Scalar field size
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
// Base field size
Expand Down Expand Up @@ -62,7 +62,7 @@ contract Groth16Verifier_CheckValue {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[2] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ contract Groth16Verifier_CheckInputsOutputsValue {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[4] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
2 changes: 1 addition & 1 deletion solidity/contracts/lib/verifier_check_nullifier_value.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract Groth16Verifier_CheckNullifierValue {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[7] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/lib/verifier_nf_anon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

pragma solidity >=0.7.0 <0.9.0;

contract Groth16Verifier_NFAnon {
contract Groth16Verifier_NfAnon {
// Scalar field size
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
// Base field size
Expand Down Expand Up @@ -62,7 +62,7 @@ contract Groth16Verifier_NFAnon {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[2] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/lib/verifier_nf_anon_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

pragma solidity >=0.7.0 <0.9.0;

contract Groth16Verifier_NFAnonNullifier {
contract Groth16Verifier_NfAnonNullifier {
// Scalar field size
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
// Base field size
Expand Down Expand Up @@ -65,7 +65,7 @@ contract Groth16Verifier_NFAnonNullifier {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[3] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
1 change: 0 additions & 1 deletion solidity/contracts/lib/zeto_common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pragma solidity ^0.8.20;

import {Commonlib} from "./common.sol";
import {Registry} from "./registry.sol";
import {Groth16Verifier_CheckValue} from "./verifier_check_hashes_value.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

/// @title A sample base implementation of a Zeto based token contract
Expand Down
6 changes: 3 additions & 3 deletions solidity/contracts/lib/zeto_fungible.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_CheckValue} from "./verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckHashesValue} from "./verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckNullifierValue} from "./verifier_check_nullifier_value.sol";
import {Commonlib} from "./common.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand All @@ -28,11 +28,11 @@ abstract contract ZetoFungible is Ownable {
// depositVerifier library for checking UTXOs against a claimed value.
// this can be used in the optional deposit calls to verify that
// the UTXOs match the deposited value
Groth16Verifier_CheckValue internal depositVerifier;
Groth16Verifier_CheckHashesValue internal depositVerifier;

IERC20 internal erc20;

constructor(Groth16Verifier_CheckValue _depositVerifier) {
constructor(Groth16Verifier_CheckHashesValue _depositVerifier) {
depositVerifier = _depositVerifier;
}

Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/lib/zeto_fungible_withdraw.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_CheckValue} from "./verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckHashesValue} from "./verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckInputsOutputsValue} from "./verifier_check_inputs_outputs_value.sol";
import {ZetoFungible} from "./zeto_fungible.sol";
import {Commonlib} from "./common.sol";
Expand All @@ -32,7 +32,7 @@ abstract contract ZetoFungibleWithdraw is ZetoFungible {
Groth16Verifier_CheckInputsOutputsValue internal withdrawVerifier;

constructor(
Groth16Verifier_CheckValue _depositVerifier,
Groth16Verifier_CheckHashesValue _depositVerifier,
Groth16Verifier_CheckInputsOutputsValue _withdrawVerifier
) ZetoFungible(_depositVerifier) {
withdrawVerifier = _withdrawVerifier;
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/lib/zeto_fungible_withdraw_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_CheckValue} from "./verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckHashesValue} from "./verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckNullifierValue} from "./verifier_check_nullifier_value.sol";
import {ZetoFungible} from "./zeto_fungible.sol";
import {Commonlib} from "./common.sol";
Expand All @@ -32,7 +32,7 @@ abstract contract ZetoFungibleWithdrawWithNullifiers is ZetoFungible {
Groth16Verifier_CheckNullifierValue internal withdrawVerifier;

constructor(
Groth16Verifier_CheckValue _depositVerifier,
Groth16Verifier_CheckHashesValue _depositVerifier,
Groth16Verifier_CheckNullifierValue _withdrawVerifier
) ZetoFungible(_depositVerifier) {
withdrawVerifier = _withdrawVerifier;
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/zeto_anon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_CheckValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckInputsOutputsValue} from "./lib/verifier_check_inputs_outputs_value.sol";
import {Groth16Verifier_Anon} from "./lib/verifier_anon.sol";
import {Registry} from "./lib/registry.sol";
Expand All @@ -37,7 +37,7 @@ contract Zeto_Anon is ZetoBase, ZetoFungibleWithdraw {
Groth16Verifier_Anon internal verifier;

constructor(
Groth16Verifier_CheckValue _depositVerifier,
Groth16Verifier_CheckHashesValue _depositVerifier,
Groth16Verifier_CheckInputsOutputsValue _withdrawVerifier,
Groth16Verifier_Anon _verifier,
Registry _registry
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/zeto_anon_enc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_CheckValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckInputsOutputsValue} from "./lib/verifier_check_inputs_outputs_value.sol";
import {Groth16Verifier_AnonEnc} from "./lib/verifier_anon_enc.sol";
import {ZetoFungibleWithdraw} from "./lib/zeto_fungible_withdraw.sol";
Expand All @@ -39,7 +39,7 @@ contract Zeto_AnonEnc is ZetoBase, ZetoFungibleWithdraw {
Groth16Verifier_AnonEnc internal verifier;

constructor(
Groth16Verifier_CheckValue _depositVerifier,
Groth16Verifier_CheckHashesValue _depositVerifier,
Groth16Verifier_CheckInputsOutputsValue _withdrawVerifier,
Groth16Verifier_AnonEnc _verifier,
Registry _registry
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/zeto_anon_enc_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_CheckValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckNullifierValue} from "./lib/verifier_check_nullifier_value.sol";
import {Groth16Verifier_AnonEncNullifier} from "./lib/verifier_anon_enc_nullifier.sol";
import {ZetoNullifier} from "./lib/zeto_nullifier.sol";
Expand All @@ -40,7 +40,7 @@ contract Zeto_AnonEncNullifier is
Groth16Verifier_AnonEncNullifier verifier;

constructor(
Groth16Verifier_CheckValue _depositVerifier,
Groth16Verifier_CheckHashesValue _depositVerifier,
Groth16Verifier_CheckNullifierValue _withdrawVerifier,
Groth16Verifier_AnonEncNullifier _verifier,
Registry _registry
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/zeto_anon_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_CheckValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckNullifierValue} from "./lib/verifier_check_nullifier_value.sol";
import {Groth16Verifier_AnonNullifier} from "./lib/verifier_anon_nullifier.sol";
import {ZetoNullifier} from "./lib/zeto_nullifier.sol";
Expand Down Expand Up @@ -44,7 +44,7 @@ contract Zeto_AnonNullifier is
Groth16Verifier_AnonNullifier verifier;

constructor(
Groth16Verifier_CheckValue _depositVerifier,
Groth16Verifier_CheckHashesValue _depositVerifier,
Groth16Verifier_CheckNullifierValue _withdrawVerifier,
Groth16Verifier_AnonNullifier _verifier,
Registry _registry
Expand Down
8 changes: 4 additions & 4 deletions solidity/contracts/zeto_nf_anon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_NFAnon} from "./lib/verifier_nf_anon.sol";
import {Groth16Verifier_NfAnon} from "./lib/verifier_nf_anon.sol";
import {ZetoBase} from "./lib/zeto_base.sol";
import {Registry} from "./lib/registry.sol";
import {Commonlib} from "./lib/common.sol";
Expand All @@ -28,11 +28,11 @@ import "hardhat/console.sol";
/// - The sender owns the private key whose public key is part of the pre-image of the input UTXOs commitments
/// (aka the sender is authorized to spend the input UTXOs)
/// - The input UTXOs and output UTXOs are valid in terms of obeying mass conservation rules
contract Zeto_NFAnon is ZetoBase {
Groth16Verifier_NFAnon internal verifier;
contract Zeto_NfAnon is ZetoBase {
Groth16Verifier_NfAnon internal verifier;

constructor(
Groth16Verifier_NFAnon _verifier,
Groth16Verifier_NfAnon _verifier,
Registry _registry
) ZetoBase(_registry) {
verifier = _verifier;
Expand Down
8 changes: 4 additions & 4 deletions solidity/contracts/zeto_nf_anon_nullifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// limitations under the License.
pragma solidity ^0.8.20;

import {Groth16Verifier_NFAnonNullifier} from "./lib/verifier_nf_anon_nullifier.sol";
import {Groth16Verifier_NfAnonNullifier} from "./lib/verifier_nf_anon_nullifier.sol";
import {ZetoNullifier} from "./lib/zeto_nullifier.sol";
import {Registry} from "./lib/registry.sol";
import {Commonlib} from "./lib/common.sol";
Expand All @@ -34,11 +34,11 @@ uint256 constant MAX_SMT_DEPTH = 64;
/// - the hashes in the input and output match the hash(value, salt, owner public key) formula
/// - the sender possesses the private BabyJubjub key, whose public key is part of the pre-image of the input commitment hashes, which match the corresponding nullifiers
/// - the nullifiers represent input commitments that are included in a Sparse Merkle Tree represented by the root hash
contract Zeto_NFAnonNullifier is ZetoNullifier {
Groth16Verifier_NFAnonNullifier verifier;
contract Zeto_NfAnonNullifier is ZetoNullifier {
Groth16Verifier_NfAnonNullifier verifier;

constructor(
Groth16Verifier_NFAnonNullifier _verifier,
Groth16Verifier_NfAnonNullifier _verifier,
Registry _registry
) ZetoNullifier(_registry) {
verifier = _verifier;
Expand Down
6 changes: 3 additions & 3 deletions solidity/contracts/zkDvP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pragma solidity ^0.8.20;

import {Commonlib} from "./lib/common.sol";
import {Zeto_Anon} from "./zeto_anon.sol";
import {Zeto_NFAnon} from "./zeto_nf_anon.sol";
import {Zeto_NfAnon} from "./zeto_nf_anon.sol";
import "hardhat/console.sol";

/// @title A sample on-chain implementation of a DvP escrow contract using ZKP based C-UTXO tokens
Expand Down Expand Up @@ -56,7 +56,7 @@ contract zkDvP {
}

Zeto_Anon paymentToken;
Zeto_NFAnon assetToken;
Zeto_NfAnon assetToken;
mapping(uint256 => Trade) trades;
uint256 tradeCount;

Expand All @@ -67,7 +67,7 @@ contract zkDvP {
constructor(address paymentTokenAddress, address assetTokenAddress) {
tradeCount = 0;
paymentToken = Zeto_Anon(paymentTokenAddress);
assetToken = Zeto_NFAnon(assetTokenAddress);
assetToken = Zeto_NfAnon(assetTokenAddress);
}

function initiateTrade(
Expand Down
4 changes: 2 additions & 2 deletions solidity/ignition/modules/zeto_anon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

const DepositVerifierModule = buildModule("Groth16Verifier_CheckValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckValue', []);
const DepositVerifierModule = buildModule("Groth16Verifier_CheckHashesValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckHashesValue', []);
return { verifier };
});

Expand Down
4 changes: 2 additions & 2 deletions solidity/ignition/modules/zeto_anon_enc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

const DepositVerifierModule = buildModule("Groth16Verifier_CheckValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckValue', []);
const DepositVerifierModule = buildModule("Groth16Verifier_CheckHashesValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckHashesValue', []);
return { verifier };
});

Expand Down
4 changes: 2 additions & 2 deletions solidity/ignition/modules/zeto_anon_enc_nullifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const SmtLibModule = buildModule("SmtLib", (m) => {
return { smtLib, poseidon3 };
});

const DepositVerifierModule = buildModule("Groth16Verifier_CheckValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckValue', []);
const DepositVerifierModule = buildModule("Groth16Verifier_CheckHashesValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckHashesValue', []);
return { verifier };
});

Expand Down
4 changes: 2 additions & 2 deletions solidity/ignition/modules/zeto_anon_nullifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const SmtLibModule = buildModule("SmtLib", (m) => {
return { smtLib, poseidon3 };
});

const DepositVerifierModule = buildModule("Groth16Verifier_CheckValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckValue', []);
const DepositVerifierModule = buildModule("Groth16Verifier_CheckHashesValue", (m) => {
const verifier = m.contract('Groth16Verifier_CheckHashesValue', []);
return { verifier };
});

Expand Down
8 changes: 4 additions & 4 deletions solidity/ignition/modules/zeto_nf_anon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

const VerifierModule = buildModule("Groth16Verifier_NFAnon", (m) => {
const verifier = m.contract('Groth16Verifier_NFAnon', []);
const VerifierModule = buildModule("Groth16Verifier_NfAnon", (m) => {
const verifier = m.contract('Groth16Verifier_NfAnon', []);
return { verifier };
});

export default buildModule("Zeto_NFAnon", (m) => {
export default buildModule("Zeto_NfAnon", (m) => {
const { verifier } = m.useModule(VerifierModule);
const commonlib = m.library('Commonlib');

const registryAddress = m.getParameter("registry");
const registry = m.contractAt('Registry', registryAddress);

const zeto = m.contract('Zeto_NFAnon', [verifier, registry], {
const zeto = m.contract('Zeto_NfAnon', [verifier, registry], {
libraries: {
Commonlib: commonlib,
},
Expand Down
Loading

0 comments on commit 39bd365

Please sign in to comment.