Skip to main content

mini-cryptography

mini-cryptography: mini ECDSA cryptography and Mekle tree root calculation

Mini-cryptography is a library that has ECC (Elliptic Curve Cryptography) arithmetic operations, including ECDSA (Elliptic Curve Digital Signature Algorithm) signature formation and verification. It also has a merkle tree root calculation.

   φ  Documentation: Mini-cryptography
   φ  Source-code: Github
   φ  Bug reports: Github

This library is suited for learning, but it is better not to use it for production.

Content

   φ  Dependencies
   φ  Installation
   φ  Uninstallation
   φ  User guide
      φ  ECDSA
         φ  Data for ECDSA examples
         φ  ECDSA examples
      φ  Merkle
         φ  Data for Merkle examples
         φ  Merkle examples
   φ  License

Dependencies

Mini-cryptography supports Python 3.7+.
Installation automatically includes the tinyec library.

Installation

The library can be installed from PyPI:

pip install mini-cryptography

Uninstallation

The library can be uninstalled using:

pip uninstall mini-cryptography

User guide

There are 2 main classes:
   φ  Ecdsa - has ECDSA arithmetic operations, including signature formation and verification;
   φ  Merkle - has a merkle tree root calculation.

Required libraries

from mini_cryptography import merkle
from mini_cryptography import ecdsa
import hashlib

ECDSA

Other required classes:
   φ  Point - ECDSA point that has x and y coordinates;
   φ  Field - describes the ECDSA field.

Data for ECDSA examples

Secp384r1 is 384-bit prime field Weierstrass curve. Also known as P-384 ansip384r1:

a = 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112316
b = 27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575
n = 39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643
p = 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319
G = ecdsa.Point(
    26247035095799689268623156744566981891852923491109213387815615900925518854738050089022388053975719786650872476732087,
    8325710961489029985546751289520108179287853048861315594709205902480503199884419224438643760392947333078086511627871
)
secp384r1 = ecdsa.Field(a, b, n, p, G)
curve = ecdsa.Ecdsa(field=secp384r1, name="secp384r1")

Points

point1 = ecdsa.Point(
    19577993669543055159462232654227477804059834554938749056365059575367343238573934152231932832497698572508881172084304,
    34797297628126597108728033628292920232095535295240081944254459873403593475466847089395925227525676205111687199013609
)
point2 = ecdsa.Point(
    2643888095364097454558349481745047911629089192351741699089972264282318601908091592262966275642198233545325090846186,
    12384549089646028340756024322986515983214437514151244063613237375835994573258040845173892755352541890195338888681840
)
ECDSA examples

Points sum

new_point = sum_points(point1, point2)
Result:
New point (
    22152009089199730593582524338115427010336291169893373839910753311913746007332469659451755453856401184556487920772225,
    21415530147108271193135517297779083081913015961082356748098427685923206883047231450346172563957532258197936273940105
)

Points multiplication

multiplier = 9868959070921577617284768940259093768032668379810297735137924030066340321810481073797782613683403119141615137083587
new_point = multiply_points(point1, multiplier)
Result: 
New point (
    14103764458811902000156928461250459647654661504776098395816220167714718139473397796549037360732342313833270939242263,
    17395148190829553535748807655250157906889415207238492158034708401150356646081290450883354819984464883347616139045011
)

Generate random private key [1, n-1]

privateKey = curve.private_key_generator()
Result: privateKey = 20989443543778090555157442102131049817299902423795685309899862760056430951462397686708870733055917820122718887042439

Multiplication of G (base) point from the given multiplier

public_key = curve.G_multiplication(privateKey) # or curve.calculate_public_key(privateKey)
Result: 
G(x, y) (
    30040694804942853208177610713088115928148181688856632998897580287365858436344609590182460206850552050293936278998346,
    17559245262757783022105893899857708160332511010412356224688036071313308531776780869864952047367968387454976435887533
)

Generate random k [1, ... n-1]

k = curve.k_generator()
Result: 11000

Sign message

message = 'Religio, Doctrina, Civilitas, prae omnibus Virtus'
hash = int(hashlib.sha1(message.encode()).hexdigest(),base=16)

r, s = curve.sign_message(privateKey, k, hash=hash)
Result:
Signature (
	r = 22152009089199730593582524338115427010336291169893373839910753311913746007332469659451755453856401184556487920772225, 
	s = 33247802217962351080804096577524498301009516670239406026864057032340769378746165513387841747729702616554540985061660
)

Verify signature validity

verification = curve.verify_signature(r, s, hash, public_key)
Result: True

Merkle

Data for Merkle examples
hashList = [
    '01000000295c297aee86096dcf6092',
    '0100000007bdc63ab3e74058a87b92',
    '01000000017b23260463311a4d1936',
    '0100000007bdc63ab3e74058a87b92'
]
Merkle examples

Merkle root calculation from transactions

merkle.Merkle().merkle_root(hashList, 0) #if 0 transactions are hashed, then 1 transactions are not hashed. 
Result: 'f3b5457f44b0a28a11ced653941ae1f2632b219f5d366a4167945eff0ed068a1'

Calculate transaction hash

merkle.Merkle().transaction_hash(hashList[0])
Result: b'3860b826dfc02feed1bbeb908eb0b2c0f5ea32a1b12ef1e8d87d2bf0e3802795'

License

GNU General Public License v3 or later (GPLv3+)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mini-cryptography-0.0.1.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mini_cryptography-0.0.1-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file mini-cryptography-0.0.1.tar.gz.

File metadata

  • Download URL: mini-cryptography-0.0.1.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for mini-cryptography-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a5ac950a0d3d66231fe5c69487381554c46ccab4598a59c15ecb7603b030b22b
MD5 7ff836d85a3de14ffbf44881db3af0ab
BLAKE2b-256 f2b37daec52bc25b84b3fe91dfa61811e170a4e1fb20c80ad43891d3485428b6

See more details on using hashes here.

File details

Details for the file mini_cryptography-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mini_cryptography-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa80cc327b777d70d189cad04a7e8690da0cd146f6868ffa03f8efa45d04e53d
MD5 9f9cd9fdb9c0557071233689336cbe98
BLAKE2b-256 2d37e3be89d646205ab735fd27a7d019bf0ec927cfe75858a1961e802b97db5e

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.2

2 files

This release

0.0.1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page