标准 EVM 预编译
OXN 实现标准以太坊预编译集,地址 0x01 到 0x09。0x0a(KZG point evaluation,随 EIP-4844 引入)不支持——OXN 没有 blob 交易。
支持矩阵
| 地址 | 名称 | 用途 | OXN | 基础 gas | 每 word gas |
|---|---|---|---|---|---|
0x01 | ecrecover | 从签名恢复以太坊签名者 | ✅ | 3000 | — |
0x02 | sha256 | SHA-256 哈希 | ✅ | 60 | 12 |
0x03 | ripemd160 | RIPEMD-160 哈希 | ✅ | 600 | 120 |
0x04 | identity | 输入拷贝到输出 | ✅ | 15 | 3 |
0x05 | modexp | 模幂运算(EIP-198) | ✅ | 复杂 | — |
0x06 | ecAdd | bn128(alt_bn128)上的椭圆曲线加法 | ✅ | 150 | — |
0x07 | ecMul | bn128 上的椭圆曲线标量乘法 | ✅ | 6000 | — |
0x08 | ecPairing | bn128 上的配对校验 | ✅ | 45000 | 34000/pair |
0x09 | blake2f | BLAKE2b F 压缩函数 | ✅ | 复杂 | — |
0x0a | pointEvaluation | KZG point evaluation(EIP-4844) | ❌ | — | — |
Gas 成本按当前 EVM 规范。
用法示例
ecrecover
从签名哈希恢复以太坊地址:
function recoverSigner(bytes32 hash, uint8 v, bytes32 r, bytes32 s)
external pure returns (address)
{
return ecrecover(hash, v, r, s);
}
用于 meta-transaction、EIP-712 typed 签名、链下签名验证。
sha256 / ripemd160
密码学哈希:
bytes memory input = "hello";
bytes32 shaResult = sha256(input);
bytes20 ripemdResult = ripemd160(input);
keccak256(地址 0x00,不是预编译)是标准以太坊哈希——除非你为了与外部系统兼容需要 SHA-256 或 RIPEMD-160,否则用它。
modexp
大数模幂——用于 RSA 验证和其他密码学协议:
function modexp(bytes memory base, bytes memory exp, bytes memory mod)
internal view returns (bytes memory)
{
(bool success, bytes memory result) = address(0x05).staticcall(
abi.encodePacked(base.length, exp.length, mod.length, base, exp, mod)
);
require(success);
return result;
}
bn128 曲线操作
用于 zkSNARK 验证(Groth16、Plonk)与其他基于配对的密码学:
// ecAdd: (x1, y1) + (x2, y2) 在 bn128 上
function ecAdd(uint256 x1, uint256 y1, uint256 x2, uint256 y2)
internal view returns (uint256, uint256)
{
(bool success, bytes memory result) = address(0x06).staticcall(
abi.encode(x1, y1, x2, y2)
);
require(success);
return abi.decode(result, (uint256, uint256));
}
不支持的原因
0x0a(pointEvaluation)——由 EIP-4844 为 blob 交易验证引入。OXN 没有 blob 交易,这个预编译在这里没有用途。
如果你的合约因为与 blob 无关的原因需要验证 KZG 承诺,只能在 Solidity 里实现(昂贵),或等待运行时升级加这个预编译。