@crisp-e3/contracts 0.0.1-test
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +165 -0
- package/README.md +21 -0
- package/contracts/CRISPInputValidator.sol +107 -0
- package/contracts/CRISPInputValidatorFactory.sol +28 -0
- package/contracts/CRISPProgram.sol +151 -0
- package/contracts/CRISPVerifier.sol +1886 -0
- package/contracts/ImageID.sol +26 -0
- package/contracts/Mocks/MockCRISPInputValidator.sol +35 -0
- package/contracts/Mocks/MockRISC0Verifier.sol +18 -0
- package/contracts/Mocks/RiscZeroGroth16Verifier.sol +16 -0
- package/package.json +72 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Solidity Contracts
|
|
2
|
+
|
|
3
|
+
This directory contains the Solidity contracts for CRISP - Coercion-Resistant Impartial Selection Protocol.
|
|
4
|
+
|
|
5
|
+
Contracts are built and tested with [forge], which is part of the [Foundry] toolkit.
|
|
6
|
+
Tests are defined in the `tests` directory in the root of this template.
|
|
7
|
+
|
|
8
|
+
## CRISP Program
|
|
9
|
+
|
|
10
|
+
This is the main logic of CRISP - an enclave program for secure voting.
|
|
11
|
+
|
|
12
|
+
It exposes two main functions:
|
|
13
|
+
|
|
14
|
+
- `validate` - that is called when a new E3 instance is requested on Enclave (`Enclave.request`).
|
|
15
|
+
- `verify` - that is called when the ciphertext output is published on Enclave (`Enclave.publishCiphertextOutput`). This function ensures that the ciphertext output is valid. CRISP uses Risc0 as the compute provider for running the FHE program, thus the proof will be a Risc0 proof.
|
|
16
|
+
|
|
17
|
+
## Input validator
|
|
18
|
+
|
|
19
|
+
The input validator contract is used to validate the input data that is submitted to the E3 instance. It is called by the Enclave contract when a new input is published (`Enclave.publishInput`). In CRISP, the data providers (the ones submitting the inputs) are the voters, and the input submitted is the vote itself.
|
|
20
|
+
|
|
21
|
+
The validator checks that gating conditions are satisfied and that the ciphertext is constructed correctly using [Greco](https://github.com/gnosisguild/enclave/tree/main/circuits/crates/libs/greco). See the Greco [paper](https://eprint.iacr.org/2024/594).
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-only
|
|
2
|
+
//
|
|
3
|
+
// This file is provided WITHOUT ANY WARRANTY;
|
|
4
|
+
// without even the implied warranty of MERCHANTABILITY
|
|
5
|
+
// or FITNESS FOR A PARTICULAR PURPOSE.
|
|
6
|
+
pragma solidity >=0.8.27;
|
|
7
|
+
|
|
8
|
+
import {IInputValidator} from "@enclave-e3/contracts/contracts/interfaces/IInputValidator.sol";
|
|
9
|
+
import {Clone} from "@excubiae/contracts/proxy/Clone.sol";
|
|
10
|
+
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
|
|
11
|
+
|
|
12
|
+
import {IVerifier} from "./CRISPVerifier.sol";
|
|
13
|
+
|
|
14
|
+
/// @title CRISPInputValidator.
|
|
15
|
+
/// @notice Enclave Input Validator
|
|
16
|
+
contract CRISPInputValidator is IInputValidator, Clone, Ownable(msg.sender) {
|
|
17
|
+
/// @notice The verifier that will be used to validate the input.
|
|
18
|
+
IVerifier internal noirVerifier;
|
|
19
|
+
|
|
20
|
+
/// @notice The governance token address.
|
|
21
|
+
address public token;
|
|
22
|
+
/// @notice The minimum balance required to pass the validation.
|
|
23
|
+
uint256 public balanceThreshold;
|
|
24
|
+
/// @notice The block number at which the balance will be checked.
|
|
25
|
+
uint256 public snapshotBlock;
|
|
26
|
+
/// @notice The Merkle root of the census.
|
|
27
|
+
uint256 public censusMerkleRoot;
|
|
28
|
+
|
|
29
|
+
/// @notice Indicates if the the round data has been set.
|
|
30
|
+
bool public isDataSet;
|
|
31
|
+
|
|
32
|
+
/// @notice Mapping to store votes. Each elegible voter has their own slot
|
|
33
|
+
/// to store their vote.
|
|
34
|
+
mapping (address => bytes) public voteSlots;
|
|
35
|
+
|
|
36
|
+
/// @notice The error emitted when the input data is empty.
|
|
37
|
+
error EmptyInputData();
|
|
38
|
+
/// @notice The error emitted when the input data is invalid.
|
|
39
|
+
error InvalidInputData(bytes reason);
|
|
40
|
+
/// @notice The error emitted when the Noir proof is invalid.
|
|
41
|
+
error InvalidNoirProof();
|
|
42
|
+
/// @notice The error emitted when the round data is not set.
|
|
43
|
+
error RoundDataNotSet();
|
|
44
|
+
/// @notice The error emitted when trying to set the round data more than once.
|
|
45
|
+
error RoundDataAlreadySet();
|
|
46
|
+
|
|
47
|
+
/// @notice Initializes the contract with appended bytes data for configuration.
|
|
48
|
+
function _initialize() internal virtual override(Clone) {
|
|
49
|
+
super._initialize();
|
|
50
|
+
|
|
51
|
+
(address _verifierAddr, address _owner) = abi.decode(
|
|
52
|
+
_getAppendedBytes(),
|
|
53
|
+
(address, address)
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
noirVerifier = IVerifier(_verifierAddr);
|
|
57
|
+
_transferOwnership(_owner);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// @notice Sets the Merkle root of the census. Can only be set once.
|
|
61
|
+
/// @param _root The Merkle root to set.
|
|
62
|
+
function setRoundData(uint256 _root, address _token, uint256 _balanceThreshold, uint256 _snapshotBlock) external onlyOwner {
|
|
63
|
+
if (isDataSet) revert RoundDataAlreadySet();
|
|
64
|
+
|
|
65
|
+
isDataSet = true;
|
|
66
|
+
token = _token;
|
|
67
|
+
balanceThreshold = _balanceThreshold;
|
|
68
|
+
snapshotBlock = _snapshotBlock;
|
|
69
|
+
censusMerkleRoot = _root;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// @notice Validates input
|
|
73
|
+
/// @param data The input to be verified.
|
|
74
|
+
/// @return input The decoded, policy-approved application payload.
|
|
75
|
+
function validate(
|
|
76
|
+
address,
|
|
77
|
+
bytes memory data
|
|
78
|
+
) external returns (bytes memory input) {
|
|
79
|
+
// we need to ensure that the CRISP admin set the merkle root of the census
|
|
80
|
+
// @todo update this once we have all components working
|
|
81
|
+
// if (!isDataSet) revert RoundDataNotSet();
|
|
82
|
+
|
|
83
|
+
if (data.length == 0) revert EmptyInputData();
|
|
84
|
+
|
|
85
|
+
(
|
|
86
|
+
bytes memory noirProof,
|
|
87
|
+
bytes32[] memory noirPublicInputs,
|
|
88
|
+
bytes memory vote,
|
|
89
|
+
address slot
|
|
90
|
+
) = abi.decode(data, (bytes, bytes32[], bytes, address));
|
|
91
|
+
|
|
92
|
+
/// @notice we need to check whether the slot is empty.
|
|
93
|
+
/// if the slot is empty
|
|
94
|
+
/// @todo pass it to the verifier
|
|
95
|
+
// bool isFirstVote = voteSlots[slot].length == 0;
|
|
96
|
+
|
|
97
|
+
// Check if the ciphertext was encrypted correctly
|
|
98
|
+
if (!noirVerifier.verify(noirProof, noirPublicInputs))
|
|
99
|
+
revert InvalidNoirProof();
|
|
100
|
+
|
|
101
|
+
/// @notice Store the vote in the correct slot.
|
|
102
|
+
voteSlots[slot] = vote;
|
|
103
|
+
|
|
104
|
+
// return the vote so that it can be stored in Enclave's input merkle tree
|
|
105
|
+
input = vote;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-only
|
|
2
|
+
//
|
|
3
|
+
// This file is provided WITHOUT ANY WARRANTY;
|
|
4
|
+
// without even the implied warranty of MERCHANTABILITY
|
|
5
|
+
// or FITNESS FOR A PARTICULAR PURPOSE.
|
|
6
|
+
pragma solidity >=0.8.27;
|
|
7
|
+
|
|
8
|
+
import {Factory} from "@excubiae/contracts/proxy/Factory.sol";
|
|
9
|
+
import {CRISPInputValidator} from "./CRISPInputValidator.sol";
|
|
10
|
+
|
|
11
|
+
/// @title CRISPInputValidatorFactory
|
|
12
|
+
/// @notice Factory for deploying minimal proxy instances of CRISPInputValidator.
|
|
13
|
+
contract CRISPInputValidatorFactory is Factory {
|
|
14
|
+
/// @notice Initializes the factory with the CRISPInputValidator implementation.
|
|
15
|
+
constructor(address inputValidator) Factory(inputValidator) {}
|
|
16
|
+
|
|
17
|
+
/// @notice Deploys a new CRISPInputValidator clone.
|
|
18
|
+
/// @param _verifierAddr Address of the associated verifier contract.
|
|
19
|
+
function deploy(
|
|
20
|
+
address _verifierAddr,
|
|
21
|
+
address _owner
|
|
22
|
+
) public returns (address clone) {
|
|
23
|
+
bytes memory data = abi.encode(_verifierAddr, _owner);
|
|
24
|
+
|
|
25
|
+
clone = super._deploy(data);
|
|
26
|
+
CRISPInputValidator(clone).initialize();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// SPDX-License-Identifier: LGPL-3.0-only
|
|
2
|
+
//
|
|
3
|
+
// This file is provided WITHOUT ANY WARRANTY;
|
|
4
|
+
// without even the implied warranty of MERCHANTABILITY
|
|
5
|
+
// or FITNESS FOR A PARTICULAR PURPOSE.
|
|
6
|
+
pragma solidity >=0.8.27;
|
|
7
|
+
|
|
8
|
+
import {IRiscZeroVerifier} from "risc0/IRiscZeroVerifier.sol";
|
|
9
|
+
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
|
|
10
|
+
import {IE3Program} from "@enclave-e3/contracts/contracts/interfaces/IE3Program.sol";
|
|
11
|
+
import {IInputValidator} from "@enclave-e3/contracts/contracts/interfaces/IInputValidator.sol";
|
|
12
|
+
import {IEnclave} from "@enclave-e3/contracts/contracts/interfaces/IEnclave.sol";
|
|
13
|
+
import {CRISPInputValidatorFactory} from "./CRISPInputValidatorFactory.sol";
|
|
14
|
+
import {HonkVerifier} from "./CRISPVerifier.sol";
|
|
15
|
+
|
|
16
|
+
contract CRISPProgram is IE3Program, Ownable {
|
|
17
|
+
// Constants
|
|
18
|
+
bytes32 public constant ENCRYPTION_SCHEME_ID = keccak256("fhe.rs:BFV");
|
|
19
|
+
|
|
20
|
+
// State variables
|
|
21
|
+
IEnclave public enclave;
|
|
22
|
+
IRiscZeroVerifier public verifier;
|
|
23
|
+
CRISPInputValidatorFactory private immutable INPUT_VALIDATOR_FACTORY;
|
|
24
|
+
HonkVerifier private immutable HONK_VERIFIER;
|
|
25
|
+
bytes32 public imageId;
|
|
26
|
+
|
|
27
|
+
// Mappings
|
|
28
|
+
mapping(address => bool) public authorizedContracts;
|
|
29
|
+
mapping(uint256 e3Id => bytes32 paramsHash) public paramsHashes;
|
|
30
|
+
|
|
31
|
+
// Events
|
|
32
|
+
event InputValidatorUpdated(address indexed newValidator);
|
|
33
|
+
|
|
34
|
+
// Errors
|
|
35
|
+
error CallerNotAuthorized();
|
|
36
|
+
error E3AlreadyInitialized();
|
|
37
|
+
error E3DoesNotExist();
|
|
38
|
+
error EnclaveAddressZero();
|
|
39
|
+
error VerifierAddressZero();
|
|
40
|
+
error InvalidInputValidatorFactory();
|
|
41
|
+
error InvalidHonkVerifier();
|
|
42
|
+
|
|
43
|
+
/// @notice Initialize the contract, binding it to a specified RISC Zero verifier.
|
|
44
|
+
/// @param _enclave The enclave address
|
|
45
|
+
/// @param _verifier The RISC Zero verifier address
|
|
46
|
+
/// @param _inputValidatorFactory The input validator factory address
|
|
47
|
+
/// @param _honkVerifier The honk verifier address
|
|
48
|
+
/// @param _imageId The image ID for the guest program
|
|
49
|
+
constructor(
|
|
50
|
+
IEnclave _enclave,
|
|
51
|
+
IRiscZeroVerifier _verifier,
|
|
52
|
+
CRISPInputValidatorFactory _inputValidatorFactory,
|
|
53
|
+
HonkVerifier _honkVerifier,
|
|
54
|
+
bytes32 _imageId
|
|
55
|
+
) Ownable(msg.sender) {
|
|
56
|
+
require(address(_enclave) != address(0), EnclaveAddressZero());
|
|
57
|
+
require(address(_verifier) != address(0), VerifierAddressZero());
|
|
58
|
+
require(
|
|
59
|
+
address(_inputValidatorFactory) != address(0),
|
|
60
|
+
InvalidInputValidatorFactory()
|
|
61
|
+
);
|
|
62
|
+
require(address(_honkVerifier) != address(0), InvalidHonkVerifier());
|
|
63
|
+
|
|
64
|
+
enclave = _enclave;
|
|
65
|
+
verifier = _verifier;
|
|
66
|
+
INPUT_VALIDATOR_FACTORY = _inputValidatorFactory;
|
|
67
|
+
HONK_VERIFIER = _honkVerifier;
|
|
68
|
+
authorizedContracts[address(_enclave)] = true;
|
|
69
|
+
imageId = _imageId;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// @notice Set the Image ID for the guest program
|
|
73
|
+
/// @param _imageId The new image ID.
|
|
74
|
+
function setImageId(bytes32 _imageId) external onlyOwner {
|
|
75
|
+
imageId = _imageId;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// @notice Set the RISC Zero verifier address
|
|
79
|
+
/// @param _verifier The new RISC Zero verifier address
|
|
80
|
+
function setVerifier(IRiscZeroVerifier _verifier) external onlyOwner {
|
|
81
|
+
verifier = _verifier;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// @notice Get the params hash for an E3 program
|
|
85
|
+
/// @param e3Id The E3 program ID
|
|
86
|
+
/// @return The params hash
|
|
87
|
+
function getParamsHash(uint256 e3Id) public view returns (bytes32) {
|
|
88
|
+
return paramsHashes[e3Id];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// @notice Validate the E3 program parameters
|
|
92
|
+
/// @param e3Id The E3 program ID
|
|
93
|
+
/// @param e3ProgramParams The E3 program parameters
|
|
94
|
+
function validate(
|
|
95
|
+
uint256 e3Id,
|
|
96
|
+
uint256,
|
|
97
|
+
bytes calldata e3ProgramParams,
|
|
98
|
+
bytes calldata
|
|
99
|
+
) external returns (bytes32, IInputValidator inputValidator) {
|
|
100
|
+
require(
|
|
101
|
+
authorizedContracts[msg.sender] || msg.sender == owner(),
|
|
102
|
+
CallerNotAuthorized()
|
|
103
|
+
);
|
|
104
|
+
require(paramsHashes[e3Id] == bytes32(0), E3AlreadyInitialized());
|
|
105
|
+
paramsHashes[e3Id] = keccak256(e3ProgramParams);
|
|
106
|
+
|
|
107
|
+
// Deploy a new input validator
|
|
108
|
+
inputValidator = IInputValidator(
|
|
109
|
+
INPUT_VALIDATOR_FACTORY.deploy(address(HONK_VERIFIER), owner())
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
return (ENCRYPTION_SCHEME_ID, inputValidator);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// @notice Verify the proof
|
|
116
|
+
/// @param e3Id The E3 program ID
|
|
117
|
+
/// @param ciphertextOutputHash The hash of the ciphertext output
|
|
118
|
+
/// @param proof The proof to verify
|
|
119
|
+
function verify(
|
|
120
|
+
uint256 e3Id,
|
|
121
|
+
bytes32 ciphertextOutputHash,
|
|
122
|
+
bytes memory proof
|
|
123
|
+
) external view override returns (bool) {
|
|
124
|
+
require(paramsHashes[e3Id] != bytes32(0), E3DoesNotExist());
|
|
125
|
+
bytes32 inputRoot = bytes32(enclave.getInputRoot(e3Id));
|
|
126
|
+
bytes memory journal = new bytes(396); // (32 + 1) * 4 * 3
|
|
127
|
+
|
|
128
|
+
encodeLengthPrefixAndHash(journal, 0, ciphertextOutputHash);
|
|
129
|
+
encodeLengthPrefixAndHash(journal, 132, paramsHashes[e3Id]);
|
|
130
|
+
encodeLengthPrefixAndHash(journal, 264, inputRoot);
|
|
131
|
+
|
|
132
|
+
verifier.verify(proof, imageId, sha256(journal));
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// @notice Encode length prefix and hash
|
|
137
|
+
/// @param journal The journal to encode into
|
|
138
|
+
/// @param startIndex The start index in the journal
|
|
139
|
+
/// @param hashVal The hash value to encode
|
|
140
|
+
function encodeLengthPrefixAndHash(
|
|
141
|
+
bytes memory journal,
|
|
142
|
+
uint256 startIndex,
|
|
143
|
+
bytes32 hashVal
|
|
144
|
+
) internal pure {
|
|
145
|
+
journal[startIndex] = 0x20;
|
|
146
|
+
startIndex += 4;
|
|
147
|
+
for (uint256 i = 0; i < 32; i++) {
|
|
148
|
+
journal[startIndex + i * 4] = hashVal[i];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|