Skip to content

Commit 8135c62

Browse files
committed
add RStarM tests
1 parent 125ae9a commit 8135c62

File tree

3 files changed

+163
-5
lines changed

3 files changed

+163
-5
lines changed

protocol-units/dispute/src/Dispute.sol protocol-units/dispute/src/RStarM.sol

+17-5
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
5151
UNVERIFIABLE
5252
}
5353

54+
uint256 public constant SECONDS_IN_DAY = 86400; // Number of seconds in a day
55+
uint256 public constant SECONDS_IN_MINUTE = 60; // Number of seconds in a minute
56+
5457
uint256 public constant MIN_STAKE = 1 ether;
55-
uint256 public delta; // Time window for filing a dispute
56-
uint256 public p; // Time to run the zero-knowledge proof
57-
uint256 public m; // Minimum number of validators required to accept a block
58+
uint256 public delta = 1 * SECONDS_IN_DAY; // Time window for filing a dispute (e.g., 1 day)
59+
uint256 public p = 1 * SECONDS_IN_MINUTE;
60+
uint256 public m; // Minimum number of validators requried
5861

5962
/// @notice Control ID hash for the identity_p254 predicate decomposed by `splitDigest`.
6063
/// @dev This value controls what set of recursion programs, and therefore what version of the
@@ -82,13 +85,22 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
8285
event BlockAccepted(bytes32 indexed blockHash);
8386
event OptimisticCommitmentSubmitted(bytes32 indexed blockHash, bytes stateCommitment, uint256 validatorCount);
8487

