arithmetic docstring and formatting

This commit is contained in:
lakshyacis 2019-12-21 15:13:03 +05:30
parent 4a369f70c1
commit b16515dc09
No known key found for this signature in database
GPG Key ID: D2C539C8EC63E9EB
2 changed files with 24 additions and 7 deletions

View File

@ -1,5 +1,6 @@
# pylint: disable=missing-docstring,too-many-function-args """
Arithmetic Expressions
"""
import hashlib import hashlib
import re import re
@ -11,6 +12,7 @@ G = (Gx, Gy)
def inv(a, n): def inv(a, n):
"""Inversion"""
lm, hm = 1, 0 lm, hm = 1, 0
low, high = a % n, n low, high = a % n, n
while low > 1: while low > 1:
@ -21,6 +23,7 @@ def inv(a, n):
def get_code_string(base): def get_code_string(base):
"""Returns string according to base value"""
if base == 2: if base == 2:
return '01' return '01'
elif base == 10: elif base == 10:
@ -36,6 +39,7 @@ def get_code_string(base):
def encode(val, base, minlen=0): def encode(val, base, minlen=0):
"""Returns the encoded string"""
code_string = get_code_string(base) code_string = get_code_string(base)
result = "" result = ""
while val > 0: while val > 0:
@ -47,6 +51,7 @@ def encode(val, base, minlen=0):
def decode(string, base): def decode(string, base):
"""Returns the decoded string"""
code_string = get_code_string(base) code_string = get_code_string(base)
result = 0 result = 0
if base == 16: if base == 16:
@ -59,10 +64,13 @@ def decode(string, base):
def changebase(string, frm, to, minlen=0): def changebase(string, frm, to, minlen=0):
"""Change base of the string"""
return encode(decode(string, frm), to, minlen) return encode(decode(string, frm), to, minlen)
def base10_add(a, b): def base10_add(a, b):
"""Adding the numbers that are of base10"""
# pylint: disable=too-many-function-args
if a is None: if a is None:
return b[0], b[1] return b[0], b[1]
if b is None: if b is None:
@ -78,6 +86,7 @@ def base10_add(a, b):
def base10_double(a): def base10_double(a):
"""Double the numbers that are of base10"""
if a is None: if a is None:
return None return None
m = ((3 * a[0] * a[0] + A) * inv(2 * a[1], P)) % P m = ((3 * a[0] * a[0] + A) * inv(2 * a[1], P)) % P
@ -87,6 +96,7 @@ def base10_double(a):
def base10_multiply(a, n): def base10_multiply(a, n):
"""Multiply the numbers that are of base10"""
if n == 0: if n == 0:
return G return G
if n == 1: if n == 1:
@ -99,28 +109,35 @@ def base10_multiply(a, n):
def hex_to_point(h): def hex_to_point(h):
"""Converting hexadecimal to point value"""
return (decode(h[2:66], 16), decode(h[66:], 16)) return (decode(h[2:66], 16), decode(h[66:], 16))
def point_to_hex(p): def point_to_hex(p):
"""Converting point value to hexadecimal"""
return '04' + encode(p[0], 16, 64) + encode(p[1], 16, 64) return '04' + encode(p[0], 16, 64) + encode(p[1], 16, 64)
def multiply(privkey, pubkey): def multiply(privkey, pubkey):
return point_to_hex(base10_multiply(hex_to_point(pubkey), decode(privkey, 16))) """Multiplying keys"""
return point_to_hex(base10_multiply(
hex_to_point(pubkey), decode(privkey, 16)))
def privtopub(privkey): def privtopub(privkey):
"""Converting key from private to public"""
return point_to_hex(base10_multiply(G, decode(privkey, 16))) return point_to_hex(base10_multiply(G, decode(privkey, 16)))
def add(p1, p2): def add(p1, p2):
"""Adding two public keys"""
if len(p1) == 32: if len(p1) == 32:
return encode(decode(p1, 16) + decode(p2, 16) % P, 16, 32) return encode(decode(p1, 16) + decode(p2, 16) % P, 16, 32)
return point_to_hex(base10_add(hex_to_point(p1), hex_to_point(p2))) return point_to_hex(base10_add(hex_to_point(p1), hex_to_point(p2)))
def hash_160(string): def hash_160(string):
"""Hashed version of public key"""
intermed = hashlib.sha256(string).digest() intermed = hashlib.sha256(string).digest()
ripemd160 = hashlib.new('ripemd160') ripemd160 = hashlib.new('ripemd160')
ripemd160.update(intermed) ripemd160.update(intermed)
@ -128,17 +145,18 @@ def hash_160(string):
def dbl_sha256(string): def dbl_sha256(string):
"""Double hashing (SHA256)"""
return hashlib.sha256(hashlib.sha256(string).digest()).digest() return hashlib.sha256(hashlib.sha256(string).digest()).digest()
def bin_to_b58check(inp): def bin_to_b58check(inp):
"""Convert binary to base58"""
inp_fmtd = '\x00' + inp inp_fmtd = '\x00' + inp
leadingzbytes = len(re.match('^\x00*', inp_fmtd).group(0)) leadingzbytes = len(re.match('^\x00*', inp_fmtd).group(0))
checksum = dbl_sha256(inp_fmtd)[:4] checksum = dbl_sha256(inp_fmtd)[:4]
return '1' * leadingzbytes + changebase(inp_fmtd + checksum, 256, 58) return '1' * leadingzbytes + changebase(inp_fmtd + checksum, 256, 58)
# Convert a public key (in hex) to a Bitcoin address
def pubkey_to_address(pubkey): def pubkey_to_address(pubkey):
"""Convert a public key (in hex) to a Bitcoin address"""
return bin_to_b58check(hash_160(changebase(pubkey, 16, 256))) return bin_to_b58check(hash_160(changebase(pubkey, 16, 256)))

View File

@ -1,8 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
src/pyelliptic/ecc.py Asymmetric cryptography using elliptic curves
=====================
""" """
# pylint: disable=protected-access # pylint: disable=protected-access