Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions contracts/ZKP/ZKPVerifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

// Importing the necessary interfaces required for the ZKPVerifier contract.
import "../interfaces/IAccessController.sol"; // Importing the Access Controller interface

/**
* @title ZKPVerifier
* @dev This contract verifies zero-knowledge proofs for RESI Protocol.
* It ensures that only authorized users can submit proofs and that the proofs are valid.
* The contract manages all the ZKP verifications and call respective verifier contracts
*/
contract ZKPVerifier {
// State variables
IAccessController public accessController; // Access controller instance
address public stablecoinCore; // Address of the stablecoin core contract

// Enum for different verification types
enum VerificationType {
Minting,
Burning,
CollateralDeposit,
CollateralWithdrawal,
OracleSubmission,
OracleAggregation,
ProposalCreation,
Voting,
ProposalExecution
}

// Mapping to track which verifiers are enabled for each verification type
mapping(VerificationType => bool) public verifierEnabled;

// Events

/**
* @notice Emitted when a ZKP is verified successfully.
* @dev This is fired when a zero-knowledge proof is verified.
* @param sender The address of the user who submitted the proof.
* @param verificationType The type of verification that was performed.
* @param proofData The data of the proof that was verified.
* @param result The result of the verification.
*/
event ZKPVerified(address sender, VerificationType verificationType, bytes proofData, bool result);

/**
* @notice Emitted when a verifier is enabled or disabled.
* @dev This is fired when a verifier is enabled or disabled for a specific verification type.
* @param verificationType The type of verification that was enabled or disabled.
* @param status The status of the verifier (enabled or disabled).
*/
event VerifierStatusChanged(VerificationType verificationType, bool status);

// Constructor for the ZKPVerifier contract
constructor(address _accessController) {
require(_accessController != address(0), "Invalid access controller address");
// Setting the access controller instance
accessController = IAccessController(_accessController);

// Initializing the verifierEnabled mapping to true for all verification types
for (uint8 i = 0; i < uint8(VerificationType.ProposalExecution) + 1; i++) {
verifierEnabled[VerificationType(i)] = true;
}
}

/**
* @dev sets the address of the stablecoin core contract
* This function is only used once during deployment to avoid circular dependencies
* @param _stablecoinCore The address of the stablecoin core contract
*/
function setStablecoinCore(address _stablecoinCore) external {
// Only allow this to be set once
require(stablecoinCore == address(0), "StablecoinCore already set");
// Check if the caller is authorized to set the stablecoin core address
// This is a security measure to ensure that only authorized addresses can set the core address
require(accessController.canSetParams(msg.sender), "Not authorized");
// Check if the new stablecoin core address is valid
require(_stablecoinCore != address(0), "Invalid address");
stablecoinCore = _stablecoinCore;
}

/**
* @dev Updates the address of the access controller
* @param _newAccessController The address of the new access controller
*/
function setAccessController(address _newAccessController) external {
// Only StablecoinCore OR admin (before StablecoinCore is set) can update
require(
msg.sender == stablecoinCore ||
(stablecoinCore == address(0) && accessController.canSetParams(msg.sender)),
"Not authorized"
);
// Check if the new access controller address is valid
require(_newAccessController != address(0), "Invalid access controller address");
accessController = IAccessController(_newAccessController);
}

/**
* @dev Enables or disables a verifier for a specific verification type.
* @param verificationType The type of verification to enable or disable.
* @param status The status to set (true for enabled, false for disabled).
*/
function setVerifierStatus(VerificationType verificationType, bool status) external {
require(accessController.canSetParams(msg.sender), "Not authorized to set parameters");
// Updating the verifier status
verifierEnabled[verificationType] = status;
emit VerifierStatusChanged(verificationType, status);
}

/**
* @dev A function that calls the minting verifier contract to verify the proof.
* @param collateralAmount The amount of collateral involved in the minting process.
* @param collateralType The type of collateral involved in the minting process.
* @param proofData The data of the proof that needs to be verified.
* @return result The result of the verification process.
*/
function verifyMinting(uint256 collateralAmount, address collateralType, bytes calldata proofData)
external returns (bool result) {
// Check if the verifier for minting is enabled
if(!verifierEnabled[VerificationType.Minting]) {
// If not enabled, just return true
// This ensures backward compatibility to allow for system functionality during upgrades
return true;
}
// return true for now, as the actual verification logic is not implemented
// Emit the ZKPVerified event with the result
emit ZKPVerified(msg.sender, VerificationType.Minting, proofData, true);
return true;
}

/**
* @dev A function that calls the burning verifier contract to verify the proof.
* @param stableAmount The amount of collateral involved in the burning process.
* @param collateralType The type of collateral involved in the burning process.
* @param proofData The data of the proof that needs to be verified.
* @return result The result of the verification process.
*/
function verifyBurning(uint256 stableAmount, address collateralType, bytes calldata proofData)
external returns (bool result) {
// Check if the verifier for burning is enabled
if(!verifierEnabled[VerificationType.Burning]) {
// If not enabled, just return true
// This ensures backward compatibility to allow for system functionality during upgrades
return true;
}
// return true for now, as the actual verification logic is not implemented
// Emit the ZKPVerified event with the result
emit ZKPVerified(msg.sender, VerificationType.Burning, proofData, true);
return true;
}

}
30 changes: 30 additions & 0 deletions contracts/core/collateral/CollateralPool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

