Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
motemotech committed Dec 20, 2023
1 parent 8fa044b commit 8b861f1
Show file tree
Hide file tree
Showing 14 changed files with 1,496 additions and 716 deletions.
253 changes: 126 additions & 127 deletions packages/contracts/src/circom-verifier/GovSigVerifier.sol

Large diffs are not rendered by default.

67 changes: 33 additions & 34 deletions packages/contracts/src/circom-verifier/UserSigVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ pragma solidity >=0.7.0 <0.9.0;

contract MynaUserSigVerifier {
// Scalar field size
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
// Base field size
uint256 constant q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
uint256 constant q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;

// Verification Key data
uint256 constant alphax = 20491192805390485299153009773594534940189261866228447918068658471970481763042;
uint256 constant alphay = 9383485363053290200918347156157836566562967994039712273449902621266178545958;
uint256 constant betax1 = 4252822878758300859123897981450591353533073413197771768651442665752259397132;
uint256 constant betax2 = 6375614351688725206403948262868962793625744043794305715222011528459656738731;
uint256 constant betay1 = 21847035105528745403288232691147584728191162732299865338377159692350059136679;
uint256 constant betay2 = 10505242626370262277552901082094356697409835680220590971873171140371331206856;
uint256 constant alphax = 20491192805390485299153009773594534940189261866228447918068658471970481763042;
uint256 constant alphay = 9383485363053290200918347156157836566562967994039712273449902621266178545958;
uint256 constant betax1 = 4252822878758300859123897981450591353533073413197771768651442665752259397132;
uint256 constant betax2 = 6375614351688725206403948262868962793625744043794305715222011528459656738731;
uint256 constant betay1 = 21847035105528745403288232691147584728191162732299865338377159692350059136679;
uint256 constant betay2 = 10505242626370262277552901082094356697409835680220590971873171140371331206856;
uint256 constant gammax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634;
uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781;
uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531;
Expand All @@ -42,38 +42,41 @@ contract MynaUserSigVerifier {
uint256 constant deltay1 = 18725697851259730151115205995739818586302219848900507076571259901558467611499;
uint256 constant deltay2 = 8820274483826261160802591723716466750382753591445168286551008864964725036883;


uint256 constant IC0x = 18224752695618958914183367431492413747486974162490887344569385249646648416277;
uint256 constant IC0y = 5016344216128181760067345326058572609715084361926468524224835931238782977838;

uint256 constant IC1x = 6059317173897392605134714272633960475425379859491719431954785157721471599975;
uint256 constant IC1y = 2060850966544339708095907308198167470739932181575372182643862053135753693070;

uint256 constant IC2x = 985057497798601122835739205581983575902339038505191061139303299939424104092;
uint256 constant IC2y = 6671456985309318849033472227878801929484937066242421645255947209207073822665;

uint256 constant IC3x = 20301738173456911323945592705026852697850600781260594801217934670306499052067;
uint256 constant IC3y = 8552512787360769136643570500461966613559990206792957246646857271555193132859;

uint256 constant IC4x = 4962406547830820156969461726416533262367738762406728272349558569770554904066;
uint256 constant IC4y = 6295828266906369808715836075094242558971552272859855652154595037625796564557;



// Memory data
uint16 constant pVk = 0;
uint16 constant pPairing = 128;

uint16 constant pLastMem = 896;

function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[4] calldata _pubSignals) public view returns (bool) {
function verifyProof(
uint256[2] calldata _pA,
uint256[2][2] calldata _pB,
uint256[2] calldata _pC,
uint256[4] calldata _pubSignals
) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
mstore(0, 0)
return(0, 0x20)
}
}

// G1 function to multiply a G1 value(x,y) to value in an address
function g1_mulAccC(pR, x, y, s) {
let success
Expand Down Expand Up @@ -108,15 +111,14 @@ contract MynaUserSigVerifier {
mstore(add(_pVk, 32), IC0y)

// Compute the linear combination vk_x

g1_mulAccC(_pVk, IC1x, IC1y, calldataload(add(pubSignals, 0)))

g1_mulAccC(_pVk, IC2x, IC2y, calldataload(add(pubSignals, 32)))

g1_mulAccC(_pVk, IC3x, IC3y, calldataload(add(pubSignals, 64)))

g1_mulAccC(_pVk, IC4x, IC4y, calldataload(add(pubSignals, 96)))


// -A
mstore(_pPairing, calldataload(pA))
Expand All @@ -142,7 +144,6 @@ contract MynaUserSigVerifier {
mstore(add(_pPairing, 384), mload(add(pMem, pVk)))
mstore(add(_pPairing, 416), mload(add(pMem, add(pVk, 32))))


// gamma2
mstore(add(_pPairing, 448), gammax1)
mstore(add(_pPairing, 480), gammax2)
Expand All @@ -159,7 +160,6 @@ contract MynaUserSigVerifier {
mstore(add(_pPairing, 704), deltay1)
mstore(add(_pPairing, 736), deltay2)


let success := staticcall(sub(gas(), 2000), 8, _pPairing, 768, _pPairing, 0x20)

isOk := and(success, mload(_pPairing))
Expand All @@ -169,23 +169,22 @@ contract MynaUserSigVerifier {
mstore(0x40, add(pMem, pLastMem))

// Validate that all evaluations ∈ F

checkField(calldataload(add(_pubSignals, 0)))

checkField(calldataload(add(_pubSignals, 32)))

checkField(calldataload(add(_pubSignals, 64)))

checkField(calldataload(add(_pubSignals, 96)))

checkField(calldataload(add(_pubSignals, 128)))


// Validate all evaluations
let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem)

mstore(0, isValid)
return(0, 0x20)
}
}
}
return(0, 0x20)
}
}
}
13 changes: 13 additions & 0 deletions packages/extention-contracts/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

