pragma solidity ^0.4.24; contract SignatureDvider { function setNum(bytes signature) public pure returns(bytes32 r, bytes32 s, uint8 v) { require(signature.length == 65); // this code use 17850 gas. // for(uint i=0;i<32; i++) { // r = r | bytes32(signature[i] & 0xff) >> (i * 8); // } // for(i=0;i<32; i++) { // s = s | bytes32(signature[32+i] & 0xff) >> (i * 8); // } // v = uint8(signature[64]); // The other way use asembly, It use gas cost is 1268. // The signature format is a compact form of: // {bytes32 r}{bytes32 s}{uint8 v} // Compact means, uint8 is not padded to 32 bytes. assembly { r := mload(add(signature, 32)) s := mload(add(signature, 64)) // Here we are loading the last 32 bytes, including 31 bytes of 's'. v := byte(0, mload(add(signature, 96))) } // Version of signature should be 27 or 28, but 0 and 1 are also possible if (v < 27) { v += 27; } } }
0.4.24