tget_per_commitment_secret_from_seed: small clean-up - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 3e899caaf4e5faa4ece823e8cd9f1f79769f0f84 DIR parent b523085fa33b35cf59288bb996a3f7fa2def25cd HTML Author: SomberNight <somber.night@protonmail.com> Date: Thu, 19 Apr 2018 20:09:44 +0200 get_per_commitment_secret_from_seed: small clean-up Diffstat: M lib/lnbase.py | 14 ++++++-------- M lib/tests/test_lnbase.py | 21 ++++++++++----------- 2 files changed, 16 insertions(+), 19 deletions(-) --- DIR diff --git a/lib/lnbase.py b/lib/lnbase.py t@@ -317,17 +317,15 @@ def derive_blinded_pubkey(basepoint, per_commitment_point): return point_to_ser(k1 + k2) -def get_per_commitment_secret_from_seed(seed: int, bits: int, i: int) -> int: +def get_per_commitment_secret_from_seed(seed: bytes, i: int, bits: int = 47) -> bytes: """Generate per commitment secret.""" - - per_commitment_secret = seed - for bitindex in range(bits, -1, -1): # 47, 46, ..., 0 + per_commitment_secret = bytearray(seed) + for bitindex in range(bits, -1, -1): mask = 1 << bitindex if i & mask: - pcs_bytes = bytearray(per_commitment_secret.to_bytes(byteorder="big", length=32)) - pcs_bytes[bitindex // 8] ^= 1 << (bitindex % 8) - per_commitment_secret = int.from_bytes(bitcoin.sha256(pcs_bytes), byteorder="big") - return per_commitment_secret + per_commitment_secret[bitindex // 8] ^= 1 << (bitindex % 8) + per_commitment_secret = bytearray(bitcoin.sha256(per_commitment_secret)) + return bytes(per_commitment_secret) def overall_weight(num_htlc): DIR diff --git a/lib/tests/test_lnbase.py b/lib/tests/test_lnbase.py t@@ -233,15 +233,14 @@ class Test_LNBase(unittest.TestCase): self.assertEqual(revocationpubkey, bfh('02916e326636d19c33f13e8c0c3a03dd157f332f3e99c317c141dd865eb01f8ff0')) def test_per_commitment_secret_from_seed(self): - self.assertEqual(bh2u(0x915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c.to_bytes(byteorder="big", length=32)), - bh2u(get_per_commitment_secret_from_seed(0x0101010101010101010101010101010101010101010101010101010101010101, 47, 1).to_bytes(byteorder="big", length=32))) - - self.assertEqual(bh2u(0x02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148.to_bytes(byteorder="big", length=32)), - bh2u(get_per_commitment_secret_from_seed(0x0000000000000000000000000000000000000000000000000000000000000000, 47, 281474976710655).to_bytes(byteorder="big", length=32))) - self.assertEqual(0x7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc, - get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 47, 281474976710655)) - self.assertEqual(0x56f4008fb007ca9acf0e15b054d5c9fd12ee06cea347914ddbaed70d1c13a528, - get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 47, 0xaaaaaaaaaaa)) - self.assertEqual(0x9015daaeb06dba4ccc05b91b2f73bd54405f2be9f217fbacd3c5ac2e62327d31, - get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 47, 0x555555555555)) + self.assertEqual(0x02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148.to_bytes(byteorder="big", length=32), + get_per_commitment_secret_from_seed(0x0000000000000000000000000000000000000000000000000000000000000000.to_bytes(byteorder="big", length=32), 281474976710655)) + self.assertEqual(0x7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc.to_bytes(byteorder="big", length=32), + get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.to_bytes(byteorder="big", length=32), 281474976710655)) + self.assertEqual(0x56f4008fb007ca9acf0e15b054d5c9fd12ee06cea347914ddbaed70d1c13a528.to_bytes(byteorder="big", length=32), + get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.to_bytes(byteorder="big", length=32), 0xaaaaaaaaaaa)) + self.assertEqual(0x9015daaeb06dba4ccc05b91b2f73bd54405f2be9f217fbacd3c5ac2e62327d31.to_bytes(byteorder="big", length=32), + get_per_commitment_secret_from_seed(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.to_bytes(byteorder="big", length=32), 0x555555555555)) + self.assertEqual(0x915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c.to_bytes(byteorder="big", length=32), + get_per_commitment_secret_from_seed(0x0101010101010101010101010101010101010101010101010101010101010101.to_bytes(byteorder="big", length=32), 1))