Verifying Elliptic Curve Digital Signature with TRON Smart Contract


Elliptic Curve Digital Signature Algorithm(ECDSA) is a variant of the Digital Signature Algorithm (DSA) based on the elliptic curve cryptography. ECDSA is widely in the blockchains of TRON, Bitcoin and Ethereum used for transaction signature, consensus verification and many other aspects. It is one of the cornerstone algorithms of blockchain technology.

Elliptic curve digital signature algorithm theory

Signature generation algorithm

ECDSA is a combination of ECC and DSA. The whole signature process is similar to DSA. The ECDSA signature consists of a pair of integers (r, s). The steps of how public and private keys calculate to sign the message are as follows:

Signature verification algorithm

After receiving the message (message) and the signature (r, s), the recipient will:

Use ecrecover for signature verification

ecrecover function

The complete function signature of ecrecover is:

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

The batchvalidatesign function is a unique function of TVM and will come into effect after the №32 proposal is adopted. The complete function signature is:

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);    }}


Verifying signatures in traditional Solidity language requires a lot of overheads, and developers need to make type conversions and write their own slicing functions. In addition, since ecrecover can only return the address, extra work is required to further compare whether the restored address match the signer’s address. In contrast, the batchvalidatesign introduced in the TRON Solidity V0.5.9 makes signature verification in smart contracts far more intuitive for users. Furthermore, the introduction of parallel verification is also conducive to more use cases of the smart contracts in the future.


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