0

Simple question but I couldn’t find the answer.

Is it the datacopy precompile? and if yes, what it does?

0
0

Yes, you are correct. It is the identity function.

It copies its input to its output. It can be used to copy between memory portions.

function dataCopy(bytes memory _input) internal view returns (bytes memory) {
    uint length = _input.length;
    bytes memory result = new bytes(length);
    assembly {
        // Call precompiled contract to copy data
        if iszero(staticcall(gas, 0x04, add(_input, 0x20), length, add(result, 0x20), length)) {
            revert(0, 0)
        }
    }
    return result;
}

Since you only pay for chunks of 32 bytes above certain threshold it is cheaper than copy byte per byte.

1
  • 1
    So the purpose is to have a cheaper way to copy data in memory? – user2284570 Oct 16 '19 at 16:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.