Skip to content

Commit

Permalink
Made files consistent, added flake8 checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmysong committed Nov 8, 2018
1 parent f1ad35b commit 6393289
Show file tree
Hide file tree
Showing 49 changed files with 304 additions and 403 deletions.
2 changes: 1 addition & 1 deletion appa.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,7 @@ tx_outs = [TxOut(total-fee, target_script)]
# create a new transaction
tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
# sign the transaction
tx_obj.sign_input(0, private_key, SIGHASH_ALL)
tx_obj.sign_input(0, private_key)
# serialize and hex to see what it looks like
print(tx_obj.serialize().hex())
# send this signed transaction on the network
Expand Down
14 changes: 7 additions & 7 deletions code-ch01/1.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def mul(self, other):
def div(self, other):
if self.prime != other.prime:
raise TypeError
num = self.num * pow(other.num, self.prime-2, self.prime) % self.prime
num = self.num * pow(other.num, self.prime - 2, self.prime) % self.prime
return self.__class__(num, self.prime)

FieldElement.__ne__ = ne
FieldElement.__sub__ = sub
FieldElement.__mul__ = mul
FieldElement.__truediv__ = div

def test_example_1(self):
a = FieldElement(7, 13)
b = FieldElement(6, 13)
Expand All @@ -42,7 +42,7 @@ def test_example_1(self):
def test_example_2(self):
self.assertEqual(7 % 3, 1)
self.assertEqual(-27 % 13, 12)

def test_exercise_2(self):
prime = 57
self.assertEqual((44 + 33) % prime, 20)
Expand Down Expand Up @@ -83,10 +83,10 @@ def test_example_5(self):

def test_exercise_7(self):
for prime in (7, 11, 17, 31, 43):
self.assertEqual([pow(i, prime-1, prime) for i in range(1, prime)], [1]*(prime-1))
self.assertEqual([pow(i, prime - 1, prime) for i in range(1, prime)], [1] * (prime - 1))

def test_exercise_8(self):
prime = 31
self.assertEqual(3 * pow(24, prime-2, prime) % prime, 4)
self.assertEqual(pow(17, prime-4, prime), 29)
self.assertEqual(pow(4, prime-5, prime) * 11 % prime, 13)
self.assertEqual(3 * pow(24, prime - 2, prime) % prime, 4)
self.assertEqual(pow(17, prime - 4, prime), 29)
self.assertEqual(pow(4, prime - 5, prime) * 11 % prime, 13)
28 changes: 15 additions & 13 deletions code-ch02/2.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ def add(self, other):
Point.__add__ = add

def test_example_1(self):
p1 = Point(-1, -1, 5, 7)
Point(-1, -1, 5, 7)
with self.assertRaises(ValueError):
p2 = Point(-1, -2, 5, 7)
Point(-1, -2, 5, 7)

def test_exercise_1(self):

def on_curve(x, y):
return y**2 == x**3 + 5*x + 7
return y**2 == x**3 + 5 * x + 7

self.assertEqual(on_curve(2,4), False)
self.assertEqual(on_curve(-1,-1), True)
self.assertEqual(on_curve(18,77), True)
self.assertEqual(on_curve(5,7), False)
self.assertEqual(on_curve(2, 4), False)
self.assertEqual(on_curve(-1, -1), True)
self.assertEqual(on_curve(18, 77), True)
self.assertEqual(on_curve(5, 7), False)

def test_example_2(self):
p1 = Point(-1, -1, 5, 7)
Expand All @@ -58,14 +58,16 @@ def test_example_2(self):
self.assertEqual(p1 + inf, p1)
self.assertEqual(inf + p2, p2)
self.assertEqual(p1 + p2, inf)

def test_exercise_3(self):

def on_curve(x, y):
return y**2 == x**3 + 5*x + 7
self.assertEqual(on_curve(2,4), False)
self.assertEqual(on_curve(-1,-1), True)
self.assertEqual(on_curve(18,77), True)
self.assertEqual(on_curve(5,7), False)
return y**2 == x**3 + 5 * x + 7

self.assertEqual(on_curve(2, 4), False)
self.assertEqual(on_curve(-1, -1), True)
self.assertEqual(on_curve(18, 77), True)
self.assertEqual(on_curve(5, 7), False)

def test_exercise_4(self):
x1, y1 = 2, 5
Expand Down
2 changes: 1 addition & 1 deletion code-ch02/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __repr__(self):
if self.x is None:
return 'Point(infinity)'
else:
return 'Point({}, {})'.format(self.x, self.y)
return 'Point({},{})_{}_{}'.format(self.x, self.y, self.a, self.b)

def __add__(self, other):
if self.a != other.a or self.b != other.b:
Expand Down
102 changes: 53 additions & 49 deletions code-ch03/3.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_add(self):

def test_exercise_1(self):

def on_curve(x,y):
return y**2 == x**3 + a*x + b
def on_curve(x, y):
return y**2 == x**3 + a * x + b

