kaiachain / kaia

GNU Lesser General Public License v3.0
21 stars 27 forks source link

evm: Add EIP-2537 BLS12-381 precompiles #111

Closed hyunsooda closed 1 month ago

hyunsooda commented 1 month ago

Proposed changes

History

Implementation

This is an example contract that calls add operations.

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity 0.8.24;

contract EIP2537 {

    function bls12381add() public returns (bytes memory) {
        bytes memory input = hex"0000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f560000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee000000000000000000000000000000000001101098f5c39893765766af4512a0c74e1bb89bc7e6fdf14e3e7337d257c
c0f94658179d83320b99f31ff94cd2bac0000000000000000000000000000000003e1a9f9f44ca2cdab4f43a1a3ee3470fdf90b2fc228eb3b709fcd72f014838ac82a6d797aeefed9a0804b22ed1ce8f7";
        (bool ok, bytes memory output) = address(0x0b).call(input);
        require(ok, "bls12381 add operation failed");
        return output;
    }
}

Once deployed, can be called like this

> kaia.call({to:addr, data:"0xa1abdc96"}) // "0xa1abdc96" is the selector of "bls12381add()" function.
> 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000001466e1373ae4a7e7ba885c5f0c3ccfa48cdb50661646ac6b779952f466ac9fc92730dcaed9be831cd1f8c4fefffd5209000000000000000000000000000000000c1fb750d2285d4ca0378e1e8cdbf6044151867c34a711b73ae818aee6dbe9e886f53d7928cc6ed9c851e0422f609b11

The output of bytes memory is encoded as offset(32bytes) | length(32bytes) | value(conductive 32bytes). The actual value of add operation starts from 64, the total output size is 192 bytes, and the actual value size is 128 bytes.

Discussion

The calculated CC numbers were based on static execution (comparison with other existing operations). add operation was selected and the CC value was derived from below.

CC = Base_CC * New_Gas / Base_Gas

Types of changes

Please put an x in the boxes related to your change.

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

Related issues

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

hyunsooda commented 1 month ago

@ian0371

I initially tried to remove this comment from this PR, but some words(bls-signature) were a little odd to me, so they have not been removed yet. If this comment just targeted BLS12381 operations, let me remove it.