solidity signature r s v
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/objects.h>
void signature_parse(uint8_t signature, ECDSA_SIG parsed_signature) {
BIGNUM r, s;
r = BN_new();
s = BN_new();
BN_bin2bn(signature, 32, r);
BN_bin2bn(signature + 32, 32, s);
ECDSA_SIG_set0(parsed_signature, r, s);
}
int main() {
uint8_t signature[64]; // The signature bytes
ECDSA_SIG *parsed_signature = ECDSA_SIG_new();
// Call signature_parse with the signature bytes and parsed_signature pointer
signature_parse(signature, parsed_signature);
// Further operations with parsed_signature
ECDSA_SIG_free(parsed_signature);
return 0;
}
Explanation:
#include <openssl/ec.h>
and other similar lines are preprocessor directives that include necessary header files for OpenSSL library functions related to elliptic curve operations (EC
), ECDSA (Elliptic Curve Digital Signature Algorithm), and related data structures.void signature_parse(uint8_t signature, ECDSA_SIG parsed_signature)
defines a functionsignature_parse
that takes a pointer to a uint8_t array (signature
) representing the signature and a pointer to anECDSA_SIG
struct (parsed_signature
). This function will parse the signature into its componentsr
ands
.BIGNUM r, s; r = BN_new(); s = BN_new();
declares pointers toBIGNUM
structuresr
ands
and initializes them usingBN_new()
.BIGNUM
is a structure in OpenSSL representing big integers.BN_bin2bn(signature, 32, r); BN_bin2bn(signature + 32, 32, s);
converts the first 32 bytes of thesignature
array into a big integerr
and the next 32 bytes into another big integers
. These represent the components of the ECDSA signature.ECDSA_SIG_set0(parsed_signature, r, s);
sets theparsed_signature
structure using ther
ands
values obtained from the signature.ECDSA_SIG_set0()
initializes ther
ands
values of anECDSA_SIG
structure with givenBIGNUM
values.In the
main()
function, an arrayuint8_t signature[64];
is declared to store the signature bytes.ECDSA_SIG *parsed_signature = ECDSA_SIG_new();
declares a pointerparsed_signature
to anECDSA_SIG
structure and initializes it usingECDSA_SIG_new()
, allocating memory for theECDSA_SIG
structure.signature_parse(signature, parsed_signature);
calls thesignature_parse
function with thesignature
array andparsed_signature
pointer to parse the signature bytes and store them in theparsed_signature
structure.Further operations can be performed with the parsed ECDSA signature as needed.
ECDSA_SIG_free(parsed_signature);
frees the memory allocated for theparsed_signature
structure once it's no longer needed.return 0;
ends themain()
function.