diff --git a/core/settings.py b/core/settings.py index e90ebc2..0afdcce 100644 --- a/core/settings.py +++ b/core/settings.py @@ -1 +1 @@ -MAX_ORDER = 60 +MAX_ORDER = 64 diff --git a/mint/ledger.py b/mint/ledger.py index 9c2a372..2e84230 100644 --- a/mint/ledger.py +++ b/mint/ledger.py @@ -78,9 +78,30 @@ class Ledger: return False return True - @staticmethod - def _get_output_split(amount): + def _verify_split_amount(self, 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].""" + self._verify_amount(amount) bits_amt = bin(amount)[::-1][:-2] rv = [] for (pos, bit) in enumerate(bits_amt): @@ -106,6 +127,7 @@ class Ledger: async def split(self, proofs, amount, output_data): """Consumes proofs and prepares new promises based on the amount split.""" + self._verify_split_amount(amount) # Verify proofs are valid if not all([self._verify_proof(p) for p in proofs]): return False @@ -132,6 +154,8 @@ class Ledger: outs_snd = amount_split(amount) B_fst = [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 ), await self._generate_promises(outs_snd, B_snd) + self._verify_equation_balanced(proofs, prom_fst + prom_snd) + return prom_fst, prom_snd diff --git a/test_wallet.py b/test_wallet.py index eb3ed27..92ecbf0 100644 --- a/test_wallet.py +++ b/test_wallet.py @@ -44,8 +44,6 @@ async def run_test(): assert wallet1.balance == 63 + 64 wallet1.status() - print("PROOFs") - print(proofs) w1_fst_proofs, w1_snd_proofs = await wallet1.split(wallet1.proofs, 20) # we expect 44 and 20 -> [4, 8, 32], [4, 16] print(w1_fst_proofs)