/
space_poker.clinc
214 lines (201 loc) · 7.98 KB
/
space_poker.clinc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
(
(include spacehandcalc.clinc)
(include assert.clinc)
(defconst END_HASH (shatree space_poker_end))
(defconst CONTINUE_RIVER (curry space_poker_continue_street END_HASH))
(defconst CONTINUE_RIVER_HASH (shatree CONTINUE_RIVER))
(defconst START_RIVER (curry space_poker_start_street CONTINUE_RIVER_HASH))
(defconst START_RIVER_HASH (shatree START_RIVER))
(defconst CONTINUE_TURN (curry space_poker_continue_street START_RIVER_HASH))
(defconst CONTINUE_TURN_HASH (shatree CONTINUE_TURN))
(defconst START_TURN (curry space_poker_start_street CONTINUE_TURN_HASH))
(defconst START_TURN_HASH (shatree START_TURN))
(defconst CONTINUE_FLOP (curry space_poker_continue_street START_TURN_HASH))
(defconst CONTINUE_FLOP_HASH (shatree CONTINUE_FLOP))
(defconst START_FLOP (curry space_poker_start_street CONTINUE_FLOP_HASH))
(defconst START_FLOP_HASH (shatree START_FLOP))
(defconst CONTINUE_PREFLOP (curry space_poker_continue_street START_FLOP_HASH))
(defconst CONTINUE_PREFLOP_HASH (shatree CONTINUE_PREFLOP))
(defconst START_PREFLOP (curry space_poker_start_street CONTINUE_PREFLOP_HASH))
(defconst START_PREFLOP_HASH (shatree START_PREFLOP))
(defconst BHASH (shatree space_poker_b))
(defconst AHASH (shatree space_poker_a))
; Bob challenging
; state is minraise
; move is Alice image5
; evidence is nil
; split should be Alice keeps an ante
(defun space_poker_a (mod_hash (move next_validation_hash mover_share previous_validation_hash
mover_puzzle_hash waiter_puzzle_hash amount timeout max_move_size referee_hash)
state me mover_puzzle solution evidence)
(assert
(not
(all
(= (strlen move) 32)
(= next_validation_hash (sha256 BHASH (shatree (list state move))))
(= mover_share (- (/ amount 2) state))
)
)
0
)
)
; Alice challenging
; state is minraise and Alice image5
; move is Bob image4
; evidence is nil
; split should be Bob keeps an ante
(defun space_poker_b (mod_hash (move next_validation_hash mover_share previous_validation_hash
mover_puzzle_hash waiter_puzzle_hash amount timeout max_move_size referee_hash)
(minraise alice_image5) me mover_puzzle solution evidence)
(assert
(not
(all
(= (strlen move) 32)
(= next_validation_hash (sha256 START_FLOP_HASH (shatree (list minraise (+ minraise minraise)
(- (/ amount 2) minraise) alice_image5 move))))
(= mover_share (- (/ amount 2) state))
)
)
0
)
)
; Elsa challlenging
; state is minraise, amount in pot, Anna's stack size, Anna's imageN, and Elsa's imageN-1
; move is Anna's imageN-1 and raise amount (0 for check)
; split should be Anna keeps new amount in pot
(defun space_poker_start_street (NEXT_PROGRAM mod_hash (move next_validation_hash mover_share previous_validation_hash
mover_puzzle_hash waiter_puzzle_hash amount timeout max_move_size referee_hash)
(minraise pot_size anna_stack anna_imagen elsa_imagen-1) me mover_puzzle solution evidence)
(assign
anna_imagen-1 (substr move 0 32)
raise_amount (substr move 32 (strlen move))
(assert
(not
(all
(= (sha256 anna_imagen-1) anna_imagen)
(>= raise_amount minraise)
(<= raise_amount anna_stack)
(= next_validation_hash (sha256 NEXT_PROGRAM (shatree (list minraise (+ pot_size raise_amount)
(- (- amount anna_stack) pot_size) elsa_imagen-1 anna_imagen-1))))
(= mover_share (+ raise_amount (- amount anna_stack)))
)
)
0
)
)
)
; Elsa challenging
; state is minraise, amount in pot, raise amount, Anna's stack size, Anna's imageN, and Elsa's imageN
; move is raise or Anna's imageN-1
; split should be Anna keeps new amount in pot
(defun space_poker_continue_street (NEXT_PROGRAM mod_hash (move next_validation_hash mover_share previous_validation_hash
mover_puzzle_hash waiter_puzzle_hash amount timeout max_move_size referee_hash)
(minraise pot_size raise_amount anna_stack_size anna_imagen elsa_imagen) me mover_puzzle solution evidence)
(if (= (strlen move) 32)
(assert
(not
(all
(= (sha256 move) anna_imagen)
(= next_validation_hash (sha256 NEXT_PROGRAM (shatree (list minraise (+ pot_size raise_amount)
(- (- amount anna_stack_size) pot_size) elsa_imagen move))))
(= mover_share (+ raise_amount (- amount anna_stack_size)))
)
)
0
)
(assert
(not
(all
(<= minraise move)
(>= anna_stack_size move)
(= next_validation_hash (sha256 mod_hash (shatree (list minraise (+ pot_size raise_amount move)
(- (- amount anna_stack_size) pot_size) elsa_imagen anna_imagen))))
)
)
0
)
)
)
(defun extract_one_card (vala valb)
(+ 2 (% (sha256 (logxor vala valb)) 13))
)
(defun extract_hole_cards (vala valb pre)
(assign
v1 (% (sha256 (logxor vala valb) (* 13 13)))
(card1 . card2) (divmod v1 13)
(list (+ card1 2) (+ card2 2) &rest pre)
)
)
(defun extract_three_cards (vala valb)
(assign
v1 (% (sha256 (logxor vala valb) (* 13 13 13)))
(card1 . v2) (divmod v1 (* 13 13))
(card2 . card3) (divmod v2 13)
(list (+ card1 2) (+ card2 2) (+ card3 2))
)
)
(defun select_cards (card_list selections)
(assign
(pos1 . pos2) (divmod selections 7)
(assert
(> pos2 pos1)
(>= 0 pos1)
(select_cards_inner 0 (list pos1 pos2 100) card_list)
)
)
)
(defun select_cards_inner (mypos poslist cards)
(if (not cards)
0
(if (= mypos (f poslist))
(select_cards_inner (+ 1 mypos) (r poslist) (r cards))
(c (f cards) (select_cards_inner (+ 1 mypos) poslist (r cards)))
)
)
)
(defun amount_due (pot_size result)
(if (not result)
(/ pot_size 2)
(= result 1)
pot_size
0
)
)
; Elsa challenging
; state is minraise, amount in pot, Anna's stack size, Anna's image1 and Elsa's preimage
; move is Anna's preimage and card selection
; evidence is Elsa's card selection
; split should send pot to winner of the hand or even split if chop
(defun space_poker_end (mod_hash (move next_validation_hash mover_share previous_validation_hash
mover_puzzle_hash waiter_puzzle_hash amount timeout max_move_size referee_hash)
(minraise pot_size anna_stack_size anna_image1 elsa_preimage) me mover_puzzle solution evidence)
(assign
anna_preimage (substr move 0 32)
anna_card_selection (substr move 32 33)
elsa_image1 (sha256 elsa_preimage)
elsa_image2 (sha256 elsa_image1)
elsa_image3 (sha256 elsa_image2)
elsa_image4 (sha256 elsa_image3)
anna_image2 (sha256 anna_image1)
anna_image3 (sha256 anna_image2)
anna_image4 (sha256 anna_image3)
table_cards (list (extract_one_card anna_image2 elsa_image2) (extract_one_card anna_image1 elsa_image1) &rest
(extract_three_cards anna_image3 elsa_image3))
anna_all_cards (extract_hole_cards anna_preimage elsa_image4 table_cards)
anna_hand (spacehandcalc (select_cards anna_all_cards anna_card_selection))
elsa_all_cards (extract_hole_cards elsa_preimage anna_image4 table_cards)
elsa_hand (spacehandcalc (select_cards elsa_all_cards evidence))
(assert
(not
(all
(= (sha256 anna_preimage anna_image1))
(= next_validation_hash 0)
(>= mover_share (+ (- (- amount anna_stack_size) pot_size)
(amount_due pot_size (hand_compare elsa_hand anna_hand))))
)
)
0
)
)
)
)