Example #1
0
from tinyec.ec import SubGroup, Curve
import pickle
import os
from algorithm.utilities import utilities
import time

field = SubGroup(p=65629, g=(4, 46171), n=65538, h=1)
curve = Curve(a=-8, b=31, field=field, name="p65629")

encode_map = {}
decode_map = {}
if os.path.exists("data/encode_map.pickle") and os.path.exists(
        "data/decode_map.pickle"):
    encode_map = pickle.load(open("data/encode_map.pickle", "rb"))
    decode_map = pickle.load(open("data/decode_map.pickle", "rb"))
else:
    for k in range(1, curve.field.n):
        p = k * curve.g
        if p.x is None:
            break
        encode_map[k] = p
        decode_map['' + str(p.x) + ',' + str(p.y)] = k

    pickle.dump(encode_map, open("data/encode_map.pickle", "wb"))
    pickle.dump(decode_map, open("data/decode_map.pickle", "wb"))

# random_k = randint(1, curve.field.n)
random_k = 5443
# random_index = randint(1, curve.field.n)
random_index = 37347
Example #2
0
from tinyec.ec import SubGroup, Curve
from Crypto.Random.random import randint
from web3 import Web3

p = int('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16)
n = int('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16)
h = 1

x = int('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16)
y = int('483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8', 16)
g = (x,y)

field = SubGroup(p, g, n, h)
curve = Curve(a = 0, b = 7, field = field, name = 'secp256k1')


def new_wallet():
    private_key = randint(1, n)
    #private_key = int("f8f8a2f43c8376ccb0871305060d7b27b0554d2cc72bccf41b2705608452f315", 16)

    private_key_hex = Web3.toHex(private_key)[2:]
    public_key_hex = get_public_key(private_key_hex)
    
    address = get_address(public_key_hex)

    return private_key_hex, public_key_hex, address

def get_public_key(priv_key):
    priv_key = int(priv_key, 16)
    pub_key = priv_key * curve.g
    pub_key_hex = Web3.toHex(pub_key.x)[2:] + Web3.toHex(pub_key.y)[2:]
Example #3
0
# antes de executar o código abaixo, é necessário importar
# o pacote tinyec no python. para isso, basta
# executar o comando abaixo.
#
# pip install tinyec

# importando biblioteca
from tinyec.ec import SubGroup, Curve

# definir os grupos da ecc
field = SubGroup(p=17, g=(15, 13), n=18, h=1)
curve = Curve(a=0, b=7, field=field, name='p1707')

# exibir a equacao que esta sendo utilizada
# curve: "p1707" => y^2 = x^3 + 0x + 7 (mod 17)
print('curve:', curve)

# loop
for k in range(0, 25):
    p = k * curve.g
    print(f"{k} * G = ({p.x}, {p.y})")
The cryptography package used in here are not capable for production.
We may switch ECDSA process to cold wallet (using embbeded system) later.
To install these package, use pip
    pip install sympy  https://pypi.org/project/tinyec/
    pip install tinyec  https://www.sympy.org/en/index.html
'''
from tinyec.ec import SubGroup, Curve

name = 'secp256k1'
p = 115792089237316195423570985008687907853269984665640564039457584007908834671663
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
a = 0
b = 7
g = (55066263022277343669578718895168534326250603453777594175500187360389116729240, 32670510020758816978083085130507043184471273380659243275938904335757337482424)
h = 1
curve = Curve(a, b, SubGroup(p, g, n, h), name)

def is_even(target_int):
    '''Check if target is even or not'''
    return (int(str(target_int)[-1]) % 2 == 0)


def to_32_bytes_hex(target_int):
    '''Return hex in 32 bytes (256 bits)'''
    result = hex(target_int)
    result = result[2:]
    while True:
        if len(result) >= 64:  # 2 * 32 = 64
            break
        result = "0" + result
    return result
Example #5
0
# pip install tinyec

# https://cryptobook.nakov.com/asymmetric-key-ciphers/elliptic-curve-cryptography-ecc

from tinyec.ec import SubGroup, Curve
from tinyec import registry
import secrets

field = SubGroup(p=17, g=(15, 13), n=18, h=1)
curve = Curve(a=0, b=7, field=field, name='p1707')
print('curve:', curve)

for k in range(0, 25):
    p = k * curve.g
    print(f"{k} * G = ({p.x}, {p.y})")

print("-" * 50)

field = SubGroup(p=17, g=(5, 9), n=18, h=1)
curve = Curve(a=0, b=7, field=field, name='p1707')
print('curve:', curve)

for k in range(0, 25):
    p = k * curve.g
    print(f"{k} * G' = ({p.x}, {p.y})")

print("=" * 50)

curve = registry.get_curve('secp192r1')
print('curve:', curve)