Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal precommit setup since it's enforced in CI #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .isort.cfg

This file was deleted.

63 changes: 63 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Skip execution of one or more hooks using the SKIP environment variable:
# $ SKIP=pylint git commit -m "foo"
# $ SKIP=mypy,pylint pre-commit run --all-files
#
# If want to disable all hooks while committing, use the --no-verify/-n option:
# $ git commit -n -m "foo"

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: mixed-line-ending
- id: check-case-conflict
# - id: check-executables-have-shebangs
# - id: check-shebang-scripts-are-executable
# - id: fix-byte-order-marker
# - id: fix-encoding-pragma
- id: check-yaml
- id: check-json
# - id: pretty-format-json
- id: check-merge-conflict
- id: check-vcs-permalinks
- id: detect-private-key
- id: debug-statements
- id: requirements-txt-fixer
#- id: check-docstring-first
# - id: name-tests-test
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (black profile, in place fixes)
args: ["--profile", "black", "--filter-files"]
language: python
types: [python]
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
language: python
types: [python]
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
name: black (in place fixes)
# args: [--diff, --check]
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
# language_version: python3.11
language: python
types: [python]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies: [types-requests]
language: python
types: [python]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Bitcoin and Blockchain Technology

University of Milano-Bicocca
Politecnico di Milano
University of Milano-Bicocca
Politecnico di Milano
University of Milano