.PHONY: format
format:
forge fmt --root $(ROOT_DIR)

.PHONY: format_check
format_check:
forge fmt --check --root $(ROOT_DIR)


setup:
node ./script/circom-verifier-setup.js
2 changes: 1 addition & 1 deletion packages/extention-contracts/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ contract DeployMainMynaTree is Script {
// console.log("Deployed main tree at: ", address(mainTree));
// vm.stopBroadcast();
// }
// }
// }
73 changes: 35 additions & 38 deletions packages/extention-contracts/src/MainMynaTree.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {BinaryIMT, BinaryIMTData} from "./utils/BinaryIMT.sol";
import {BinaryIMT, BinaryIMTData} from "./utils/BinaryIMT.sol";
import "./interfaces/IMainMynaRegistrationVerifier.sol";
import "./interfaces/IMainMynaInclusionVerifier.sol";
import "./interfaces/IMainMynaTree.sol";
Expand All @@ -19,67 +19,70 @@ contract MainMynaTree is IMainMynaTree {
mapping(uint256 => uint256) internal isRegisteredModulus;
mapping(uint256 => MainAccount) public mainAccounts;
uint256 internal totalMainAccounts;

error Myna__RegisteredModulus();
error Myna__InvalidSignature();
error Myna__NeedToRegisterTwoValue();

event MainAccountAdded(uint256 index, uint256 hashedModulus, uint256 hashedUserSecret);

constructor(
IMainMynaRegistrationVerifier _registrationVerifier,
IMainMynaInclusionVerifier _inclusionVerifier
) {
uint256 zeroValue = uint256(keccak256(abi.encodePacked(uint(42)))) >> 8;
constructor(IMainMynaRegistrationVerifier _registrationVerifier, IMainMynaInclusionVerifier _inclusionVerifier) {
uint256 zeroValue = uint256(keccak256(abi.encodePacked(uint256(42)))) >> 8;
BinaryIMT.init(mynaMainAccounts, 16, zeroValue);
registrationVerifier = _registrationVerifier;
inclusionVerifier = _inclusionVerifier;
}

function addMainAccount(
uint hashedModulus,
uint hashedUserSecret,
uint[17] calldata signature,
uint[8] calldata proof
uint256 hashedModulus,
uint256 hashedUserSecret,
uint256[17] calldata signature,
uint256[8] calldata proof
) public {
if (isRegisteredModulus[hashedModulus] == 1) {
revert Myna__RegisteredModulus();
}

bool res = registrationVerifier.verifyProof(
[proof[0], proof[1]],
[
[proof[2], proof[3]],
[proof[4], proof[5]]
],
[[proof[2], proof[3]], [proof[4], proof[5]]],
[proof[6], proof[7]],
[
hashedModulus, hashedUserSecret,
signature[0], signature[1], signature[2],
signature[3], signature[4], signature[5],
signature[6], signature[7], signature[8],
signature[9], signature[10], signature[11],
signature[12], signature[13], signature[14],
signature[15], signature[16]
hashedModulus,
hashedUserSecret,
signature[0],
signature[1],
signature[2],
signature[3],
signature[4],
signature[5],
signature[6],
signature[7],
signature[8],
signature[9],
signature[10],
signature[11],
signature[12],
signature[13],
signature[14],
signature[15],
signature[16]
]
);
if(!res) {
if (!res) {
revert Myna__InvalidSignature();
}

BinaryIMT.insert(mynaMainAccounts, hashedModulus);
BinaryIMT.insert(mynaMainAccounts, hashedUserSecret);
uint num = mynaMainAccounts.numberOfLeaves;

uint256 num = mynaMainAccounts.numberOfLeaves;
if (num % 2 != 0) {
revert Myna__NeedToRegisterTwoValue();
}

isRegisteredModulus[hashedModulus] = 1;
mainAccounts[totalMainAccounts] = MainAccount(
hashedModulus,
hashedUserSecret
);
mainAccounts[totalMainAccounts] = MainAccount(hashedModulus, hashedUserSecret);
totalMainAccounts += 1;
emit MainAccountAdded(totalMainAccounts, hashedModulus, hashedUserSecret);
}
Expand All @@ -96,18 +99,12 @@ contract MainMynaTree is IMainMynaTree {
return mynaMainAccounts.numberOfLeaves;
}

function verifyMainAccount(
uint[8] calldata proof,
uint identityCommitment
) public view returns (bool) {
function verifyMainAccount(uint256[8] calldata proof, uint256 identityCommitment) public view returns (bool) {
return inclusionVerifier.verifyProof(
[proof[0], proof[1]],
[
[proof[2], proof[3]],
[proof[4], proof[5]]
],
[[proof[2], proof[3]], [proof[4], proof[5]]],
[proof[6], proof[7]],
[identityCommitment, mynaMainAccounts.root]
);
}
}
}
Loading

0 comments on commit 8b861f1

Please sign in to comment.