2
2
3
3
pragma solidity ^ 0.8.0 ;
4
4
5
- import {Verifier} from "./verifier .sol " ;
5
+ import {Verifier} from "./Verifier .sol " ;
6
6
7
7
// The query input struct passed into the processQuery function
8
8
struct QueryInput {
@@ -40,7 +40,7 @@ enum QueryErrorCode {
40
40
ComputationOverflow
41
41
}
42
42
43
- contract Query is Verifier {
43
+ contract Groth16VerifierExtension is Verifier {
44
44
// Top 3 bits mask.
45
45
uint256 constant TOP_THREE_BIT_MASK = ~ (uint256 (7 ) << 253 );
46
46
@@ -94,7 +94,12 @@ contract Query is Verifier {
94
94
// Then ensure this hash value equals to the last Groth16 input (groth16_inputs[2]).
95
95
// 3. Parse the items from public inputs, and check as expected for query.
96
96
// 4. Parse and return the query output from public inputs.
97
- function processQuery (bytes32 [] calldata data , QueryInput memory query ) public view returns (QueryOutput memory ) {
97
+ function processQuery (bytes32 [] calldata data , QueryInput memory query )
98
+ public
99
+ view
100
+ virtual
101
+ returns (QueryOutput memory )
102
+ {
98
103
// 1. Groth16 verification
99
104
uint256 [3 ] memory groth16Inputs = verifyGroth16Proof (data);
100
105
@@ -109,7 +114,7 @@ contract Query is Verifier {
109
114
}
110
115
111
116
// Parse the Groth16 proofs and inputs, do verification, and returns the Groth16 inputs.
112
- function verifyGroth16Proof (bytes32 [] calldata data ) internal view returns (uint256 [3 ] memory ) {
117
+ function verifyGroth16Proof (bytes32 [] calldata data ) internal view virtual returns (uint256 [3 ] memory ) {
113
118
uint256 [8 ] memory proofs;
114
119
uint256 [3 ] memory inputs;
115
120
@@ -130,7 +135,7 @@ contract Query is Verifier {
130
135
}
131
136
132
137
// Compute sha256 on the public inputs, and ensure it equals to the last Groth16 input.
133
- function verifyPublicInputs (bytes32 [] calldata data , uint256 [3 ] memory groth16Inputs ) internal pure {
138
+ function verifyPublicInputs (bytes32 [] calldata data , uint256 [3 ] memory groth16Inputs ) internal pure virtual {
134
139
// Parse the public inputs from calldata.
135
140
bytes memory pi = parsePublicInputs (data);
136
141
@@ -166,13 +171,18 @@ contract Query is Verifier {
166
171
}
167
172
168
173
// Verify the public inputs with the expected query.
169
- function verifyQuery (bytes32 [] calldata data , QueryInput memory query ) internal pure returns (QueryErrorCode) {
174
+ function verifyQuery (bytes32 [] calldata data , QueryInput memory query )
175
+ internal
176
+ view
177
+ virtual
178
+ returns (QueryErrorCode)
179
+ {
170
180
// Retrieve the last Uint256 of public inputs.
171
181
bytes32 rem = data[PI_REM_OFFSET];
172
182
173
183
// Check the block hash and computational hash.
174
184
bytes32 blockHash = convertToBlockHash (data[PI_OFFSET + BLOCK_HASH_POS]);
175
- require (blockHash == query.blockHash, " Block hash must equal as expected. " );
185
+ verifyBlockHash (blockHash, query.blockHash);
176
186
bytes32 computationalHash = data[PI_OFFSET + COMPUTATIONAL_HASH_POS];
177
187
require (computationalHash == query.computationalHash, "Computational hash must equal as expected. " );
178
188
@@ -215,8 +225,21 @@ contract Query is Verifier {
215
225
return QueryErrorCode.ComputationOverflow;
216
226
}
217
227
228
+ /// @notice verifies two blockhashed are equal
229
+ /// @param blockHash the blockhash computed from the proof
230
+ /// @param expectedBlockHash the expected blockhash, retrieved from the query
231
+ /// @dev this function is virtual to allow for different implementations in different environments
232
+ function verifyBlockHash (bytes32 blockHash , bytes32 expectedBlockHash ) internal view virtual {
233
+ require (blockHash == expectedBlockHash, "Block hash must equal as expected. " );
234
+ }
235
+
218
236
// Parse the query output from the public inputs.
219
- function parseOutput (bytes32 [] calldata data , QueryErrorCode error ) internal pure returns (QueryOutput memory ) {
237
+ function parseOutput (bytes32 [] calldata data , QueryErrorCode error )
238
+ internal
239
+ pure
240
+ virtual
241
+ returns (QueryOutput memory )
242
+ {
220
243
bytes32 rem = data[PI_REM_OFFSET];
221
244
222
245
// Retrieve total number of the matched rows.
0 commit comments