Ethereum: Extract ECDSA R and S Signed as signatures
Electronic Cash Transactions (ECC) is a digital currency that is used by the elliptical curve digital signature algorithm (ECDSA) to create secure and private signatures. In this article, we examine how to extract the R and S components from an ECDSA signed message in Ethereum.
Overview of ECDSA and Signature Construction
When creating a signature using ECDSA, two key ingredients are produced: r
ands
. These values are used to sign messages and are parts of the elliptical curve. In this article, we focus on Rand "s
extract the signed message from ECDSA.
How to expand r and s
The process of extracting r
and ‘s’ includes several steps:
- Hasing : First, the input signature should be subtracted using “verififykey” (ie
v
,p
andq
). This creates a digestion that corresponds to the signed message.
- Divide division : Then divide the digest into two parts: the first part is used to extract
r
and” s, while the second part discards it.
- Coding ECDSA values : Ethereum is coded as R and S a couple of bytes (
[r, s]
). To obtain these values, you must carry the first two bytes (or four bytes) from the first half of the digestion.
Code example
To illustrate the process, consider an example of strength:
`Solidity
Pragma solidity ^0.8.0;
signing a contract {
// ... Other features ...
Function signal (string memory sigst) Public returns (bytes32 r, bytes32 s) {
// hash input signature with check key
Bytes32 Digest = Keccak256 (Abi.encodepacked (Sigst));
// divide digestion into two parts
uint160 Firsthalf = Uint160 (Digest);
Uint160 Secondhalf = Uint160 (Digest) | 0x80000000;
// ECDSA values are coded as a byte array
Bytes32 r;
Bytes32 s;
uint8 i, j;
for (i = 1; i
J = I + 2;
r = keccak256 (abi.encodepacked ((FIRSTHALF & 0X7FFFFFFF) << (J - 1), (SiviceHALF >> 64) | ((J - 2) * 65536))));
}
for (i = 1; i
J = I + 2;
S = keccak256 (abi.encodepacked ((FIRSTHALF & 0X7FFFFFFF) << (J - 1), ((((Secondhalf >> 64) | ((J - 2) * 65536)))))));
}
Return (R, S);
}
}
In this example, the Sign
function takes a signed message as an input and returns the appropriate R and S values. The function first indicates the input signature using the check button and then divides the digestion into two parts. Finally, ECDSA values are coded as a byte array using the Keccak-256 hash function.
Conclusion
In this article, we examined how to obtain ECDSA’s signed message in ECDSA from ECDSA. By understanding the process of the hash, dividing digestion and coding ECDSA values, you can now write your own functions to sign messages with ECC signatures.