Example #1
0
def secp224k1():
    _a = 0x0000000000000000000000000000000000000000000000000000000000
    _b = 0x0000000000000000000000000000000000000000000000000000000005
    _p = 0x00fffffffffffffffffffffffffffffffffffffffffffffffeffffe56d
    _Gx = 0x00a1455b334df099df30fc28a169a467e9e47075a90f7e650eb6b7a45c
    _Gy = 0x007e089fed7fba344282cafbd6f7e319f7c0b0bd59e2ca4bdb556d61a5
    _r = 0x010000000000000000000000000001dce8d2ec6184caf0a971769fb1f7
    curve_secp224k1 = ellipticcurve.CurveFp(_p, _a, _b)
    generator_secp224k1 = ellipticcurve.Point(curve_secp224k1, _Gx, _Gy, _r)
    secp224k1_instance = curves.Curve("SECP224k1", curve_secp224k1,
                                      generator_secp224k1, (1, 3, 132, 0, 20),
                                      "secp256k1")
    return secp224k1_instance
Example #2
0
# Note that we could also get that from ecdsa lib from the curve, e.g.:
# SECP256k1.__dict__['curve'].__dict__['_CurveFp__p']
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
# Curve's a and b are (y**2 = x**3 + a*x + b)
_a = 0x0000000000000000000000000000000000000000000000000000000000000000
_b = 0x0000000000000000000000000000000000000000000000000000000000000007
# Curve's generator point is:
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
# prime number of points in the group (the order)
_order = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

# The ECDSA curve (secp256k1) is:
# Note that we could get that from ecdsa lib, e.g.:
# SECP256k1.__dict__['curve']
_curve = ellipticcurve.CurveFp(_p, _a, _b)

# The generator base point is:
# Note that we could get that from ecdsa lib, e.g.:
# SECP256k1.__dict__['generator']
_G = ellipticcurve.Point(_curve, _Gx, _Gy, _order)


# method used by both PrivateKey and PublicKey - TODO clean - add in another module?
def add_magic_prefix(message):
    magic_prefix = b'\x18Bitcoin Signed Message:\n'
    message_size = len(message).to_bytes(1, byteorder='big')
    message_encoded = message.encode('utf-8')
    message_magic = magic_prefix + message_size + message_encoded
    return message_magic
from ecdsa import ellipticcurve
import hashlib
import base58

# Compressed Public Key Config
compressed_public_key = True

_a = 0x0000000000000000000000000000000000000000000000000000000000000000
_b = 0x0000000000000000000000000000000000000000000000000000000000000007
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
_Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

curve_secp256k1 = ellipticcurve.CurveFp(_p, _a, _b, 1)
generator_secp256k1 = ellipticcurve.PointJacobi(curve_secp256k1,
                                                _Gx,
                                                _Gy,
                                                1,
                                                _r,
                                                generator=True)


def create_private_key_from_passphrase(passphrase):
    priv_key_bytes = hashlib.sha256(passphrase.encode()).digest()
    # print(f"Priv key bytes: {priv_key_bytes}")
    priv_key_hex = priv_key_bytes.hex()
    # print(f"Priv key hex: {priv_key_hex}")
    return priv_key_hex

Example #4
0
# ---------  End Client  ---------
'''


# https://github.com/warner/python-ecdsa/blob/333ee3feb1dfc6797db7a83d221e5a3a9fafdc3f/src/ecdsa/ecdsa.py
#  ----------------------------- Begin NIST Curve P-256: -----------------------------
PRIME = 115792089210356248762697446949407573530086143415290314195533631308867097853951
R = 115792089210356248762697446949407573529996955224135760342422259061068512044369
# s = 0xc49d360886e704936a6678e1139d26b7819f7e90L
# c = 0x7efba1662985be9403cb055c75d4f7e0ce8d84a9c5114abcaf3177680104fa0dL
A = -3
B = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B
Gx = 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296
Gy = 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5

curve_256 = ecc.CurveFp(PRIME, A, B, 1)
curve_256_generator = ecc.PointJacobi(curve_256, Gx, Gy, 1, R, generator=True)
# ----------------------------- END NIST Curve P-256: -------------------------------

# https://github.com/bdauvergne/python-pkcs1/blob/master/pkcs1/primitives.py
def OS2IP(x: str) -> int:
    '''Converts the byte string x representing an integer reprented using the
       big-endian convient to an integer.
    '''
    h = hexlify(x) #.binascii
    return int(h, 16)

# https://github.com/bdauvergne/python-pkcs1/blob/master/pkcs1/primitives.py
def I2OSP(x: int, x_len: int = 4) -> str:
    '''Converts the integer x to its big-endian representation of length
       x_len.
Example #5
0
from ecdsa import numbertheory
from random import SystemRandom
import codecs
# NIST Curve P-384:
_p = 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319
_r = 39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643
# s = 0xa335926aa319a27a1d00896a6773a4827acdac73L
# c = 0x79d1e655f868f02fff48dcdee14151ddb80643c1406d0ca10dfe6fc52009540a495e8042ea5f744f6e184667cc722483L
_b = 0xB3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF
_Gx = 0xAA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7
_Gy = 0x3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F

# y^2 = x^3+ax+b mod p
# ellipticcurve.CurveFp(p, a, _b, h(flag, not join the encrypt))
# a = -3 mod p = p - 3, so a = -3
curve_384 = ellipticcurve.CurveFp(_p, -3, _b, 1)

# __init__(self, curve, x, y, z, order=None, generator=False)
#
# Initialise a point that uses Jacobi representation internally.
generator_384 = ellipticcurve.PointJacobi(curve_384,
                                          _Gx,
                                          _Gy,
                                          1,
                                          _r,
                                          generator=True)

g = generator_384
n = g.order()
print(hex(n))
# print(hex(n))