forked from HarryR/ethsnarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiximus.sol
132 lines (94 loc) · 3.26 KB
/
Miximus.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
copyright 2018 to the Miximus Authors
This file is part of Miximus.
Miximus is 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.
Miximus 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 Miximus. If not, see <https://www.gnu.org/licenses/>.
*/
pragma solidity ^0.4.24;
import "./Verifier.sol";
import "./SnarkUtils.sol";
import "./MerkleTree.sol";
import "./LongsightL.sol";
contract Miximus
{
using MerkleTree for MerkleTree.Data;
uint constant public AMOUNT = 1 ether;
mapping (uint256 => bool) public nullifiers;
MerkleTree.Data internal tree;
function GetRoot()
public view returns (uint256)
{
return tree.GetRoot();
}
/**
* Returns leaf offset
*/
function Deposit(uint256 leaf)
public payable returns (uint256 new_root, uint256 new_offset)
{
require( msg.value == AMOUNT );
return tree.Insert(leaf);
}
function MakeLeafHash(uint256 spend_preimage, uint256 nullifier)
public pure returns (uint256)
{
uint256[10] memory round_constants;
LongsightL.ConstantsL12p5(round_constants);
uint256 spend_hash = LongsightL.LongsightL12p5_MP([spend_preimage, nullifier], 0, round_constants);
return LongsightL.LongsightL12p5_MP([nullifier, spend_hash], 0, round_constants);
}
function GetPath(uint256 leaf)
public view returns (uint256[29] out_path, bool[29] out_addr)
{
return tree.GetProof(leaf);
}
function GetExtHash()
public view returns (uint256)
{
return uint256(sha256(
abi.encodePacked(
address(this),
msg.sender
))) % Verifier.ScalarField();
}
function IsSpent(uint256 nullifier)
public view returns (bool)
{
return nullifiers[nullifier];
}
function VerifyProof( uint256 in_root, uint256 in_nullifier, uint256 in_exthash, uint256[8] proof )
public view returns (bool)
{
uint256[] memory snark_input = new uint256[](3);
snark_input[0] = in_root;
snark_input[1] = in_nullifier;
snark_input[2] = in_exthash;
uint256[14] memory vk;
uint256[] memory vk_gammaABC;
(vk, vk_gammaABC) = GetVerifyingKey();
return Verifier.Verify( vk, vk_gammaABC, proof, snark_input );
}
function Withdraw(
uint256 in_root,
uint256 in_nullifier,
uint256[8] proof
)
public
{
require( false == nullifiers[in_nullifier] );
bool is_valid = VerifyProof(in_root, in_nullifier, GetExtHash(), proof);
require( is_valid );
nullifiers[in_nullifier] = true;
msg.sender.transfer(AMOUNT);
}
function GetVerifyingKey ()
public view returns (uint256[14] out_vk, uint256[] out_gammaABC);
}