-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathVerifierChannel.sol
249 lines (207 loc) · 8.23 KB
/
VerifierChannel.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
/*
Copyright 2019-2022 StarkWare Industries Ltd.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.starkware.co/open-source-license/
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions
and limitations under the License.
*/
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.12;
import "./Prng.sol";
/*
Implements the communication channel from the verifier to the prover in the non-interactive case
(See the BCS16 paper for more details).
The state of the channel is stored in a uint256[3] as follows:
[0] proof pointer.
[1] prng digest.
[2] prng counter.
*/
contract VerifierChannel is Prng {
event LogValue(bytes32 val);
event SendRandomnessEvent(uint256 val);
event ReadFieldElementEvent(uint256 val);
event ReadHashEvent(bytes32 val);
function getPrngPtr(uint256 channelPtr) internal pure returns (uint256) {
return channelPtr + 0x20;
}
function initChannel(
uint256 channelPtr,
uint256 proofPtr,
bytes32 publicInputHash
) internal pure {
assembly {
// Skip 0x20 bytes length at the beginning of the proof.
mstore(channelPtr, add(proofPtr, 0x20))
}
initPrng(getPrngPtr(channelPtr), publicInputHash);
}
/*
Sends a field element through the verifier channel.
Note that the logic of this function is inlined in many places throughout the code to reduce
gas costs.
*/
function sendFieldElements(
uint256 channelPtr,
uint256 nElements,
uint256 targetPtr
) internal pure {
require(nElements < 0x1000000, "Overflow protection failed.");
assembly {
// 31 * PRIME.
let BOUND := 0xf80000000000020f00000000000000000000000000000000000000000000001f
let digestPtr := add(channelPtr, 0x20)
let counterPtr := add(channelPtr, 0x40)
let endPtr := add(targetPtr, mul(nElements, 0x20))
for {
} lt(targetPtr, endPtr) {
targetPtr := add(targetPtr, 0x20)
} {
// *targetPtr = getRandomFieldElement(getPrngPtr(channelPtr));
let fieldElement := BOUND
// while (fieldElement >= 31 * K_MODULUS).
for {
} iszero(lt(fieldElement, BOUND)) {
} {
// keccak256(abi.encodePacked(digest, counter));
fieldElement := keccak256(digestPtr, 0x40)
// *counterPtr += 1;
mstore(counterPtr, add(mload(counterPtr), 1))
}
// *targetPtr = fromMontgomery(fieldElement);
mstore(targetPtr, mulmod(fieldElement, K_MONTGOMERY_R_INV, K_MODULUS))
}
}
}
/*
Sends random queries and returns an array of queries sorted in ascending order.
Generates count queries in the range [0, mask] and returns the number of unique queries.
Note that mask is of the form 2^k-1 (for some k <= 64).
Note that queriesOutPtr may be (and is) interleaved with other arrays. The stride parameter
is passed to indicate the distance between every two entries in the queries array, i.e.
stride = 0x20*(number of interleaved arrays).
*/
function sendRandomQueries(
uint256 channelPtr,
uint256 count,
uint256 mask,
uint256 queriesOutPtr,
uint256 stride
) internal pure returns (uint256) {
require(mask < 2**64, "mask must be < 2**64.");
uint256 val;
uint256 shift = 0;
uint256 endPtr = queriesOutPtr;
for (uint256 i = 0; i < count; i++) {
if (shift == 0) {
val = uint256(getRandomBytes(getPrngPtr(channelPtr)));
shift = 0x100;
}
shift -= 0x40;
uint256 queryIdx = (val >> shift) & mask;
uint256 ptr = endPtr;
// Initialize 'curr' to -1 to make sure the condition 'queryIdx != curr' is satisfied
// on the first iteration.
uint256 curr = uint256(-1);
// Insert new queryIdx in the correct place like insertion sort.
while (ptr > queriesOutPtr) {
assembly {
curr := mload(sub(ptr, stride))
}
if (queryIdx >= curr) {
break;
}
assembly {
mstore(ptr, curr)
}
ptr -= stride;
}
if (queryIdx != curr) {
assembly {
mstore(ptr, queryIdx)
}
endPtr += stride;
} else {
// Revert right shuffling.
while (ptr < endPtr) {
assembly {
mstore(ptr, mload(add(ptr, stride)))
ptr := add(ptr, stride)
}
}
}
}
return (endPtr - queriesOutPtr) / stride;
}
function readBytes(uint256 channelPtr, bool mix) internal pure returns (bytes32) {
uint256 proofPtr;
bytes32 val;
assembly {
proofPtr := mload(channelPtr)
val := mload(proofPtr)
mstore(channelPtr, add(proofPtr, 0x20))
}
if (mix) {
// Mix the bytes that were read into the state of the channel.
assembly {
let digestPtr := add(channelPtr, 0x20)
let counterPtr := add(digestPtr, 0x20)
// digest += 1.
mstore(digestPtr, add(mload(digestPtr), 1))
mstore(counterPtr, val)
// prng.digest := keccak256(digest + 1||val), nonce was written earlier.
mstore(digestPtr, keccak256(digestPtr, 0x40))
// prng.counter := 0.
mstore(counterPtr, 0)
}
}
return val;
}
function readHash(uint256 channelPtr, bool mix) internal pure returns (bytes32) {
bytes32 val = readBytes(channelPtr, mix);
return val;
}
/*
Reads a field element from the verifier channel (that is, the proof in the non-interactive
case).
The field elements on the channel are in Montgomery form and this function converts
them to the standard representation.
Note that the logic of this function is inlined in many places throughout the code to reduce
gas costs.
*/
function readFieldElement(uint256 channelPtr, bool mix) internal pure returns (uint256) {
uint256 val = fromMontgomery(uint256(readBytes(channelPtr, mix)));
return val;
}
function verifyProofOfWork(uint256 channelPtr, uint256 proofOfWorkBits) internal pure {
if (proofOfWorkBits == 0) {
return;
}
uint256 proofOfWorkDigest;
assembly {
// [0:0x29) := 0123456789abcded || digest || workBits.
// 8 bytes || 0x20 bytes || 1 byte.
mstore(0, 0x0123456789abcded000000000000000000000000000000000000000000000000)
let digest := mload(add(channelPtr, 0x20))
mstore(0x8, digest)
mstore8(0x28, proofOfWorkBits)
mstore(0, keccak256(0, 0x29))
let proofPtr := mload(channelPtr)
mstore(0x20, mload(proofPtr))
// proofOfWorkDigest:= keccak256(keccak256(0123456789abcded || digest || workBits) || nonce).
proofOfWorkDigest := keccak256(0, 0x28)
mstore(0, add(digest, 1))
// prng.digest := keccak256(digest + 1||nonce), nonce was written earlier.
mstore(add(channelPtr, 0x20), keccak256(0, 0x28))
// prng.counter := 0.
mstore(add(channelPtr, 0x40), 0)
mstore(channelPtr, add(proofPtr, 0x8))
}
uint256 proofOfWorkThreshold = uint256(1) << (256 - proofOfWorkBits);
require(proofOfWorkDigest < proofOfWorkThreshold, "Proof of work check failed.");
}
}