Skip to content

Commit 3fb7dab

Browse files
committed
feat: more work to have an example on Keccak
1 parent ad424c0 commit 3fb7dab

File tree

10 files changed

+1005
-331
lines changed

10 files changed

+1005
-331
lines changed

.github/workflows/rocq.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Run the Rocq tests
2424
uses: coq-community/docker-coq-action@v1
2525
with:
26-
custom_image: coqorg/coq:8.17-ocaml-4.14-flambda
26+
custom_image: coqorg/coq:8.20-ocaml-4.14-flambda
2727
custom_script: |
2828
startGroup "Install dependencies"
2929
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
@@ -43,7 +43,7 @@ jobs:
4343
cd ..
4444
endGroup
4545
startGroup "Convert to Rocq"
46-
python scripts/rocq_of_noir.py noir_stdlib/monomorphized_program.json >RocqOfNoir/keccak_monomorphic.v
46+
python scripts/rocq_of_noir.py noir_stdlib/monomorphized_program.json >RocqOfNoir/keccak/monomorphic.v
4747
python scripts/rocq_of_noir.py RocqOfNoir/base64/monomorphized_program.json >RocqOfNoir/base64/monomorphic.v
4848
endGroup
4949
startGroup "Compile Rocq translations"

RocqOfNoir/RecordUpdate.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Ltac2 rec strip_foralls (t : constr) :=
1515

1616
Ltac2 app_arg_count (t : constr) :=
1717
match Constr.Unsafe.kind t with
18-
| Constr.Unsafe.App f args => Array.length args
18+
| Constr.Unsafe.App _ args => Array.length args
1919
| _ => 0
2020
end.
2121

@@ -31,7 +31,7 @@ Ltac2 field_names (ctor_ref : Std.reference) :=
3131

