diff --git a/squeaknode/core/secret_keys.py b/squeaknode/core/secret_keys.py new file mode 100644 index 00000000..03c1659d --- /dev/null +++ b/squeaknode/core/secret_keys.py @@ -0,0 +1,48 @@ +# MIT License +# +# Copyright (c) 2020 Jonathan Zernik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +from squeak.core.elliptic import generate_random_scalar +from squeak.core.elliptic import scalar_difference +from squeak.core.elliptic import scalar_from_bytes +from squeak.core.elliptic import scalar_sum +from squeak.core.elliptic import scalar_to_bytes + + +DATA_KEY_LENGTH = 32 + + +def generate_tweak(): + tweak = generate_random_scalar() + return scalar_to_bytes(tweak) + + +def add_tweak(n, tweak): + n_int = scalar_from_bytes(n) + tweak_int = scalar_from_bytes(tweak) + sum_int = scalar_sum(n_int, tweak_int) + return scalar_to_bytes(sum_int) + + +def subtract_tweak(n, tweak): + n_int = scalar_from_bytes(n) + tweak_int = scalar_from_bytes(tweak) + sum_int = scalar_difference(n_int, tweak_int) + return scalar_to_bytes(sum_int) diff --git a/squeaknode/core/squeak_core.py b/squeaknode/core/squeak_core.py index 1e1497a1..4a54ab87 100644 --- a/squeaknode/core/squeak_core.py +++ b/squeaknode/core/squeak_core.py @@ -38,6 +38,9 @@ from squeaknode.core.peer_address import PeerAddress from squeaknode.core.received_offer import ReceivedOffer from squeaknode.core.received_payment import ReceivedPayment from squeaknode.core.received_payment_stream import ReceivedPaymentsStream +from squeaknode.core.secret_keys import add_tweak +from squeaknode.core.secret_keys import generate_tweak +from squeaknode.core.secret_keys import subtract_tweak from squeaknode.core.sent_offer import SentOffer from squeaknode.core.sent_payment import SentPayment from squeaknode.core.squeak_profile import SqueakProfile @@ -46,9 +49,6 @@ from squeaknode.core.squeaks import get_decrypted_content from squeaknode.core.squeaks import get_hash from squeaknode.core.squeaks import get_payment_point_of_secret_key from squeaknode.core.squeaks import make_squeak_with_block -from squeaknode.core.util import add_tweak -from squeaknode.core.util import generate_tweak -from squeaknode.core.util import subtract_tweak from squeaknode.lightning.lnd_lightning_client import LNDLightningClient diff --git a/squeaknode/core/util.py b/squeaknode/core/util.py index c4d19c73..741e49a8 100644 --- a/squeaknode/core/util.py +++ b/squeaknode/core/util.py @@ -23,11 +23,6 @@ import random from bitcoin.base58 import Base58ChecksumError from bitcoin.wallet import CBitcoinAddressError -from squeak.core.elliptic import generate_random_scalar -from squeak.core.elliptic import scalar_difference -from squeak.core.elliptic import scalar_from_bytes -from squeak.core.elliptic import scalar_sum -from squeak.core.elliptic import scalar_to_bytes from squeak.core.signing import CSqueakAddress @@ -46,25 +41,6 @@ def generate_ping_nonce() -> int: return random.SystemRandom().getrandbits(64) -def generate_tweak(): - tweak = generate_random_scalar() - return scalar_to_bytes(tweak) - - -def add_tweak(n, tweak): - n_int = scalar_from_bytes(n) - tweak_int = scalar_from_bytes(tweak) - sum_int = scalar_sum(n_int, tweak_int) - return scalar_to_bytes(sum_int) - - -def subtract_tweak(n, tweak): - n_int = scalar_from_bytes(n) - tweak_int = scalar_from_bytes(tweak) - sum_int = scalar_difference(n_int, tweak_int) - return scalar_to_bytes(sum_int) - - def is_address_valid(address: str) -> bool: if not address: return False diff --git a/tests/core/test_secret_keys.py b/tests/core/test_secret_keys.py new file mode 100644 index 00000000..aad793c9 --- /dev/null +++ b/tests/core/test_secret_keys.py @@ -0,0 +1,44 @@ +# MIT License +# +# Copyright (c) 2020 Jonathan Zernik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +import pytest +from squeak.core.elliptic import generate_secret_key + +from squeaknode.core.secret_keys import add_tweak +from squeaknode.core.secret_keys import generate_tweak +from squeaknode.core.secret_keys import subtract_tweak + + +@pytest.fixture +def secret_key(): + yield generate_secret_key() + + +@pytest.fixture +def tweak(): + yield generate_tweak() + + +def test_add_subtract_tweak(secret_key, tweak): + tweaked_secret_key = add_tweak(secret_key, tweak) + untweaked_secret_key = subtract_tweak(tweaked_secret_key, tweak) + + assert untweaked_secret_key == secret_key