mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-20 18:44:20 +01:00
Merge pull request #12 from callebtc/notation/alice-to-bob
Notation/alice to bob
This commit is contained in:
@@ -6,7 +6,7 @@ Alice:
|
|||||||
A = a*G
|
A = a*G
|
||||||
return A
|
return A
|
||||||
Bob:
|
Bob:
|
||||||
Y = hash_to_curve(secret_message)
|
Y = hash_to_point(secret_message)
|
||||||
r = random blinding factor
|
r = random blinding factor
|
||||||
B'= Y + r*G
|
B'= Y + r*G
|
||||||
return B'
|
return B'
|
||||||
@@ -20,7 +20,7 @@ C = C' - r*A
|
|||||||
(= a*Y)
|
(= a*Y)
|
||||||
return C, secret_message
|
return C, secret_message
|
||||||
Alice:
|
Alice:
|
||||||
Y = hash_to_curve(secret_message)
|
Y = hash_to_point(secret_message)
|
||||||
C == a*Y
|
C == a*Y
|
||||||
If true, C must have originated from Alice
|
If true, C must have originated from Alice
|
||||||
"""
|
"""
|
||||||
@@ -30,7 +30,7 @@ import hashlib
|
|||||||
from secp256k1 import PrivateKey, PublicKey
|
from secp256k1 import PrivateKey, PublicKey
|
||||||
|
|
||||||
|
|
||||||
def hash_to_curve(secret_msg):
|
def hash_to_point(secret_msg):
|
||||||
"""Generates x coordinate from the message hash and checks if the point lies on the curve.
|
"""Generates x coordinate from the message hash and checks if the point lies on the curve.
|
||||||
If it does not, it tries computing again a new x coordinate from the hash of the coordinate."""
|
If it does not, it tries computing again a new x coordinate from the hash of the coordinate."""
|
||||||
point = None
|
point = None
|
||||||
@@ -49,26 +49,26 @@ def hash_to_curve(secret_msg):
|
|||||||
return point
|
return point
|
||||||
|
|
||||||
|
|
||||||
def step1_bob(secret_msg):
|
def step1_alice(secret_msg):
|
||||||
secret_msg = secret_msg.encode("utf-8")
|
secret_msg = secret_msg.encode("utf-8")
|
||||||
Y = hash_to_curve(secret_msg)
|
Y = hash_to_point(secret_msg)
|
||||||
r = PrivateKey()
|
r = PrivateKey()
|
||||||
B_ = Y + r.pubkey
|
B_ = Y + r.pubkey
|
||||||
return B_, r
|
return B_, r
|
||||||
|
|
||||||
|
|
||||||
def step2_alice(B_, a):
|
def step2_bob(B_, a):
|
||||||
C_ = B_.mult(a)
|
C_ = B_.mult(a)
|
||||||
return C_
|
return C_
|
||||||
|
|
||||||
|
|
||||||
def step3_bob(C_, r, A):
|
def step3_alice(C_, r, A):
|
||||||
C = C_ - A.mult(r)
|
C = C_ - A.mult(r)
|
||||||
return C
|
return C
|
||||||
|
|
||||||
|
|
||||||
def verify(a, C, secret_msg):
|
def verify(a, C, secret_msg):
|
||||||
Y = hash_to_curve(secret_msg.encode("utf-8"))
|
Y = hash_to_point(secret_msg.encode("utf-8"))
|
||||||
return C == Y.mult(a)
|
return C == Y.mult(a)
|
||||||
|
|
||||||
|
|
||||||
@@ -78,9 +78,9 @@ def verify(a, C, secret_msg):
|
|||||||
# a = PrivateKey()
|
# a = PrivateKey()
|
||||||
# A = a.pubkey
|
# A = a.pubkey
|
||||||
# secret_msg = "test"
|
# secret_msg = "test"
|
||||||
# B_, r = step1_bob(secret_msg)
|
# B_, r = step1_alice(secret_msg)
|
||||||
# C_ = step2_alice(B_, a)
|
# C_ = step2_bob(B_, a)
|
||||||
# C = step3_bob(C_, r, A)
|
# C = step3_alice(C_, r, A)
|
||||||
# print("C:{}, secret_msg:{}".format(C, secret_msg))
|
# print("C:{}, secret_msg:{}".format(C, secret_msg))
|
||||||
# assert verify(a, C, secret_msg)
|
# assert verify(a, C, secret_msg)
|
||||||
# assert verify(a, C + C, secret_msg) == False # adding C twice shouldn't pass
|
# assert verify(a, C + C, secret_msg) == False # adding C twice shouldn't pass
|
||||||
|
|||||||
@@ -13,9 +13,14 @@ from core.secp import PrivateKey, PublicKey
|
|||||||
from core.settings import LIGHTNING, MAX_ORDER
|
from core.settings import LIGHTNING, MAX_ORDER
|
||||||
from core.split import amount_split
|
from core.split import amount_split
|
||||||
from lightning import WALLET
|
from lightning import WALLET
|
||||||
from mint.crud import (get_lightning_invoice, get_proofs_used,
|
from mint.crud import (
|
||||||
invalidate_proof, store_lightning_invoice,
|
get_lightning_invoice,
|
||||||
store_promise, update_lightning_invoice)
|
get_proofs_used,
|
||||||
|
invalidate_proof,
|
||||||
|
store_lightning_invoice,
|
||||||
|
store_promise,
|
||||||
|
update_lightning_invoice,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Ledger:
|
class Ledger:
|
||||||
@@ -58,7 +63,7 @@ class Ledger:
|
|||||||
async def _generate_promise(self, amount: int, B_: PublicKey):
|
async def _generate_promise(self, amount: int, B_: PublicKey):
|
||||||
"""Generates a promise for given amount and returns a pair (amount, C')."""
|
"""Generates a promise for given amount and returns a pair (amount, C')."""
|
||||||
secret_key = self.keys[amount] # Get the correct key
|
secret_key = self.keys[amount] # Get the correct key
|
||||||
C_ = b_dhke.step2_alice(B_, secret_key)
|
C_ = b_dhke.step2_bob(B_, secret_key)
|
||||||
await store_promise(
|
await store_promise(
|
||||||
amount, B_=B_.serialize().hex(), C_=C_.serialize().hex(), db=self.db
|
amount, B_=B_.serialize().hex(), C_=C_.serialize().hex(), db=self.db
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "cashu"
|
name = "cashu"
|
||||||
version = "0.1.8"
|
version = "0.1.9"
|
||||||
description = "Ecash wallet and mint."
|
description = "Ecash wallet and mint."
|
||||||
authors = ["calle <callebtc@protonmail.com>"]
|
authors = ["calle <callebtc@protonmail.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -13,7 +13,7 @@ entry_points = {"console_scripts": ["cashu = wallet.cashu:cli"]}
|
|||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="cashu",
|
name="cashu",
|
||||||
version="0.1.8",
|
version="0.1.9",
|
||||||
description="Ecash wallet and mint with Bitcoin Lightning support",
|
description="Ecash wallet and mint with Bitcoin Lightning support",
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ async def burn(ctx, token: str, all: bool, force: bool):
|
|||||||
if all:
|
if all:
|
||||||
# check only those who are flagged as reserved
|
# check only those who are flagged as reserved
|
||||||
proofs = await get_reserved_proofs(wallet.db)
|
proofs = await get_reserved_proofs(wallet.db)
|
||||||
if force:
|
elif force:
|
||||||
# check all proofs in db
|
# check all proofs in db
|
||||||
proofs = wallet.proofs
|
proofs = wallet.proofs
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class LedgerAPI:
|
|||||||
proofs = []
|
proofs = []
|
||||||
for promise, (r, secret) in zip(promises, secrets):
|
for promise, (r, secret) in zip(promises, secrets):
|
||||||
C_ = PublicKey(bytes.fromhex(promise.C_), raw=True)
|
C_ = PublicKey(bytes.fromhex(promise.C_), raw=True)
|
||||||
C = b_dhke.step3_bob(C_, r, self.keys[promise.amount])
|
C = b_dhke.step3_alice(C_, r, self.keys[promise.amount])
|
||||||
proof = Proof(amount=promise.amount, C=C.serialize().hex(), secret=secret)
|
proof = Proof(amount=promise.amount, C=C.serialize().hex(), secret=secret)
|
||||||
proofs.append(proof)
|
proofs.append(proof)
|
||||||
return proofs
|
return proofs
|
||||||
@@ -74,20 +74,20 @@ class LedgerAPI:
|
|||||||
for amount in amounts:
|
for amount in amounts:
|
||||||
secret = self._generate_secret()
|
secret = self._generate_secret()
|
||||||
secrets.append(secret)
|
secrets.append(secret)
|
||||||
B_, r = b_dhke.step1_bob(secret)
|
B_, r = b_dhke.step1_alice(secret)
|
||||||
rs.append(r)
|
rs.append(r)
|
||||||
payload: BlindedMessage = BlindedMessage(
|
payload: BlindedMessage = BlindedMessage(
|
||||||
amount=amount, B_=B_.serialize().hex()
|
amount=amount, B_=B_.serialize().hex()
|
||||||
)
|
)
|
||||||
payloads.blinded_messages.append(payload)
|
payloads.blinded_messages.append(payload)
|
||||||
promises_dict = requests.post(
|
promises_list = requests.post(
|
||||||
self.url + "/mint",
|
self.url + "/mint",
|
||||||
json=payloads.dict(),
|
json=payloads.dict(),
|
||||||
params={"payment_hash": payment_hash},
|
params={"payment_hash": payment_hash},
|
||||||
).json()
|
).json()
|
||||||
if "error" in promises_dict:
|
if "error" in promises_list:
|
||||||
raise Exception("Error: {}".format(promises_dict["error"]))
|
raise Exception("Error: {}".format(promises_list["error"]))
|
||||||
promises = [BlindedSignature.from_dict(p) for p in promises_dict]
|
promises = [BlindedSignature.from_dict(p) for p in promises_list]
|
||||||
return self._construct_proofs(promises, [(r, s) for r, s in zip(rs, secrets)])
|
return self._construct_proofs(promises, [(r, s) for r, s in zip(rs, secrets)])
|
||||||
|
|
||||||
def split(self, proofs, amount):
|
def split(self, proofs, amount):
|
||||||
@@ -101,7 +101,7 @@ class LedgerAPI:
|
|||||||
payloads: MintPayloads = MintPayloads()
|
payloads: MintPayloads = MintPayloads()
|
||||||
for output_amt in fst_outputs + snd_outputs:
|
for output_amt in fst_outputs + snd_outputs:
|
||||||
secret = self._generate_secret()
|
secret = self._generate_secret()
|
||||||
B_, r = b_dhke.step1_bob(secret)
|
B_, r = b_dhke.step1_alice(secret)
|
||||||
secrets.append((r, secret))
|
secrets.append((r, secret))
|
||||||
payload: BlindedMessage = BlindedMessage(
|
payload: BlindedMessage = BlindedMessage(
|
||||||
amount=output_amt, B_=B_.serialize().hex()
|
amount=output_amt, B_=B_.serialize().hex()
|
||||||
@@ -159,7 +159,7 @@ class Wallet(LedgerAPI):
|
|||||||
async def request_mint(self, amount):
|
async def request_mint(self, amount):
|
||||||
return super().request_mint(amount)
|
return super().request_mint(amount)
|
||||||
|
|
||||||
async def mint(self, amount, payment_hash=None):
|
async def mint(self, amount: int, payment_hash: str = None):
|
||||||
split = amount_split(amount)
|
split = amount_split(amount)
|
||||||
proofs = super().mint(split, payment_hash)
|
proofs = super().mint(split, payment_hash)
|
||||||
if proofs == []:
|
if proofs == []:
|
||||||
|
|||||||
Reference in New Issue
Block a user