Skip to content

Commit 8c7b932

Browse files
committed
Pure python EC
This removes the dependency on OpenSSL for the interaction tests, by providing a pure-Python toy implementation of secp256k1.
1 parent 5983239 commit 8c7b932

File tree

5 files changed

+346
-227
lines changed

5 files changed

+346
-227
lines changed

test/functional/feature_assumevalid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import time
3333

3434
from test_framework.blocktools import (create_block, create_coinbase)
35-
from test_framework.key import CECKey
35+
from test_framework.key import ECKey
3636
from test_framework.messages import (
3737
CBlockHeader,
3838
COutPoint,
@@ -104,9 +104,9 @@ def run_test(self):
104104
self.blocks = []
105105

106106
# Get a pubkey for the coinbase TXO
107-
coinbase_key = CECKey()
108-
coinbase_key.set_secretbytes(b"horsebattery")
109-
coinbase_pubkey = coinbase_key.get_pubkey()
107+
coinbase_key = ECKey()
108+
coinbase_key.generate()
109+
coinbase_pubkey = coinbase_key.get_pubkey().get_bytes()
110110

111111
# Create the first block with a coinbase output to our key
112112
height = 1

test/functional/feature_block.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
get_legacy_sigopcount_block,
1515
MAX_BLOCK_SIGOPS,
1616
)
17-
from test_framework.key import CECKey
17+
from test_framework.key import ECKey
1818
from test_framework.messages import (
1919
CBlock,
2020
COIN,
@@ -86,9 +86,9 @@ def run_test(self):
8686
self.bootstrap_p2p() # Add one p2p connection to the node
8787

8888
self.block_heights = {}
89-
self.coinbase_key = CECKey()
90-
self.coinbase_key.set_secretbytes(b"horsebattery")
91-
self.coinbase_pubkey = self.coinbase_key.get_pubkey()
89+
self.coinbase_key = ECKey()
90+
self.coinbase_key.generate()
91+
self.coinbase_pubkey = self.coinbase_key.get_pubkey().get_bytes()
9292
self.tip = None
9393
self.blocks = {}
9494
self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16)
@@ -528,7 +528,7 @@ def run_test(self):
528528
tx.vin.append(CTxIn(COutPoint(b39.vtx[i].sha256, 0), b''))
529529
# Note: must pass the redeem_script (not p2sh_script) to the signature hash function
530530
(sighash, err) = SignatureHash(redeem_script, tx, 1, SIGHASH_ALL)
531-
sig = self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))
531+
sig = self.coinbase_key.sign_ecdsa(sighash) + bytes(bytearray([SIGHASH_ALL]))
532532
scriptSig = CScript([sig, redeem_script])
533533

534534
tx.vin[1].scriptSig = scriptSig
@@ -1284,7 +1284,7 @@ def sign_tx(self, tx, spend_tx):
12841284
tx.vin[0].scriptSig = CScript()
12851285
return
12861286
(sighash, err) = SignatureHash(spend_tx.vout[0].scriptPubKey, tx, 0, SIGHASH_ALL)
1287-
tx.vin[0].scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))])
1287+
tx.vin[0].scriptSig = CScript([self.coinbase_key.sign_ecdsa(sighash) + bytes(bytearray([SIGHASH_ALL]))])
12881288

12891289
def create_and_sign_transaction(self, spend_tx, value, script=CScript([OP_TRUE])):
12901290
tx = self.create_tx(spend_tx, 0, value, script)

test/functional/p2p_segwit.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import time
1010

1111
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER
12-
from test_framework.key import CECKey, CPubKey
12+
from test_framework.key import ECKey
1313
from test_framework.messages import (
1414
BIP125_SEQUENCE_NUMBER,
1515
CBlock,
@@ -100,7 +100,7 @@ def get_p2pkh_script(pubkeyhash):
100100
def sign_p2pk_witness_input(script, tx_to, in_idx, hashtype, value, key):
101101
"""Add signature for a P2PK witness program."""
102102
tx_hash = SegwitVersion1SignatureHash(script, tx_to, in_idx, hashtype, value)
103-
signature = key.sign(tx_hash) + chr(hashtype).encode('latin-1')
103+
signature = key.sign_ecdsa(tx_hash) + chr(hashtype).encode('latin-1')
104104
tx_to.wit.vtxinwit[in_idx].scriptWitness.stack = [signature, script]
105105
tx_to.rehash()
106106

@@ -1479,10 +1479,9 @@ def test_uncompressed_pubkey(self):
14791479

14801480
# Segwit transactions using uncompressed pubkeys are not accepted
14811481
# under default policy, but should still pass consensus.
1482-
key = CECKey()
1483-
key.set_secretbytes(b"9")
1484-
key.set_compressed(False)
1485-
pubkey = CPubKey(key.get_pubkey())
1482+
key = ECKey()
1483+
key.generate(False)
1484+
pubkey = key.get_pubkey().get_bytes()
14861485
assert_equal(len(pubkey), 65) # This should be an uncompressed pubkey
14871486

14881487
utxo = self.utxo.pop(0)
@@ -1512,7 +1511,7 @@ def test_uncompressed_pubkey(self):
15121511
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_wsh))
15131512
script = get_p2pkh_script(pubkeyhash)
15141513
sig_hash = SegwitVersion1SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
1515-
signature = key.sign(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
1514+
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
15161515
tx2.wit.vtxinwit.append(CTxInWitness())
15171516
tx2.wit.vtxinwit[0].scriptWitness.stack = [signature, pubkey]
15181517
tx2.rehash()
@@ -1566,7 +1565,7 @@ def test_uncompressed_pubkey(self):
15661565
tx5.vin.append(CTxIn(COutPoint(tx4.sha256, 0), b""))
15671566
tx5.vout.append(CTxOut(tx4.vout[0].nValue - 1000, CScript([OP_TRUE])))
15681567
(sig_hash, err) = SignatureHash(script_pubkey, tx5, 0, SIGHASH_ALL)
1569-
signature = key.sign(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
1568+
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
15701569
tx5.vin[0].scriptSig = CScript([signature, pubkey])
15711570
tx5.rehash()
15721571
# Should pass policy and consensus.
@@ -1579,9 +1578,9 @@ def test_uncompressed_pubkey(self):
15791578
@subtest
15801579
def test_signature_version_1(self):
15811580

1582-
key = CECKey()
1583-
key.set_secretbytes(b"9")
1584-
pubkey = CPubKey(key.get_pubkey())
1581+
key = ECKey()
1582+
key.generate()
1583+
pubkey = key.get_pubkey().get_bytes()
15851584

15861585
witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
15871586
witness_hash = sha256(witness_program)
@@ -1716,7 +1715,7 @@ def test_signature_version_1(self):
17161715

17171716
script = get_p2pkh_script(pubkeyhash)
17181717
sig_hash = SegwitVersion1SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
1719-
signature = key.sign(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
1718+
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
17201719

17211720
# Check that we can't have a scriptSig
17221721
tx2.vin[0].scriptSig = CScript([signature, pubkey])

0 commit comments

Comments
 (0)