prime = 223
a = FieldElement(0, prime)
Expand Down Expand Up @@ -100,21 +100,21 @@ def test_exercise_4(self):
b = FieldElement(7, prime)
x1 = FieldElement(num=192, prime=prime)
y1 = FieldElement(num=105, prime=prime)
p = Point(x1,y1,a,b)
p = Point(x1, y1, a, b)
p2 = p + p
self.assertEqual(p2.x.num, 49)
self.assertEqual(p2.y.num, 71)
self.assertEqual(p2.x.prime, prime)
x1 = FieldElement(num=143, prime=prime)
y1 = FieldElement(num=98, prime=prime)
p = Point(x1,y1,a,b)
p = Point(x1, y1, a, b)
p2 = p + p
self.assertEqual(p2.x.num, 64)
self.assertEqual(p2.y.num, 168)
self.assertEqual(p2.x.prime, prime)
x1 = FieldElement(num=47, prime=prime)
y1 = FieldElement(num=71, prime=prime)
p = Point(x1,y1,a,b)
p = Point(x1, y1, a, b)
p2 = p + p
self.assertEqual(p2.x.num, 36)
self.assertEqual(p2.y.num, 111)
Expand All @@ -123,11 +123,11 @@ def test_exercise_4(self):
self.assertEqual(p2.x.num, 194)
self.assertEqual(p2.y.num, 51)
self.assertEqual(p2.x.prime, prime)
p2 = p+p+p+p+p+p+p+p
p2 = p + p + p + p + p + p + p + p
self.assertEqual(p2.x.num, 116)
self.assertEqual(p2.y.num, 55)
self.assertEqual(p2.x.prime, prime)
p2 = p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p+p
p2 = p + p + p + p + p + p + p + p + p + p + p + p + p + p + p + p + p + p + p + p + p
self.assertEqual(p2.x, None)

def test_example_4(self):
Expand All @@ -139,29 +139,29 @@ def test_example_4(self):
p = Point(x, y, a, b)
want = (
(None, None),
(47,71),
(36,111),
(15,137),
(194,51),
(126,96),
(139,137),
(92,47),
(116,55),
(69,86),
(154,150),
(154,73),
(69,137),
(116,168),
(92,176),
(139,86),
(126,127),
(194,172),
(15,86),
(36,112),
(47,152),
(47, 71),
(36, 111),
(15, 137),
(194, 51),
(126, 96),
(139, 137),
(92, 47),
(116, 55),
(69, 86),
(154, 150),
(154, 73),
(69, 137),
(116, 168),
(92, 176),
(139, 86),
(126, 127),
(194, 172),
(15, 86),
(36, 112),
(47, 152),
)
for s in range(1, 21):
result = s*p
result = s * p
self.assertEqual((result.x.num, result.y.num), want[s])

def test_exercise_5(self):
Expand All @@ -186,7 +186,7 @@ def test_example_5(self):
x = FieldElement(15, prime)
y = FieldElement(86, prime)
p = Point(x, y, a, b)
p2 = 7*p
p2 = 7 * p
self.assertEqual(p2.x, None)

def test_example_6(self):
Expand All @@ -205,47 +205,47 @@ def test_example_7(self):
seven = FieldElement(7, p)
zero = FieldElement(0, p)
G = Point(x, y, zero, seven)
p2 = n*G
p2 = n * G
self.assertEqual(p2.x, None)

def test_example_8(self):
p = N*G
p = N * G
self.assertEqual(p.x, None)

def test_example_9(self):
z = 0xbc62d4b80d9e36da29c16c5d4d9f11731f36052c72401a76c23c0fb5a9b74423
r = 0x37206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c6
s = 0x8ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec
point = S256Point(0x04519fac3d910ca7e7138f7013706f619fa8f033e6ec6e09370ea38cee6a7574, 0x82b51eab8c27c66e26c858a079bcdf4f1ada34cec420cafc7eac1a42216fb6c4)
u = z * pow(s, N-2, N) % N
v = r * pow(s, N-2, N) % N
self.assertEqual((u*G + v*point).x.num, r)
u = z * pow(s, N - 2, N) % N
v = r * pow(s, N - 2, N) % N
self.assertEqual((u * G + v * point).x.num, r)

def test_exercise_6(self):
point = S256Point(
0x887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c,
0x887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c,
0x61de6d95231cd89026e286df3b6ae4a894a3378e393e93a0f45b666329a0ae34)
z = 0xec208baa0fc1c19f708a9ca96fdeff3ac3f230bb4a7ba4aede4942ad003c0f60
r = 0xac8d1c87e51d0d441be8b3dd5b05c8795b48875dffe00b7ffcfac23010d3a395
s = 0x68342ceff8935ededd102dd876ffd6ba72d6a427a3edb13d26eb0781cb423c4
u = z * pow(s, N-2, N) % N
v = r * pow(s, N-2, N) % N
self.assertEqual((u*G + v*point).x.num, r)
u = z * pow(s, N - 2, N) % N
v = r * pow(s, N - 2, N) % N
self.assertEqual((u * G + v * point).x.num, r)
z = 0x7c076ff316692a3d7eb3c3bb0f8b1488cf72e1afcd929e29307032997a838a3d
r = 0xeff69ef2b1bd93a66ed5219add4fb51e11a840f404876325a1e8ffe0529a2c
s = 0xc7207fee197d27c618aea621406f6bf5ef6fca38681d82b2f06fddbdce6feab6
u = z * pow(s, N-2, N) % N
v = r * pow(s, N-2, N) % N
self.assertEqual((u*G + v*point).x.num, r)
u = z * pow(s, N - 2, N) % N
v = r * pow(s, N - 2, N) % N
self.assertEqual((u * G + v * point).x.num, r)