// Importing the required libraries
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; // ERC20 interface
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; // SafeERC20 library for safe token transfers
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; // Reentrancy guard to prevent reentrant calls
import "@openzeppelin/contracts/utils/Pausable.sol"; // Pausable contract to allow pausing of operations

// Importing the necessary interfaces
import "../../interfaces/IAccessController.sol"; // Access controller interface
import "../../interfaces/IOracleAggregator.sol"; // Oracle aggregator interface

/**
* @title CollateralPool
* @dev This contract manages collateral for the RESI protocol.
*/
contract CollateralPool is ReentrancyGuard, Pausable {
using SafeERC20 for IERC20; // Using SafeERC20 for safe token transfers

// Module interfaces
IAccessController public accessController; // Access controller interface
IOracleAggregator public oracleAggregator; // Oracle aggregator interface

// Collateral configuration
struct CollateralConfig {
bool isSupported; // Indicates if the collateral type is supported
}

}
36 changes: 31 additions & 5 deletions contracts/core/stablecoin/StablecoinCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import "../../interfaces/IFeeManager.sol"; // Importing the Fee Manager interfac
import "../../interfaces/IPegMechanism.sol"; // Importing the Peg Mechanism interface
import "../../interfaces/IVolumeController.sol"; // Importing the Volume Controller interface
import "../../interfaces/IAccessController.sol"; // Importing the Access Controller interface
import "../../interfaces/IZKPVerifier.sol"; // Importing the ZKP Verifier interface


contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
// State variables
Expand All @@ -31,6 +33,7 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
ICollateralPool public collateralPool; // Collateral Pool interface
IOracleAggregator public oracleAggregator; // Oracle Aggregator interface
IStabilityReserve public stabilityReserve; // Stability Reserve interface
IZKPVerifier public zkpVerifier; // ZKP Verifier interface

// Constants
uint256 private constant BASIS_POINTS = 10000; // Basis points constant for calculations
Expand Down Expand Up @@ -108,6 +111,13 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
*/
event AccessControllerSet(address indexed accessController);

/**
* @notice Emitted when the ZKP verifier address is set
* @dev This is fired during the setZKPVerifier function
* @param zkpVerifier The address of the new ZKP verifier
*/
event ZKPVerifierSet(address indexed zkpVerifier);

