-
Notifications
You must be signed in to change notification settings - Fork 2
/
swiftlancer_final.sol
257 lines (207 loc) · 9.36 KB
/
swiftlancer_final.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;
import "./SimplifiedBigNumber.sol";
contract SwiftLancerParameters {
function get_g () returns (BigNumber.instance memory) {}
function get_h () returns (BigNumber.instance memory) {}
function get_p () returns (BigNumber.instance memory) {}
}
contract RegistrationAuthority {
address public authority;
mapping(address => bool) public users;
constructor () public { }
//function add (address user) public {}
//function revoke (address user) public { }
}
contract SwiftLancer{
using BigNumber for *;
event debug(bytes32 a);
event FillGold(string a);
event OpenGold(string b);
event PlainTextProof(string a);
event Ciphertexts(
bytes c1,
bytes c2
);
event Answers_Test(string b);
// answers of workers
struct answers {
bytes32[106] ciphers;
uint counter;
uint[3] err_indexes;
uint err_counter;
}
address public requester;
uint[6] public actual_gold_indices;
uint[6] public actual_gold_solutions;
SwiftLancerParameters parameters;
RegistrationAuthority ra;
bytes32 public task_swarm_addr = 0xb833be6d483c981488a6b0f32fd133f9f7b8810a9663becb38359c1731210529;
//https://swarm-gateways.net/bzz:/b833be6d483c981488a6b0f32fd133f9f7b8810a9663becb38359c1731210529/
struct GoldenStandard{
bytes32[6] index_val;
bytes32[6] sol_val;
uint ctr;
}
GoldenStandard public gs;
mapping(address => answers) public answers_map;
address[4] public workers = [0,0,0,0];
uint public workers_counter = 0;
constructor () public{
parameters = SwiftLancerParameters(0xe259adC77c6F214233Ab42e1c10f07ffd3314C64);
ra = RegistrationAuthority(0x8884A1aca7D2F031d674994232c10Ba64fC33903);
requester = msg.sender;
}
//filling the golden standard commitments with indices and their respective solutions
function fill_golden(bytes32[6] i_val, bytes32[6] s_val){
require(msg.sender == requester);
gs.index_val = i_val;
gs.sol_val = s_val;
emit FillGold("Gold array filled!");
}
// opening the commitments and checking if it matches with the committed values
function opening_phase(uint[] indices, uint[] sols, uint r_val) returns (bool){
//require(msg.sender == requester);
for(uint i=0;i<6;i++){
bytes32 lhs1 = sha3(sha3(indices[i]), sha3(r_val));
bytes32 lhs2 = sha3(sha3(sols[i]), sha3(r_val));
if((lhs1 == gs.index_val[i]) && (lhs2 == gs.sol_val[i])){
actual_gold_indices[i] = indices[i];
actual_gold_solutions[i] = sols[i];
emit OpenGold("Commitment to index and sol correct!");
continue;
}
else{
emit OpenGold("Commitment to index and sol false!");
return false;
}
}
return true;
}
// different plaintext proof for proving that worker did not provide correct solution of golden standard questions
function different_plaintext_proof(bytes memory c1_val, uint c1_bitlen, bytes memory c2_val, uint c2_bitlen, uint q_index, address worker, bytes memory a_val, uint a_bitlen, bytes memory z_val, uint z_bitlen, uint hash_length) returns (bool){
for(uint i=0; i<6; i++){
if(q_index == actual_gold_indices[i]){
if (answers_map[worker].err_indexes[0] == q_index) {
return false;
}
if (answers_map[worker].err_indexes[1] == q_index) {
return false;
}
if (answers_map[worker].err_indexes[2] == q_index) {
return false;
}
if(answers_map[worker].ciphers[q_index] == sha3(c1_val, c2_val)){
//emit PlainTextProof("Hashes of Ciphertexts match");
if (check_proof(c1_val, c1_bitlen, c2_val, c2_bitlen, a_val, a_bitlen, z_val, z_bitlen, 1 - actual_gold_solutions[i], hash_length)) {
answers_map[worker].err_indexes[answers_map[worker].err_counter] = q_index;
answers_map[worker].err_counter += 1;
emit PlainTextProof("Different Plaintext Verified!");
return true;
}
else {
return false;
}
}
else{
//emit PlainTextProof("Hashes of Ciphertexts don't match");
return false;
}
}
}
return false;
}
//
function check_proof(bytes memory c1_val, uint c1_bitlen, bytes memory c2_val, uint c2_bitlen, bytes memory a_val, uint a_bitlen, bytes memory z_val, uint z_bitlen, uint solexp, uint hash_length) internal returns(bool) {
BigNumber.instance memory c = prepare_nizk_challenge(a_val, hash_length);
BigNumber.instance memory p = parameters.get_p();
BigNumber.instance memory rhs = different_plaintext_proof_rhs(c2_val, c2_bitlen, a_val, a_bitlen, c, p);
BigNumber.instance memory lhs = different_plaintext_proof_lhs(c1_val, c1_bitlen, z_val, z_bitlen, p);
if(solexp == 1) {
lhs = lhs.modmul(parameters.get_g().prepare_modexp(c, p), p);
}
return (BigNumber.equal(lhs,rhs));
}
//
function prepare_nizk_challenge(bytes a_val, uint hash_length) internal returns (BigNumber.instance memory) {
BigNumber.instance memory c = BigNumber.instance(toBytes(sha3(a_val, parameters.get_g().val, parameters.get_h().val)), hash_length);
return c;
}
// computing left hand side of the different plaintext proof
function different_plaintext_proof_lhs(bytes c1_val, uint c1_bitlen, bytes z_val, uint z_bitlen, BigNumber.instance memory p) internal returns (BigNumber.instance memory) {
BigNumber.instance memory z = BigNumber.instance(z_val, z_bitlen);
BigNumber.instance memory c1 = BigNumber.instance(c1_val, c1_bitlen);
BigNumber.instance memory lhs = c1.prepare_modexp(z, p);
return lhs;
}
// computing right hand side of the different plaintext proof
function different_plaintext_proof_rhs(bytes memory c2_val, uint c2_bitlen, bytes memory a_val, uint a_bitlen, BigNumber.instance memory c, BigNumber.instance memory p) internal returns (BigNumber.instance memory) {
BigNumber.instance memory a = BigNumber.instance(a_val, a_bitlen);
BigNumber.instance memory c2 = BigNumber.instance(c2_val, c2_bitlen);
BigNumber.instance memory rhs = a.modmul( c2.prepare_modexp(c, p), p);
return rhs;
}
// workers submitting answers to the contract
function submit_answers(uint[40] memory i, bytes[40] memory c_1, bytes[40] memory c_2) {
address worker = msg.sender;
require (ra.users(worker));
for (uint j = 0; j < 40; j++) {
bytes32 hash = sha3(c_1[j],c_2[j]);
emit Ciphertexts(c_1[j], c_2[j]);
if (i[j] > 0) {
answers_map[worker].ciphers[i[j]] = hash;
answers_map[worker].counter += 1;
} else {
answers hisAnswers;
hisAnswers.ciphers[0] = hash;
answers_map[worker] = hisAnswers;
answers_map[worker].counter = 1;
}
if (answers_map[worker].counter == 106 && workers_counter < 4) {
for (uint k = 0; k < workers_counter; k++) {
if (workers[k] == worker) {
return;
}
}
workers[workers_counter] = worker;
workers_counter += 1;
answers_map[worker].err_indexes = [106,106,106];
}
}
}
// workers submitting answers to the contract
function submit_answers(uint[33] memory i, bytes[33] memory c_1, bytes[33] memory c_2) {
address worker = msg.sender;
require (ra.users(worker));
for (uint j = 0; j < 33; j++) {
bytes32 hash = sha3(c_1[j],c_2[j]);
emit Ciphertexts(c_1[j], c_2[j]);
if (i[j] > 0) {
answers_map[worker].ciphers[i[j]] = hash;
answers_map[worker].counter += 1;
} else {
answers hisAnswers;
hisAnswers.ciphers[0] = hash;
answers_map[worker] = hisAnswers;
answers_map[worker].counter = 1;
}
if (answers_map[worker].counter == 106 && workers_counter < 4) {
for (uint k = 0; k < workers_counter; k++) {
if (workers[k] == worker) {
return;
}
}
workers[workers_counter] = worker;
workers_counter += 1;
answers_map[worker].err_indexes = [106,106,106];
}
}
}
function toBytes(uint256 x) returns (bytes memory b) {
b = new bytes(32);
assembly { mstore(add(b, 32), x) }
}
function toBytes(bytes32 _data) public pure returns (bytes memory) {
return abi.encodePacked(_data);
}
}