Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Decapacitor into Capacitor or make it a library #257

Closed
smitrajput opened this issue Jun 16, 2023 · 1 comment
Closed

Merge Decapacitor into Capacitor or make it a library #257

smitrajput opened this issue Jun 16, 2023 · 1 comment

Comments

@smitrajput
Copy link

smitrajput commented Jun 16, 2023

Is your feature request related to a problem? Please describe.

In their current state, the SingleDecapacitor and the HashChainDecapacitor only seem to be doing some mathematical computation (verifyMessageInclusion()), without accessing any meaningful state. If no more state-based computations are planned to be added in them in the future, it makes sense to either

  1. add verifyMessageInclusion()s of SingleDecapacitor and HashChainDecapacitor in SingleCapacitor and HashChainCapacitor respectively
  2. or convert them into libraries, if more such stateless computations are anticipated

to save deployment costs for a separate contract (>= 21k gas).

Describe the solution you'd like

(1) above is self-explanatory.
For (2), the HashChainDecapacitor library could look something like this:

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.7;

library HashChainDecapacitor {

    /**
     * @notice Verifies whether a message is included in the given hash chain.
     * @param root_ The root of the hash chain.
     * @param packedMessage_ The packed message whose inclusion in the hash chain needs to be verified.
     * @param proof_ The proof for the inclusion of the packed message in the hash chain.
     * @return True if the packed message is included in the hash chain and the provided root is the calculated root; otherwise, false.
     */
    function verifyMessageInclusion(
        bytes32 root_,
        bytes32 packedMessage_,
        bytes calldata proof_
    ) internal pure returns (bool) {
        bytes32[] memory chain = abi.decode(proof_, (bytes32[]));
        uint256 len = chain.length;
        bytes32 generatedRoot;
        bool isIncluded;
        for (uint256 i = 0; i < len; i++) {
            generatedRoot = keccak256(abi.encode(generatedRoot, chain[i]));
            if (chain[i] == packedMessage_) isIncluded = true;
        }

        return root_ == generatedRoot && isIncluded;
    }
}

and be used in the HashChainCapacitor contract as:

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.7;

import "./BaseCapacitor.sol";
import "../libraries/HashChainDecapacitor.sol";

/**
 * @title HashChainCapacitor
 * @notice This is an experimental contract and have known bugs
 * @dev A contract that implements ICapacitor and stores packed messages in a hash chain.
 * The hash chain is made of packets, each packet contains a maximum of 10 messages.
 * Each new message added to the chain is hashed with the previous root to create a new root.
 * When a packet is full, a new packet is created and the root of the last packet is sealed.
 */
contract HashChainCapacitor is BaseCapacitor {
    uint256 private _chainLength;
    uint256 private constant _MAX_LEN = 10;

    /** rest of the contract */

    /**
     * @notice Verifies whether a message is included in the given hash chain.
     * @param root_ The root of the hash chain.
     * @param packedMessage_ The packed message whose inclusion in the hash chain needs to be verified.
     * @param proof_ The proof for the inclusion of the packed message in the hash chain.
     * @return True if the packed message is included in the hash chain and the provided root is the calculated root; otherwise, false.
     */
    function verifyMsgInclusion(
        bytes32 root_,
        bytes32 packedMessage_,
        bytes calldata proof_
    ) external pure returns (bool) {
        HashChainDecapacitor.verifyMessageInclusion(root_, packedMessage_, proof_);
    }    
}

Plus, this library fits perfectly with the gas optimization suggested in #256.

A similar argument could also be made for Hasher.

Describe alternatives you've considered

Alternative has been discussed above.

@arthcp
Copy link
Contributor

arthcp commented Jun 27, 2023

Thanks for reporting this.

We want to leave room to make the decapacitor stateful and to support different hash functions when sibling chain is non EVM. Both have not been made libraries due to this.

Closing the issue for now.

@arthcp arthcp closed this as completed Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants