Skip to content

Commit

Permalink
adding zk_preimage hash
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsaki committed Feb 12, 2023
1 parent 004d2b0 commit 4706f5f
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 4 deletions.
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

8 changes: 5 additions & 3 deletions .gitignore
Git LFS file not shown
30 changes: 30 additions & 0 deletions zk_hash_preImage/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"inputs": [
{
"name": "a",
"public": false,
"type": "field"
},
{
"name": "b",
"public": false,
"type": "field"
},
{
"name": "c",
"public": false,
"type": "field"
},
{
"name": "d",
"public": false,
"type": "field"
}
],
"output": {
"type": "tuple",
"components": {
"elements": []
}
}
}
11 changes: 11 additions & 0 deletions zk_hash_preImage/hashexample.zok
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "hashes/sha256/512bitPacked" as sha256packed;

def main(private field a, private field b, private field c, private field d) {
field[2] h = sha256packed([a,b,c,d]);
assert(h[0] == 263561599766550617289250058199814760685);
assert(h[1] == 65303172752238645975888084098459749904);
return;
}



25 changes: 25 additions & 0 deletions zk_hash_preImage/proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"scheme": "g16",
"curve": "bn128",
"proof": {
"a": [
"0x2a66cfcaa3429034ffabf25215b6355794703788cfd7a0b6895b4ed2b458b96a",
"0x14e1357031059d3a80cca8fb66d1928cea9739a6f594e275acba6ffed374e382"
],
"b": [
[
"0x2cf19180d05eeac8140ae31f54b9ec0ce7487a4568aa71ab0c4f1507bb0b4339",
"0x2acd074e94b1cf7cf1074587b2c833fcd328b4095d4e4075e2cc068b731087df"
],
[
"0x0ed8fdeb347d05ec39de5c1b6ad014aba541b1aeca3215ac1500aaee1d3efdf2",
"0x20dc2eb075c75690be93ac1177062d90f9a79ce570e0a260d57ac472712e6fd5"
]
],
"c": [
"0x08e104f6b3acf08ff84cb9bc4d4a793e3df88b0d0927d7c80dfffcd5e175131f",
"0x234660b3c5e077fc4af6f4ebe7f69f8be9735ca5ed3ac09d30807b1ebc49cab7"
]
},
"inputs": []
}
197 changes: 197 additions & 0 deletions zk_hash_preImage/verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// 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(0x110281f944600c421e84d3da1b6e1de65f18dabee168b9a29fa3eac548a866d8), uint256(0x02504fb5baa36c34fd6bd22a3db5d82dc501c1986814996e873f18f535340d14));
vk.beta = Pairing.G2Point([uint256(0x1604df2c867af0893d63998078865a6f008116e52fbbfdf8f93c026364d4875e), uint256(0x2785229ce2e0aadac479c28ba15269c020533efe2b61f7317b5b87c21303c423)], [uint256(0x18519fc7f668a05f6b49478a48fdc2d6224012651045b853afa6c640e05bee8b), uint256(0x26577a4d1e0d4eaaf4bfa62a0258318787df19ee6f8b3d606e9c308eb62752f3)]);
vk.gamma = Pairing.G2Point([uint256(0x021ac51875c79c9951eb200fa71c30a1e185fbd5401b56e93c6433f07c6388bd), uint256(0x18c01216c0ac77d1d1ce07f7415da144f9b67172b0f9cdc2f7273e7b16243501)], [uint256(0x11b194f8aa97f3206607463bf28e9bfa74d32dc516fe8b8a2693197bb25a5a5f), uint256(0x01c3799262948406549fa4084b9e5e4cb8a725e5f9a6f5580515f2e1ddc4d6f0)]);
vk.delta = Pairing.G2Point([uint256(0x12c8afbfbab90410170659921b3439ce425b5cc2d1dbe837999bfa63ce2ef875), uint256(0x14db7e3ed9dbfb91108eebe9c0b0e87cff88e1f264ba786496e1507f450aafb1)], [uint256(0x15ebbe3c377f3b08389da6fa34e0d4d8f081bf87079484b2e9cdc3f3318b21db), uint256(0x2f57a45e76e4545fd9c725dfd8a05227dd9d62c81061d23de6c7e71538d222de)]);
vk.gamma_abc = new Pairing.G1Point[](1);
vk.gamma_abc[0] = Pairing.G1Point(uint256(0x15410263d364f0eafb284f0e6361d8954cf392999703fa00ffbb01949a11496d), uint256(0x17ebec8db4dc52192f0f19b92d4b37f14b92bbc1ff6de2e9a467cb576b74cf50));
}
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
) public view returns (bool r) {
uint[] memory inputValues = new uint[](0);

if (verify(inputValues, proof) == 0) {
return true;
} else {
return false;
}
}
}

0 comments on commit 4706f5f

Please sign in to comment.