-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rsh
138 lines (111 loc) · 3.56 KB
/
index.rsh
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
'reach 0.1';
const[gameOutcome, A_WINS, B_WINS, DRAW] = makeEnum(3);
const winner = (playHandA, playHandB, gHandA, gHandB)=> {
if(gHandA == gHandB){
return DRAW;
}
else{
if(gHandA == (playHandA + playHandB)){
return A_WINS;
}
else{
if(gHandB == (playHandA + playHandB)){
return B_WINS;
}
else{
return DRAW;
}
}
}
};
assert(winner(0,4,0,4)== B_WINS);
assert(winner(4,0,4,0)== A_WINS);
assert(winner(0,1,0,4)== DRAW);
assert(winner(5,5,5,5)== DRAW);
forall(UInt, a =>
forall(UInt, b =>
forall(UInt, c =>
forall(UInt, d =>
assert(gameOutcome(winner(a,b,c,d)))))));
forall(UInt, a =>
forall(UInt, b =>
forall(UInt, c =>
assert(winner(a,b,c,c) == DRAW))));
const Shared = {
...hasRandom,
getHand: Fun([],UInt),
getGuess: Fun([],UInt),
seeOutcome: Fun([UInt], Null),
informTimeout: Fun([],Null),
};
export const main = Reach.App(() =>{
const Alice = Participant('Alice', {
...Shared,
wager: UInt,
deadline: UInt,
});
const Bob = Participant('Bob',{
...Shared,
acceptWager: Fun([UInt],Null),
});
init();
const informTimeout = () => {
each([Alice,Bob], () => {
interact.informTimeout();
});
};
Alice.only(() => {
const amt = declassify(interact.wager);
const time = declassify(interact.deadline);
});
Alice.publish(amt,time)
.pay(amt);
commit();
Bob.interact.acceptWager(amt);
Bob.pay(amt)
.timeout(relativeTime(time), () => closeTo(Alice, informTimeout));
var outcome = DRAW;
invariant(balance() == 2 * amt && gameOutcome(outcome));
while(outcome == DRAW){
commit();
Alice.only(() => {
const _playHandA = interact.getHand();
const _gHandA = interact.getGuess();
const [_commitA,_saltA] = makeCommitment(interact,_playHandA);
const commitA = declassify(_commitA);
const[_guessCommitA, _guessSaltA] = makeCommitment(interact,_gHandA);
const guessCommitA = declassify(_guessCommitA);
});
Alice.publish(commitA, guessCommitA)
.timeout(relativeTime(time), () => closeTo(Bob, informTimeout));
commit();
unknowable(Bob, Alice(_playHandA, _saltA));
unknowable(Bob, Alice(_gHandA, _guessSaltA));
Bob.only(() =>{
const _playHandB = interact.getHand();
const _gHandB = interact.getGuess();
const playHandB = declassify(_playHandB);
const gHandB = declassify(_gHandB);
});
Bob.publish(playHandB,gHandB)
.timeout(relativeTime(time), () => closeTo(Alice, informTimeout));
commit();
Alice.only(() => {
const [saltA, playHandA] = declassify([_saltA, _playHandA]);
const [guessSaltA, gHandA] = declassify([_guessSaltA,_gHandA]);
})
Alice.publish(saltA, playHandA, guessSaltA, gHandA)
.timeout(relativeTime(time), () => closeTo(Bob, informTimeout));
checkCommitment(commitA, saltA, playHandA);
checkCommitment(guessCommitA, guessSaltA, gHandA);
outcome = winner(playHandA, playHandB, gHandA, gHandB);
continue;
}//end of while loop
assert(outcome == A_WINS || outcome == B_WINS);
transfer(2 * amt).to(outcome == A_WINS ? Alice : Bob);
commit();
each([Alice, Bob], () => {
interact.seeOutcome(outcome);
});
exit();
});