From 8297e60eae0674a2e194c0478fcaca5b6f4340cc Mon Sep 17 00:00:00 2001 From: Jumaru Date: Tue, 24 Oct 2023 14:20:56 +0300 Subject: [PATCH] docs: :memo: Add example use case --- examples/.gitignore | 3 + examples/address-proof.json | 110 +++++++++++++++++++ examples/addresses.txt | 4 + examples/verifier.sol | 203 ++++++++++++++++++++++++++++++++++++ examples/zkToken.sol | 100 ++++++++++++++++++ 5 files changed, 420 insertions(+) create mode 100644 examples/.gitignore create mode 100644 examples/address-proof.json create mode 100644 examples/addresses.txt create mode 100644 examples/verifier.sol create mode 100644 examples/zkToken.sol diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..a87fea3 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,3 @@ +!address-proof.json +!addresses.txt +!verifier.sol diff --git a/examples/address-proof.json b/examples/address-proof.json new file mode 100644 index 0000000..6853c16 --- /dev/null +++ b/examples/address-proof.json @@ -0,0 +1,110 @@ +{ + "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4": { + "inputs": [ + "0x000000000000000000000000000000000000000000006e47ddff70d8b26c22d2", + "0x00000000000000000000000000000000000000000000bc1872bdf10f44beddc4", + "0x0000000000000000000000000000000000000000000000000000000000000001" + ], + "proof": [ + [ + "0x1f15bf875b87d8b67bccf3412d4c130e75e7a090b3bf1488bb61ffd2a21a6d3a", + "0x2a2c1a472ca5deec865598ee892e5e0204cca26c9816975a9a5ac1659a4c8b8a" + ], + [ + [ + "0x2c04e20aed5f253770f971d36db2c35d9a5290b57ccd8db313841867a6350568", + "0x05b86220c664eed0dbcadb98bb94f735aee55a2fdf798c7a5bb0ce216e90ee08" + ], + [ + "0x060fe7f61bb3dce0289e68cbf9c72d93e75fcc7fb160f36f9bda02d141987fac", + "0x029d09300d43b28a30db9e5e4ae35123a6cf46cd3435e0a6eb2a71c1bba439e0" + ] + ], + [ + "0x004a32bd0399e5a4e92074ccdbf33c5c2922203d57d03a8af4494156cc172b51", + "0x117b1ca0680454894bd527ebfa3142b8d1bd4ff2b839cd04d20103b5ec020beb" + ] + ] + }, + "0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB": { + "inputs": [ + "0x00000000000000000000000000000000000000000000919d5d9f83f67c2ef55b", + "0x000000000000000000000000000000000000000000006affbb2b8ffd0e5cabab", + "0x0000000000000000000000000000000000000000000000000000000000000001" + ], + "proof": [ + [ + "0x094f4a68c4a8d732fe70535cd3910e45b7b392ea84a22e9aa3d3970888070a5b", + "0x1927e8db46810e7e3d28015b64ce8565ce6454933bf46a2fedc747ff21eacaae" + ], + [ + [ + "0x13e88419dd61dc0ceb1f7bc901506b660cb0f3bd64f5e0ae194669f4293dc5ba", + "0x1521c25835dd7c54bffee60dd194b51ce3d39186274d9dde1bfc05910627104b" + ], + [ + "0x27373813c0149352ba7a8d9b3407faefa5110eacc207040e60b80006fae3c341", + "0x29b5f09e073cc3a42b9bbd206c40811950ecaee271b8e4f71d8cf534280a28a1" + ] + ], + [ + "0x005d2c395a9ff8e33eb46bf4dadb44b5435cdf4e432b875d718f236b273d0eee", + "0x256f0c75919e88d64e95b3a36f8bdea70882a65aa79274c52dce112ebe51bc39" + ] + ] + }, + "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2": { + "inputs": [ + "0x00000000000000000000000000000000000000000000cf5a2357f990b8cd3e94", + "0x000000000000000000000000000000000000000000000f7e4745e9d401835cb2", + "0x0000000000000000000000000000000000000000000000000000000000000001" + ], + "proof": [ + [ + "0x28f194215182e31cc4881ddc7b62f78600091e5cbc998a82beb8f97e20812d97", + "0x0ee8d4f88675bdeffdc4f203bd5cfa8469a7bcf253c6018e000860834d5ea7f6" + ], + [ + [ + "0x24f600c41d69502833cd1d7b92e9d51a6c4994972148249237a4352422e6844a", + "0x10e7faedf54da8efc5b8634b52bce2382c483fc7f7e6caeffbe091c63507d7e2" + ], + [ + "0x19148f5f3123a860645aacf8017ff4dbd58cd41c0db2c8eb580443485e79dca9", + "0x164021c6c0eb373ffb59b56d43b28b0b551a14426e7106933962b44e33f1cfe3" + ] + ], + [ + "0x174cdfd481e3c030db7cd4bbb72470d49e9bdf54166742ca1696a46db56064ab", + "0x259ffb6f68cc933bd4b9da794b87929b9c67a964f3d58f88d27804ca4e6fbe29" + ] + ] + }, + "0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db": { + "inputs": [ + "0x000000000000000000000000000000000000000000005ad2c8f4ef77e055b70a", + "0x000000000000000000000000000000000000000000002f3d1572944a982c02db", + "0x0000000000000000000000000000000000000000000000000000000000000001" + ], + "proof": [ + [ + "0x1a88cea939abbc78963978e5236d50dbac1e18dbd8075d5f8c5e0b82670a4d2e", + "0x1e0f849986add5129290961f962206cae2b80ca4b07be67449a4b88c124335f2" + ], + [ + [ + "0x0ab755ead4471175f208af06760ef1d59cc17464a2e73be85229b3b93b5b89f1", + "0x17a4f88745f5158c2b209ff77ca06075308f30d474d2745f60d3c7099e0e232b" + ], + [ + "0x21e9e7c14c7943b784fd417cb74de68cbb3bedd5073d702e8b8884018757c0ba", + "0x0d5daf9ae401d835a37bb8295f5351ec5c4375827f4d0a8a09f3c02099f42a44" + ] + ], + [ + "0x1e834b9280d90805370a02e771a2a491386841f38126c60d87abd5a4a4715a93", + "0x120f1183ef6636ced4ca25db9e6b5cb287251b2f59d645f250c2e2bd71874374" + ] + ] + } +} \ No newline at end of file diff --git a/examples/addresses.txt b/examples/addresses.txt new file mode 100644 index 0000000..22196db --- /dev/null +++ b/examples/addresses.txt @@ -0,0 +1,4 @@ +0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 +0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 +0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db +0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB \ No newline at end of file diff --git a/examples/verifier.sol b/examples/verifier.sol new file mode 100644 index 0000000..b9dab7e --- /dev/null +++ b/examples/verifier.sol @@ -0,0 +1,203 @@ +// This file is MIT Licensed. +// +// Copyright 2017 Christian Reitwiessner +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +pragma solidity ^0.8.0; +library Pairing { + struct G1Point { + uint X; + uint Y; + } + // Encoding of field elements is: X[0] * z + X[1] + struct G2Point { + uint[2] X; + uint[2] Y; + } + /// @return the generator of G1 + function P1() pure internal returns (G1Point memory) { + return G1Point(1, 2); + } + /// @return the generator of G2 + function P2() pure internal returns (G2Point memory) { + return G2Point( + [10857046999023057135944570762232829481370756359578518086990519993285655852781, + 11559732032986387107991004021392285783925812861821192530917403151452391805634], + [8495653923123431417604973247489272438418190587263600148770280649306958101930, + 4082367875863433681332203403145435568316851327593401208105741076214120093531] + ); + } + /// @return the negation of p, i.e. p.addition(p.negate()) should be zero. + function negate(G1Point memory p) pure internal returns (G1Point memory) { + // The prime q in the base field F_q for G1 + uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; + if (p.X == 0 && p.Y == 0) + return G1Point(0, 0); + return G1Point(p.X, q - (p.Y % q)); + } + /// @return r the sum of two points of G1 + function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) { + uint[4] memory input; + input[0] = p1.X; + input[1] = p1.Y; + input[2] = p2.X; + input[3] = p2.Y; + bool success; + assembly { + success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60) + // Use "invalid" to make gas estimation work + switch success case 0 { invalid() } + } + require(success); + } + + + /// @return r the product of a point on G1 and a scalar, i.e. + /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p. + function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) { + uint[3] memory input; + input[0] = p.X; + input[1] = p.Y; + input[2] = s; + bool success; + assembly { + success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60) + // Use "invalid" to make gas estimation work + switch success case 0 { invalid() } + } + require (success); + } + /// @return the result of computing the pairing check + /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1 + /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should + /// return true. + function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) { + require(p1.length == p2.length); + uint elements = p1.length; + uint inputSize = elements * 6; + uint[] memory input = new uint[](inputSize); + for (uint i = 0; i < elements; i++) + { + input[i * 6 + 0] = p1[i].X; + input[i * 6 + 1] = p1[i].Y; + input[i * 6 + 2] = p2[i].X[1]; + input[i * 6 + 3] = p2[i].X[0]; + input[i * 6 + 4] = p2[i].Y[1]; + input[i * 6 + 5] = p2[i].Y[0]; + } + uint[1] memory out; + bool success; + assembly { + success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20) + // Use "invalid" to make gas estimation work + switch success case 0 { invalid() } + } + require(success); + return out[0] != 0; + } + /// Convenience method for a pairing check for two pairs. + function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) { + G1Point[] memory p1 = new G1Point[](2); + G2Point[] memory p2 = new G2Point[](2); + p1[0] = a1; + p1[1] = b1; + p2[0] = a2; + p2[1] = b2; + return pairing(p1, p2); + } + /// Convenience method for a pairing check for three pairs. + function pairingProd3( + G1Point memory a1, G2Point memory a2, + G1Point memory b1, G2Point memory b2, + G1Point memory c1, G2Point memory c2 + ) internal view returns (bool) { + G1Point[] memory p1 = new G1Point[](3); + G2Point[] memory p2 = new G2Point[](3); + p1[0] = a1; + p1[1] = b1; + p1[2] = c1; + p2[0] = a2; + p2[1] = b2; + p2[2] = c2; + return pairing(p1, p2); + } + /// Convenience method for a pairing check for four pairs. + function pairingProd4( + G1Point memory a1, G2Point memory a2, + G1Point memory b1, G2Point memory b2, + G1Point memory c1, G2Point memory c2, + G1Point memory d1, G2Point memory d2 + ) internal view returns (bool) { + G1Point[] memory p1 = new G1Point[](4); + G2Point[] memory p2 = new G2Point[](4); + p1[0] = a1; + p1[1] = b1; + p1[2] = c1; + p1[3] = d1; + p2[0] = a2; + p2[1] = b2; + p2[2] = c2; + p2[3] = d2; + return pairing(p1, p2); + } +} + +contract Verifier { + using Pairing for *; + struct VerifyingKey { + Pairing.G1Point alpha; + Pairing.G2Point beta; + Pairing.G2Point gamma; + Pairing.G2Point delta; + Pairing.G1Point[] gamma_abc; + } + struct Proof { + Pairing.G1Point a; + Pairing.G2Point b; + Pairing.G1Point c; + } + function verifyingKey() pure internal returns (VerifyingKey memory vk) { + vk.alpha = Pairing.G1Point(uint256(0x0e404afacea0eccff6ec9b3ec3c0aa1df1bd281d9b384980dc8debade1d40aad), uint256(0x116ec791092222fafea6aeb3c134f57f9654fed91f6aafa7c96300c13118fef9)); + vk.beta = Pairing.G2Point([uint256(0x1dc0dd5bce9e4a2f22340d0226f7492a8c6bf28aa6894c09bd06ea0231cd50b8), uint256(0x120e98d7f8b3a9d70767d8294acfdbe3a8590eca5bb6c0300259a9d11689922d)], [uint256(0x08073c0dc7d2f4fb1ca5f103dcf0adb15f0209979fcbec668da675f4db133357), uint256(0x1381dad1ae317f196d33d1032a1f2e511a5b3461e39cb968fe602b697f0dd18b)]); + vk.gamma = Pairing.G2Point([uint256(0x159c656d086b162b59d63031616f407a941839f6ada591fb307dcf9c3460f9eb), uint256(0x1cbb0b9793e7c08b5c707e6c31a40c4ba3029510626e1e00243a20d4f88e56e5)], [uint256(0x28582c8b246813544e3e2f065cf0fe7796ef7642abeb11dc6bbf9de609bca521), uint256(0x0860588300e43216d88bb33d522d749858073dc42c1a5721d116ffd2fa64c0c3)]); + vk.delta = Pairing.G2Point([uint256(0x16b7c67e2650bbf883544db029d9831373e18dc14b33b0233f23bfd37ab021ee), uint256(0x2559829ec37de1c916b1e05e25dad9e3c9ca06dd090bcd8ba17bb2c01155d2d7)], [uint256(0x2de4e92677f54669d325647f54ece587ce1b2a614acb43640bdc1dc08b666adc), uint256(0x1b5cdd8e63fe900f28d96359af6545231a794946df06ece58b8c383b4e596c99)]); + vk.gamma_abc = new Pairing.G1Point[](4); + vk.gamma_abc[0] = Pairing.G1Point(uint256(0x01202359188b8f15a8a1058dcd76cfe62103bf3c50ee478dde6040b092318099), uint256(0x2d131626c3be1d2729686af488d0bf0a99638f2eb2e114b132a4c3aa48701e9b)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(0x2d053c3f70ebd681cac0a227888e63c267c5bb51f4404b3b343baad18f987e18), uint256(0x16c9c8145ce5a825a455497ecb1a06297ec0dc9f42deaebaae479668bd81ee9d)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(0x1e6b29bc4d32968a37c3e247f8a796a12a67ca3b8723c03df94979173e99e3e1), uint256(0x004291dc05f678b503a501213695ee906106c2e06b070e5eac70ac20e812086f)); + vk.gamma_abc[3] = Pairing.G1Point(uint256(0x160716f17c8004470dc6ec0e860517d3512241b2cd1b202ceb619bf98c0594d0), uint256(0x1448fccfea17ad3fb4a97959d11d1ffcf49fc83db8c0d2af7a59150fc4e4da09)); + } + function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { + uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; + VerifyingKey memory vk = verifyingKey(); + require(input.length + 1 == vk.gamma_abc.length); + // Compute the linear combination vk_x + Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); + for (uint i = 0; i < input.length; i++) { + require(input[i] < snark_scalar_field); + vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.gamma_abc[i + 1], input[i])); + } + vk_x = Pairing.addition(vk_x, vk.gamma_abc[0]); + if(!Pairing.pairingProd4( + proof.a, proof.b, + Pairing.negate(vk_x), vk.gamma, + Pairing.negate(proof.c), vk.delta, + Pairing.negate(vk.alpha), vk.beta)) return 1; + return 0; + } + function verifyTx( + Proof memory proof, uint[3] memory input + ) public view returns (bool r) { + uint[] memory inputValues = new uint[](3); + + for(uint i = 0; i < input.length; i++){ + inputValues[i] = input[i]; + } + if (verify(inputValues, proof) == 0) { + return true; + } else { + return false; + } + } +} diff --git a/examples/zkToken.sol b/examples/zkToken.sol new file mode 100644 index 0000000..97279d0 --- /dev/null +++ b/examples/zkToken.sol @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +// import the verifier that the program created +import "./verifier.sol"; +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; +import "@openzeppelin/contracts/utils/Strings.sol"; + +/// @title YourToken Contract +/// @notice This contract represents a unique ERC20 token with additional verification logic. +contract YourToken is ERC20 { + Verifier public verifier; + mapping(address => bool) public claimed; + + constructor() ERC20("YourToken", "YTK") { + verifier = new Verifier(); + } + + /* + * @notice Mints new tokens after verifying a provided proof. + * @param input The inputs array from `addresses-proof.json` + * @param proof The proof array from `addresses-proof.json` + * @return A boolean value indicating whether the function executed successfully. Reverts otherwise. + */ + function mint(uint256[3] memory input, Verifier.Proof memory proof) public returns (bool) { + string memory senderString = Strings.toString(uint256(uint160(msg.sender))); + uint256 midpoint = bytes(senderString).length / 2; + + string memory aString = substring(senderString, 0, midpoint); + string memory bString = substring(senderString, midpoint, bytes(senderString).length); + + // Convert input values to decimal strings + string memory input0String = hexToDecimalString(input[0]); + string memory input1String = hexToDecimalString(input[1]); + + // Add leading zero if necessary + input0String = addLeadingZeroIfNeeded(input0String, bytes(aString).length); + input1String = addLeadingZeroIfNeeded(input1String, bytes(bString).length); + + // Ensure the sender matches the a and b inputs of the proof + require( + keccak256(abi.encodePacked(aString)) == keccak256(abi.encodePacked(input0String)), + "Not your proof or invalid input" + ); + require( + keccak256(abi.encodePacked(bString)) == keccak256(abi.encodePacked(input1String)), + "Not your proof or invalid input" + ); + + // Ensure the tokens haven't been claimed yet + require(!claimed[msg.sender], "Tokens already claimed"); + + // Verify the proof + require(verifier.verifyTx(proof, input), "Invalid proof"); + + // Mark as claimed and mint the tokens + claimed[msg.sender] = true; + _mint(msg.sender, 10 * 10 ** decimals()); + return true; + } + + /* + * @notice Extracts a substring from a given string. + * @param str The original string. + * @param startIndex The starting index from where to extract the substring. + * @param endIndex The ending index to where extract the substring. + * @return The extracted substring. + */ + function substring(string memory str, uint256 startIndex, uint256 endIndex) internal pure returns (string memory) { + bytes memory strBytes = bytes(str); + bytes memory result = new bytes(endIndex-startIndex); + for (uint256 i = startIndex; i < endIndex; i++) { + result[i - startIndex] = strBytes[i]; + } + return string(result); + } + + /* + * @notice Converts a uint value to its decimal string representation. + * @param value The unsigned integer to convert. + * @return The decimal string representation of the given unsigned integer. + */ + function hexToDecimalString(uint256 value) internal pure returns (string memory) { + return Strings.toString(value); + } + + /* + * @notice Adds a leading zero to a string if needed to reach a target length. + * @param str The original string. + * @param targetLength The target length for the resulting string. + * @return The string with a leading zero added if needed. + */ + function addLeadingZeroIfNeeded(string memory str, uint256 targetLength) internal pure returns (string memory) { + uint256 strLength = bytes(str).length; + if (strLength >= targetLength) { + return str; + } + return string(abi.encodePacked("0", str)); + } +}