Skip to content

Commit

Permalink
feat: structure contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Nov 29, 2024
1 parent b8e3251 commit af6592a
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 279 deletions.
File renamed without changes.
7 changes: 2 additions & 5 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[submodule "contracts/lib/forge-std"]
[submodule "circom/contracts/lib/forge-std"]
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "contracts/lib/openzeppelin-contracts"]
path = contracts/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
path = circom/contracts/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions circom/contracts/src/IGroth16Verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract IGroth16Verifier {
function verifyProof(
uint[2] calldata _pA,
uint[2][2] calldata _pB,
uint[2] calldata _pC,
uint[] calldata _pubSignals
) external view returns (bool);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {ERC721URIStorage} from "@openzeppelin/contracts/token/ERC721/extensions/
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {IDKIMRegistry} from "@zk-email/contracts/interfaces/IDKIMRegistry.sol";
import {IGroth16Verifier} from "./IGroth16Verifier.sol";

struct Proof {
uint256[2] a;
Expand Down Expand Up @@ -84,7 +86,7 @@ contract ZKEmailProof is ERC721, Ownable {
* @param publicOutputs uint256[] of public outputs
* @param decodedPublicOutputs Decoded public outputs as flattened json
*/
function safeMint(
function mintProof(
address to,
string memory blueprintId,
address verifier,
Expand All @@ -93,6 +95,21 @@ contract ZKEmailProof is ERC721, Ownable {
string memory decodedPublicOutputs,
uint proverEthAddressIdx
) public onlyVerifier {
// verify RSA
bytes32 ph = bytes32(publicOutputs[0]);
require(
dkimRegistry.isDKIMPublicKeyHashValid(domain, ph),
"RSA public key incorrect"
);

IGroth16Verifier v = IGroth16Verifier(verifier);

// verify proof
require(
v.verifyProof(proof.a, proof.b, proof.c, publicOutputs),
"Invalid proof"
);

// Owner should be committed to in each proof. This prevents
// frontrunning safeMint with a valid proof but malicious "to" address
if (address(uint160(publicOutputs[proverEthAddressIdx])) != to) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 42 additions & 1 deletion circom/templates/template.sol.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import "@zk-email/contracts/interfaces/IDKIMRegistry.sol";
import "@zk-email/contracts/utils/StringUtils.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./verifier.sol";
import {ZKEmailProof, Proof} from "../contracts/src/ZKEmailProof.sol";

contract Contract is Ownable {
// ============================
// Dependent Contracts
// ============================
IDKIMRegistry public dkimRegistry;
Groth16Verifier public verifier;
ZKEmailProof public proofMinter;

// ============================
// Prover Constants (Auto-generated)
Expand All @@ -26,9 +28,10 @@ contract Contract is Ownable {
uint16 public constant {{ field.name }}_len = {{ field.pack_size }};
{% endfor %}

constructor (IDKIMRegistry r, Groth16Verifier v) Ownable(msg.sender) {
constructor (IDKIMRegistry r, Groth16Verifier v, ZKEmailProof p) Ownable(msg.sender) {
dkimRegistry = r;
verifier = v;
proofMinter = p;
}

function verify(uint[2] calldata a, uint[2][2] calldata b, uint[2] calldata c, uint[{{ signal_size }}] calldata signals) external view {
Expand All @@ -39,4 +42,42 @@ contract Contract is Ownable {
// verify proof
require(verifier.verifyProof(a,b,c,signals), "Invalid proof");
}

function verifyAndMint(uint[2] calldata a, uint[2][2] calldata b, uint[2] calldata c, uint[{{ signal_size }}] calldata signals, address to, string memory blueprintId) external {
// verify RSA
bytes32 ph = bytes32(signals[0]);
require(dkimRegistry.isDKIMPublicKeyHashValid(domain, ph), "RSA public key incorrect");

// verify proof
require(verifier.verifyProof(a,b,c,signals), "Invalid proof");

Proof memory proof = Proof(a, b, c);

// Convert signals to uint256[]
uint256[] memory signals256 = new uint256[](signals.length);
for (uint i = 0; i < signals.length; i++) {
signals256[i] = uint256(signals[i]);
}

{% for field in values %}
// unpack {{ field.name }}
uint[] memory packed_{{ field.name }} = new uint[]({{ field.name }}_len);
for (uint i = 0; i < {{ field.name }}_len; i++) {
packed_{{ field.name }}[i] = signals[{{ field.start_idx }} + i];
}
string memory {{ field.name }}_string = StringUtils.convertPackedBytesToString(packed_{{ field.name }}, pack_size * {{ field.name }}_len, pack_size);
{% endfor %}

{% for field in external_inputs %}
// unpack {{ field.name }}
uint[] memory packed_{{ field.name }} = new uint[]({{ field.name }}_len);
for (uint i = 0; i < {{ field.name }}_len; i++) {
packed_{{ field.name }}[i] = signals[{{ field.start_idx }} + i];
}
string memory {{ field.name }}_string = StringUtils.convertPackedBytesToString(packed_{{ field.name }}, pack_size * {{ field.name }}_len, pack_size);
{% endfor %}

// mint proof
proofMinter.mintProof(to, blueprintId, address(this), proof, signals256,
}
}
14 changes: 0 additions & 14 deletions contracts/.gitignore

This file was deleted.

85 changes: 0 additions & 85 deletions contracts/broadcast/ZKEmailProof.s.sol/84532/run-1732711124.json

This file was deleted.

85 changes: 0 additions & 85 deletions contracts/broadcast/ZKEmailProof.s.sol/84532/run-1732712857.json

This file was deleted.

85 changes: 0 additions & 85 deletions contracts/broadcast/ZKEmailProof.s.sol/84532/run-latest.json

This file was deleted.

1 change: 0 additions & 1 deletion contracts/lib/forge-std
Submodule forge-std deleted from 1eea5b
1 change: 0 additions & 1 deletion contracts/lib/openzeppelin-contracts
Submodule openzeppelin-contracts deleted from 54b3f1
1 change: 0 additions & 1 deletion lib/openzeppelin-contracts
Submodule openzeppelin-contracts deleted from 54b3f1

0 comments on commit af6592a

Please sign in to comment.