Skip to content

Commit

Permalink
Make Control Id Parameters to Constructor (#813)
Browse files Browse the repository at this point in the history
This PR makes the control_id fields parameters of the constructor. The
fields on the contract are immutable so when the contract is deployed,
the provided values will be hardcoded into the deployed bytecode.

Closes: risc0/risc0-foundry-template#46
  • Loading branch information
jjtny1 committed Sep 12, 2023
1 parent 66a90f6 commit e370ca2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bonsai/ethereum/contracts/BonsaiTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {BonsaiRelayQueueWrapper} from "./BonsaiRelayQueueWrapper.sol";
import {BonsaiTestRelay} from "./BonsaiTestRelay.sol";
import {BonsaiCheats} from "./BonsaiCheats.sol";
import {IRiscZeroVerifier} from "./IRiscZeroVerifier.sol";
import {RiscZeroGroth16Verifier} from "./groth16/RiscZeroGroth16Verifier.sol";
import {ControlID, RiscZeroGroth16Verifier} from "./groth16/RiscZeroGroth16Verifier.sol";

/// @notice A base contract for testing a Bonsai callback receiver contract
/// @dev Based on the BONSAI_PROVING environment, a real or a mock BonsaiRelay will be used:
Expand Down Expand Up @@ -54,7 +54,7 @@ abstract contract BonsaiTest is Test, BonsaiCheats {
bonsaiTestRelay = new BonsaiTestRelay(vm.envOr("TEST_BONSAI_TEST_RELAY_EXPECTED_CHAIN_ID", uint256(31337)));
bonsaiRelay = new BonsaiRelayQueueWrapper(bonsaiTestRelay);
} else {
IRiscZeroVerifier verifier = new RiscZeroGroth16Verifier();
IRiscZeroVerifier verifier = new RiscZeroGroth16Verifier(ControlID.CONTROL_ID_0, ControlID.CONTROL_ID_1);
bonsaiVerifyingRelay = new BonsaiRelay(verifier);
bonsaiRelay = new BonsaiRelayQueueWrapper(bonsaiVerifyingRelay);
}
Expand Down
17 changes: 15 additions & 2 deletions bonsai/ethereum/contracts/groth16/RiscZeroGroth16Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,26 @@ struct Seal {
uint256[2] c;
}

/// Control ID hash for the identity_p254 predicate decomposed as implemented by splitDigest.
/// New releases of RISC Zero's zkvm may require updating these values. These values can be
/// obtained by running `cargo run --bin bonsai-ethereum-contracts -F control-id`
library ControlID {
uint256 public constant CONTROL_ID_0 = 0x68e42d8b3ddc499f4e1799a767052ab3;
uint256 public constant CONTROL_ID_1 = 0x3802684f1645e0a028585b0445d39231;
}

contract RiscZeroGroth16Verifier is IRiscZeroVerifier, Groth16Verifier {
using ReceiptMetadataLib for ReceiptMetadata;
using SafeCast for uint256;

// Control ID hash for the identity_p254 predicate decomposed as implemented by splitDigest.
uint256 internal constant CONTROL_ID_0 = uint256(0x68e42d8b3ddc499f4e1799a767052ab3);
uint256 internal constant CONTROL_ID_1 = uint256(0x3802684f1645e0a028585b0445d39231);
uint256 public immutable CONTROL_ID_0;
uint256 public immutable CONTROL_ID_1;

constructor(uint256 control_id_0, uint256 control_id_1) {
CONTROL_ID_0 = control_id_0;
CONTROL_ID_1 = control_id_1;
}

/// @notice splits a digest into two 128-bit words to use as public signal inputs.
/// @dev RISC Zero's Circom verifier circuit takes each of two hash digests in two 128-bit
Expand Down
4 changes: 2 additions & 2 deletions bonsai/ethereum/test/RiscZeroGroth16Verifier.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
ExitCode,
SystemExitCode
} from "../contracts/IRiscZeroVerifier.sol";
import {RiscZeroGroth16Verifier} from "../contracts/groth16/RiscZeroGroth16Verifier.sol";
import {ControlID, RiscZeroGroth16Verifier} from "../contracts/groth16/RiscZeroGroth16Verifier.sol";

contract RiscZeroGroth16VerifierTest is Test {
using ReceiptMetadataLib for ReceiptMetadata;
Expand Down Expand Up @@ -55,7 +55,7 @@ contract RiscZeroGroth16VerifierTest is Test {
IRiscZeroVerifier internal verifier;

function setUp() external {
verifier = new RiscZeroGroth16Verifier();
verifier = new RiscZeroGroth16Verifier(ControlID.CONTROL_ID_0, ControlID.CONTROL_ID_1);
}

function testVerifyKnownGoodReceipt() external view {
Expand Down
4 changes: 2 additions & 2 deletions bonsai/examples/governance/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {Script} from "forge-std/Script.sol";
import {console2} from "forge-std/console2.sol";
import {BonsaiRelay} from "bonsai/BonsaiRelay.sol";
import {BonsaiCheats} from "bonsai/BonsaiCheats.sol";
import {RiscZeroGroth16Verifier} from "bonsai/groth16/RiscZeroGroth16Verifier.sol";
import {ControlID, RiscZeroGroth16Verifier} from "bonsai/groth16/RiscZeroGroth16Verifier.sol";
import {IRiscZeroVerifier} from "bonsai/IRiscZeroVerifier.sol";
import {IVotes} from "openzeppelin/contracts/governance/utils/IVotes.sol";

Expand Down Expand Up @@ -80,7 +80,7 @@ contract Deploy is Script, BonsaiCheats {
console2.log("Using IRiscZeroVerifier at ", address(verifierAddr));
verifier = IRiscZeroVerifier(verifierAddr);
} else {
verifier = new RiscZeroGroth16Verifier();
verifier = new RiscZeroGroth16Verifier(ControlID.CONTROL_ID_0, ControlID.CONTROL_ID_1);
console2.log("Deployed RiscZeroGroth16Verifier to ", address(verifier));
}

Expand Down

0 comments on commit e370ca2

Please sign in to comment.