Skip to content

Commit

Permalink
Ag 32 zokrates init (#36)
Browse files Browse the repository at this point in the history
* feat(@zkp): AG-32 zokrates init

init a zokrates proof as example

* docs(docs): AG-32 updated docs

updated documentation
  • Loading branch information
g3k0 authored Apr 14, 2024
1 parent cf01269 commit 6575a17
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 39 deletions.
26 changes: 13 additions & 13 deletions contracts/MunicipalityElection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "./Election.sol";
contract MunicipalityElection is Election {
struct Candidate {
string name;
string candidatesFor; // major or councilior
string candidatesFor; // major or councilor
uint256 points;
}

Expand All @@ -27,8 +27,8 @@ contract MunicipalityElection is Election {
Coalition[] private coalitions;

/// @dev this is a mapping used in private function
mapping(string => bool) private isCounciliorCandidateInArray;
Candidate[] private counciliorCandidatesArray;
mapping(string => bool) private iscouncilorCandidateInArray;
Candidate[] private councilorCandidatesArray;

/// @dev this is a mapping used in private function
mapping(string => bool) private partyExistsInCoalition;
Expand Down Expand Up @@ -92,32 +92,32 @@ contract MunicipalityElection is Election {
return (coalition.majorCandidate, partyNames);
}

/// @notice as first step, during the registration period the parties register their names and list of councilior candidates
/// @notice as first step, during the registration period the parties register their names and list of councilor candidates
function registerParty(
string memory name,
string[] memory counciliorCandidates
string[] memory councilorCandidates
) external onlyOwner isRegistrationPeriod {
require(
counciliorCandidates.length == 5,
"The list of councilior candidates must be composed of 5 names"
councilorCandidates.length == 5,
"The list of councilor candidates must be composed of 5 names"
);

Candidate[] storage candidatesList = parties[name];

for (uint i = 0; i < counciliorCandidates.length; i++) {
for (uint i = 0; i < councilorCandidates.length; i++) {
require(
!isCounciliorCandidateInArray[counciliorCandidates[i]],
"Councilior candidate names must be unique"
!iscouncilorCandidateInArray[councilorCandidates[i]],
"councilor candidate names must be unique"
);

Candidate memory c = Candidate({
name: counciliorCandidates[i],
candidatesFor: "councilior",
name: councilorCandidates[i],
candidatesFor: "councilor",
points: 0
});

candidatesList.push(c);
isCounciliorCandidateInArray[counciliorCandidates[i]] = true;
iscouncilorCandidateInArray[councilorCandidates[i]] = true;
}

partiesLength++;
Expand Down
Binary file added docs/assets/dec_class.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/decs_registry_class.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/election_class.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 19 additions & 25 deletions docs/diagrams/Agora diagrams.drawio

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "agora",
"version": "0.6.0",
"version": "0.7.0",
"description": "A confidentiality-first electronic voting system",
"author": {
"name": "nova collective",
Expand Down
20 changes: 20 additions & 0 deletions zokrates/example/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"inputs": [
{
"name": "a",
"public": false,
"type": "field"
},
{
"name": "b",
"public": true,
"type": "field"
}
],
"output": {
"type": "tuple",
"components": {
"elements": []
}
}
}
Binary file added zokrates/example/out
Binary file not shown.
Binary file added zokrates/example/out.r1cs
Binary file not shown.
Binary file added zokrates/example/out.wtns
Binary file not shown.
27 changes: 27 additions & 0 deletions zokrates/example/proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"scheme": "g16",
"curve": "bn128",
"proof": {
"a": [
"0x13a494590cd3611b3f99892d1a500996d1720e6a0ff914e80200a90cda9253c3",
"0x304739d412e31aab8e92da6db59841c6145fdc7b029d758cd17d2e5339363988"
],
"b": [
[
"0x26d2a85ce5a207780b907c4b494d60c237b64c79733326877bb0186be97ac010",
"0x1aacd62104f32ecdc1369913daab58f0e0029bbf0a9b115a34a46b838f4741b6"
],
[
"0x14445684c9500b3ac39764ba2eb053cd682d0bde9c0222622f739a202f21bbf8",
"0x1a3fa66376458c9870ba0de3ddc29872d61cbbad423da04e93458ec42ff303d1"
]
],
"c": [
"0x2d1d8e3991a5820dab06ffb23cb78a5fbce396325f837ff2599d637786f0fb82",
"0x18c2c215c889f4e6abe473f6b1f84fe83529705eac10bc81d0ac5ae9f3a092b7"
]
},
"inputs": [
"0x000000000000000000000000000000000000000000000000000000000001bba1"
]
}
Binary file added zokrates/example/proving.key
Binary file not shown.
5 changes: 5 additions & 0 deletions zokrates/example/root.zok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def main(private field a, field b) {
assert(a * a == b);
return;
}

48 changes: 48 additions & 0 deletions zokrates/example/verification.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"scheme": "g16",
"curve": "bn128",
"alpha": [
"0x1f9e716d1a7cc63c1e07ba09e762bae1326a6204f334b18913e4e919a6d0e064",
"0x0692f7de6ef3fbbdd6ab896517c6cdd67915a66a35dfca399e5bab43d9cd8733"
],
"beta": [
[
"0x1aaed8506d67d1f7deae5eeeb0f3d6ec6643b536128d1ce97f2ef52dfd86996d",
"0x2d466f12cd90035edb520f695933b74f16e12360630834b3c2feaf909ece8fbd"
],
[
"0x1783da1f135c023089043ae327bd2779b2af303e13d5c25cc9f6a6adfe6aa6b7",
"0x13b3ee585a3e269df4b1fb1c2f8d52089bbe3654f35012ec33909ff6737fb8d9"
]
],
"gamma": [
[
"0x2e4f04a66ade653cc98984e1c6595f91506e25a65e801b58bae37c17aed43018",
"0x2183bd54c9d43db344a895b31fa0f9d08e4081b9deb0745583716214f67129ba"
],
[
"0x2f1ebb5ebb52a1159f5b98319ef76bc280ff0c659080fc5813b75882707b73aa",
"0x0da1457454afba9ffc705805187369ff966e3048c3365b50d41170d39b45de63"
]
],
"delta": [
[
"0x0b6b8cb3908dc31c41789292533daf608fbca8c75363fdc17ad79b1c1d32b1c3",
"0x0b3de4718b7e12c4b6e20caaf618c43adae1cb0c50e8f59e4177fe9378557b61"
],
[
"0x23e7d597e8385d24398c7542954ff75571a194491ecb21ec089ab0775639501b",
"0x28eff9715119798a9a1dd3cbcf8ffc609928f0585b77742a933153293bf25166"
]
],
"gamma_abc": [
[
"0x01f44aa244b9ecd20c9b94381581a408a60cda2281ccabe39b4e3e9c80ff047e",
"0x2117ff110b33b756808a293cd3f69bfcebf9d5cc3184efc2110aff7b886d4286"
],
[
"0x21fe20b04b62b25099638a3e6f3da919547d51be5e1d27b7287097a72e3c1bce",
"0x01c6a6f3707f5310023e22ce0a112d1a04f082018962983fb263274e7b52007c"
]
]
}
201 changes: 201 additions & 0 deletions zokrates/example/verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// 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(0x1f9e716d1a7cc63c1e07ba09e762bae1326a6204f334b18913e4e919a6d0e064), uint256(0x0692f7de6ef3fbbdd6ab896517c6cdd67915a66a35dfca399e5bab43d9cd8733));
vk.beta = Pairing.G2Point([uint256(0x1aaed8506d67d1f7deae5eeeb0f3d6ec6643b536128d1ce97f2ef52dfd86996d), uint256(0x2d466f12cd90035edb520f695933b74f16e12360630834b3c2feaf909ece8fbd)], [uint256(0x1783da1f135c023089043ae327bd2779b2af303e13d5c25cc9f6a6adfe6aa6b7), uint256(0x13b3ee585a3e269df4b1fb1c2f8d52089bbe3654f35012ec33909ff6737fb8d9)]);
vk.gamma = Pairing.G2Point([uint256(0x2e4f04a66ade653cc98984e1c6595f91506e25a65e801b58bae37c17aed43018), uint256(0x2183bd54c9d43db344a895b31fa0f9d08e4081b9deb0745583716214f67129ba)], [uint256(0x2f1ebb5ebb52a1159f5b98319ef76bc280ff0c659080fc5813b75882707b73aa), uint256(0x0da1457454afba9ffc705805187369ff966e3048c3365b50d41170d39b45de63)]);
vk.delta = Pairing.G2Point([uint256(0x0b6b8cb3908dc31c41789292533daf608fbca8c75363fdc17ad79b1c1d32b1c3), uint256(0x0b3de4718b7e12c4b6e20caaf618c43adae1cb0c50e8f59e4177fe9378557b61)], [uint256(0x23e7d597e8385d24398c7542954ff75571a194491ecb21ec089ab0775639501b), uint256(0x28eff9715119798a9a1dd3cbcf8ffc609928f0585b77742a933153293bf25166)]);
vk.gamma_abc = new Pairing.G1Point[](2);
vk.gamma_abc[0] = Pairing.G1Point(uint256(0x01f44aa244b9ecd20c9b94381581a408a60cda2281ccabe39b4e3e9c80ff047e), uint256(0x2117ff110b33b756808a293cd3f69bfcebf9d5cc3184efc2110aff7b886d4286));
vk.gamma_abc[1] = Pairing.G1Point(uint256(0x21fe20b04b62b25099638a3e6f3da919547d51be5e1d27b7287097a72e3c1bce), uint256(0x01c6a6f3707f5310023e22ce0a112d1a04f082018962983fb263274e7b52007c));
}
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[1] memory input
) public view returns (bool r) {
uint[] memory inputValues = new uint[](1);

for(uint i = 0; i < input.length; i++){
inputValues[i] = input[i];
}
if (verify(inputValues, proof) == 0) {
return true;
} else {
return false;
}
}
}
Binary file added zokrates/example/witness
Binary file not shown.

0 comments on commit 6575a17

Please sign in to comment.