// Constructor for the StablecoinCore contract
constructor(
string memory name_,
Expand Down Expand Up @@ -135,9 +145,6 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
stabilityReserve = IStabilityReserve(_stabilityReserve);

// Set the module interfaces
feeManager = IFeeManager(_feeManager);
pegMechanism = IPegMechanism(_pegMechanism);
volumeController = IVolumeController(_volumeController);
accessController = IAccessController(_accessController);

// Emit events for the set addresses
Expand Down Expand Up @@ -262,6 +269,17 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
pegMechanism.setStabilityReserve(_newStabilityReserve);
}

/**
* @dev Updates the address of the ZKP verifier
* @param _newZKPVerifier The address of the new ZKP verifier
*/
function setZKPVerifier(address _newZKPVerifier) external {
require(accessController.canSetParams(msg.sender), "Not authorized to set parameters");
require(_newZKPVerifier != address(0), "Invalid ZKP verifier address");
zkpVerifier = IZKPVerifier(_newZKPVerifier);
emit ZKPVerifierSet(_newZKPVerifier);
}

// Emergency functions

/**
Expand Down Expand Up @@ -324,7 +342,7 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
* @param collateralType Address of the collateral token
* @return amountMinted Amount of stablecoins minted
*/
function mint(uint256 collateralAmount, address collateralType)
function mint(uint256 collateralAmount, address collateralType, bytes calldata proofData)
external
whenNotPaused
nonReentrant
Expand All @@ -333,6 +351,10 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
// Check whether minting is allowed
require(accessController.canMint(msg.sender), "Not authorized to mint");

if (address(zkpVerifier) != address(0)) {
// Verify the proof using the ZKP verifier
require(zkpVerifier.verifyMinting(collateralAmount, collateralType, proofData), "ZKP verification failed");
}
// Check if the collateral pool is set
require(address(collateralPool) != address(0), "Collateral pool not set");
// Check if the oracle aggregator is set
Expand Down Expand Up @@ -385,7 +407,7 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
* @param collateralType Address of the collateral token
* @return collateralRedeemed Amount of collateral redeemed
*/
function burn(uint256 stableAmount, address collateralType)
function burn(uint256 stableAmount, address collateralType, bytes calldata proofData)
external
whenNotPaused
nonReentrant
Expand All @@ -394,6 +416,10 @@ contract StablecoinCore is ERC20Pausable, ReentrancyGuard {
// Check whether burning is allowed
require(accessController.canBurn(msg.sender), "Not authorized to burn");

if (address(zkpVerifier) != address(0)) {
// Verify the proof using the ZKP verifier
require(zkpVerifier.verifyBurning(stableAmount, collateralType, proofData), "ZKP verification failed");
}
// Check if the collateral pool is set
require(address(collateralPool) != address(0), "Collateral pool not set");
// Check if the oracle aggregator is set
Expand Down
37 changes: 37 additions & 0 deletions contracts/interfaces/IZKPVerifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

/**
* @title IZKPVerifier
* @dev Interface for Zero-Knowledge Proof Verifier
* This interface defines the functions for the ZKP Verifier contract
* which is responsible for verifying zero-knowledge proofs.
*/
interface IZKPVerifier {

/**
* @dev Updates the address of the access controller
* @param _newAccessController The address of the new access controller
*/
function setAccessController(address _newAccessController) external;

/**
* @dev A function that calls the minting verifier contract to verify the proof.
* @param collateralAmount The amount of collateral involved in the minting process.
* @param collateralType The type of collateral involved in the minting process.
* @param proofData The data of the proof that needs to be verified.
* @return result The result of the verification process.
*/
function verifyMinting(uint256 collateralAmount, address collateralType, bytes calldata proofData)
external returns (bool result);

/**
* @dev A function that calls the burning verifier contract to verify the proof.
* @param stableAmount The amount of collateral involved in the burning process.
* @param collateralType The type of collateral involved in the burning process.
* @param proofData The data of the proof that needs to be verified.
* @return result The result of the verification process.
*/
function verifyBurning(uint256 stableAmount, address collateralType, bytes calldata proofData)
external returns (bool result);
}
Loading