def test_example_10(self):
e = int.from_bytes(hash256(b'my secret'), 'big')
z = int.from_bytes(hash256(b'my message'), 'big')
k = randint(0, N)
r = (k*G).x.num
k_inv = pow(k, N-2, N)
s = (z+r*e) * k_inv % N
point = e*G
r = (k * G).x.num
k_inv = pow(k, N - 2, N)
s = (z + r * e) * k_inv % N
point = e * G
self.assertEqual(
point.x.num,
0x028d003eab2e428d11983f3e97c3fa0addf3b42740df0d211795ffb3be2f6c52)
Expand All @@ -255,15 +255,17 @@ def test_example_10(self):
self.assertEqual(
z,
0x231c6f3d980a6b0fb7152f85cee7eb52bf92433d9919b9c5218cb08e79cce78)
self.assertTrue(r)
self.assertTrue(s)

def test_exercise_7(self):
e = 12345
z = int.from_bytes(hash256(b'Programming Bitcoin!'), 'big')
k = randint(0, N)
r = (k*G).x.num
k_inv = pow(k, N-2, N)
s = (z+r*e) * k_inv % N
point = e*G
r = (k * G).x.num
k_inv = pow(k, N - 2, N)
s = (z + r * e) * k_inv % N
point = e * G
self.assertEqual(
point.x.num,
0xf01d6b9018ab421dd410404cb869072065522bf85734008f105cf385a023a80f)
Expand All @@ -273,3 +275,5 @@ def test_exercise_7(self):
self.assertEqual(
z,
0x969f6056aa26f7d2795fd013fe88868d09c9f6aed96965016e1936ae47060d48)
self.assertTrue(r)
self.assertTrue(s)
2 changes: 1 addition & 1 deletion code-ch04/4.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_exercise_2(self):
def test_exercise_3(self):
r = 0x37206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c6
s = 0x8ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec
sig = Signature(r,s)
sig = Signature(r, s)
self.assertEqual(sig.der().hex(), '3045022037206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c60221008ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec')

def test_exercise_4(self):
Expand Down
2 changes: 1 addition & 1 deletion code-ch04/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def __repr__(self):
if self.x is None:
return 'Point(infinity)'
else:
return 'Point({},{})_{}'.format(self.x.num, self.y.num, self.x.prime)
return 'Point({},{})_{}_{}'.format(self.x, self.y, self.a, self.b)

def __add__(self, other):
if self.a != other.a or self.b != other.b:
Expand Down
4 changes: 2 additions & 2 deletions code-ch05/5.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def tx_in_parse(cls, s):
script_sig = Script.parse(s)
sequence = little_endian_to_int(s.read(4))
return cls(prev_tx, prev_index, script_sig, sequence)

@classmethod
def tx_out_parse(cls, s):
amount = little_endian_to_int(s.read(8))
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_exercise_5(self):
tx_obj = Tx.parse(stream)
self.assertEqual(
tx_obj.tx_ins[1].script_sig.instructions[0].hex(),
'304402207899531a52d59a6de200179928ca900254a36b8dff8bb75f5f5d71b1cdc26125022008b422690b8461cb52c3cc30330b23d574351872b7c361e9aae3649071c1a71601')
'304402207899531a52d59a6de200179928ca900254a36b8dff8bb75f5f5d71b1cdc26125022008b422690b8461cb52c3cc30330b23d574351872b7c361e9aae3649071c1a71601')
self.assertEqual(
tx_obj.tx_ins[1].script_sig.instructions[1].hex(),
'035d5c93d9ac96881f19ba1f686f15f009ded7c62efe85a872e6a19b43c15a2937')
Expand Down
2 changes: 1 addition & 1 deletion code-ch05/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def __repr__(self):
if self.x is None:
return 'Point(infinity)'
else:
return 'Point({},{})_{}'.format(self.x.num, self.y.num, self.x.prime)
return 'Point({},{})_{}_{}'.format(self.x, self.y, self.a, self.b)

def __add__(self, other):
if self.a != other.a or self.b != other.b:
Expand Down
4 changes: 2 additions & 2 deletions code-ch05/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def encode_num(num):
result.append(abs_num & 0xff)
abs_num >>= 8
# if the top bit is set,
# for negative numbers we ensure that the top bit is set by adding 0x80
# for positive numbers we ensure that the top bit is not set by adding 0x00
# for negative numbers we ensure that the top bit is set
# for positive numbers we ensure that the top bit is not set
if result[-1] & 0x80:
if negative:
result.append(0x80)
Expand Down
Loading

0 comments on commit 6393289

Please sign in to comment.