美文网首页
ecrecover指令

ecrecover指令

作者: 雪落无留痕 | 来源:发表于2021-04-20 21:04 被阅读0次

Solidity有一个ecrecover 指令,可以根据消息hash 和签名,返回签名者的地址:

ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address)

具体代码实现为:

func (s *PrivateAccountAPI) EcRecover(ctx context.Context, data, sig hexutil.Bytes) (common.Address, error) {
    if len(sig) != 65 {
        return common.Address{}, fmt.Errorf("signature must be 65 bytes long")
    }
    if sig[64] != 27 && sig[64] != 28 {
        return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)")
    }
    sig[64] -= 27 // Transform yellow paper V from 27/28 to 0/1

    rpk, err := crypto.Ecrecover(signHash(data), sig)
    if err != nil {
        return common.Address{}, err
    }
    pubKey := crypto.ToECDSAPub(rpk)
    recoveredAddr := crypto.PubkeyToAddress(*pubKey)
    return recoveredAddr, nil
}

vbuterin指出可以使用该指令进行点乘运算的验证,其Solidity 代码实现为:

  function ecmulVerify(uint256[2] memory multiplicand, uint256 scalar,
    uint256[2] memory product) internal pure returns(bool verifies)
  {
    require(scalar != 0); // Rules out an ecrecover failure case
    uint256 x = multiplicand[0]; // x ordinate of multiplicand
    uint8 v = multiplicand[1] % 2 == 0 ? 27 : 28; // parity of y ordinate
    // https://ethresear.ch/t/you-can-kinda-abuse-ecrecover-to-do-ecmul-in-secp256k1-today/2384/9
    // Point corresponding to address ecrecover(0, v, x, s=scalar*x) is
    // (x⁻¹ mod GROUP_ORDER) * (scalar * x * multiplicand - 0 * g), i.e.
    // scalar*multiplicand. See https://crypto.stackexchange.com/a/18106
    bytes32 scalarTimesX = bytes32(mulmod(scalar, x, GROUP_ORDER));
    address actual = ecrecover(bytes32(0), v, bytes32(x), scalarTimesX);
    // Explicit conversion to address takes bottom 160 bits
    address expected = address(uint256(keccak256(abi.encodePacked(product))));
    return (actual == expected);
  }

该函数可以检查product==scalar*multiplicand, 其中productmultiplicandsecp256k1 曲线上的点。

参考

https://github.com/protofire/zeppelin-solidity/blob/master/contracts/ECRecovery.sol

https://www.cnblogs.com/wanghui-garcia/p/9664559.html

https://ethresear.ch/t/you-can-kinda-abuse-ecrecover-to-do-ecmul-in-secp256k1-today/2384

https://github.com/smartcontractkit/chainlink/blob/ead2121a4c68f9d060c3675eb29fd688dffe0f24/evm-contracts/src/v0.6/VRF.sol#L256

相关文章

  • ecrecover指令

    Solidity有一个ecrecover 指令,可以根据消息hash 和签名,返回签名者的地址: 具体代码实现为:...

  • MIPS指令集与简要分析

    R格式指令 基本格式 指令 算数类指令 逻辑类指令 位移类指令 跳转指令 I格式指令 基本格式 指令 算数指令 逻...

  • 指令指令

    /tellraw @a {"rawtext":[{"text":"你的名字 获得了成就 §a[你要的成就]"}]}...

  • Linux——DAY2进阶指令

    1、df 指令 2、free指令 3、head指令 4、tail指令 5、less指令 6、wc指令 7、date...

  • Java Web开发学习中2.(JSP指令元素)

    JSP指令元素: page指令, include指令,taglib指令. 一. page指令: 用来设定JSP页面...

  • linux指令大全(归类整理)

    一.文件目录指令 1 pwd指令 2 ls指令 3 cd指令 4 mkdir指令 5 rmdir指令 6 touc...

  • 汇编笔记4(跳转)

    第9章(转移指令): 8086转移指令: 无条件转移指令,条件转移指令,循环指令,过程,中断 offset(伪指令...

  • 控制器、微程序控制

    1.控制器的功能 (1)取指令(指令地址,控制信号) (2)分析指令(解释指令,指令译码) (3)执行指令 (4)...

  • (32位汇编 七)堆栈/栈(stack)

    PUSH指令 POP指令 PUSHAD指令 保护现场 POPAD指令 恢复现场

  • Angular 指令,管道,服务

    1. 指令 Directive 指令是 Angular 提供的操作 DOM 的途径。指令分为属性指令和结构指令...

网友评论

      本文标题:ecrecover指令

      本文链接:https://www.haomeiwen.com/subject/faanlltx.html