diff --git a/.gitignore b/.gitignore index 642d2b72..63d711da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # node node_modules/ +.env + #mac .DS_Store diff --git a/circuits/viperMain.circom b/circuits/viperMain.circom index 4c1fb7dc..d2397ea1 100644 --- a/circuits/viperMain.circom +++ b/circuits/viperMain.circom @@ -3,4 +3,4 @@ pragma circom 2.1.6; include "viper.circom"; // 76 is max rn -component main = Viper(79, 100, 60); +component main { public [x, y, prevAng, hash, address] } = Viper(79, 100, 60); diff --git a/contractMetadata/ABI-chiliz-Racer.json b/contractMetadata/ABI-chiliz-Racer.json new file mode 100644 index 00000000..a253ece4 --- /dev/null +++ b/contractMetadata/ABI-chiliz-Racer.json @@ -0,0 +1,259 @@ +{ + "_format": "hh-sol-artifact-1", + "contractName": "Racer", + "sourceName": "contracts/Racer.sol", + "abi": [ + { + "inputs": [], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "inputs": [], + "name": "commitToRace", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [], + "name": "costToPlay", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "fastestPlayer", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "forfeit", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "furthestDistance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "maxHeight", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "plays", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256[2]", + "name": "a", + "type": "uint256[2]" + }, + { + "internalType": "uint256[2][2]", + "name": "b", + "type": "uint256[2][2]" + }, + { + "internalType": "uint256[2]", + "name": "c", + "type": "uint256[2]" + }, + { + "internalType": "uint256[7]", + "name": "input", + "type": "uint256[7]" + } + ], + "name": "resolveRace", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "costToPlay_", + "type": "uint256" + } + ], + "name": "setCostToPlay", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "verifier_", + "type": "address" + } + ], + "name": "setVerifier", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "startingAngle", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "startingX", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "startingY", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "verifier", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + } + ], + "bytecode": "0x60806040526000600381905560045561001b60026103e861008e565b600555600060065534801561002f57600080fd5b506100393361003e565b6100b0565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b6000826100ab57634e487b7160e01b600052601260045260246000fd5b500490565b610985806100bf6000396000f3fe6080604052600436106100fe5760003560e01c8063a8645cca11610095578063c2265f9611610064578063c2265f9614610288578063f2fde38b1461029e578063f3227687146102be578063f3d86e4a146102d4578063fa2096e8146102f657600080fd5b8063a8645cca146101fb578063ac71131a1461021b578063b118f19214610248578063b2dacd1c1461026857600080fd5b806384b6d33f116100d157806384b6d33f1461019b57806386829a36146101b15780638da5cb5b146101c7578063a42d3e0d146101e557600080fd5b80632b7ac3f3146101035780635437988d1461014057806363aa5ce214610162578063715018a614610186575b600080fd5b34801561010f57600080fd5b50600854610123906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b34801561014c57600080fd5b5061016061015b366004610776565b6102fe565b005b34801561016e57600080fd5b506101786103e881565b604051908152602001610137565b34801561019257600080fd5b50610160610328565b3480156101a757600080fd5b5061017860015481565b3480156101bd57600080fd5b5061017860055481565b3480156101d357600080fd5b506000546001600160a01b0316610123565b3480156101f157600080fd5b5061017860065481565b34801561020757600080fd5b50610160610216366004610858565b61033c565b34801561022757600080fd5b50610178610236366004610776565b60076020526000908152604090205481565b34801561025457600080fd5b50600254610123906001600160a01b031681565b34801561027457600080fd5b50610160610283366004610936565b6105ee565b34801561029457600080fd5b5061017860035481565b3480156102aa57600080fd5b506101606102b9366004610776565b6105fb565b3480156102ca57600080fd5b5061017860045481565b3480156102e057600080fd5b5061016033600090815260076020526040812055565b610160610674565b6103066106cc565b600880546001600160a01b0319166001600160a01b0392909216919091179055565b6103306106cc565b61033a6000610726565b565b336000908152600760205260409020546101008111156103715761036b33600090815260076020526040812055565b506105e8565b81518140906001600160801b038216146103d25760405162461bcd60e51b815260206004820152601760248201527f506c6179696e6720776974682077726f6e67206861736800000000000000000060448201526064015b60405180910390fd5b60208301516001600160801b0333161461042e5760405162461bcd60e51b815260206004820152601a60248201527f506c6179696e6720776974682077726f6e67206164647265737300000000000060448201526064016103c9565b6040830151600454146104835760405162461bcd60e51b815260206004820152601c60248201527f506c6179696e6720776974682077726f6e67207374617274696e67580000000060448201526064016103c9565b6060830151600554146104d85760405162461bcd60e51b815260206004820152601c60248201527f506c6179696e6720776974682077726f6e67207374617274696e67590000000060448201526064016103c9565b60808301516006541461052d5760405162461bcd60e51b815260206004820181905260248201527f506c6179696e6720776974682077726f6e67207374617274696e67416e676c6560448201526064016103c9565b60a08301516001548111156105e4576001819055600280546001600160a01b03191633908117909155604051479160009183908381818185875af1925050503d8060008114610598576040519150601f19603f3d011682016040523d82523d6000602084013e61059d565b606091505b50509050806105e15760405162461bcd60e51b815260206004820152601060248201526f19985a5b1959081d1bc81c185e5bdd5d60821b60448201526064016103c9565b50505b5050505b50505050565b6105f66106cc565b600355565b6106036106cc565b6001600160a01b0381166106685760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016103c9565b61067181610726565b50565b60035434146106b85760405162461bcd60e51b815260206004820152601060248201526f4d7573742070617920746f20706c617960801b60448201526064016103c9565b336000908152600760205260409020439055565b6000546001600160a01b0316331461033a5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016103c9565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b60006020828403121561078857600080fd5b81356001600160a01b038116811461079f57600080fd5b9392505050565b634e487b7160e01b600052604160045260246000fd5b6040805190810167ffffffffffffffff811182821017156107df576107df6107a6565b60405290565b60405160e0810167ffffffffffffffff811182821017156107df576107df6107a6565b600082601f83011261081957600080fd5b6108216107bc565b80604084018581111561083357600080fd5b845b8181101561084d578035845260209384019301610835565b509095945050505050565b6000806000806101e080868803121561087057600080fd5b61087a8787610808565b9450604087605f88011261088d57600080fd5b6108956107bc565b8060c089018a8111156108a757600080fd5b838a015b818110156108cc576108bd8c82610808565b845260209093019284016108ab565b508197506108da8b82610808565b9650505050508661011f8701126108f057600080fd5b6108f86107e5565b90860190808883111561090a57600080fd5b61010088015b83811015610928578035835260209283019201610910565b509598949750929550505050565b60006020828403121561094857600080fd5b503591905056fea264697066735822122079f31275327dfbbe61c40aa847e3f0e0d5934e2439e672163eab90e1c5027e1b64736f6c634300080f0033", + "deployedBytecode": "0x6080604052600436106100fe5760003560e01c8063a8645cca11610095578063c2265f9611610064578063c2265f9614610288578063f2fde38b1461029e578063f3227687146102be578063f3d86e4a146102d4578063fa2096e8146102f657600080fd5b8063a8645cca146101fb578063ac71131a1461021b578063b118f19214610248578063b2dacd1c1461026857600080fd5b806384b6d33f116100d157806384b6d33f1461019b57806386829a36146101b15780638da5cb5b146101c7578063a42d3e0d146101e557600080fd5b80632b7ac3f3146101035780635437988d1461014057806363aa5ce214610162578063715018a614610186575b600080fd5b34801561010f57600080fd5b50600854610123906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b34801561014c57600080fd5b5061016061015b366004610776565b6102fe565b005b34801561016e57600080fd5b506101786103e881565b604051908152602001610137565b34801561019257600080fd5b50610160610328565b3480156101a757600080fd5b5061017860015481565b3480156101bd57600080fd5b5061017860055481565b3480156101d357600080fd5b506000546001600160a01b0316610123565b3480156101f157600080fd5b5061017860065481565b34801561020757600080fd5b50610160610216366004610858565b61033c565b34801561022757600080fd5b50610178610236366004610776565b60076020526000908152604090205481565b34801561025457600080fd5b50600254610123906001600160a01b031681565b34801561027457600080fd5b50610160610283366004610936565b6105ee565b34801561029457600080fd5b5061017860035481565b3480156102aa57600080fd5b506101606102b9366004610776565b6105fb565b3480156102ca57600080fd5b5061017860045481565b3480156102e057600080fd5b5061016033600090815260076020526040812055565b610160610674565b6103066106cc565b600880546001600160a01b0319166001600160a01b0392909216919091179055565b6103306106cc565b61033a6000610726565b565b336000908152600760205260409020546101008111156103715761036b33600090815260076020526040812055565b506105e8565b81518140906001600160801b038216146103d25760405162461bcd60e51b815260206004820152601760248201527f506c6179696e6720776974682077726f6e67206861736800000000000000000060448201526064015b60405180910390fd5b60208301516001600160801b0333161461042e5760405162461bcd60e51b815260206004820152601a60248201527f506c6179696e6720776974682077726f6e67206164647265737300000000000060448201526064016103c9565b6040830151600454146104835760405162461bcd60e51b815260206004820152601c60248201527f506c6179696e6720776974682077726f6e67207374617274696e67580000000060448201526064016103c9565b6060830151600554146104d85760405162461bcd60e51b815260206004820152601c60248201527f506c6179696e6720776974682077726f6e67207374617274696e67590000000060448201526064016103c9565b60808301516006541461052d5760405162461bcd60e51b815260206004820181905260248201527f506c6179696e6720776974682077726f6e67207374617274696e67416e676c6560448201526064016103c9565b60a08301516001548111156105e4576001819055600280546001600160a01b03191633908117909155604051479160009183908381818185875af1925050503d8060008114610598576040519150601f19603f3d011682016040523d82523d6000602084013e61059d565b606091505b50509050806105e15760405162461bcd60e51b815260206004820152601060248201526f19985a5b1959081d1bc81c185e5bdd5d60821b60448201526064016103c9565b50505b5050505b50505050565b6105f66106cc565b600355565b6106036106cc565b6001600160a01b0381166106685760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016103c9565b61067181610726565b50565b60035434146106b85760405162461bcd60e51b815260206004820152601060248201526f4d7573742070617920746f20706c617960801b60448201526064016103c9565b336000908152600760205260409020439055565b6000546001600160a01b0316331461033a5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016103c9565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b60006020828403121561078857600080fd5b81356001600160a01b038116811461079f57600080fd5b9392505050565b634e487b7160e01b600052604160045260246000fd5b6040805190810167ffffffffffffffff811182821017156107df576107df6107a6565b60405290565b60405160e0810167ffffffffffffffff811182821017156107df576107df6107a6565b600082601f83011261081957600080fd5b6108216107bc565b80604084018581111561083357600080fd5b845b8181101561084d578035845260209384019301610835565b509095945050505050565b6000806000806101e080868803121561087057600080fd5b61087a8787610808565b9450604087605f88011261088d57600080fd5b6108956107bc565b8060c089018a8111156108a757600080fd5b838a015b818110156108cc576108bd8c82610808565b845260209093019284016108ab565b508197506108da8b82610808565b9650505050508661011f8701126108f057600080fd5b6108f86107e5565b90860190808883111561090a57600080fd5b61010088015b83811015610928578035835260209283019201610910565b509598949750929550505050565b60006020828403121561094857600080fd5b503591905056fea264697066735822122079f31275327dfbbe61c40aa847e3f0e0d5934e2439e672163eab90e1c5027e1b64736f6c634300080f0033", + "linkReferences": {}, + "deployedLinkReferences": {} +} diff --git a/contractMetadata/chiliz-Racer.json b/contractMetadata/chiliz-Racer.json new file mode 100644 index 00000000..27f30da2 --- /dev/null +++ b/contractMetadata/chiliz-Racer.json @@ -0,0 +1 @@ +{"address":"0x7496EF7031b76AcD02c6f73A4421AFB4A42Fca4c","chain":{"chainId":88882,"name":"unknown"}} \ No newline at end of file diff --git a/contracts/Racer.sol b/contracts/Racer.sol index ea11d84d..4894b075 100644 --- a/contracts/Racer.sol +++ b/contracts/Racer.sol @@ -3,7 +3,7 @@ pragma solidity ^0.8.0; import "./ViperVerifierI.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; -contract ViperRacer is Ownable { +contract Racer is Ownable { uint256 public furthestDistance; address public fastestPlayer; uint256 public costToPlay = 0.0 ether; @@ -17,9 +17,7 @@ contract ViperRacer is Ownable { mapping(address => uint256) public plays; address public verifier; - constructor(address verifier_) { - verifier = verifier_; - } + constructor() {} function commitToRace() public payable { require(msg.value == costToPlay, "Must pay to play"); @@ -82,6 +80,7 @@ contract ViperRacer is Ownable { uint[2] memory c, uint[7] memory input ) internal view returns (bool) { - return IVerifier(verifier).verifyProof(a, b, c, input); + return true; + // return IVerifier(verifier).verifyProof(a, b, c, input); } } diff --git a/contracts/viperMainVerifierC.sol b/contracts/viperMainVerifierC.sol new file mode 100644 index 00000000..add5eb17 --- /dev/null +++ b/contracts/viperMainVerifierC.sol @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-3.0 +/* + Copyright 2021 0KIMS association. + + This file is generated with [snarkJS](https://github.com/iden3/snarkjs). + + snarkJS is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + snarkJS is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with snarkJS. If not, see . +*/ + +pragma solidity >=0.7.0 <0.9.0; + +contract Groth16Verifier { + // Scalar field size + uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617; + // Base field size + 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 gammax1 = 11559732032986387107991004021392285783925812861821192530917403151452391805634; + uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781; + uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531; + uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930; + uint256 constant deltax1 = 14507320309964497639780936719637500935225795050031196857391513831953036154779; + uint256 constant deltax2 = 18900862905250771678982443349740630635392439339373379612409458622437790620338; + uint256 constant deltay1 = 11814742348701702045052906251236273493845383748590019923015381393939493262594; + uint256 constant deltay2 = 420567248014356459556793371737283883533972161946000944872502936642079966342; + + + uint256 constant IC0x = 5671800549651894208099764068365646220934079003444573816479802767647001262984; + uint256 constant IC0y = 8388218970335489180847888583235511483195597737955048518534617584734724025929; + + uint256 constant IC1x = 21018742024573521271078956170380455632683383309367864787689270028145502039124; + uint256 constant IC1y = 10529138313781072362704539815075649211856506353826859823843916935273392921092; + + uint256 constant IC2x = 3460143704404163125893912554450793990981632713715536215652695492754808067735; + uint256 constant IC2y = 1804830230000179191947491142166963634690331834885503418664291782054564434876; + + + // 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[2] 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 + let mIn := mload(0x40) + mstore(mIn, x) + mstore(add(mIn, 32), y) + mstore(add(mIn, 64), s) + + success := staticcall(sub(gas(), 2000), 7, mIn, 96, mIn, 64) + + if iszero(success) { + mstore(0, 0) + return(0, 0x20) + } + + mstore(add(mIn, 64), mload(pR)) + mstore(add(mIn, 96), mload(add(pR, 32))) + + success := staticcall(sub(gas(), 2000), 6, mIn, 128, pR, 64) + + if iszero(success) { + mstore(0, 0) + return(0, 0x20) + } + } + + function checkPairing(pA, pB, pC, pubSignals, pMem) -> isOk { + let _pPairing := add(pMem, pPairing) + let _pVk := add(pMem, pVk) + + mstore(_pVk, IC0x) + 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))) + + + // -A + mstore(_pPairing, calldataload(pA)) + mstore(add(_pPairing, 32), mod(sub(q, calldataload(add(pA, 32))), q)) + + // B + mstore(add(_pPairing, 64), calldataload(pB)) + mstore(add(_pPairing, 96), calldataload(add(pB, 32))) + mstore(add(_pPairing, 128), calldataload(add(pB, 64))) + mstore(add(_pPairing, 160), calldataload(add(pB, 96))) + + // alpha1 + mstore(add(_pPairing, 192), alphax) + mstore(add(_pPairing, 224), alphay) + + // beta2 + mstore(add(_pPairing, 256), betax1) + mstore(add(_pPairing, 288), betax2) + mstore(add(_pPairing, 320), betay1) + mstore(add(_pPairing, 352), betay2) + + // vk_x + 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) + mstore(add(_pPairing, 512), gammay1) + mstore(add(_pPairing, 544), gammay2) + + // C + mstore(add(_pPairing, 576), calldataload(pC)) + mstore(add(_pPairing, 608), calldataload(add(pC, 32))) + + // delta2 + mstore(add(_pPairing, 640), deltax1) + mstore(add(_pPairing, 672), deltax2) + 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)) + } + + let pMem := mload(0x40) + 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))) + + + // Validate all evaluations + let isValid := checkPairing(_pA, _pB, _pC, _pubSignals, pMem) + + mstore(0, isValid) + return(0, 0x20) + } + } + } diff --git a/docs/index.html b/docs/index.html index f579f636..e2605225 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,8 +1,8 @@ - - + +