Real-World Example of Multiplication of EC Points in Python

It’s basically taking one point on the curve (let’s call it P) and multiplying it by another number (let’s call it k). This gives us a new point, which we can write as kP.

Now, you might be thinking: “Wait, what if I want to multiply two points instead? Can I do that?” And the answer is… sort of! But let’s not get ahead of ourselves here.

To start with, let’s take a look at some code. Here’s an example using the popular Python library called PyCrypto:

# Import necessary libraries
from pycryptodome.elliptic import curve, point
import random
import hashlib

# Define our elliptic curve and base point (P)
curve = curve.SECP256K1() # Define the elliptic curve to be used
base_point = point(int(random.uniform(-2**30, 2**30)), int(random.uniform(-2**30, 2**30))) # Generate a random base point within the range of the curve
base_point += curve.G # Add the generator point of the curve to the base point

# Define our secret key (k) and message to be signed (m)
secret_key = random.randint(1, 999999999999999999999999999999999999999999999999999) # Generate a random secret key within the range of the curve
message_to_sign = b"Hello, world!" # Define the message to be signed as a byte string

# Calculate the signature using kP and m (using SHA-256 for hashing)
signature = [] # Initialize an empty list to store the signature
hash_value = hashlib.sha256(message_to_sign).digest() # Hash the message using SHA-256
for i in range(32): # Loop 32 times (since the hash value is 32 bytes long)
    x, y = base_point * secret_key # Multiply the base point by the secret key to get a point on the curve
    signature.append((x % curve.order()).to_bytes(1, 'big')) # Take the x-coordinate of the point and convert it to a byte string, then append it to the signature list
    signature.append((y % curve.order()).to_bytes(1, 'big')) # Take the y-coordinate of the point and convert it to a byte string, then append it to the signature list

# The resulting signature is a list of 64 bytes, with each pair representing the x and y coordinates of a point on the curve. This can be used to verify the authenticity of the message.

Now, let’s break down what this code is doing:

– We first import the necessary libraries and define our elliptic curve (SECP256K1) and base point (P). Next, we generate a random secret key (k) between 1 and 999,999,999,999,999,999. This is the number that will be multiplied by our base point to get our signature. We then define our message to be signed (m), which in this case is “Hello, world!”. To calculate the signature using kP and m, we first hash the message using SHA-256 for hashing. This gives us a 32-byte value that will be used as input for our point multiplication algorithm. We then loop through each byte of this hash value (which is essentially just a string of 0s and 1s) and calculate kP for each one using the PyCrypto library’s `point` class. This gives us two new points on the curve, which we append to our signature list as bytes with big-endian format. And that’s it! You now know how to multiply elliptic curve points like a pro (or not) in Python using PyCrypto. Of course, this is just one example of many possible implementations, and there are plenty of other libraries out there for working with elliptic curves as well. But hopefully this gives you a good starting point if you’re interested in learning more about this fascinating topic!

SICORPS