|
| 1 | + |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from matt import NUMS_KEY |
| 5 | +from matt.argtypes import BytesType, IntType, SignerType |
| 6 | +from matt.btctools.common import sha256 |
| 7 | +from matt.btctools.script import OP_ADD, OP_CHECKSIG, OP_DUP, OP_EQUAL, OP_FROMALTSTACK, OP_NOT, OP_PICK, OP_ROT, OP_SHA256, OP_SWAP, OP_TOALTSTACK, OP_VERIFY, CScript |
| 8 | +from matt.contracts import ClauseOutput, StandardClause, StandardAugmentedP2TR, StandardP2TR, ContractState |
| 9 | +from matt.hub.fraud import Bisect_1, Computer, Leaf |
| 10 | +from matt.merkle import MerkleTree |
| 11 | +from matt.script_helpers import check_input_contract, check_output_contract, drop, dup, merkle_root, older |
| 12 | +from matt.utils import encode_wit_element |
| 13 | + |
| 14 | + |
| 15 | +# TODO: add forfait clauses whenever needed |
| 16 | + |
| 17 | +# TODO: how to generalize what the contract does after the leaf? We should be able to compose clauses with some external code. |
| 18 | +# Do we need "clause" algebra? |
| 19 | + |
| 20 | +class G256_S0(StandardP2TR): |
| 21 | + def __init__(self, alice_pk: bytes, bob_pk: bytes, forfait_timeout: int = 10): |
| 22 | + self.alice_pk = alice_pk |
| 23 | + self.bob_pk = bob_pk |
| 24 | + self.forfait_timeout = forfait_timeout |
| 25 | + |
| 26 | + g256_s1 = G256_S1(alice_pk, bob_pk, forfait_timeout) |
| 27 | + # witness: <bob_sig> <x> |
| 28 | + choose = StandardClause( |
| 29 | + name="choose", |
| 30 | + script=CScript([ |
| 31 | + *g256_s1.State.encoder_script(), |
| 32 | + *check_output_contract(g256_s1), |
| 33 | + |
| 34 | + bob_pk, |
| 35 | + OP_CHECKSIG |
| 36 | + ]), |
| 37 | + arg_specs=[ |
| 38 | + ('bob_sig', SignerType(bob_pk)), |
| 39 | + ('x', IntType()), |
| 40 | + ], |
| 41 | + next_outputs_fn=lambda args, _: [ClauseOutput( |
| 42 | + n=-1, |
| 43 | + next_contract=g256_s1, |
| 44 | + next_state=g256_s1.State(x=args['x']) |
| 45 | + )] |
| 46 | + ) |
| 47 | + |
| 48 | + super().__init__(NUMS_KEY, choose) |
| 49 | + |
| 50 | + |
| 51 | +class G256_S1(StandardAugmentedP2TR): |
| 52 | + @dataclass |
| 53 | + class State(ContractState): |
| 54 | + x: int |
| 55 | + |
| 56 | + def encode(self): |
| 57 | + return sha256(encode_wit_element(self.x)) |
| 58 | + |
| 59 | + def encoder_script(): |
| 60 | + return CScript([OP_SHA256]) |
| 61 | + |
| 62 | + def __init__(self, alice_pk: bytes, bob_pk: bytes, forfait_timeout): |
| 63 | + self.alice_pk = alice_pk |
| 64 | + self.bob_pk = bob_pk |
| 65 | + self.forfait_timeout = forfait_timeout |
| 66 | + |
| 67 | + g256_s2 = G256_S2(alice_pk, bob_pk, forfait_timeout) |
| 68 | + |
| 69 | + # reveal: <alice_sig> <t_a> <y> <sha256(x)> |
| 70 | + reveal = StandardClause( |
| 71 | + name="reveal", |
| 72 | + script=CScript([ |
| 73 | + OP_DUP, |
| 74 | + |
| 75 | + # check that the top of the stack is the embedded data |
| 76 | + *self.State.encoder_script(), |
| 77 | + *check_input_contract(), |
| 78 | + |
| 79 | + # <alice_sig> <t_a> <y> <x> |
| 80 | + *g256_s2.State.encoder_script(), |
| 81 | + *check_output_contract(g256_s2), |
| 82 | + |
| 83 | + alice_pk, |
| 84 | + OP_CHECKSIG |
| 85 | + ]), |
| 86 | + arg_specs=[ |
| 87 | + ('alice_sig', SignerType(alice_pk)), |
| 88 | + ('t_a', BytesType()), |
| 89 | + ('y', IntType()), |
| 90 | + ('x', IntType()), |
| 91 | + ], |
| 92 | + next_outputs_fn=lambda args, _: [ClauseOutput( |
| 93 | + n=-1, |
| 94 | + next_contract=g256_s2, |
| 95 | + next_state=g256_s2.State(t_a=args['t_a'], y=args['y'], x=args['x']) |
| 96 | + )] |
| 97 | + ) |
| 98 | + |
| 99 | + super().__init__(NUMS_KEY, reveal) |
| 100 | + |
| 101 | + |
| 102 | +Compute2x = Computer( |
| 103 | + encoder=CScript([OP_SHA256]), |
| 104 | + func=CScript([OP_DUP, OP_ADD]), |
| 105 | + specs=[('x', IntType())], |
| 106 | +) |
| 107 | + |
| 108 | + |
| 109 | +NopInt = Computer( |
| 110 | + encoder=CScript([]), |
| 111 | + func=CScript([]), |
| 112 | + specs=[('x', IntType())], |
| 113 | +) |
| 114 | + |
| 115 | + |
| 116 | +class G256_S2(StandardAugmentedP2TR): |
| 117 | + @dataclass |
| 118 | + class State(ContractState): |
| 119 | + t_a: bytes |
| 120 | + y: int |
| 121 | + x: bytes |
| 122 | + |
| 123 | + def encode(self): |
| 124 | + return MerkleTree([self.t_a, sha256(encode_wit_element(self.y)), sha256(encode_wit_element(self.x))]).root |
| 125 | + |
| 126 | + def encoder_script(): |
| 127 | + return CScript([ |
| 128 | + OP_TOALTSTACK, OP_SHA256, OP_FROMALTSTACK, OP_SHA256, |
| 129 | + *merkle_root(3) |
| 130 | + ]) |
| 131 | + |
| 132 | + def __init__(self, alice_pk: bytes, bob_pk: bytes, forfait_timeout: int = 10): |
| 133 | + self.alice_pk = alice_pk |
| 134 | + self.bob_pk = bob_pk |
| 135 | + self.forfait_timeout = forfait_timeout |
| 136 | + |
| 137 | + # reveal: <alice_sig> |
| 138 | + withdraw = StandardClause( |
| 139 | + name="withdraw", |
| 140 | + script=CScript([ |
| 141 | + *older(forfait_timeout), |
| 142 | + |
| 143 | + alice_pk, |
| 144 | + OP_CHECKSIG |
| 145 | + ]), |
| 146 | + arg_specs=[('alice_sig', SignerType(alice_pk))] |
| 147 | + ) |
| 148 | + |
| 149 | + def leaf_factory(i: int): return Leaf(alice_pk, bob_pk, Compute2x) |
| 150 | + |
| 151 | + bisectg256_0 = Bisect_1(alice_pk, bob_pk, 0, 7, leaf_factory, forfait_timeout) |
| 152 | + # start_challenge: <bob_sig> <t_a> <y> <x> <z> <t_b> |
| 153 | + start_challenge = StandardClause( |
| 154 | + name="start_challenge", |
| 155 | + script=CScript([ |
| 156 | + OP_TOALTSTACK, |
| 157 | + |
| 158 | + # check that y != z |
| 159 | + OP_DUP, 3, OP_PICK, OP_EQUAL, OP_NOT, OP_VERIFY, |
| 160 | + |
| 161 | + OP_TOALTSTACK, |
| 162 | + |
| 163 | + # <bob_sig> <t_a> <y> <x> --- <t_b> <z> |
| 164 | + |
| 165 | + *dup(3), |
| 166 | + |
| 167 | + # verify the embedded data |
| 168 | + *self.State.encoder_script(), |
| 169 | + *check_input_contract(), |
| 170 | + |
| 171 | + # <bob_sig> <t_a> <y> <x> --- <t_b> <z> |
| 172 | + OP_SHA256, OP_SWAP, OP_SHA256, |
| 173 | + # <bob_sig> <t_a> <sha256(x)> <sha256(y)> --- <t_b> <z> |
| 174 | + OP_ROT, |
| 175 | + # <bob_sig> <sha256(x)> <sha256(y)> <t_a> --- <t_b> <sha256(z)> |
| 176 | + |
| 177 | + OP_FROMALTSTACK, OP_SHA256, |
| 178 | + # <bob_sig> <sha256(x)> <sha256(y)> <t_a> <sha256(z)> --- <t_b> |
| 179 | + OP_SWAP, |
| 180 | + # <bob_sig> <sha256(x)> <sha256(y)> <sha256(z)> <t_a> --- <t_b> |
| 181 | + |
| 182 | + OP_FROMALTSTACK, |
| 183 | + |
| 184 | + # <bob_sig> <sha256(x)> <sha256(y)> <sha256(z)> <t_a> <t_b> |
| 185 | + |
| 186 | + *bisectg256_0.State.encoder_script(), |
| 187 | + *check_output_contract(bisectg256_0), |
| 188 | + |
| 189 | + bob_pk, |
| 190 | + OP_CHECKSIG |
| 191 | + ]), |
| 192 | + arg_specs=[ |
| 193 | + ('bob_sig', SignerType(bob_pk)), |
| 194 | + ('t_a', BytesType()), |
| 195 | + ('y', IntType()), |
| 196 | + ('x', IntType()), |
| 197 | + ('z', IntType()), |
| 198 | + ('t_b', BytesType()), |
| 199 | + ], |
| 200 | + next_outputs_fn=lambda args, _: [ClauseOutput( |
| 201 | + n=-1, |
| 202 | + next_contract=bisectg256_0, |
| 203 | + next_state=bisectg256_0.State( |
| 204 | + h_start=sha256(encode_wit_element(args['x'])), |
| 205 | + h_end_a=sha256(encode_wit_element(args['y'])), |
| 206 | + h_end_b=sha256(encode_wit_element(args['z'])), |
| 207 | + trace_a=args['t_a'], |
| 208 | + trace_b=args['t_b'], |
| 209 | + ) |
| 210 | + )] |
| 211 | + ) |
| 212 | + |
| 213 | + super().__init__(NUMS_KEY, [withdraw, start_challenge]) |
0 commit comments