Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dvush/81/new_deposits_exits_node…
Browse files Browse the repository at this point in the history
…' into tymurkhr/76/circuit_refactoring
  • Loading branch information
TymurKhr committed Sep 19, 2019
2 parents 444dd9f + c56e81e commit 4aca8d1
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 229 deletions.
96 changes: 37 additions & 59 deletions contracts/contracts/Franklin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";

import "./Governance.sol";
import "./Verifier.sol";
import "./VerificationKey.sol";
import "./Bytes.sol";

// GLOBAL TODOS:
// - check overflows

contract Franklin {
// Verification key contract
VerificationKey verificationKey;
// Verifier contract
Verifier verifier;
// Governance contract
Expand Down Expand Up @@ -230,12 +227,10 @@ contract Franklin {
// sets genesis root
constructor(
address _verifierAddress,
address _vkAddress,
bytes32 _genesisRoot,
address _governanceAddress
) public {
verifier = Verifier(_verifierAddress);
verificationKey = VerificationKey(_vkAddress);
governance = Governance(_governanceAddress);

blocks[0].stateRoot = _genesisRoot;
Expand Down Expand Up @@ -808,10 +803,10 @@ contract Franklin {
); // fvk12 - not a validator in verify

// TODO: - doesnt work in integration test - revert with vfyfp3 code. Need to be fixed
// require(
// verifyBlockProof(_proof, blocks[_blockNumber].commitment),
// "fvk13"
// ); // fvk13 - verification failed
require(
verifier.verifyBlockProof(_proof, blocks[_blockNumber].commitment),
"fvk13"
); // fvk13 - verification failed

consummateOnchainOps(_blockNumber);

Expand All @@ -825,24 +820,6 @@ contract Franklin {
emit BlockVerified(_blockNumber);
}

// Proof verification
// Params:
// - _proof - block number
// - _commitment - block commitment
function verifyBlockProof(uint256[8] memory _proof, bytes32 _commitment)
internal
view
returns (bool valid)
{
uint256 mask = (~uint256(0)) >> 3;
uint256[14] memory vk;
uint256[] memory gammaABC;
(vk, gammaABC) = verificationKey.getVk();
uint256[] memory inputs = new uint256[](1);
inputs[0] = uint256(_commitment) & mask;
return verifier.Verify(vk, gammaABC, _proof, inputs);
}

// If block is verified the onchain operations from it must be completed
// (user must have possibility to withdraw funds if withdrawed)
// Params:
Expand Down Expand Up @@ -900,40 +877,41 @@ contract Franklin {

// Checks that commitment is expired and revert blocks
function triggerRevertIfBlockCommitmentExpired() internal returns (bool) {
if (
totalBlocksCommitted > totalBlocksVerified
&& blocks[totalBlocksVerified + 1].committedAtBlock > 0
&& block.number > blocks[totalBlocksVerified + 1].committedAtBlock + EXPECT_VERIFICATION_IN
) {
revertBlocks();
return true;
}
// TODO: uncomment
// if (
// totalBlocksCommitted > totalBlocksVerified
// && blocks[totalBlocksVerified + 1].committedAtBlock > 0
// && block.number > blocks[totalBlocksVerified + 1].committedAtBlock + EXPECT_VERIFICATION_IN
// ) {
// revertBlocks();
// return true;
// }
return false;
}

// Reverts unverified blocks
function revertBlocks() internal {
for (uint32 i = totalBlocksVerified + 1; i <= totalBlocksCommitted; i++) {
Block memory reverted = blocks[i];
revertBlock(reverted);
delete blocks[i];
}
totalBlocksCommitted -= totalBlocksCommitted - totalBlocksVerified;
emit BlocksReverted(totalBlocksVerified, totalBlocksCommitted);
}

// Reverts block onchain operations
// Params:
// - _reverted - reverted block
function revertBlock(Block memory _reverted) internal {
require(
_reverted.committedAtBlock > 0,
"frk11"
); // frk11 - block not found
revertOnchainOps(_reverted.operationStartId, _reverted.onchainOperations);
totalCommittedPriorityRequests -= _reverted.priorityOperations;
}

// TODO: uncomment
// // Reverts unverified blocks
// function revertBlocks() internal {
// for (uint32 i = totalBlocksVerified + 1; i <= totalBlocksCommitted; i++) {
// Block memory reverted = blocks[i];
// revertBlock(reverted);
// delete blocks[i];
// }
// totalBlocksCommitted -= totalBlocksCommitted - totalBlocksVerified;
// emit BlocksReverted(totalBlocksVerified, totalBlocksCommitted);
// }
//
// // Reverts block onchain operations
// // Params:
// // - _reverted - reverted block
// function revertBlock(Block memory _reverted) internal {
// require(
// _reverted.committedAtBlock > 0,
// "frk11"
// ); // frk11 - block not found
// revertOnchainOps(_reverted.operationStartId, _reverted.onchainOperations);
// totalCommittedPriorityRequests -= _reverted.priorityOperations;
// }
//
// MARK: - EXODUS MODE

// Checks that current state not is exodus mode
Expand Down
22 changes: 0 additions & 22 deletions contracts/contracts/FranklinTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import "./Bytes.sol";
// - check overflows

contract FranklinTest {
// Verification key contract
VerificationKey verificationKey;
// Verifier contract
Verifier verifier;
// Governance contract
Expand Down Expand Up @@ -227,12 +225,10 @@ contract FranklinTest {
// sets genesis root
constructor(
address _verifierAddress,
address _vkAddress,
bytes32 _genesisRoot,
address _governanceAddress
) public {
verifier = Verifier(_verifierAddress);
verificationKey = VerificationKey(_vkAddress);
governance = Governance(_governanceAddress);

blocks[0].stateRoot = _genesisRoot;
Expand Down Expand Up @@ -815,24 +811,6 @@ contract FranklinTest {
emit BlockVerified(_blockNumber);
}

// Proof verification
// Params:
// - _proof - block number
// - _commitment - block commitment
function verifyBlockProof(uint256[8] memory _proof, bytes32 _commitment)
internal
view
returns (bool valid)
{
uint256 mask = (~uint256(0)) >> 3;
uint256[14] memory vk;
uint256[] memory gammaABC;
(vk, gammaABC) = verificationKey.getVk();
uint256[] memory inputs = new uint256[](1);
inputs[0] = uint256(_commitment) & mask;
return verifier.Verify(vk, gammaABC, _proof, inputs);
}

// If block is verified the onchain operations from it must be completed
// (user must have possibility to withdraw funds if withdrawed)
// Params:
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/VerificationKey.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pragma solidity ^0.5.1;
// Hardcoded constants to avoid accessing store
contract VerificationKey {

function getVk() external pure returns (uint256[14] memory vk, uint256[] memory gammaABC) {
function getVk() internal pure returns (uint256[14] memory vk, uint256[] memory gammaABC) {


vk[0] = 0x2998fa4d1bd5e9cb49d9f091ecb5ecab72d368d76884a73ef97f669b35bdb26e;
Expand Down
25 changes: 23 additions & 2 deletions contracts/contracts/Verifier.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
// from https://github.com/HarryR/ethsnarks/blob/master/contracts/Verifier.sol
pragma solidity ^0.5.8;

contract Verifier {
import "./VerificationKey.sol";

contract Verifier is VerificationKey {

// Proof verification
// Params:
// - _proof - block number
// - _commitment - block commitment
function verifyBlockProof(uint256[8] calldata _proof, bytes32 _commitment)
external
view
returns (bool valid)
{
uint256 mask = (~uint256(0)) >> 3;
uint256[14] memory vk;
uint256[] memory gammaABC;
(vk, gammaABC) = getVk();
uint256[] memory inputs = new uint256[](1);
inputs[0] = uint256(_commitment) & mask;
return Verify(vk, gammaABC, _proof, inputs);
}

function NegateY(uint256 Y) internal pure returns (uint256) {
uint256 q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
return q - (Y % q);
Expand All @@ -12,7 +33,7 @@ contract Verifier {
uint256[] memory vk_gammaABC,
uint256[8] memory in_proof,
uint256[] memory proof_inputs
) public view returns (bool) {
) internal view returns (bool) {
require(
((vk_gammaABC.length / 2) - 1) == proof_inputs.length,
"vvy11"
Expand Down
21 changes: 2 additions & 19 deletions contracts/contracts/VerifyTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,16 @@ import "./VerificationKey.sol";
import "./Franklin.sol";

contract VerifyTest {
VerificationKey verificationKey;
Verifier verifier;

constructor(address _verifierAddress, address _vkAddress) public {
constructor(address _verifierAddress) public {
verifier = Verifier(_verifierAddress);
verificationKey = VerificationKey(_vkAddress);
}

function verifyProof(bytes32 commitment, uint256[8] calldata proof)
external
view
{
require(verifyBlockProof(proof, commitment), "verification failed");
require(verifier.verifyBlockProof(proof, commitment), "verification failed");
}

function verifyBlockProof(uint256[8] memory proof, bytes32 commitment)
internal
view
returns (bool valid)
{
uint256 mask = (~uint256(0)) >> 3;
uint256[14] memory vk;
uint256[] memory gammaABC;
(vk, gammaABC) = verificationKey.getVk();
uint256[] memory inputs = new uint256[](1);
inputs[0] = uint256(commitment) & mask;
return verifier.Verify(vk, gammaABC, proof, inputs);
}

}
5 changes: 0 additions & 5 deletions contracts/src.ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,16 @@ export async function deployFranklin(
genesisRoot = ethers.constants.HashZero,
franklinCode = franklinContractCode,
verifierCode = verifierContractCode,
vkCode = vkContractCode
) {
try {
let verifier = await deployContract(wallet, verifierCode, [], {
gasLimit: 1000000,
});
let vk = await deployContract(wallet, vkCode, [], {
gasLimit: 1000000,
});
let contract = await deployContract(
wallet,
franklinCode,
[
verifier.address,
vk.address,
genesisRoot,
governanceAddress
],
Expand Down
3 changes: 2 additions & 1 deletion core/key_generator/src/vk_contract_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pragma solidity ^0.5.1;
// Hardcoded constants to avoid accessing store
contract {contract_name} {{
function {function_name}() external pure returns (uint256[14] memory vk, uint256[] memory gammaABC) {{
function {function_name}() interna
l pure returns (uint256[14] memory vk, uint256[] memory gammaABC) {{
{vk}
Expand Down
2 changes: 0 additions & 2 deletions core/models/src/circuit/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ impl std::default::Default for CircuitAccount<Bn256> {
Self {
nonce: Fr::zero(),
pub_key_hash: Fr::zero(),
// pub_x: Fr::zero(),
// pub_y: Fr::zero(),
subtree: SparseMerkleTree::new(params::BALANCE_TREE_DEPTH as u32),
}
}
Expand Down
Loading

0 comments on commit 4aca8d1

Please sign in to comment.