Last active
July 26, 2025 18:05
-
-
Save MurageKibicho/ee5d67ef1f623d12cebcde927e44fa05 to your computer and use it in GitHub Desktop.
Bitcoin Elliptic Curve Generate Private Key in C Starter code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Full guide here:https://leetarxiv.substack.com/p/hacking-dormant-bitcoin-wallets-c | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <gmp.h> | |
#include <secp256k1.h> | |
#include <openssl/sha.h> | |
#include <openssl/ripemd.h> | |
//Run: clear && gcc PrivateKey.c -lsecp256k1 -lcrypto -lgmp -o m.o && ./m.o | |
typedef struct elliptic_curve_point_struct *EllipticCurvePoint; | |
struct elliptic_curve_point_struct | |
{ | |
mpz_t x; | |
mpz_t y; | |
int infinity; | |
}; | |
EllipticCurvePoint CreatePoint() | |
{ | |
EllipticCurvePoint point = malloc(sizeof(struct elliptic_curve_point_struct)); | |
mpz_init(point->x); | |
mpz_init(point->y); | |
point->infinity = 0; | |
return point; | |
} | |
void CopyPoint(EllipticCurvePoint source, EllipticCurvePoint destination) | |
{ | |
mpz_set(destination->x, source->x); | |
mpz_set(destination->y, source->y); | |
destination->infinity = source->infinity; | |
} | |
int TestPointEquality(EllipticCurvePoint a, EllipticCurvePoint b) | |
{ | |
if(a->infinity && b->infinity) return 1; | |
if(a->infinity || b->infinity) return 0; | |
return mpz_cmp(a->x, b->x) == 0 && mpz_cmp(a->y, b->y) == 0; | |
} | |
int TestLSBParity(mpz_t y) | |
{ | |
return mpz_tstbit(y, 0); // Returns 1 if odd, 0 if even | |
} | |
void DestroyPoint(EllipticCurvePoint point) | |
{ | |
if(point) | |
{ | |
mpz_clear(point->x); | |
mpz_clear(point->y); | |
free(point); | |
} | |
} | |
void TestECPoint() | |
{ | |
//Curve properties | |
char *primeNumberHexadecimal = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"; | |
char *generatorXHexadecimal = "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"; | |
char *generatorYHexadecimal = "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"; | |
mpz_t primeNumber, privateKey; | |
//Set prime number in base 16 | |
mpz_init_set_str(primeNumber, primeNumberHexadecimal, 16); | |
//Set private key in base 10 | |
mpz_init_set_str(privateKey, "5", 10); | |
//Create Generator and Result points | |
EllipticCurvePoint generator = CreatePoint(); | |
EllipticCurvePoint resultant = CreatePoint(); | |
//Set generator X and Y values and infinity = 0 | |
mpz_set_str(generator->x, generatorXHexadecimal, 16); | |
mpz_set_str(generator->y, generatorYHexadecimal, 16); | |
generator->infinity = 0; | |
DestroyPoint(generator); | |
DestroyPoint(resultant); | |
mpz_clears(primeNumber, privateKey,NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment