mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-20 18:44:20 +01:00
verifications
This commit is contained in:
@@ -1 +1 @@
|
|||||||
MAX_ORDER = 60
|
MAX_ORDER = 64
|
||||||
|
|||||||
@@ -78,9 +78,30 @@ class Ledger:
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
def _verify_split_amount(self, amount):
|
||||||
def _get_output_split(amount):
|
"""Split amount like output amount can't be negative or too big."""
|
||||||
|
try:
|
||||||
|
self._verify_amount(amount)
|
||||||
|
except:
|
||||||
|
# For better error message
|
||||||
|
raise Exception("Invalid split amount: " + str(amount))
|
||||||
|
|
||||||
|
def _verify_amount(self, amount):
|
||||||
|
"""Any amount used should be a positive integer not larger than 2^MAX_ORDER."""
|
||||||
|
valid = isinstance(amount, int) and amount > 0 and amount < 2**MAX_ORDER
|
||||||
|
if not valid:
|
||||||
|
raise Exception("Invalid amount: " + str(amount))
|
||||||
|
return amount
|
||||||
|
|
||||||
|
def _verify_equation_balanced(self, proofs, outs):
|
||||||
|
"""Verify that Σoutputs - Σinputs = 0."""
|
||||||
|
sum_inputs = sum(self._verify_amount(p["amount"]) for p in proofs)
|
||||||
|
sum_outputs = sum(self._verify_amount(p["amount"]) for p in outs)
|
||||||
|
assert sum_outputs - sum_inputs == 0
|
||||||
|
|
||||||
|
def _get_output_split(self, amount):
|
||||||
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
|
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
|
||||||
|
self._verify_amount(amount)
|
||||||
bits_amt = bin(amount)[::-1][:-2]
|
bits_amt = bin(amount)[::-1][:-2]
|
||||||
rv = []
|
rv = []
|
||||||
for (pos, bit) in enumerate(bits_amt):
|
for (pos, bit) in enumerate(bits_amt):
|
||||||
@@ -106,6 +127,7 @@ class Ledger:
|
|||||||
|
|
||||||
async def split(self, proofs, amount, output_data):
|
async def split(self, proofs, amount, output_data):
|
||||||
"""Consumes proofs and prepares new promises based on the amount split."""
|
"""Consumes proofs and prepares new promises based on the amount split."""
|
||||||
|
self._verify_split_amount(amount)
|
||||||
# Verify proofs are valid
|
# Verify proofs are valid
|
||||||
if not all([self._verify_proof(p) for p in proofs]):
|
if not all([self._verify_proof(p) for p in proofs]):
|
||||||
return False
|
return False
|
||||||
@@ -132,6 +154,8 @@ class Ledger:
|
|||||||
outs_snd = amount_split(amount)
|
outs_snd = amount_split(amount)
|
||||||
B_fst = [od["B'"] for od in output_data[: len(outs_fst)]]
|
B_fst = [od["B'"] for od in output_data[: len(outs_fst)]]
|
||||||
B_snd = [od["B'"] for od in output_data[len(outs_fst) :]]
|
B_snd = [od["B'"] for od in output_data[len(outs_fst) :]]
|
||||||
return await self._generate_promises(
|
prom_fst, prom_snd = await self._generate_promises(
|
||||||
outs_fst, B_fst
|
outs_fst, B_fst
|
||||||
), await self._generate_promises(outs_snd, B_snd)
|
), await self._generate_promises(outs_snd, B_snd)
|
||||||
|
self._verify_equation_balanced(proofs, prom_fst + prom_snd)
|
||||||
|
return prom_fst, prom_snd
|
||||||
|
|||||||
@@ -44,8 +44,6 @@ async def run_test():
|
|||||||
assert wallet1.balance == 63 + 64
|
assert wallet1.balance == 63 + 64
|
||||||
wallet1.status()
|
wallet1.status()
|
||||||
|
|
||||||
print("PROOFs")
|
|
||||||
print(proofs)
|
|
||||||
w1_fst_proofs, w1_snd_proofs = await wallet1.split(wallet1.proofs, 20)
|
w1_fst_proofs, w1_snd_proofs = await wallet1.split(wallet1.proofs, 20)
|
||||||
# we expect 44 and 20 -> [4, 8, 32], [4, 16]
|
# we expect 44 and 20 -> [4, 8, 32], [4, 16]
|
||||||
print(w1_fst_proofs)
|
print(w1_fst_proofs)
|
||||||
|
|||||||
Reference in New Issue
Block a user