Verifying Elliptic Curve Digital Signature with TRON Smart Contract

Overview

Elliptic curve digital signature algorithm theory

Signature generation algorithm

Signature verification algorithm

Use ecrecover for signature verification

ecrecover function

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

slice function

function slice(bytes memory data, uint start, uint len) returns (bytes){    bytes memory b = new bytes(len);    for(uint i = 0; i < len; i++){        b[i] = data[i + start];    }    return b;}The full contract code is as follows://return the address according to the signature and and the datafunction validate(bytes32 hash, bytes signature) returns (address){    bytes memory signedString = signature;    bytes32  r = convertToBytes32(slice(signedString, 0, 32));    bytes32  s = convertToBytes32(slice(signedString, 32, 32));    byte  v1 = slice(signedString, 64, 1)[0];    uint8 v = uint8(v1) + 27;    return ecrecover(hash, r, s, v);}//slice functionfunction slice(bytes memory data, uint start, uint len) returns (bytes){    bytes memory b = new bytes(len);    for(uint i = 0; i < len; i++){        b[i] = data[i + start];    }    return b;}//convert bytes to bytes32function convertToBytes32(bytes memory source) returns (bytes32 result) {    assembly {        result := mload(add(source, 32))    }
}

Parallel signature verification using batchvalidatesign

batchvalidatesign(bytes32 hash,bytes[] signatures,address[] addresses) returns(bytes32)
pragma experimental ABIEncoderV2;contract BatchValidate {    function validatesign(bytes32 hash, bytes[] memory signatures,         address[] memory addresses) public returns(bytes32){        return batchvalidatesign(hash, signatures, addresses);    }}

Conclusion

References

For more information

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store