|
| 1 | +import math |
| 2 | +from itertools import combinations as comb |
| 3 | + |
| 4 | +def combinations(n, t): |
| 5 | + return [tuple(a) for a in comb(range(0, n), t)] |
| 6 | + |
| 7 | +# Test whether the proposed spending paths Cp are actually sane |
| 8 | +def test_paths(Cp, n, t, k): |
| 9 | + if k > n - t: |
| 10 | + return False |
| 11 | + # no duplicates |
| 12 | + if len(Cp) != len(set(Cp)): |
| 13 | + return False |
| 14 | + for c in Cp: |
| 15 | + if len(c) != t: |
| 16 | + return False |
| 17 | + if max(c) >= n: |
| 18 | + return False |
| 19 | + D = combinations(n, k) |
| 20 | + for d in D: |
| 21 | + not_in_common = 0 |
| 22 | + for c in Cp: |
| 23 | + c_set = set(c) |
| 24 | + d_set = set(d) |
| 25 | + if not (c_set & d_set): |
| 26 | + not_in_common = 1 |
| 27 | + if not_in_common == 0: |
| 28 | + return False |
| 29 | + return True |
| 30 | + |
| 31 | +# Test test_paths |
| 32 | +n = 5 |
| 33 | +t = 3 |
| 34 | +k = 1 |
| 35 | +assert(test_paths(combinations(n, t), n, t, k)) |
| 36 | +assert(not test_paths([(1,2,3)], n, t, k)) |
| 37 | +k = 2 |
| 38 | +assert(test_paths(combinations(n, t), n, t, k)) |
| 39 | +k = 1 |
| 40 | +# 2 have 2 common, 1 has only one common |
| 41 | +assert(test_paths([(1,2,3), (0,2,3), (0,1,4)], n, t, k)) |
| 42 | +# doesn't work since 0 is always a required signer |
| 43 | +assert(not test_paths([(0,1,2), (0,2,3), (0,1,4)], n, t, k)) |
| 44 | + |
| 45 | +n = 6 |
| 46 | +t = 4 |
| 47 | +k = 1 |
| 48 | +assert(test_paths(combinations(n, t), n, t, k)) |
| 49 | +k = 2 |
| 50 | +assert(test_paths(combinations(n, t), n, t, k)) |
| 51 | +k = 1 |
| 52 | +assert(test_paths([(1,2,3,4), (0,3,4,5), (0,1,2,5)], n, t, k)) |
| 53 | +# has at most 2 common elements with every other |
| 54 | + |
| 55 | +# Check if d is a subset in any element of Cpdiff |
| 56 | +def d_included(Cpdiff, d): |
| 57 | + for ci in Cpdiff: |
| 58 | + # if all elements are included |
| 59 | + if set(d).issubset(set(ci)): |
| 60 | + return True |
| 61 | + return False |
| 62 | + |
| 63 | +# Minimum size of intersection between c and all elements of Cp |
| 64 | +def mininsect(Cp, c, n): |
| 65 | + m = n |
| 66 | + for cp in Cp: |
| 67 | + m_tmp = n - len(set(c).intersection(set(cp))) |
| 68 | + if m_tmp < m: |
| 69 | + m = m_tmp |
| 70 | + return m |
| 71 | + |
| 72 | +# Generate t-of-n spending paths with up to k non-cooperative |
| 73 | +def generate_paths(n, t, k): |
| 74 | + a = set(range(0,n)) |
| 75 | + C = combinations(n,t) |
| 76 | + D = combinations(n,k) |
| 77 | + Cp = [] |
| 78 | + Cpdiff = [] |
| 79 | + for d in D: |
| 80 | + if d_included(Cpdiff, d): |
| 81 | + continue |
| 82 | + # choose some c |
| 83 | + c_candidates = [] |
| 84 | + for c in C: |
| 85 | + if not d_included([tuple(a.difference(set(c)))], d): |
| 86 | + continue |
| 87 | + if not c in Cp: |
| 88 | + c_candidates += [(c, mininsect(Cp, c, n))] |
| 89 | + c = max(c_candidates,key=lambda item:item[1])[0] |
| 90 | + Cp += [(c)] |
| 91 | + Cpdiff += [tuple(a.difference(set(c)))] |
| 92 | + return Cp |
| 93 | + |
| 94 | +def cost(Cplen, n, t, k): |
| 95 | + sig = 64 |
| 96 | + pk = 32 |
| 97 | + branch = 32 |
| 98 | + print("- %s-of-%s with up to %s signers non-cooperative" % (t, n, k)) |
| 99 | + # + 1 for for the cooperative case |
| 100 | + spending_paths = Cplen + 1 |
| 101 | + print(" - Parallel signing sessions:", spending_paths) |
| 102 | + print(" - Everyone in key path cooperative: 1 sig, 1 pk:", sig + pk, "WU") |
| 103 | + # only balanced tree part, i.e. exclude keypath and fallback |
| 104 | + tree_depth = math.ceil(1+math.log(spending_paths-2, 2)) |
| 105 | + print(" - Up to %s non-cooperative: 1 sig, 1 pk, %s deep: %s WU" % (k, tree_depth, sig + pk + tree_depth*branch)) |
| 106 | + print(" - More than %s non-cooperative: %s sig, %s pk, 1 deep: %s WU" % (k, t, n, t*sig + n*pk + branch)) |
| 107 | + sessions = math.comb(n-1,t-1) # exclude combinations without the signer |
| 108 | + tree_depth = math.ceil(math.log(math.comb(n, t), 2)) |
| 109 | + print(" - In Comparison, fully merkleized multisig (%s parallel sessions): 1 sig, 1 pk, %s deep: %s WU" % (sessions, tree_depth, sig + pk + tree_depth*branch)) |
| 110 | + |
| 111 | +# Examples |
| 112 | +n = 5 |
| 113 | +t = 3 |
| 114 | +k = 1 |
| 115 | +Cp = generate_paths(n,t,k) |
| 116 | +cost(len(Cp), n, t, k) |
| 117 | +assert(test_paths(Cp, n, t, k)) |
| 118 | + |
| 119 | +n = 15 |
| 120 | +t = 11 |
| 121 | +k = 2 |
| 122 | +Cp = generate_paths(n,t,k) |
| 123 | +cost(len(Cp), n, t, k) |
| 124 | +assert(test_paths(Cp, n, t, k)) |
| 125 | + |
| 126 | +n = 20 |
| 127 | +t = 15 |
| 128 | +k = 2 |
| 129 | +Cp = generate_paths(n,t,k) |
| 130 | +cost(len(Cp), n, t, k) |
| 131 | +assert(test_paths(Cp, n, t, k)) |
| 132 | + |
| 133 | +k = 3 |
| 134 | +Cp = generate_paths(n,t,k) |
| 135 | +cost(len(Cp), n, t, k) |
| 136 | +assert(test_paths(Cp, n, t, k)) |
0 commit comments