forked from AU-COBRA/ConCert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardroomVotingExtractionLiquidity.v
358 lines (298 loc) · 12.6 KB
/
BoardroomVotingExtractionLiquidity.v
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
(** * Extraction of the Boardroom voting contract Liquidity *)
(** NOTE: Currently does not compile due to some restrictions on closures in Liquidity. Moreover, the printing of literals might need adjustments. *)
From MetaCoq.Template Require Import All.
From ConCert.Extraction Require Import LiquidityPretty.
From ConCert.Extraction Require Import LiquidityExtract.
From ConCert.Extraction Require Import Common.
From ConCert.Execution Require Import Blockchain.
From ConCert.Execution Require Import ContractCommon.
From ConCert.Execution Require Import ContractMonads.
From ConCert.Execution Require Import ResultMonad.
From ConCert.Execution Require Monad.
From ConCert.Execution Require OptionMonad.
From ConCert.Execution.Test Require Import LocalBlockchain.
From ConCert.Examples.BoardroomVoting Require Import BoardroomVotingZ.
From Coq Require Import ZArith.
From Coq Require Import List.
From Coq Require Import String.
Import MCMonadNotation.
Local Open Scope string_scope.
Open Scope Z.
Definition PREFIX := "".
(* In this example we just use xor for the hash function, which is
obviously not cryptographically secure. *)
Definition modulus : Z := 201697267445741585806196628073.
Definition four := 4%nat.
Definition seven := 7%nat.
Definition _1234583932 := 1234583932.
Definition _23241 := 23241.
Definition _159338231 := 159338231.
Definition oneN : N := 1%N.
Definition Z3 : Z := 3.
Definition generator : Z := Z3.
Definition hash_func (l : list positive) : positive :=
N.succ_pos (fold_left (fun a p => N.lxor (Npos p) a) l oneN).
Definition AddrSize := (2^128)%N.
#[local]
Instance Base : ChainBase := LocalBlockchain.LocalChainBase AddrSize.
Module Params <: BoardroomParams.
Definition H : list positive -> positive := hash_func.
Definition Base := Base.
Definition prime := modulus.
Definition generator := generator.
End Params.
Module BV := BoardroomVoting Params. Import BV.
(* Compute the signup messages that would be sent by each party.
We just use the public key as the chosen randomness here. *)
Definition _3 := 3%nat.
Definition _5 := 5.
Definition _11 := 11.
Definition num_parties : nat := seven.
Definition votes_for : nat := four.
(* A pseudo-random generator for secret keys *)
Definition sk n := (Z.of_nat n + _1234583932) * (modulus - _23241)^_159338231.
(* Make a list of secret keys, here starting at i=7 *)
Definition sks : list Z := map sk (seq seven num_parties).
(* Make a list of votes for each party *)
Definition svs : list bool :=
Eval compute in map (fun _ => true)
(seq 0 votes_for)
++ map (fun _ => false)
(seq 0 (num_parties - votes_for)).
(* Get string representation of modulus, and remap it. This way we avoid having the extraction compute the number. *)
Definition modulus_ := StringExtra.string_of_Z modulus.
Definition init_ctx := (Chain × ContractCallContext).
Definition init_wrapper (cctx : init_ctx) := (run_contract_initer BV.init) cctx.1 cctx.2.
Notation msg := (Chain × ContractCallContext × option BV.Msg).
Definition receive_wrapper (msg : msg)
(st : BV.State)
: result (list ActionBody * BV.State) Error :=
match (run_contract_receiver BV.receive) msg.1 msg.2.1 st msg.2.2 with
| Ok (st, acts) => Ok (acts, st)
| Err e => Err e
end.
Definition dummy_init : init_ctx -> BV.Setup -> result BV.State Error := fun _ _ => Err default_error.
Definition dummy_receive : msg -> BV.State -> result (list ActionBody × BV.State) Error :=
fun m s =>
let x := handle_signup 0 (0, 0) s s.(owner) 0%nat in
Err default_error.
Definition storage_alias := "type storage = state".
Definition bruteforce_tally_def :=
"let bruteforce_tally_aux =
let rec bruteforce_tally_aux (n, votes_product) =
if elmeqb (pow_p generator (int n)) votes_product then
Some (n)
else if n = 0p then
None
else
let n0 = n - 1p in
(bruteforce_tally_aux (unsafe_int_to_nat n0, votes_product))
in fun n votes_product -> bruteforce_tally_aux (n, votes_product)".
Definition extra_ops :=
"let unsafe_int_to_nat (n : int) =
let n = match%nat n with
| Plus n -> n
| Minus _ -> failwith ""n shound not be negative"" in
n
let predN (n : nat) = unsafe_int_to_nat (n - 1p)
let nth = let rec nth (n, l, default) =
if n = 0p then (match l with
[] -> default
| x :: l' -> x)
else let m = predN n in (match l with
[] -> default
| x :: t -> (nth (m, t, default)))
in fun n l default -> nth (n, l, default)
let prod (l : int list) =
List.fold (fun (a, b) -> mulInt a b) l 1
let firstn (n : nat) (l : 'a list) =
let (_,r) = List.fold (fun (b,(n, a)) ->
if n = 0p then (0p, a)
else (predN n, b :: a)) l (n,[]) in
List.rev r
let skipn = let rec skipn (n, l) =
if n = 0p then l
else let n0 = predN n in (match l with
[] -> []
| a :: l0 -> (skipn (n0, l0)))
in fun n l -> skipn (n, l)
let existsb (f : 'a -> bool) = let rec existsb (l) =
match l with
[] -> false
| a :: l0 -> (if (f a) then true else (existsb (l0)))
in fun l -> existsb (l)".
Definition hash_func_def := "let hash_func (l : ((nat) list)) = addNat 1p (List.fold (fun (p,a) -> lxorNat p a) l 1p)".
Definition BV_MODULE : LiquidityMod msg init_ctx BV.Setup BV.State ActionBody Error :=
{| (* a name for the definition with the extracted code *)
lmd_module_name := "liquidity_boardroomvoting" ;
(* definitions of operations on pairs and ints *)
lmd_prelude := concat nl [LiquidityPrelude; extra_ops; hash_func_def];
(* initial storage *)
lmd_init := dummy_init;
(* no extra operations in [init] are required *)
lmd_init_prelude := "" ;
(* the main functionality *)
lmd_receive := dummy_receive;
(* code for the entry point *)
lmd_entry_point := storage_alias ++ nl
++ printWrapper (PREFIX ++ "receive_wrapper") ++ nl
++ printMain |}.
Definition inline_boardroom_params : list kername :=
[ <%% Params.H %%>
; <%% Params.generator %%>
].
Definition inline_contract_monad_projection : list kername :=
[ <%% @ContractMonads.chain_height %%>
; <%% @ContractMonads.current_slot %%>
; <%% @ContractMonads.finalized_height %%>
; <%% @ContractMonads.caller_addr %%>
; <%% @ContractMonads.my_addr %%>
; <%% @ContractMonads.my_balance %%>
; <%% @ContractMonads.call_amount %%>
; <%% @ContractMonads.deployment_setup %%>
; <%% @ContractMonads.reject_deployment %%>
; <%% @ContractMonads.accept_deployment %%>
; <%% @ContractMonads.call_msg %%>
; <%% @ContractMonads.my_state %%>
; <%% @ContractMonads.queue %%>
; <%% @ContractMonads.reject_call %%>
; <%% @ContractMonads.accept_call %%>
; <%% @ContractMonads.build_contract %%>
].
Definition to_inline : list kername :=
inline_contract_monad_projection
++ inline_boardroom_params
++ [
<%% OptionMonad.Monad_option %%>
; <%% @ConCert.Execution.ResultMonad.Monad_result %%>
; <%% ContractIniterSetupState %%>
; <%% ContractReceiverStateMsgState %%>
; <%% @contract_initer_monad %%>
; <%% @run_contract_initer %%>
; <%% @run_contract_receiver %%>
; <%% @contract_receiver_monad %%>
; <%% @contract_reader_to_contract_initer %%>
; <%% @result_to_contract_initer %%>
; <%% @contract_reader_to_receiver %%>
; <%% @result_to_contract_receiver %%>
; <%% @ContractReceiver %%>
; <%% @ContractIniter %%>
; <%% @Monad.bind %%>
; <%% @Monad.ret %%>
; <%% @Monad.lift %%>
; <%% bool_rect %%>
; <%% bool_rec %%>
; <%% option_map %%>
; <%% @Extras.with_default %%>
; <%% @BV.setter_from_getter_State_owner %%>
; <%% @BV.setter_from_getter_State_registered_voters %%>
; <%% @BV.setter_from_getter_State_public_keys %%>
; <%% @BV.setter_from_getter_State_setup %%>
; <%% @BV.setter_from_getter_State_tally %%>
; <%% @BV.setter_from_getter_VoterInfo_voter_index %%>
; <%% @BV.setter_from_getter_VoterInfo_vote_hash %%>
; <%% @BV.setter_from_getter_VoterInfo_public_vote %%>
; <%% @BV.set_State_owner %%>
; <%% @BV.set_State_registered_voters %%>
; <%% @BV.set_State_public_keys %%>
; <%% @BV.set_State_setup %%>
; <%% @BV.set_State_tally %%>
; <%% @BV.set_VoterInfo_voter_index %%>
; <%% @BV.set_VoterInfo_vote_hash %%>
; <%% @BV.set_VoterInfo_public_vote %%>
].
(* Time MetaCoq Run ('(env, init_nm, receive_nm) <- quote_and_preprocess to_inline BV_MODULE ;;
tmDefinition "bv_env" env ;;
tmDefinition "bv_init_nm" init_nm ;;
tmDefinition "bv_receive_nm" receive_nm). *)
(** A translation table for definitions we want to remap. The corresponding top-level definitions will be *ignored* *)
Definition TT_remap : list (kername * string) :=
[
remap <%% Amount %%> "tez"
; remap <%% BV.amount_eqb %%> "eqTez"
; remap <%% positive %%> "nat"
; remap <%% Z %%> "int"
; remap <%% Z.of_nat %%> "int"
; remap <%% Z.add %%> "addInt"
; remap <%% Z.sub %%> "subInt"
; remap <%% Z.leb %%> "leInt"
; remap <%% Z.ltb %%> "ltInt"
; remap <%% Z.add %%> "addInt"
; remap <%% Z.eqb %%> "eqInt"
; remap <%% Z.gtb %%> "gtbInt"
; remap <%% Nat.ltb %%> "ltNat"
; remap <%% Z.modulo %%> "modInt"
; remap <%% Z.mul %%> "mulInt"
; remap <%% N.lxor %%> "lxorNat"
; remap <%% N.succ_pos %%> "addNat 1p"
; remap <%% mod_pow %%> "mod_pow"
; remap <%% Egcd.egcd %%> "egcd"
; remap <%% bruteforce_tally_aux %%> (bruteforce_tally_def ++ "in bruteforce_tally_aux")
; remap <%% @List.existsb %%> "existsb"
; remap <%% @List.nth %%> "nth"
; remap <%% @List.firstn %%> "firstn"
; remap <%% @List.skipn %%> "skipn"
; remap <%% Euler.prod %%> "prod"
; remap <%% hash_func %%> "hash_func"
(* ; remap <%% oneN %%> "1p" *)
(* ; remap <%% onePos %%> "1p" *)
; remap <%% four %%> "4p"
; remap <%% seven %%> "7p"
; remap <%% _1234583932 %%> "1234583932"
; remap <%% _23241 %%> "23241"
; remap <%% _159338231 %%> "159338231"
; remap <%% _5 %%> "5"
; remap <%% _3 %%> "3p"
; remap <%% Z3 %%> "3"
; remap <%% _11 %%> "11"
; remap <%% @ActionBody %%> "operation"
; remap <%% @ContractCallContext %%> "(address * (address * (tez * tez)))"
; remap <%% @Chain %%> "(nat * (nat * nat))" (* chain_height, current_slot, finalized_height *)
; remap <%% @chain_height %%> "fst" (* small hack, but valid since ContractCallContext is mapped to a tuple *)
; remap <%% @Blockchain.current_slot %%> "(fun c -> c.(1).(0))" (* small hack, but valid since Chain is mapped to a tuple *)
; remap <%% @finalized_height %%> "(fun c -> snd (snd c)" (* small hack, but valid since Chain is mapped to a tuple *)
; remap <%% @ctx_from %%> "fst" (* small hack, but valid since ContractCallContext is mapped to a tuple *)
; remap <%% @ctx_amount %%> "(fun c -> c.(1).(1).(1))" (* small hack, but valid since ContractCallContext is mapped to a tuple *)
; remap <%% @ctx_contract_address %%> "(fun c -> c.(1).(0))" (* small hack, but valid since ContractCallContext is mapped to a tuple *)
; remap <%% @ctx_contract_balance %%> "(fun c -> c.(1).(1).(0))" (* small hack, but valid since ContractCallContext is mapped to a tuple *)
; remap <%% @AddressMap.add %%> "Map.add"
; remap <%% @AddressMap.find %%> "Map.find"
; remap <%% @AddressMap.of_list %%> "Map.of_list"
; remap <%% @AddressMap.values %%> "Map.values"
; remap <%% @AddressMap.keys %%> "Map.keys"
; remap <%% @AddressMap.empty %%> "(Map [])"
; remap <%% modulus %%> modulus_
; remap <%% BV.encodeA %%> "unsafe_int_to_nat"
; remap <%% BV.encodeNat %%> ""
; remap <%% @List.fold_left %%> "List.fold"
; remap <%% @List.map %%> "List.map"
; remap <%% @List.find %%> "List.find"
; remap <%% @List.length %%> "List.length"
; remap <%% @List.app %%> "List.append"
].
(** A translation table of constructors and some constants. The corresponding definitions will be extracted and renamed. *)
Definition TT_rename : list (string * string) :=
[ ("Some", "Some")
; ("None", "None")
; ("Zpos" ,"int")
; ("Npos" ,"")
; ("Zneg" ,"-")
; ("Z0" ,"0")
; ("0" ,"0p")
; ("N0" ,"0p")
; ("xH" ,"0")
; ("1" ,"1")
; ("2" ,"2p")
; ("S" ,"1p +")
; ("nil", "[]")
; ("true", "true")
; ("false", "false")
; (String.to_string (string_of_kername <%% BV.State %%>), "state") (* we add [storage] so it is printed without the prefix *)
; ("tt", "()")
].
(* Time MetaCoq Run (
t <- liquidity_prepare_extraction PREFIX TT_remap TT_rename to_inline BV_MODULE bv_env bv_init_nm bv_receive_nm ;;
tmDefinition BV_MODULE.(lmd_module_name) t
).
(** We redirect the extraction result for later processing and compiling with the Liquidity compiler *)
Redirect "../extraction/tests/extracted-code/liquidity-extract/BoardroomVoting.liq" MetaCoq Run (tmMsg liquidity_boardroomvoting). *)