@crisp-e3/contracts 0.15.0 → 0.16.0

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.
@@ -10,6 +10,7 @@ import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
10
10
  import { IE3Program } from "@interfold/contracts/contracts/interfaces/IE3Program.sol";
11
11
  import { IInterfold } from "@interfold/contracts/contracts/interfaces/IInterfold.sol";
12
12
  import { E3 } from "@interfold/contracts/contracts/interfaces/IE3.sol";
13
+ import { Risc0ComputeProof } from "@interfold/contracts/contracts/lib/Risc0ComputeProof.sol";
13
14
  import { LazyIMTData, InternalLazyIMT } from "@zk-kit/lazy-imt.sol/InternalLazyIMT.sol";
14
15
  import { HonkVerifier } from "./CRISPVerifier.sol";
15
16
 
@@ -100,6 +101,7 @@ contract CRISPProgram is IE3Program, Ownable {
100
101
  error InputDeadlinePassed(uint256 e3Id, uint256 deadline);
101
102
  error KeyNotPublished(uint256 e3Id);
102
103
  error E3NotAcceptingInputs(uint256 e3Id);
104
+ error InvalidComputeContext();
103
105
 
104
106
  // Events
105
107
  event InputPublished(uint256 indexed e3Id, bytes encryptedVote, uint256 index);
@@ -172,7 +174,7 @@ contract CRISPProgram is IE3Program, Ownable {
172
174
  paramsHash = round.paramsHash;
173
175
  numOptions = round.numOptions;
174
176
  creditMode = round.creditMode;
175
- inputRoot = round.votes._root(TREE_DEPTH);
177
+ inputRoot = round.votes._root();
176
178
  numberOfVotes = round.votes.numberOfLeaves;
177
179
  }
178
180
 
@@ -342,17 +344,25 @@ contract CRISPProgram is IE3Program, Ownable {
342
344
  bytes32 ciphertextCommitment,
343
345
  bytes memory proof
344
346
  ) external view override returns (bool) {
347
+ E3 memory e3 = interfold.getE3(e3Id);
345
348
  bytes32 paramsHash = getParamsHash(e3Id);
349
+ bytes32 inputRoot = bytes32(e3Data[e3Id].votes._root());
350
+ Risc0ComputeProof.Proof memory computeProof = Risc0ComputeProof.decode(proof);
351
+ if (computeProof.paramsHash != paramsHash || computeProof.inputRoot != inputRoot) revert InvalidComputeContext();
352
+
353
+ bytes memory journal = Risc0ComputeProof.journal(
354
+ bytes32(block.chainid),
355
+ bytes32(uint256(uint160(address(interfold)))),
356
+ bytes32(e3Id),
357
+ e3.encryptionSchemeId,
358
+ e3.committeePublicKey,
359
+ ciphertextOutputHash,
360
+ ciphertextCommitment,
361
+ paramsHash,
362
+ inputRoot
363
+ );
346
364
 
347
- bytes32 inputRoot = bytes32(e3Data[e3Id].votes._root(TREE_DEPTH));
348
- bytes memory journal = new bytes(528); // (32 + 1) * 4 * 4
349
-
350
- _encodeLengthPrefixAndHash(journal, 0, ciphertextOutputHash);
351
- _encodeLengthPrefixAndHash(journal, 132, ciphertextCommitment);
352
- _encodeLengthPrefixAndHash(journal, 264, paramsHash);
353
- _encodeLengthPrefixAndHash(journal, 396, inputRoot);
354
-
355
- risc0Verifier.verify(proof, imageId, sha256(journal));
365
+ risc0Verifier.verify(computeProof.seal, imageId, sha256(journal));
356
366
  return true;
357
367
  }
358
368
 
@@ -381,19 +391,6 @@ contract CRISPProgram is IE3Program, Ownable {
381
391
  }
382
392
  }
383
393
 
384
- /// @notice Encode length prefix and hash
385
- /// @param journal The journal to encode into
386
- /// @param startIndex The start index in the journal
387
- /// @param hashVal The hash value to encode
388
- function _encodeLengthPrefixAndHash(bytes memory journal, uint256 startIndex, bytes32 hashVal) internal pure {
389
- journal[startIndex] = 0x20;
390
- startIndex += 4;
391
-
392
- for (uint256 i = 0; i < 32; i++) {
393
- journal[startIndex + i * 4] = hashVal[i];
394
- }
395
- }
396
-
397
394
  /// @notice Decode bytes to uint64 array
398
395
  /// @param data The bytes to decode (must be multiple of 8)
399
396
  /// @return result Array of uint64 values