Merge pull request #12 from callebtc/notation/alice-to-bob

Notation/alice to bob
This commit is contained in:
calle
2022-09-23 23:46:28 +03:00
committed by GitHub
6 changed files with 31 additions and 26 deletions

View File

@@ -6,7 +6,7 @@ Alice:
A = a*G
return A
Bob:
Y = hash_to_curve(secret_message)
Y = hash_to_point(secret_message)
r = random blinding factor
B'= Y + r*G
return B'
@@ -20,7 +20,7 @@ C = C' - r*A
(= a*Y)
return C, secret_message
Alice:
Y = hash_to_curve(secret_message)
Y = hash_to_point(secret_message)
C == a*Y
If true, C must have originated from Alice
"""
@@ -30,7 +30,7 @@ import hashlib
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.
If it does not, it tries computing again a new x coordinate from the hash of the coordinate."""
point = None
@@ -49,26 +49,26 @@ def hash_to_curve(secret_msg):
return point
def step1_bob(secret_msg):
def step1_alice(secret_msg):
secret_msg = secret_msg.encode("utf-8")
Y = hash_to_curve(secret_msg)
Y = hash_to_point(secret_msg)
r = PrivateKey()
B_ = Y + r.pubkey
return B_, r
def step2_alice(B_, a):
def step2_bob(B_, a):
C_ = B_.mult(a)
return C_
def step3_bob(C_, r, A):
def step3_alice(C_, r, A):
C = C_ - A.mult(r)
return C
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)
@@ -78,9 +78,9 @@ def verify(a, C, secret_msg):
# a = PrivateKey()
# A = a.pubkey
# secret_msg = "test"
# B_, r = step1_bob(secret_msg)
# C_ = step2_alice(B_, a)
# C = step3_bob(C_, r, A)
# B_, r = step1_alice(secret_msg)
# C_ = step2_bob(B_, a)
# C = step3_alice(C_, r, A)
# print("C:{}, secret_msg:{}".format(C, secret_msg))
# assert verify(a, C, secret_msg)
# assert verify(a, C + C, secret_msg) == False # adding C twice shouldn't pass

View File

@@ -13,9 +13,14 @@ from core.secp import PrivateKey, PublicKey
from core.settings import LIGHTNING, MAX_ORDER
from core.split import amount_split
from lightning import WALLET
from mint.crud import (get_lightning_invoice, get_proofs_used,
invalidate_proof, store_lightning_invoice,
store_promise, update_lightning_invoice)
from mint.crud import (
get_lightning_invoice,
get_proofs_used,
invalidate_proof,
store_lightning_invoice,
store_promise,
update_lightning_invoice,
)
class Ledger:
@@ -58,7 +63,7 @@ class Ledger:
async def _generate_promise(self, amount: int, B_: PublicKey):
"""Generates a promise for given amount and returns a pair (amount, C')."""
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(
amount, B_=B_.serialize().hex(), C_=C_.serialize().hex(), db=self.db
)

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "cashu"
version = "0.1.8"
version = "0.1.9"
description = "Ecash wallet and mint."
authors = ["calle <callebtc@protonmail.com>"]
license = "MIT"

View File

@@ -13,7 +13,7 @@ entry_points = {"console_scripts": ["cashu = wallet.cashu:cli"]}
setuptools.setup(
name="cashu",
version="0.1.8",
version="0.1.9",
description="Ecash wallet and mint with Bitcoin Lightning support",
long_description=long_description,
long_description_content_type="text/markdown",

View File

@@ -139,7 +139,7 @@ async def burn(ctx, token: str, all: bool, force: bool):
if all:
# check only those who are flagged as reserved
proofs = await get_reserved_proofs(wallet.db)
if force:
elif force:
# check all proofs in db
proofs = wallet.proofs
else:

View File

@@ -52,7 +52,7 @@ class LedgerAPI:
proofs = []
for promise, (r, secret) in zip(promises, secrets):
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)
proofs.append(proof)
return proofs
@@ -74,20 +74,20 @@ class LedgerAPI:
for amount in amounts:
secret = self._generate_secret()
secrets.append(secret)
B_, r = b_dhke.step1_bob(secret)
B_, r = b_dhke.step1_alice(secret)
rs.append(r)
payload: BlindedMessage = BlindedMessage(
amount=amount, B_=B_.serialize().hex()
)
payloads.blinded_messages.append(payload)
promises_dict = requests.post(
promises_list = requests.post(
self.url + "/mint",
json=payloads.dict(),
params={"payment_hash": payment_hash},
).json()
if "error" in promises_dict:
raise Exception("Error: {}".format(promises_dict["error"]))
promises = [BlindedSignature.from_dict(p) for p in promises_dict]
if "error" in promises_list:
raise Exception("Error: {}".format(promises_list["error"]))
promises = [BlindedSignature.from_dict(p) for p in promises_list]
return self._construct_proofs(promises, [(r, s) for r, s in zip(rs, secrets)])
def split(self, proofs, amount):
@@ -101,7 +101,7 @@ class LedgerAPI:
payloads: MintPayloads = MintPayloads()
for output_amt in fst_outputs + snd_outputs:
secret = self._generate_secret()
B_, r = b_dhke.step1_bob(secret)
B_, r = b_dhke.step1_alice(secret)
secrets.append((r, secret))
payload: BlindedMessage = BlindedMessage(
amount=output_amt, B_=B_.serialize().hex()
@@ -159,7 +159,7 @@ class Wallet(LedgerAPI):
async def request_mint(self, 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)
proofs = super().mint(split, payment_hash)
if proofs == []: