-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmimblewimble_chain_test.py
More file actions
executable file
·170 lines (142 loc) · 7.4 KB
/
mimblewimble_chain_test.py
File metadata and controls
executable file
·170 lines (142 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /usr/bin/env nix-shell
#! nix-shell -i python2 -p "with python2Packages; [python]"
from mimblewimble_chain import *
import unittest
# Deterministic tests for the moment
seed = 70
random.seed(seed)
class GeneratorTests(unittest.TestCase):
"""Tests the used G, H values for being generator."""
def test_commutitative_G_G(self):
p1 = secp256k1.plus(G.scalarMul(5), G.scalarMul(7))
p2 = secp256k1.plus(G.scalarMul(7), G.scalarMul(5))
self.assertEqual(p1, p2)
def test_commutitative_G_H(self):
p1 = secp256k1.plus(G.scalarMul(5), H.scalarMul(7))
p2 = secp256k1.plus(H.scalarMul(7), G.scalarMul(5))
self.assertEqual(p1, p2)
def test_scalar_G(self):
p1 = secp256k1.plus(G.scalarMul(5), G.scalarMul(7))
p2 = G.scalarMul(12)
self.assertEqual(p1, p2)
def test_scalar_H(self):
p1 = secp256k1.plus(H.scalarMul(5), H.scalarMul(7))
p2 = H.scalarMul(12)
self.assertEqual(p1, p2)
def test_full_cross_G_H(self):
p11 = secp256k1.plus(G.scalarMul(5), H.scalarMul(7))
p12 = secp256k1.plus(G.scalarMul(11), H.scalarMul(13))
p1 = secp256k1.plus(p11, p12)
p2 = secp256k1.plus(G.scalarMul(16), H.scalarMul(20))
self.assertEqual(p1, p2)
def test_cancel_out_G_H(self):
p11 = secp256k1.plus(G.scalarMul(5), H.scalarMul(7))
p12 = H.scalarMul(7).plusInv()
p1 = secp256k1.plus(p11, p12)
p2 = secp256k1.plus(G.scalarMul(5), H.scalarMul(0))
self.assertEqual(p1, p2)
# assert H.scalarMul(excess_add.plusInv().toInt()) == H.scalarMul(excess_add.toInt()).plusInv()
self.assertTrue(secp256k1.plus(G.scalarMul(100).plusInv(), G.scalarMul(100)).isPlusID())
def test_owned_output(self):
self.assertEqual(G.scalarMul(100), OwnedOutput(100, Signature.nF.make(0)).blind())
self.assertEqual(G.scalarMul(100).plusInv(), OwnedOutput(100, Signature.nF.make(0)).blind().plusInv())
excess_add = Signature.gen_private_key()
self.assertEqual(H.scalarMul(int(excess_add.plusInv())), H.scalarMul(int(excess_add)).plusInv())
class TestFoo(unittest.TestCase):
def setUp(self):
genesis_output = OwnedOutput.generate(1000)
self.genesis_tx = Transaction([], [genesis_output.blind()], None, None)
self.c = Chain(genesis_output.blind())
self.satoshi = Actor([genesis_output], self.c)
self.clemens = Actor([], self.c)
def test_ping_pong(self):
"""Ping-pongs a few coins between satoshi and clemens."""
self.assertEqual(self.satoshi.coins_owned(), 1000)
self.c.process_tx(self.clemens.receive(self.satoshi.send(100)))
self.assertEqual(self.satoshi.coins_owned(), 900)
self.assertEqual(self.clemens.coins_owned(), 100)
with self.assertRaises(ValueError):
self.c.process_tx(self.satoshi.receive(self.clemens.send(150)))
self.assertEqual(self.satoshi.coins_owned(), 900)
self.assertEqual(self.clemens.coins_owned(), 100)
self.c.process_tx(self.clemens.receive(self.satoshi.send(150)))
self.assertEqual(self.clemens.coins_owned(), 250)
self.assertEqual(self.satoshi.coins_owned(), 750)
with self.assertRaises(ValueError):
self.c.process_tx(self.clemens.receive(self.satoshi.send(1050)))
self.assertEqual(self.clemens.coins_owned(), 250)
self.assertEqual(self.satoshi.coins_owned(), 750)
self.c.process_tx(self.satoshi.receive(self.clemens.send(250)))
self.assertEqual(self.clemens.coins_owned(), 0)
self.assertEqual(self.satoshi.coins_owned(), 1000)
# FIXME add malicious receivers as discussed in Actor.receive comments.
def xtest_input_txid_error(self):
tx = Transaction([], [])
s_to_c = Transaction(
[Input(tx_id=tx.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=900),
Output(pub_key=self.clemens_pub, amount=100)])
with self.assertRaises(InputReferenceError):
self.c.process_tx(s_to_c, s_to_c.make_witness([self.satoshi_priv]))
def xtest_input_index_error(self):
s_to_c = Transaction(
[Input(tx_id=self.genesis_tx.tx_id(), index=1)],
[Output(pub_key=self.satoshi_pub, amount=900),
Output(pub_key=self.clemens_pub, amount=100)])
with self.assertRaises(InputReferenceError):
self.c.process_tx(s_to_c, s_to_c.make_witness([self.satoshi_priv]))
def xtest_double_spend_tx(self):
"""Tests double spending an output with two txs."""
s_to_c = Transaction(
[Input(tx_id=self.genesis_tx.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=900),
Output(pub_key=self.clemens_pub, amount=100)])
self.c.process_tx(s_to_c, s_to_c.make_witness([self.satoshi_priv]))
with self.assertRaises(InputReferenceError):
self.c.process_tx(s_to_c, s_to_c.make_witness([self.satoshi_priv]))
def xtest_double_spend_input(self):
"""Tests double spending an output within the same tx by making it an input twice."""
tx = Transaction(
[Input(tx_id=self.genesis_tx.tx_id(), index=0),
Input(tx_id=self.genesis_tx.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=2000)])
with self.assertRaises(InputReferenceError):
self.c.process_tx(tx, tx.make_witness([self.satoshi_priv, self.satoshi_priv]))
def xtest_split_and_combine(self):
# tx1: Satoshi sends 100 coins to clemens, and takes 900 into a change output.
# tx2: Satoshi moves the 900 change output one more time.
# tx3: Clemens & Satoshi recombine 100 + 900 into the genesis like 1000.
s_to_c = Transaction(
[Input(tx_id=self.genesis_tx.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=900),
Output(pub_key=self.clemens_pub, amount=100)])
s_to_s = Transaction(
[Input(tx_id=s_to_c.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=900)])
self.c.process_tx(s_to_c, s_to_c.make_witness([self.satoshi_priv]))
# Spend the new satoshi output
self.c.process_tx(s_to_s, s_to_s.make_witness([self.satoshi_priv]))
recombine = Transaction(
[Input(tx_id=s_to_c.tx_id(), index=1),
Input(tx_id=s_to_s.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=1000)])
self.c.process_tx(recombine, recombine.make_witness([self.clemens_priv, self.satoshi_priv]))
def xtest_amounts(self):
# Check against amount exceeds
s_to_c = Transaction(
[Input(tx_id=self.genesis_tx.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=900),
Output(pub_key=self.clemens_pub, amount=1000)])
with self.assertRaises(ValueError):
self.c.process_tx(s_to_c, s_to_c.make_witness([self.satoshi_priv]))
def xtest_wrong_sigs(self):
# Check against wrong signatures
# Sign with clemens' key instead of satoshi's.
s_to_c = Transaction(
[Input(tx_id=self.genesis_tx.tx_id(), index=0)],
[Output(pub_key=self.satoshi_pub, amount=900),
Output(pub_key=self.clemens_pub, amount=100)])
with self.assertRaises(BadSignatureError):
self.c.process_tx(s_to_c, s_to_c.make_witness([self.clemens_priv]))
if __name__ == '__main__':
unittest.main()