Skip to content

Commit

Permalink
add entanglements and bump dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
backmeupplz committed May 17, 2023
1 parent be56c5a commit 484d95a
Show file tree
Hide file tree
Showing 10 changed files with 1,338 additions and 5,745 deletions.
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "^0.8.19"],
"compiler-version": ["error", "^0.8.20"],
"func-visibility": [
"warn",
{
Expand Down
532 changes: 266 additions & 266 deletions .yarn/releases/yarn-3.5.0.cjs → .yarn/releases/yarn-3.5.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ plugins:
- path: .yarn/plugins/yarn-up-all-plugin.cjs
spec: "https://github.com/e5mode/yarn-up-all/releases/download/1.1.0/index.js"

yarnPath: .yarn/releases/yarn-3.5.0.cjs
yarnPath: .yarn/releases/yarn-3.5.1.cjs
170 changes: 151 additions & 19 deletions contracts/KetlAttestation.sol
Original file line number Diff line number Diff line change
@@ -1,29 +1,108 @@
// ,-,
// * . /.( .
// \|/ \ {
// . _ . , . -*- . `-`
// ,'-. * / \_ * / \_ /|\ * /\'__ *. *
// (____". / \ / \, __ . _/ / \ * . .
// . /\/\ /\/ :' __ \_ / \ _^/ ^/ `—./\ /\ .
// * _ / \/ \ _/ \-‘\/ ` \ /\ /.' ^_ \_ .’\\ /_/\ ,'-.
// /_\ /\ .- `. \/ \ /. / \ ;. _/ \ -. `_/ \/. \ _ (____". *
// . / \ / `-.__ ^ / .-'.--\ - \/ _ `--./ .-' `-/. \ / \ .
// / /. `. / / `. / ` .-' '-._ `._ /. \
// ~._,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'
// ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~~
// ~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~
// ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~
// ๐
// _
// ₒ ><_>
// _______ __ _______
// .-' | _ "\ |" \ /" _ "| ๐
// '--./ / _.---. (. |_) :) || | (: ( \___)
// '-, (__..-` \ |: \/ |: | \/ \
// \ . | (| _ \\ |. | // \ ___
// `,.__. ,__.--/ |: |_) :) |\ | (: _( _|
// '._/_.'___.-` (_______/ |__\| \_______) ๐
//
// __ __ ___ __ __ __ ___ _______
// |" |/ \| "| /" | | "\ /""\ |" | /" "|
// ๐ |' / \: |(: (__) :) / \ || | (: ______)
// |: /' | \/ \/ /' /\ \ |: | ₒ \/ |
// \// /\' | // __ \\ // __' \ \ |___ // ___)_
// / / \\ |(: ( ) :) / / \\ \ ( \_|: \ (: "|
// |___/ \___| \__| |__/ (___/ \___) \_______) \_______)
// ₒ৹
// ___ __ _______ ________
// _ |" | ₒ /""\ | _ "\ /" )
// ><_> || | / \ (. |_) :) (: \___/
// |: | /' /\ \ |: \/ \___ \
// \ |___ // __' \ (| _ \\ __/ \\ \_____)\_____
// ( \_|: \ / / \\ \ |: |_) :) /" \ :) /--v____ __`<
// \_______) (___/ \___)(_______/ (_______/ )/
// '
//
// ๐ . ' , ₒ
// ₒ _______
// ____ .`_|___|_`. ____
// \ \ / / ₒ৹
// \ ' / ๐
// ₒ \/
// ₒ / \ ) (
// ( ₒ৹ ( ( )
// ) ) _ ) ) (
// ( ) ( ( ><_> ( ( ( )
// ) ) ( ( ) ) ) ) ) ) (
// ( ( ) ) ( ( ( ( ( ( )
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma solidity ^0.8.20;

import "@big-whale-labs/versioned-contract/contracts/Versioned.sol";
import "@opengsn/contracts/src/ERC2771Recipient.sol";
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@zk-kit/incremental-merkle-tree.sol/IncrementalBinaryTree.sol";
import "./interfaces/IAttestationCheckerVerifier.sol";
import "./interfaces/IPasswordCheckerVerifier.sol";

contract KetlAttestation is ERC1155, Ownable, Versioned, ERC2771Recipient {
using Counters for Counters.Counter;
using IncrementalBinaryTree for IncrementalTreeData;

// Attestations
uint public attestorPublicKey;
IAttestationCheckerVerifier public attestationCheckerVerifier;
mapping(uint => uint) public attestationMerkleRoots;
IAttestationCheckerVerifier public attestationCheckerVerifier;
// Entanglements
mapping(uint => IncrementalTreeData) public entanglementsTrees;
mapping(uint => uint[]) public entanglements;
mapping(uint => mapping(bytes32 => bool)) public entanglementsRoots;
mapping(uint => bool) private attestationHashesEntangled;

mapping(uint => Counters.Counter) public entanglementsCounts;
mapping(uint => uint16) private minimumEntanglementCounts;
IPasswordCheckerVerifier public passwordCheckerVerifier;
// Nullifiers
mapping(uint => bool) public nullifiers;

constructor(
string memory _uri,
string memory _version,
uint _attestorPublicKey,
address _attestationCheckerVerifier,
address _passwordCheckerVerifier,
address _forwarder
) ERC1155(_uri) Versioned(_version) {
attestorPublicKey = _attestorPublicKey;
attestationCheckerVerifier = IAttestationCheckerVerifier(
_attestationCheckerVerifier
);
passwordCheckerVerifier = IPasswordCheckerVerifier(
_passwordCheckerVerifier
);
_setTrustedForwarder(_forwarder);
}

Expand All @@ -33,37 +112,90 @@ contract KetlAttestation is ERC1155, Ownable, Versioned, ERC2771Recipient {

function addAttestationMerkleRoot(
uint _id,
uint _merkleRoot
uint _merkleRoot,
uint16 _minimumEntanglementCount
) public onlyOwner {
attestationMerkleRoots[_id] = _merkleRoot;
minimumEntanglementCounts[_id] = _minimumEntanglementCount;
entanglementsTrees[_id].init(20, 0);
}

function setMinimumEntanglementCount(
uint _id,
uint16 _minimumEntanglementCount
) public onlyOwner {
minimumEntanglementCounts[_id] = _minimumEntanglementCount;
}

function registerEntanglement(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c,
uint[5] memory input
) external {
// Destruct the input
uint attestationType = input[0];
uint attestationMerkleRoot = input[1];
uint entanglement = input[2];
uint attestationHash = input[3];
uint attestorPublicKey = input[4];
// Check the proof
require(
attestationCheckerVerifier.verifyProof(a, b, c, input),
"Invalid ZK proof"
);
// Check if this attestation has already been used
require(
!attestationHashesEntangled[attestationHash],
"Attestation has already been entangled"
);
// Check the attestations merkle root
require(
attestationMerkleRoots[attestationType] == attestationMerkleRoot,
"Attestation merkle root is wrong"
);
// Check the attestation pubkey
require(
attestorPublicKey == attestorPublicKey,
"Attestation public key is wrong"
);
// Save the entanglement fact
attestationHashesEntangled[attestationHash] = true;
// Get the entanglement tree
IncrementalTreeData entanglementsTree = entanglementsTrees[attestationType];
// Add the entanglement to the tree
entanglementsTree.insert(entanglement);
// Save the entanglement in the array
entanglements[attestationType].push(entanglement);
// Increment the entanglement count
entanglementsCounts[attestationType].increment();
// Register the entanglement root
bytes32 merkleRoot = bytes32(entanglementsTree.root);
entanglementsRoots[attestationType][merkleRoot] = true;
}

// Mint only if the attestation is valid
function mint(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c,
uint[4] memory input
uint[3] memory input
) external {
// Deconstruct input
uint _id = input[0];
uint _merkleRoot = input[1];
uint _attestorPublicKey = input[2];
uint _nullifier = input[3];
uint _attestationType = input[0];
uint _nullifier = input[1];
uint _entanglementMerkleRoot = input[2];
// Check requirements
require(
attestationMerkleRoots[_id] == _merkleRoot,
"Merkle root is not valid"
passwordCheckerVerifier.verifyProof(a, b, c, input),
"ZKP is not valid"
);
require(nullifiers[_nullifier] == false, "Nullifier has already been used");
require(!nullifiers[_nullifier], "Nullifier has already been used");
require(
_attestorPublicKey == attestorPublicKey,
"Attestor public key is not valid"
);
require(
attestationCheckerVerifier.verifyProof(a, b, c, input),
"Attestation is not valid"
entanglementsRoots[_attestationType][_entanglementMerkleRoot],
"Entanglement merkle root is not valid"
);
// Save nullifier
nullifiers[_nullifier] = true;
// Mint token
_mint(_msgSender(), _id, 1, "");
}
Expand Down
9 changes: 7 additions & 2 deletions contracts/interfaces/IAttestationCheckerVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma solidity ^0.8.20;

interface IAttestationCheckerVerifier {
function verifyProof(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c,
uint[4] memory input
// attestation type
// attestation merkle root
// entanglement
// attestation hash
// attestor pubkey
uint[5] memory input
) external view returns (bool r);
}
72 changes: 72 additions & 0 deletions contracts/interfaces/IPasswordCheckerVerifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// ,-,
// * . /.( .
// \|/ \ {
// . _ . , . -*- . `-`
// ,'-. * / \_ * / \_ /|\ * /\'__ *. *
// (____". / \ / \, __ . _/ / \ * . .
// . /\/\ /\/ :' __ \_ / \ _^/ ^/ `—./\ /\ .
// * _ / \/ \ _/ \-‘\/ ` \ /\ /.' ^_ \_ .’\\ /_/\ ,'-.
// /_\ /\ .- `. \/ \ /. / \ ;. _/ \ -. `_/ \/. \ _ (____". *
// . / \ / `-.__ ^ / .-'.--\ - \/ _ `--./ .-' `-/. \ / \ .
// / /. `. / / `. / ` .-' '-._ `._ /. \
// ~._,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'
// ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~~
// ~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~
// ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~
// ๐
// _
// ₒ ><_>
// _______ __ _______
// .-' | _ "\ |" \ /" _ "| ๐
// '--./ / _.---. (. |_) :) || | (: ( \___)
// '-, (__..-` \ |: \/ |: | \/ \
// \ . | (| _ \\ |. | // \ ___
// `,.__. ,__.--/ |: |_) :) |\ | (: _( _|
// '._/_.'___.-` (_______/ |__\| \_______) ๐
//
// __ __ ___ __ __ __ ___ _______
// |" |/ \| "| /" | | "\ /""\ |" | /" "|
// ๐ |' / \: |(: (__) :) / \ || | (: ______)
// |: /' | \/ \/ /' /\ \ |: | ₒ \/ |
// \// /\' | // __ \\ // __' \ \ |___ // ___)_
// / / \\ |(: ( ) :) / / \\ \ ( \_|: \ (: "|
// |___/ \___| \__| |__/ (___/ \___) \_______) \_______)
// ₒ৹
// ___ __ _______ ________
// _ |" | ₒ /""\ | _ "\ /" )
// ><_> || | / \ (. |_) :) (: \___/
// |: | /' /\ \ |: \/ \___ \
// \ |___ // __' \ (| _ \\ __/ \\ \_____)\_____
// ( \_|: \ / / \\ \ |: |_) :) /" \ :) /--v____ __`<
// \_______) (___/ \___)(_______/ (_______/ )/
// '
//
// ๐ . ' , ₒ
// ₒ _______
// ____ .`_|___|_`. ____
// \ \ / / ₒ৹
// \ ' / ๐
// ₒ \/
// ₒ / \ ) (
// ( ₒ৹ ( ( )
// ) ) _ ) ) (
// ( ) ( ( ><_> ( ( ( )
// ) ) ( ( ) ) ) ) ) ) (
// ( ( ) ) ( ( ( ( ( ( )
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IPasswordCheckerVerifier {
function verifyProof(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c,
// attestation type
// nullifier
// entanglement merkle root
uint[3] memory input
) external view returns (bool r);
}
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const {

const config: HardhatUserConfig = {
solidity: {
version: '0.8.19',
version: '0.8.20',
settings: {
optimizer: {
enabled: true,
Expand Down
Loading

0 comments on commit 484d95a

Please sign in to comment.