Source code for pksig_ecdsa
'''
**Elliptic Curve Digital Signature Algorithm (ECDSA)**
*Authors:* NIST
| **Title:** "Digital Signature Standard (DSS)"
| **Published in:** FIPS 186, 1994
| **Available from:** https://csrc.nist.gov/publications/detail/fips/186/4/final
| **Notes:** Elliptic curve variant of DSA.
.. rubric:: Scheme Properties
* **Type:** signature (public key)
* **Setting:** elliptic curve groups
* **Assumption:** ECDLP (Elliptic Curve Discrete Logarithm)
.. rubric:: Implementation
:Authors: J. Ayo Akinyele
:Date: 5/2011
'''
from charm.toolbox.ecgroup import ECGroup,ZR,G
from charm.toolbox.PKSig import PKSig
debug = False
[docs]
class ECDSA(PKSig):
"""
>>> from charm.toolbox.eccurve import prime192v2
>>> group = ECGroup(prime192v2)
>>> ecdsa = ECDSA(group)
>>> (public_key, secret_key) = ecdsa.keygen(0)
>>> msg = "hello world! this is a test message."
>>> signature = ecdsa.sign(public_key, secret_key, msg)
>>> ecdsa.verify(public_key, signature, msg)
True
"""
def __init__(self, groupObj):
PKSig.__init__(self)
global group
group = groupObj
[docs]
def keygen(self, bits):
group.paramgen(bits)
x, g = group.random(), group.random(G)
y = (g ** x)
return ({'g':g, 'y':y}, x)
[docs]
def sign(self, pk, x, M):
while True:
k = group.random()
r = group.zr(pk['g'] ** k)
e = group.hash(M)
s = (k ** -1) * (e + x * r)
if (r == 0 or s == 0):
print ("unlikely error r = %s, s = %s" % (r,s))
continue
else:
break
return { 'r':r, 's':s }
[docs]
def verify(self, pk, sig, M):
w = sig['s'] ** -1
u1 = group.hash(M) * w
u2 = sig['r'] * w
v = (pk['g'] ** u1) * (pk['y'] ** u2)
if group.zr(v) == sig['r']:
return True
else:
return False