Skip to content

Commit

Permalink
updated some more stuff around blocklock solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
CluEleSsUK committed Nov 9, 2024
1 parent c919583 commit 0cac39f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 29 deletions.
2 changes: 1 addition & 1 deletion contracts/src/AbstractDecryptionReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.24;
import {IDecryptionReceiver} from "./IDecryptionReceiver.sol";

abstract contract AbstractDecryptionReceiver {
address public immutable DECRYPTION_PROVIDER = 0x11045878ed62ec3acc91ce36a46f4ef61d4616e1;
address public immutable DECRYPTION_PROVIDER = 0x11045878Ed62Ec3aCC91cE36A46F4EF61d4616e1;

error NotAuthorizedDecryptionProvider();

Expand Down
47 changes: 47 additions & 0 deletions contracts/src/BlocklockSignatureScheme.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {BLS} from "./BLS.sol";
import {ISignatureScheme} from "./ISignatureScheme.sol";

contract BlocklockSignatureScheme is ISignatureScheme {
using BLS for bytes;

string public constant SCHEME_ID = "BN254-BLS-BLOCKLOCK";
bytes public constant DST = bytes("BLOCKLOCK_BN254G1_XMD:KECCAK-256_SVDW_RO_H1_");

/**
* @dev See {ISignatureScheme-verifySignature}.
*/
function verifySignature(bytes calldata message, bytes calldata signature, bytes calldata publicKey)
external
view
returns (bool isValid)
{
// convert message hash bytes to g1
BLS.PointG1 memory _message = BLS.g1Unmarshal(message);
// convert signature bytes to g1
BLS.PointG1 memory _signature = BLS.g1Unmarshal(signature);
// convert public key bytes to g2
BLS.PointG2 memory _publicKey = BLS.g2Unmarshal(publicKey);
// call evm precompile for pairing check
(bool pairingSuccess, bool callSuccess) = BLS.verifySingle(_signature, _publicKey, _message);
return (pairingSuccess && callSuccess);
}

/**
* @dev See {ISignatureScheme-hashToPoint}.
*/
function hashToPoint(bytes calldata message) public view returns (uint256, uint256) {
BLS.PointG1 memory point = BLS.hashToPoint(DST, message);
return (point.x, point.y);
}

/**
* @dev See {ISignatureScheme-hashToBytes}.
*/
function hashToBytes(bytes calldata message) external view returns (bytes memory) {
(uint256 x, uint256 y) = hashToPoint(message);
return BLS.g1Marshal(BLS.PointG1({x: x, y: y}));
}
}
43 changes: 25 additions & 18 deletions contracts/src/IDecryptionProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ interface IDecryptionProvider {
/// @param ciphertext The encrypted data to be registered
/// @param conditions The conditions that need to be met to decrypt the ciphertext
/// @return requestID The unique ID assigned to the registered decryption request
function registerCiphertext(bytes calldata ciphertext, bytes calldata conditions)
function registerCiphertext(string calldata schemeID, bytes calldata ciphertext, bytes calldata conditions)
external
returns (uint256 requestID);

/**
* @notice Decrypts the given Ciphertext for the specified request ID.
* @dev This function is intended to be called after a decryption operation has been completed off-chain.
* The function accepts the request ID associated with the original encryption request and the
* decrypted text in bytes format.
* @notice Provide the decryption key for a specific requestID alongside a signature.
* @dev This function is intended to be called after a decryption key has been generated off-chain.
*
* @param requestID The unique identifier for the encryption request. This should match the ID used
* when the encryption was initially requested.
* @param decryptedText The decrypted content in bytes format. The data should represent the original
* @param decryptionKey The decrypted content in bytes format. The data should represent the original
* message in its decrypted form.
* @param signature The signature associated with the request, provided as a byte array
*/
function sendDecryptedCiphertext(uint256 requestID, bytes calldata decryptedText) external;
function fulfilDecryptionRequest(uint256 requestID, bytes calldata decryptionKey, bytes calldata signature)
external;

// Getters

Expand All @@ -37,20 +38,26 @@ interface IDecryptionProvider {
* @param requestId The ID of the request to retrieve.
* @return The Request struct corresponding to the given requestId.
*/
function getRequest(uint256 requestId) external view returns (TypesLib.DecryptionRequest memory);
function getRequestInFlight(uint256 requestId) external view returns (TypesLib.DecryptionRequest memory);

/**
* @notice Retrieves all requests.
* @dev This function returns an array of all Request structs stored in the contract.
* @return An array containing all the Request structs.
* @notice Verifies whether a specific request is in flight or not.
* @param requestID The ID of the request to check.
* @return boolean indicating whether the request is in flight or not.
*/
function getAllRequests() external view returns (TypesLib.DecryptionRequest[] memory);
function isInFlight(uint256 requestID) external view returns (bool);

/**
* @notice Retrieves the public key associated with the decryption process.
* @dev Returns the public key as two elliptic curve points.
* @return Two pairs of coordinates representing the public key points on the elliptic curve.
*/
function getPublicKey() external view returns (uint256[2] memory, uint256[2] memory);

/**
* @notice Generates a message from the given request.
* @dev Creates a hash-based message using the `conditions` of the `Request` struct.
* The resulting message is the hash of the encoded values, packed into a byte array.
* @param r The `Request` struct containing the data for generating the message.
* @return A byte array representing the hashed and encoded message.
* @notice Retrieves the public key associated with the decryption process.
* @dev Returns the public key as bytes.
* @return Bytes string representing the public key points on the elliptic curve.
*/
function messageFrom(TypesLib.DecryptionRequest memory r) external pure returns (bytes memory);
function getPublicKeyBytes() external view returns (bytes memory);
}
36 changes: 36 additions & 0 deletions contracts/src/ISignatureScheme.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

interface ISignatureScheme {
/// Getters

/**
* @notice Returns the scheme identifier as a string, e.g., "BN254", "BLS12-381", "TESS"
*/
function SCHEME_ID() external returns (string memory);

/**
* @notice Verifies a signature using the given signature scheme.
* @param message The message that was signed. Message is a G1 point represented as bytes.
* @param signature The signature to verify. Signature is a G1 point represented as bytes.
* @param publicKey The public key of the signer. Public key is a G2 point represented as bytes.
* @return isValid boolean which evaluates to true if the signature is valid, false otherwise.
*/
function verifySignature(bytes calldata message, bytes calldata signature, bytes calldata publicKey)
external
view
returns (bool isValid);

/**
* @notice Hashes a message to a G1 point on the elliptic curve.
* @param message The message to be hashed.
* @return (uint256, uint256) A point on the elliptic curve in G1, represented as x and y coordinates.
*/
function hashToPoint(bytes memory message) external view returns (uint256, uint256);
/**
* @notice Hashes a message to a G1 point on the elliptic curve.
* @param message The message to be hashed.
* @return bytes A point on the elliptic curve in G1, represented as bytes.
*/
function hashToBytes(bytes calldata message) external view returns (bytes memory);
}
10 changes: 0 additions & 10 deletions contracts/src/TypesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ pragma solidity 0.8.24;
import "./BLS.sol";

library TypesLib {
// Decryption request stores details for each decryption request
struct DecryptionRequest {
bytes Ciphertext;
bytes conditions;
bytes decryptedText;
bytes messageToSign;
bytes signature;
address callback;
}

// Blocklock request stores details needed to generate blocklock decryption keys
struct BlocklockRequest {
uint256 decryptionRequestID;
Expand Down

0 comments on commit 0cac39f

Please sign in to comment.