✖️Scalar Multiplication in Python
Scalar multiplication forms the basis of elliptic curve cryptography. We can easily express multiplication of a point by a scalar in the form of repeated additions.
class Point:
... # add these methods to the previously defined Point class
def __rmul__(self, scalar: int) -> "Point":
# Naive approach:
#
# result = I
# for _ in range(scalar): # or range(scalar % N)
# result = result + self
# return result
# Optimized approach using binary expansion
current = self
result = I
while scalar:
if scalar & 1: # same as scalar % 2
result = result + current
current = current + current # point doubling
scalar >>= 1 # same as scalar / 2
return resultWe can now verify the correctness of the generator point used in Bitcoin, as follows:
Resources
Goundar, Raveen R., et al. "Scalar multiplication on Weierstraß elliptic curves from Co-Z arithmetic." Journal of cryptographic engineering 1.2 (2011): 161. https://www.matthieurivain.com/files/jcen11b.pdf
Last updated
Was this helpful?