-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rsh
138 lines (117 loc) · 4.4 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';
// make some enumerations
const [isHand, ZERU, UNU, DUI, TRE, QUATTRU, CINQUE] = makeEnum(6);
const [isGuess, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN] = makeEnum(11);
const [isOutcome, B_WINS, DRAW, A_WINS] = makeEnum(3);
// find the outcome
const winner = (handAlice, handBob, guessAlice, guessBob) => {
if(guessAlice == guessBob) {
const winner = DRAW;
return winner;
} else if(guessAlice == (handAlice + handBob)) {
const winner = A_WINS;
return winner;
} else if(guessBob == (handAlice + handBob)) {
const winner = B_WINS;
return winner;
} else {
const winner = DRAW;
return winner;
};
};
// make some assertions for the outcome
assert(winner(UNU, TRE, UNU, TRE) == DRAW); //both get it wrong
assert(winner(UNU, TRE, QUATTRU, QUATTRU) == DRAW); //both get it right
assert(winner(UNU, TRE, QUATTRU, TRE) == A_WINS); //alice gets it, bob doesn't
assert(winner(UNU, TRE, TRE, QUATTRU) == B_WINS); //bob gets it, alice doesn't
forall(UInt, handAlice =>
forall(UInt, handBob =>
forall(UInt, guessAlice =>
forall(UInt, guessBob =>
assert(isOutcome(winner(handAlice, handBob, guessAlice, guessBob)))))));
forall(UInt, (guess) =>
forall(UInt, handAlice =>
forall(UInt, handBob =>
assert(winner(handAlice, handBob, guess, guess) == DRAW))));
const Player = {
...hasRandom,
getHand: Fun([], UInt),
getGuess: Fun([], UInt),
seeOutcome: Fun([UInt], Null),
informTimeout: Fun([], Null)
};
export const main = Reach.App(() => {
const Alice = Participant('Alice', {
...Player,
wager: UInt,
deadline: UInt
});
const Bob = Participant('Bob', {
...Player,
acceptWager: Fun([UInt], Null)
});
init();
const informTimeout = () => {
each([Alice, Bob], () => {
interact.informTimeout();
});
};
Alice.only(() => {
const wager = declassify(interact.wager);
const deadline = declassify(interact.deadline);
});
Alice.publish(wager, deadline).pay(wager);
commit();
Bob.only(() => {
interact.acceptWager(wager);
});
Bob.pay(wager)
.timeout(relativeTime(deadline), () => closeTo(Alice, informTimeout));
//note no commit here due to loop
const totalWager = 2 * wager;
//loop start
var outcome = DRAW;
invariant(balance() == totalWager && isOutcome(outcome));
while (outcome == DRAW) {
//commit inside the loop
commit();
Alice.only(() => {
const _handAlice = interact.getHand();
const _guessAlice = interact.getGuess();
const [_commitHandAlice, _saltHandAlice] = makeCommitment(interact, _handAlice);
const [_commitGuessAlice, _saltGuessAlice] = makeCommitment(interact, _guessAlice);
const commitHandAlice = declassify(_commitHandAlice);
const commitGuessAlice = declassify(_commitGuessAlice);
});
Alice.publish(commitHandAlice, commitGuessAlice)
.timeout(relativeTime(deadline), () => closeTo(Bob, informTimeout));
commit();
unknowable(Bob, Alice(_handAlice, _saltHandAlice));
unknowable(Bob, Alice(_guessAlice, _saltGuessAlice));
Bob.only(() => {
const handBob = declassify(interact.getHand());
const guessBob = declassify(interact.getGuess());
});
Bob.publish(handBob, guessBob)
.timeout(relativeTime(deadline), () => closeTo(Alice, informTimeout));
commit();
Alice.only(() => {
const handAlice = declassify(_handAlice);
const guessAlice = declassify(_guessAlice);
const saltHandAlice = declassify(_saltHandAlice);
const saltGuessAlice = declassify(_saltGuessAlice);
});
Alice.publish(handAlice, guessAlice, saltHandAlice, saltGuessAlice)
.timeout(relativeTime(deadline), () => closeTo(Bob, informTimeout));
checkCommitment(commitHandAlice, saltHandAlice, handAlice);
checkCommitment(commitGuessAlice, saltGuessAlice, guessAlice);
outcome = winner(handAlice, handBob, guessAlice, guessBob);
continue;
}
assert(outcome == A_WINS || outcome == B_WINS);
transfer(totalWager).to(outcome == A_WINS ? Alice : Bob);
commit();
each([Alice, Bob], () => {
interact.seeOutcome(outcome);
});
});