The latest version of the course slides is available from the course page at <http://www.ametrano.net/bbt/>.
Expand Down
2 changes: 1 addition & 1 deletion ipynb/PartialHashInversion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion ipynb/field_table.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -753,4 +753,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
7 changes: 3 additions & 4 deletions lab-tutorial/01.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To generate the private key on [bitaddress.org](http://bitaddress.org), we move

[![bitaddress wallet](image/bitaddress_end.png)](http://bitaddress.org)

The private key is the alphanumerical string in the bottom-right of the box, in this example:
The private key is the alphanumerical string in the bottom-right of the box, in this example:

`Kx6XvUyToSE1o9eWmVimdyQhrp8ANCM2HGUhXxdNUsrroNnGaRNa`

Expand All @@ -35,7 +35,7 @@ We can now go to the "addresses" tab and see our Bitcoin address, that must be t
![electrum addresses](image/electrum_addresses_screen.png)

## Sign a message using Electrum
We are now ready to sign a message using our new Bitcoin wallet.
We are now ready to sign a message using our new Bitcoin wallet.
To sign a message we go to "Instruments -> Sign/Verify message" and we insert the following data:
- the message, for example here we will use `"My first digital signature"`
- the Bitcoin address associated to our private key, in this example: `13QGCc9EBUtLCTbxDXjNok66hhWgN4SHv7`
Expand Down Expand Up @@ -64,7 +64,7 @@ If we click on "Verify" we obtain:

![electrum start](image/electrum_signverify_screen_4.png)

Please note that if we changed even a single character in the message, the digital signature is no longer valid.
Please note that if we changed even a single character in the message, the digital signature is no longer valid.

For example we remove "!" in the message:

Expand All @@ -82,4 +82,3 @@ The scope of this exercise is to check the transaction history and the balance o
We can even search the transaction history of the previously created Bitcoin address, in this example `13QGCc9EBUtLCTbxDXjNok66hhWgN4SHv7` . Since it is a new address, the transactions history is empty and the balance is equal to 0:

![electrum start](image/esplora_search_1.png)

5 changes: 2 additions & 3 deletions py-scripts/bms_example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

from btclib.b58 import p2pkh, p2wpkh_p2sh
from btclib.b58 import wif_from_prv_key
from btclib.b32 import p2wpkh
from btclib.b58 import p2pkh, p2wpkh_p2sh, wif_from_prv_key
from btclib.ecc.bms import sign, verify
from btclib.to_prv_key import prv_keyinfo_from_prv_key
from btclib.to_pub_key import pub_keyinfo_from_prv_key

msg = "Paolo is afraid of ephemeral random numbers".encode()
msg = b"Paolo is afraid of ephemeral random numbers"
print("\n0. Message:", msg.decode())

wif = b"Kx45GeUBSMPReYQwgXiKhG9FzNXrnCeutJp4yjTd5kKxCitadm3C"
Expand Down
72 changes: 35 additions & 37 deletions py-scripts/curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""Elliptic curves.

* SEC 2 v.2 curves
Expand All @@ -20,7 +19,6 @@
* Brainpool_params standard curves
https://tools.ietf.org/html/rfc5639
* test curves with very low cardinality

"""

# scroll down at the end of the file for 'relevant' code
Expand All @@ -35,7 +33,7 @@
# http://www.secg.org/SEC2-Ver-1.0.pdf
SEC2v1_params = {}

p = (2 ** 128 - 3) // 76439
p = (2**128 - 3) // 76439
a = 0xDB7C2ABF62E35E668076BEAD2088
b = 0x659EF8BA043916EEDE8911702B22
Gx = 0x09487239995A5EE76B55F9C2F098
Expand All @@ -44,7 +42,7 @@
h = 1
SEC2v1_params["secp112r1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = (2 ** 128 - 3) // 76439
p = (2**128 - 3) // 76439
a = 0x6127C24C05F38A0AAAF65C0EF02C
b = 0x51DEF1815DB5ED74FCC34C85D709
Gx = 0x4BA30AB5E892B4E1649DD0928643
Expand All @@ -53,7 +51,7 @@
h = 4
SEC2v1_params["secp112r2"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 128 - 2 ** 97 - 1
p = 2**128 - 2**97 - 1
a = 0xFFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC
b = 0xE87579C11079F43DD824993C2CEE5ED3
Gx = 0x161FF7528B899B2D0C28607CA52C5B86
Expand All @@ -62,7 +60,7 @@
h = 1
SEC2v1_params["secp128r1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 128 - 2 ** 97 - 1
p = 2**128 - 2**97 - 1
a = 0xD6031998D1B3BBFEBF59CC9BBFF9AEE1
b = 0x5EEEFCA380D02919DC2C6558BB6D8A5D
Gx = 0x7B6AA5D85E572983E6FB32A7CDEBC140
Expand All @@ -72,15 +70,15 @@
SEC2v1_params["secp128r2"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = (
2 ** 160
- 2 ** 32
- 2 ** 14
- 2 ** 12
- 2 ** 9
- 2 ** 8
- 2 ** 7
- 2 ** 3
- 2 ** 2
2**160
- 2**32
- 2**14
- 2**12
- 2**9
- 2**8
- 2**7
- 2**3
- 2**2
- 1
)
a = 0x0000000000000000000000000000000000000000
Expand All @@ -91,7 +89,7 @@
h = 1
SEC2v1_params["secp160k1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 160 - 2 ** 31 - 1
p = 2**160 - 2**31 - 1
a = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC
b = 0x1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45
Gx = 0x4A96B5688EF573284664698968C38BB913CBFC82
Expand All @@ -101,15 +99,15 @@
SEC2v1_params["secp160r1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = (
2 ** 160
- 2 ** 32
- 2 ** 14
- 2 ** 12
- 2 ** 9
- 2 ** 8
- 2 ** 7
- 2 ** 3
- 2 ** 2
2**160
- 2**32
- 2**14
- 2**12
- 2**9
- 2**8
- 2**7
- 2**3
- 2**2
- 1
)
a = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC70
Expand All @@ -128,7 +126,7 @@
# http://www.secg.org/sec2-v2.pdf
SEC2v2_params = {}

p = 2 ** 192 - 2 ** 32 - 2 ** 12 - 2 ** 8 - 2 ** 7 - 2 ** 6 - 2 ** 3 - 1
p = 2**192 - 2**32 - 2**12 - 2**8 - 2**7 - 2**6 - 2**3 - 1
a = 0
b = 3
Gx = 0xDB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D
Expand All @@ -137,7 +135,7 @@
h = 1
SEC2v2_params["secp192k1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 192 - 2 ** 64 - 1
p = 2**192 - 2**64 - 1
a = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC
b = 0x64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1
Gx = 0x188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012
Expand All @@ -146,7 +144,7 @@
h = 1
SEC2v2_params["secp192r1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 224 - 2 ** 32 - 2 ** 12 - 2 ** 11 - 2 ** 9 - 2 ** 7 - 2 ** 4 - 2 - 1
p = 2**224 - 2**32 - 2**12 - 2**11 - 2**9 - 2**7 - 2**4 - 2 - 1
a = 0
b = 5
Gx = 0xA1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C
Expand All @@ -155,7 +153,7 @@
h = 1
SEC2v2_params["secp224k1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 224 - 2 ** 96 + 1
p = 2**224 - 2**96 + 1
a = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE
b = 0xB4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4
Gx = 0xB70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21
Expand All @@ -165,7 +163,7 @@
SEC2v2_params["secp224r1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

# bitcoin curve
p = 2 ** 256 - 2 ** 32 - 977
p = 2**256 - 2**32 - 977
a = 0
b = 7
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
Expand All @@ -174,7 +172,7 @@
h = 1
SEC2v2_params["secp256k1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 256 - 2 ** 224 + 2 ** 192 + 2 ** 96 - 1
p = 2**256 - 2**224 + 2**192 + 2**96 - 1
a = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC
b = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B
Gx = 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296
Expand All @@ -183,7 +181,7 @@
h = 1
SEC2v2_params["secp256r1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 384 - 2 ** 128 - 2 ** 96 + 2 ** 32 - 1
p = 2**384 - 2**128 - 2**96 + 2**32 - 1
a = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC
b = 0xB3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF
Gx = 0xAA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7
Expand All @@ -192,7 +190,7 @@
h = 1
SEC2v2_params["secp384r1"] = [hex(p), hex(a), hex(b), [hex(Gx), hex(Gy)], hex(n), h]

p = 2 ** 521 - 1
p = 2**521 - 1
a = 0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC
b = 0x0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00
Gx = 0x00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66
Expand Down Expand Up @@ -399,21 +397,21 @@

if __name__ == "__main__":
filename = path.join(datadir, "ec_Brainpool.json")
with open(filename, "r") as file_:
with open(filename) as file_:
Brainpool_params2 = json.load(file_)
assert Brainpool_params == Brainpool_params2

filename = path.join(datadir, "ec_NIST.json")
with open(filename, "r") as file_:
with open(filename) as file_:
NIST_params2 = json.load(file_)
assert NIST_params == NIST_params2

filename = path.join(datadir, "ec_SEC2v1_insecure.json")
with open(filename, "r") as file_:
with open(filename) as file_:
SEC2v1_params2 = json.load(file_)
assert SEC2v1_params == SEC2v1_params2

filename = path.join(datadir, "ec_SEC2v2.json")
with open(filename, "r") as file_:
with open(filename) as file_:
SEC2v2_params2 = json.load(file_)
assert SEC2v2_params == SEC2v2_params2
3 changes: 1 addition & 2 deletions py-scripts/det_keychain_type1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

""" Deterministic Key Sequence (Type-1)"""
"""Deterministic Key Sequence (Type-1)."""

import secrets
from hashlib import sha256 as hf
Expand Down
3 changes: 1 addition & 2 deletions py-scripts/det_keychain_type2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

""" Deterministic Key Sequence (Type-1)"""
"""Deterministic Key Sequence (Type-1)."""

import secrets
from hashlib import sha256 as hf
Expand Down
2 changes: 1 addition & 1 deletion py-scripts/diffie_hellman.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from hashlib import sha256 as hf

from btclib import dsa
from btclib.dh import ansi_x9_63_kdf
from btclib.ecc.curve import mult
from btclib.ecc.curve import secp256k1 as ec
from btclib.dh import ansi_x9_63_kdf

# Diffie-Hellman
print("\n Diffie-Hellman")
Expand Down
Loading