A DIY Guide to High-Security Cryptography

RealmLink
Coinmonks
3 min readFeb 19, 2023

--

Here is a walkthrough on how to generate an Elliptic Curve public and private key from scratch using only Python’s built-in random library.

Before we begin, let’s discuss some background information on Elliptic Curve Cryptography (ECC). ECC is a type of public key cryptography that uses the mathematics of elliptic curves to create keys. One of the benefits of ECC is that it offers a higher level of security compared to other public key cryptography algorithms like RSA.

Now, let’s dive into the code. First, we need to define the curve that we want to use for our key generation. For this example, we will use the secp256k1 curve, which is the same curve used by Bitcoin. Here is the equation for the curve:

y^2 = x^3 + 7 (mod p)

Where p is a prime number that defines the field over which the curve is defined. For secp256k1, p is:

p = 2^256 - 2^32 - 977

We will represent points on the curve using a tuple of two integers, (x, y). Here is the code to define the curve:

import random

# Define the curve parameters for secp256k1
p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
a = 0
b = 7

# Define the base point (also called the generator point) for secp256k1
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)

Next, we need to generate a random private key. The private key is just a random number between 1 and p-1. Here is the code to generate a private key:

# Generate a random private key
privkey = random.randint(1, p-1)

Now that we have a private key, we can use it to calculate the corresponding public key. The public key is just a point on the curve that is calculated by multiplying the base point G by the private key privkey. We can use the double-and-add algorithm to perform the multiplication. Here is the code to calculate the public key:

# Calculate the public key
def double_and_add(p, n, a, b):
# Double and add algorithm for point multiplication
# https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add
result = None
for bit in bin(n)[2:]:
result = result + result if result else p
if bit == '1':
result = add_points(result, p, a, b)
return result

def add_points(p, q, a, b):
# Point addition for secp256k1
# https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_addition
if not p:
return q
if not q:
return p
if p[0] == q[0] and p[1] != q[1]:
return None
if p != q:
s = ((q[1]-p[1]) * pow(q[0]-p[0], p-2, p)) % p
else:
s = ((3*p[0]*p[0]+a) * pow(2*p[1], p-2, p)) % p
x = (s*s - p[0] - q[0]) % p
y = (s*(p[0]-x) - p[1]) % p

return (x, y)
pubkey = double_and_add(G, privkey, a, b)

Now we have a public key, which is just a point on the curve. We can represent the public key as a tuple of two integers, `(x, y)`. Here is the code to print out the public and private keys:

# Print out the public and private keys
print("Private key: {}".format(privkey))
print("Public key: ({}, {})".format(pubkey[0], pubkey[1]))

And that’s it! We have successfully generated an Elliptic Curve public and private key using only Python’s built-in random library. Note that this code is not suitable for production use, as it does not include any key management or security features. If you are working on a real-world application, be sure to consult with a security expert and use a well-tested and trusted library for your ECC key generation.

If you liked this check these out!

-Interoperability between games — https://medium.com/p/6648f8aaa275

-Self supportable ecosystems — https://medium.com/p/9eda96d06973

-User made web3 game ecosystems — https://medium.com/p/a069b267c6e0

-Decentralised credit scores — https://medium.com/p/8c0439770fd3

-Solidity Dev Study Group — https://discord.gg/KzbcGmrnfN

-Polygon Alliance — https://www.polygonalliance.com/

-Polygon Alliance Discord — https://discord.gg/kJKPCGQu66

Did you enjoy this article?
Feel like buying me a cup of coffee?
Polygon/Eth/Bsc — 0x4A581E0eaf6b71D05905e8E6014dc0277A1B10ad

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

RealmLink
Coinmonks

Focused on interoperability between MMO RPG games