85-
constructor(uint256 _delta, uint256 _p, uint256 _m, address _verifier) {
88+
constructor(
89+
uint256 _delta,
90+
uint256 _p,
91+
uint256 _m,
92+
uint256 control_id_0,
93+
uint256 control_id_1
94+
) {
8695
delta = _delta;
8796
p = _p;
8897
m = _m;
89-
verifier = IRiscZeroVerifier(_verifier);
98+
CONTROL_ID_0 = control_id_0;
99+
CONTROL_ID_1 = control_id_1;
100+
//verifier = IRiscZeroVerifier(_verifier);
90101
}
91102

103+
92104
function registerValidator() external payable {
93105
require(msg.value >= MIN_STAKE, "Insufficient stake");
94106
require(!validators[msg.sender].isRegistered, "Validator already registered");
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import {Test} from "forge-std/Test.sol";
5+
import {console2} from "forge-std/console2.sol";
6+
7+
import "ds-test/test.sol";
8+
import "../src/RStarM.sol";
9+
import "forge-std/Vm.sol";
10+
import {
11+
IRiscZeroVerifier,
12+
Output,
13+
OutputLib,
14+
Receipt as RiscZeroReceipt,
15+
ReceiptClaim,
16+
ReceiptClaimLib,
17+
ExitCode,
18+
SystemExitCode
19+
} from "../src/IRiscZeroVerifier.sol";
20+
import {TestReceipt} from "./TestReceipt.sol";
21+
import {ControlID} from "../src/groth16/ControlID.sol";
22+
23+
contract RStartM is DSTest {
24+
using OutputLib for Output;
25+
using ReceiptClaimLib for ReceiptClaim;
26+
27+
Vm vm = Vm(HEVM_ADDRESS);
28+
RStarM rStarM;
29+
address signer1 = address(0x1);
30+
address signer2 = address(0x2);
31+
bytes exampleProofData = "exampleProof";
32+
address validator = address(0x1);
33+
34+
RiscZeroReceipt internal TEST_RECEIPT = RiscZeroReceipt(
35+
TestReceipt.SEAL,
36+
ReceiptClaim(
37+
TestReceipt.IMAGE_ID,
38+
TestReceipt.POST_DIGEST,
39+
ExitCode(SystemExitCode.Halted, 0),
40+
bytes32(0x0000000000000000000000000000000000000000000000000000000000000000),
41+
Output(sha256(TestReceipt.JOURNAL), bytes32(0)).digest()
42+
)
43+
);
44+
45+
function setUp() public {
46+
rStarM = new RStarM(1, 15, 1, ControlID.CONTROL_ID_0, ControlID.CONTROL_ID_1);
47+
rStarM.registerValidator();
48+
}
49+
50+
function testRegisterValidator() public {
51+
vm.deal(validator, rStarM.MIN_STAKE());
52+
vm.prank(validator);
53+
rStarM.registerValidator();
54+
55+
(bool isRegistered, uint256 stake) = rStarM.validators(validator);
56+
assertTrue(isRegistered, "Validator should be registered");
57+
assertEq(stake, rStarM.MIN_STAKE(), "Validator stake should match the minimum stake");
58+
}
59+
60+
// function testAddSigner() public {
61+
// assertTrue(rStarM.isSigner(signer1), "signer1 should be a signer after addition");
62+
// }
63+
64+
// function testRemoveSigner() public {
65+
// rStarM.removeSigner(signer1);
66+
// assertTrue(!rStarM.isSigner(signer1), "signer1 should not be a signer after removal");
67+
// }
68+
69+
function testVerifyKnownGoodReceipt() external view {
70+
require(rStarM.verify_integrity(TEST_RECEIPT), "verification failed");
71+
}
72+
73+
// Make sure changing the bits causes a failure.
74+
function testVerifyMangledReceipts() external view {
75+
RiscZeroReceipt memory mangled = TEST_RECEIPT;
76+
77+
mangled.seal[0] ^= bytes1(uint8(1));
78+
require(!rStarM.verify_integrity(mangled), "verification passed on mangled seal value");
79+
mangled = TEST_RECEIPT;
80+
81+
mangled.claim.preStateDigest ^= bytes32(uint256(1));
82+
require(!rStarM.verify_integrity(mangled), "verification passed on mangled preStateDigest value");
83+
mangled = TEST_RECEIPT;
84+
85+
mangled.claim.postStateDigest ^= bytes32(uint256(1));
86+
require(!rStarM.verify_integrity(mangled), "verification passed on mangled postStateDigest value");
87+
mangled = TEST_RECEIPT;
88+
89+
mangled.claim.exitCode = ExitCode(SystemExitCode.SystemSplit, 0);
90+
require(!rStarM.verify_integrity(mangled), "verification passed on mangled exitCode value");
91+
mangled = TEST_RECEIPT;
92+
93+
mangled.claim.input ^= bytes32(uint256(1));
94+
require(!rStarM.verify_integrity(mangled), "verification passed on mangled input value");
95+
mangled = TEST_RECEIPT;
96+
97+
mangled.claim.output ^= bytes32(uint256(1));
98+
require(!rStarM.verify_integrity(mangled), "verification passed on mangled input value");
99+
mangled = TEST_RECEIPT;
100+
101+
// Just a quick sanity check
102+
require(rStarM.verify_integrity(mangled), "verification failed");
103+
}
104+
105+
// function testFailSettleNotSigner() public {
106+
// vm.prank(signer2);
107+
// rStarM.settle(1, exampleProofData);
108+
// }
109+
110+
// function testSettleAndRetrieve() public {
111+
// vm.prank(signer1);
112+
// rStarM.settle(1, exampleProofData);
113+
//
114+
// bytes[] memory proofs = rStarM.getProofsAtHeight(1);
115+
// assertEq(proofs.length, 1, "There should be one proof for block height 1");
116+
// assertEq(string(proofs[0]), string(exampleProofData), "The proofData should match exampleProofData");
117+
// }
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2024 RISC Zero, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
// This file is automatically generated by:
18+
// cargo xtask bootstrap-groth16
19+
20+
pragma solidity ^0.8.13;
21+
22+
library TestReceipt {
23+
bytes public constant SEAL =
24+
hex"0b79052f45ac3c4873607632575e2f8777ee1776e702eb09c419487a80228fd51ae4c3bec6344ddcc9c31f180ea571326102d5f640e2f2a17dec9f608007e63802f4c00951d50a256afc2fb6829d3679748b5ea57931bf6921ab07a7492ca3751fddf7493b473b6f99c3c036004f9f1c0d985fb00cbd6d64d4ddb5cabafd49fc29c7ac262d12fc190522384f0b77b3139fcc51516eefc088a6bc49093b28029827d7612bff120f2880eab4b7bb1aa1cdaa563193827f01119f8e57cef6daf6a516d8c0445ea5f5514fdbe9e3a1e2b6d4cc4ac7983982bbc8cffee7a9177032a32382aadee5f7017ab25b2a7c82b355d46e7c9cbe13587d552cebd14a816b448e";
25+
bytes32 public constant POST_DIGEST = bytes32(0xb8e6dde26b99d84dd257eef56a4f9adc1b03a0bb88ca5a3ad39a91e367f5c3f7);
26+
bytes public constant JOURNAL = hex"";
27+
bytes32 public constant IMAGE_ID = bytes32(0x35e14f92e1c7989a467611bb39d2cf7f46dc4488404f8e8179960cc7be13f8a8);
28+
}

0 commit comments

Comments
 (0)