3232
Ltac2 constructor_of_record (t : constr) :=
3333
match Constr.Unsafe.kind t with
34-
| Constr.Unsafe.Ind ind inst =>
34+
| Constr.Unsafe.Ind ind _ =>
3535
Std.ConstructRef (Constr.Unsafe.constructor ind 0)
3636
| _ => Control.throw (Invalid_argument (Some (Message.of_constr t)))
3737
end.
@@ -44,7 +44,7 @@ Ltac2 record_with_set_val (ty : constr) (record : constr)
4444
let (h, args) :=
4545
match Constr.Unsafe.kind ty with
4646
| Constr.Unsafe.App h args => (h, args)
47-
| _ => (ty, Array.empty ())
47+
| _ => (ty, Array.empty)
4848
end in
4949
let ctor := constructor_of_record h in
5050
let getters := List.map (fun getterRef =>

RocqOfNoir/base64/polymorphic.v

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,64 @@ Require Import base64.monomorphic.
55
(** This module provides helpers to show the equality to debug [reflexivity] when it is too long or
66
failing *)
77
Module Eq.
8-
Lemma LowLet {A : Set} (e e' : LowM.t A) k k'
8+
Lemma LowLet {A : Set} (e e' : LowM.t A) k k'
99
(H_e : e = e')
1010
(H_k : forall x, k x = k' x) :
11-
LowM.Let e k = LowM.Let e' k'.
11+
LowM.Let e k = LowM.Let e' k'.
1212
Proof.
1313
rewrite H_e.
1414
replace k with k' by now apply functional_extensionality.
1515
reflexivity.
1616
Qed.
1717

18-
Fixpoint LetMinus {A : Set} (e1 e1' : LowM.t A) (e2 e2' : A -> LowM.t A)
18+
Lemma LetMinus {A : Set} (e1 e1' : LowM.t A) (e2 e2' : A -> LowM.t A)
1919
(H_e1 : e1 = e1')
20-
(H_e2 : forall x, e2 x = e2' x)
21-
{struct e1} :
22-
LowM.let_ e1 e2 = LowM.let_ e1' e2'.
20+
(H_e2 : forall x, e2 x = e2' x) :
21+
LowM.let_ e1 e2 = LowM.let_ e1' e2'.
2322
Proof.
24-
destruct e1; rewrite <- H_e1; simpl.
25-
{ apply H_e2. }
26-
{ f_equal.
27-
apply functional_extensionality; intro x.
28-
now rewrite LetMinus with (e1' := k x) (e2' := e2').
29-
}
30-
{ f_equal.
31-
apply functional_extensionality; intro x.
32-
now rewrite LetMinus with (e1' := k x) (e2' := e2').
33-
}
34-
{ f_equal.
35-
apply functional_extensionality; intro x.
36-
now rewrite LetMinus with (e1' := k x) (e2' := e2').
37-
}
38-
{ reflexivity. }
23+
rewrite H_e1.
24+
replace e2 with e2' by now apply functional_extensionality.
25+
reflexivity.
3926
Qed.
4027

4128
Lemma LetStar e1 e1' e2 e2'
4229
(H_e1 : e1 = e1')
4330
(H_e2 : forall x, e2 x = e2' x) :
44-
M.let_ e1 e2 = M.let_ e1' e2'.
31+
M.let_ e1 e2 = M.let_ e1' e2'.
4532
Proof.
46-
apply LetMinus; hauto lq: on.
33+
rewrite H_e1.
34+
replace e2 with e2' by now apply functional_extensionality.
35+
reflexivity.
4736
Qed.
4837

4938
Lemma LetTilde e1 e1' e2 e2'
5039
(H_e1 : e1 = e1')
5140
(H_e2 : forall x, e2 x = e2' x) :
52-
M.let_strong e1 e2 = M.let_strong e1' e2'.
41+
M.let_strong e1 e2 = M.let_strong e1' e2'.
5342
Proof.
54-
apply LowLet; hauto lq: on.
43+
rewrite H_e1.
44+
replace e2 with e2' by now apply functional_extensionality.
45+
reflexivity.
5546
Qed.
5647

5748
Lemma If e e' t t' f f'
5849
(H_e : e = e')
5950
(H_t : t = t')
6051
(H_f : f = f') :
61-
M.if_ e t f = M.if_ e' t' f'.
52+
M.if_ e t f = M.if_ e' t' f'.
6253
Proof.
6354
f_equal; assumption.
6455
Qed.
56+
57+
Ltac tactic :=
58+
repeat (
59+
intro ||
60+
apply LowLet ||
61+
apply LetMinus ||
62+
apply LetStar ||
63+
apply LetTilde ||
64+
apply If
65+
).
6566
End Eq.
6667

6768
Module Field.
@@ -92,7 +93,7 @@ Module Field.
9293
| _ => M.impossible "wrong number of arguments"
9394
end.
9495

95-
Lemma eq_to_be_radix₁ : get_function "to_be_radix" 1 = closure (to_be_radix (U32.Build_t 40)).
96+
Lemma eq_to_be_radix₁ : get_function "to_be_radix" 1 = closure (to_be_radix {| Integer.value := 40 |}).
9697
Proof.
9798
autorewrite with get_function; f_equal.
9899
Qed.
@@ -101,7 +102,7 @@ End Field.
101102

102103
Module Base64EncodeBE.
103104
Record t : Set := {
104-
table : Array.t U8.t (U32.Build_t 64);
105+
table : Array.t U8.t {| Integer.value := 64 |};
105106
}.
106107

107108
Definition new (α : list Value.t) : M.t :=
@@ -231,7 +232,7 @@ Definition base64_encode_elements (InputElements : U32.t) (α : list Value.t) :
231232
|) ]] in
232233
let~ result := [[ M.copy_mutable (|
233234
M.alloc (Value.Array (
234-
List.repeat (Value.Integer IntegerKind.U8 0) (Z.to_nat (Integer.to_Z InputElements))
235+
List.repeat (Value.Integer IntegerKind.U8 0) (Z.to_nat InputElements.(Integer.value))
235236
))
236237
|) ]] in
237238
do~ [[
@@ -270,7 +271,7 @@ Definition base64_encode_elements (InputElements : U32.t) (α : list Value.t) :
270271
end.
271272

272273
Lemma eq_base64_encode_elements₀ :
273-
get_function "base64_encode_elements" 0 = closure (base64_encode_elements (U32.Build_t 118)).
274+
get_function "base64_encode_elements" 0 = closure (base64_encode_elements {| Integer.value := 118 |}).
274275
Proof.
275276
autorewrite with get_function; apply f_equal.
276277
apply functional_extensionality; intro α.
@@ -288,7 +289,7 @@ Definition base64_encode (InputBytes OutputElements : U32.t) (α : list Value.t)
288289
let~ result := [[ M.copy_mutable (|
289290
M.alloc (Value.Array (List.repeat
290291
(Value.Integer IntegerKind.U8 0)
291-
(Z.to_nat (Integer.to_Z OutputElements))
292+
(Z.to_nat OutputElements.(Integer.value))
292293
))
293294
|) ]] in
294295
let~ BASE64_ELEMENTS_PER_CHUNK := [[ M.copy (|
@@ -371,7 +372,7 @@ Definition base64_encode (InputBytes OutputElements : U32.t) (α : list Value.t)
371372
]] in
372373
let~ slice_base64_chunks := [[ M.copy (|
373374
M.alloc (M.call_closure (|
374-
closure (Field.to_be_radix (U32.Build_t 30)),
375+
closure (Field.to_be_radix {| Integer.value := 30 |}),
375376
[
376377
M.read (| slice |);
377378
Value.Integer IntegerKind.U32 64
@@ -478,7 +479,7 @@ Definition base64_encode (InputBytes OutputElements : U32.t) (α : list Value.t)
478479
]] in
479480
let~ slice_base64_chunks := [[ M.copy (|
480481
M.alloc (M.call_closure (|
481-
closure (Field.to_be_radix (U32.Build_t 30)),
482+
closure (Field.to_be_radix {| Integer.value := 30 |}),
482483
[
483484
M.read (| slice |);
484485
Value.Integer IntegerKind.U32 64
@@ -529,7 +530,7 @@ Definition base64_encode (InputBytes OutputElements : U32.t) (α : list Value.t)
529530
M.alloc (M.write (|
530531
result,
531532
M.call_closure (|
532-
closure (base64_encode_elements (U32.Build_t 118)),
533+
closure (base64_encode_elements {| Integer.value := 118 |}),
533534
[
534535
M.read (| result |)
535536
]
@@ -547,44 +548,14 @@ Definition base64_encode (InputBytes OutputElements : U32.t) (α : list Value.t)
547548
end.
548549

549550
Lemma eq_base64_encode₀ :
550-
get_function "base64_encode" 0 = closure (base64_encode (U32.Build_t 88) (U32.Build_t 118)).
551+
get_function "base64_encode" 0 = closure (base64_encode {| Integer.value := 88 |} {| Integer.value := 118 |}).
551552
Proof.
552553
autorewrite with get_function; apply f_equal.
553554
apply functional_extensionality; intro α.
554555
unfold base64_encode₀.
555556
autorewrite with get_function_eq.
556557
destruct α as [|input α]; [reflexivity|].
557558
destruct α; [|reflexivity].
558-
apply Eq.LetStar; [|reflexivity].
559-
apply Eq.LetTilde; [reflexivity|intro result].
560-
apply Eq.LetTilde; [reflexivity|intro BASE64_ELEMENTS_PER_CHUNK].
561-
apply Eq.LetTilde; [reflexivity|intro BYTES_PER_CHUNK].
562-
apply Eq.LetTilde; [reflexivity|intro num_chunks].
563-
apply Eq.LetTilde. {
564-
apply Eq.LetStar; [reflexivity|intro v].
565-
apply Eq.If; [reflexivity | | reflexivity].
566-
apply Eq.LetTilde. {
567-
reflexivity.
568-
}
569-
intro.
570-
apply Eq.LetTilde; [reflexivity|intro bytes_in_final_chunk].
571-
apply Eq.LetTilde; [reflexivity|intro slice].
572-
apply Eq.LetTilde. {
573-
reflexivity.
574-
}
575-
intro.
576-
apply Eq.LetTilde. {
577-
reflexivity.
578-
}
579-
intro.
580-
apply Eq.LetTilde; [reflexivity|intro slice_base64_chunks].
581-
apply Eq.LetTilde; [reflexivity|intro num_elements_in_final_chunk].
582-
apply Eq.LetTilde. {
583-
reflexivity.
584-
}
585-
intro.
586-
reflexivity.
587-
}
588-
intro.
559+
apply Eq.LetStar; [reflexivity|intro result].
589560
reflexivity.
590561
Qed.

RocqOfNoir/base64/simulation.v

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ Require Import RocqOfNoir.proof.RocqOfNoir.
33
Require Import RocqOfNoir.simulation.RocqOfNoir.
44
Require RocqOfNoir.base64.polymorphic.
55

6-
Import Run.
7-
86
Module Base64EncodeBE.
97
(*
108
struct Base64EncodeBE {
119
table: [u8; 64]
1210
}
1311
*)
1412
Record t : Set := {
15-
table : Array.t U8.t (U32.Build_t 64);
13+
table : Array.t U8.t {| Integer.value := 64 |};
1614
}.
1715

18-
Global Instance Impl_ToValue : ToValue.Trait t := {
16+
Global Instance Impl_ToValue : ToValue t := {
1917
to_value (x : t) :=
2018
Value.Tuple [to_value x.(table)];
2119
}.
@@ -25,7 +23,7 @@ Module Base64EncodeBE.
2523
Proof. reflexivity. Qed.
2624
Global Hint Rewrite rewrite_to_value : to_value.
2725

28-
Definition ascii_codes : list U8.t := List.map U8.Build_t [
26+
Definition ascii_codes : list U8.t := List.map (Integer.Build_t IntegerKind.U8) [
2927
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;
3028
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;
3129
48; 49; 50; 51; 52; 53; 54; 55; 56; 57;
@@ -226,12 +224,12 @@ pub fn base64_encode_elements<let InputElements: u32>(input: [u8; InputElements]
226224
Definition base64_encode_elements_for_init {InputElements : U32.t}
227225
(input : Array.t U8.t InputElements) :
228226
Array.t U8.t InputElements :=
229-
Array.repeat InputElements (U8.Build_t 0).
227+
Array.repeat InputElements {| Integer.value := 0 |}.
230228

231229
Definition base64_encode_elements_for_body (p : Z) {InputElements : U32.t}
232230
(input : Array.t U8.t InputElements) (i : Z) :
233231
MS! (Array.t U8.t InputElements) unit :=
234-
let i : U32.t := U32.Build_t i in
232+
let i : U32.t := {| Integer.value := i |} in
235233
letS! input_i := return!toS! (Array.read input i) in
236234
letS! input_i := return!toS! (cast_to_field p input_i) in
237235
letS! new_result_i :=
@@ -249,7 +247,7 @@ Definition base64_encode_elements (p : Z) {InputElements : U32.t}
249247
doS! (
250248
foldS!
251249
tt
252-
(List.map Z.of_nat (List.seq 0 (Z.to_nat (ToZ.to_Z InputElements))))
250+
(List.map Z.of_nat (List.seq 0 (Z.to_nat InputElements.(Integer.value))))
253251
(fun result i => base64_encode_elements_for_body p input i)
254252
) in
255253
letS! result := readS! in
@@ -270,7 +268,7 @@ Lemma map_listUpdate_error_eq {A B : Type} (f : A -> B) (l : list A) (i : nat) (
270268
List.listUpdate_error (List.map f l) i y = option_map (List.map f) (List.listUpdate_error l i x).
271269
Proof.
272270
unfold List.listUpdate_error.
273-
rewrite List.map_length.
271+
rewrite List.length_map.
274272
destruct (_ <? _)%nat; cbn; f_equal.
275273
now erewrite map_listUpdate_eq.
276274
Qed.
@@ -280,7 +278,7 @@ Lemma run_base64_encode_elements
280278
{InputElements : U32.t}
281279
(input : Array.t U8.t InputElements)
282280
(H_InputElements : Integer.Valid.t InputElements)
283-
(H_input : Array.Valid.t input) :
281+
(H_input : Array.Valid.t (fun _ => True) input) :
284282
let output := base64_encode_elements p input in
285283
let state_end : State.t :=
286284
State.init <|
@@ -324,7 +322,7 @@ Proof.
324322
|>
325323
)
326324
(accumulator_in := base64_encode_elements_for_init input)
327-
(len := Z.to_nat InputElements.(U32.value))
325+
(len := Z.to_nat InputElements.(Integer.value))
328326
(body_expression := base64_encode_elements_for_body p input).
329327
2: {
330328
unfold set.
@@ -354,7 +352,7 @@ Proof.
354352
autorewrite with to_value.
355353
match goal with
356354
| |- context[Value.Integer IntegerKind.Field ?i] =>
357-
change (Value.Integer IntegerKind.Field i) with (to_value (Field.Build_t i))
355+
change (Value.Integer IntegerKind.Field i) with (to_value (Self := Field.t) {| Integer.value := i |})
358356
end.
359357
apply Base64EncodeBE.run_get.
360358
}

0 commit comments

Comments
 (0)