Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bedrock2 pipeline to call external functions. #1640

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
765f692
wip
jadephilipoom Aug 1, 2023
eaefa72
wip
jadephilipoom Aug 1, 2023
33f24e1
pre-sed
jadephilipoom Aug 3, 2023
4c9b915
post sed
jadephilipoom Aug 3, 2023
ca6c256
cmd working with add-get-carry
jadephilipoom Aug 7, 2023
e640bb7
wip, about to start on expr
jadephilipoom Aug 8, 2023
9332427
wip, command proof was working before change to fun nextn =>
jadephilipoom Aug 10, 2023
518afa0
cmd proof working again
jadephilipoom Aug 10, 2023
f2c148b
proved stuff about inversion things
jadephilipoom Aug 10, 2023
4b40d92
wip
jadephilipoom Aug 11, 2023
e62d661
wip
jadephilipoom Aug 11, 2023
f0a8ad2
func proof works
jadephilipoom Aug 11, 2023
4952bcf
wip on signature, why is it broken?
jadephilipoom Aug 11, 2023
5bd78bf
signature passing
jadephilipoom Aug 11, 2023
af6a79b
wip
jadephilipoom Aug 11, 2023
08e852d
wip, changing expr cast rules for tuples
jadephilipoom Aug 16, 2023
8628f75
starting with turning fst/snd case to only apply to vars
jadephilipoom Aug 16, 2023
408e44e
reverting previous and loosening require_cast_for_arg instead
jadephilipoom Aug 16, 2023
d2da65f
computable valid expr impl1 works
jadephilipoom Aug 17, 2023
db521d8
computable valid expr works completely
jadephilipoom Aug 17, 2023
2cc14ac
starting to allow zz vars
jadephilipoom Aug 17, 2023
ad7b9fb
zz vars working?
jadephilipoom Aug 17, 2023
aa087e3
starting to adapt zselect, which is failing because of the new [0,1] …
jadephilipoom Aug 17, 2023
fe747d2
won't work, wrote a new idea
jadephilipoom Aug 17, 2023
deca8c7
proof working, starting on valid computable
jadephilipoom Aug 24, 2023
f4fcd25
starting to adjust to allow any maskable range in cast, because pairs…
jadephilipoom Aug 24, 2023
b0f1ab7
mask proof case working
jadephilipoom Aug 24, 2023
3f92703
works except for sub
jadephilipoom Aug 24, 2023
908f6e5
works with sub, but fails to handle carries which are literals
jadephilipoom Aug 25, 2023
b24b730
p224 add compiles
jadephilipoom Aug 25, 2023
c3aad15
clean up debugging printouts and reset them to focus on mul, comment …
jadephilipoom Aug 25, 2023
5340e29
remove debugging file
jadephilipoom Aug 25, 2023
09c490d
revert fst/snd cast changes
jadephilipoom Aug 25, 2023
13b7e0a
remove printing statements and dead lemma
jadephilipoom Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Bedrock/Field/Common/Arrays/ByteBounds.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Import Partition.

Section ByteBounds.
Context
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}.
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Context {ok : ok}.
Context (n : nat).

Expand Down
4 changes: 2 additions & 2 deletions src/Bedrock/Field/Common/Arrays/MakeAccessSizes.v
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Import Types.Notations.

Section __.
Context
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}.
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Existing Instances rep.Z rep.listZ_mem.

Let all_access_sizes :=
Expand Down
4 changes: 2 additions & 2 deletions src/Bedrock/Field/Common/Arrays/MakeListLengths.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Existing Instances rep.Z rep.listZ_mem.

Section with_parameters.
Context
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}.
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.

Fixpoint list_lengths_repeat_base (n : nat) t : base_listonly nat t :=
match t as t0 return base_listonly nat t0 with
Expand Down
4 changes: 2 additions & 2 deletions src/Bedrock/Field/Common/Arrays/MaxBounds.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Import ListNotations.

Section MaxBounds.
Context
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}.
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Context {ok : ok}.
Context (n : nat).

Expand Down
4 changes: 2 additions & 2 deletions src/Bedrock/Field/Common/Names/MakeNames.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Existing Instances rep.Z rep.listZ_mem.

Section with_parameters.
Context
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}.
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Context {inname_gen outname_gen : nat -> string}.

Fixpoint make_names
Expand Down
20 changes: 12 additions & 8 deletions src/Bedrock/Field/Common/Types.v
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ Class parameters
{env: map.map String.string (list String.string * list String.string * Syntax.cmd)}
{ext_spec: bedrock2.Semantics.ExtSpec}
{varname_gen : nat -> String.string}
(* add_carryx should take 3 arguments (x,y,carry) and return 2 values (sum, carry) *)
{add_carryx_funcname : String.string}
(* sub_borrowx should take 3 arguments (x,y,borrow) and return 2 values (difference, borrow) *)
{sub_borrowx_funcname : String.string}
{error : Syntax.expr.expr} := parameters_sentinel : unit.

Section WithParameters.
Context
{width BW word mem locals env ext_spec varname_gen error}
Context
{width BW word mem locals env ext_spec add_carryx sub_borrowx varname_gen error}
`{parameters_sentinel : @parameters
width BW word mem locals env ext_spec varname_gen error}.
width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing).
Class ok {parameters_sentinel : parameters} :=
{
Expand All @@ -68,10 +72,10 @@ End WithParameters.

Module rep.
Section rep.
Context
{width BW word mem locals env ext_spec varname_gen error}
Context
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters
width BW word mem locals env ext_spec varname_gen error}.
width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing).

Class rep {parameters_sentinel : parameters} (t : base.type) :=
Expand Down Expand Up @@ -161,9 +165,9 @@ End rep.

Section defs.
Context
{width BW word mem locals env ext_spec varname_gen error}
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters
width BW word mem locals env ext_spec varname_gen error}.
width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing).
Context
(* list representation -- could be local or in-memory *)
Expand Down
72 changes: 72 additions & 0 deletions src/Bedrock/Field/Common/Util.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,78 @@ Import ListNotations.

Import AbstractInterpretation.Compilers.

Section invert_expr.
(* TODO: move somewhere appropriate in the rewriter *)
Lemma invert_App_Z_cast_Some {var} e r x :
invert_expr.invert_App_Z_cast (var:=var) e = Some (r, x) ->
e = (expr.App (expr.App (expr.Ident ident.Z_cast)
(expr.Ident (ident.Literal (t:=base.type.zrange) r))) x).
Proof.
cbv [invert_expr.invert_App_Z_cast Crypto.Util.Option.bind].
lazymatch goal with
| |- context [invert_expr.invert_App ?x] =>
let H := fresh in
destruct (invert_expr.invert_App x) as [ [? [? ?] ] | ] eqn:H;
[ | congruence ];
apply Inversion.Compilers.expr.invert_App_Some in H
end.
cbn [fst snd projT2] in *; subst.
break_match; try congruence; [ ]. intros.
repeat lazymatch goal with
| H : Some _ = Some _ |- _ => inversion H; subst; clear H
| H : invert_expr.invert_Z_cast _ = Some _ |- _ =>
apply InversionExtra.Compilers.expr.invert_Z_cast_Some_Z in H;
subst
end.
reflexivity.
Qed.

(* TODO: move somewhere appropriate in the rewriter *)
Lemma invert_App_Z_cast2_Some {var} e r1 r2 x :
invert_expr.invert_App_Z_cast2 (var:=var) e = Some (r1, r2, x) ->
e = (expr.App (expr.App (expr.Ident ident.Z_cast2)
(expr.App (expr.App (expr.Ident ident.pair)
(expr.Ident (ident.Literal (t:=base.type.zrange) r1)))
(expr.Ident (ident.Literal (t:=base.type.zrange) r2)))) x).
Proof.
cbv [invert_expr.invert_App_Z_cast2 Crypto.Util.Option.bind].
lazymatch goal with
| |- context [invert_expr.invert_App ?x] =>
let H := fresh in
destruct (invert_expr.invert_App x) as [ [? [? ?] ] | ] eqn:H;
[ | congruence ];
apply Inversion.Compilers.expr.invert_App_Some in H
end.
cbn [fst snd projT2] in *; subst.
break_match; try congruence; [ ]. intros.
repeat lazymatch goal with
| H : Some _ = Some _ |- _ => inversion H; subst; clear H
| H : invert_expr.invert_Z_cast2 _ = Some _ |- _ =>
apply InversionExtra.Compilers.expr.invert_Z_cast2_Some_ZZ in H;
subst
end.
reflexivity.
Qed.

(* TODO: move somewhere appropriate in the rewriter *)
Lemma invert_App_Z_cast_eq_Some {var} x r :
invert_expr.invert_App_Z_cast (var:=var)
(expr.App (expr.App (expr.Ident ident.Z_cast)
(expr.Ident (ident.Literal r)))
x) = Some (r, x).
Proof. reflexivity. Qed.

(* TODO: move somewhere appropriate in the rewriter *)
Lemma invert_App_Z_cast2_eq_Some {var} x r1 r2 :
invert_expr.invert_App_Z_cast2 (var:=var)
(expr.App (expr.App (expr.Ident ident.Z_cast2)
(expr.App (expr.App (expr.Ident ident.pair)
(expr.Ident (ident.Literal r1)))
(expr.Ident (ident.Literal r2))))
x) = Some (r1, r2, x).
Proof. reflexivity. Qed.
End invert_expr.

Section Maps.
Local Hint Mode map.map - - : typeclass_instances.
Context {key value} {key_eqb}
Expand Down
4 changes: 2 additions & 2 deletions src/Bedrock/Field/Interface/Representation.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.

Section Representation.
Context
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}.
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.
Context {field_parameters : FieldParameters}
{p_ok : Types.ok}.
Context (n n_bytes : nat) (weight : nat -> Z)
Expand Down
8 changes: 4 additions & 4 deletions src/Bedrock/Field/Stringification/Stringification.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Local Open Scope list_scope.

Section with_parameters.
Context
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}.
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}.

Fixpoint make_base_var_data {t}
: base_ltype t -> list_lengths (type.base t) ->
Expand Down Expand Up @@ -114,8 +114,8 @@ Definition bedrock_func_to_lines (f : string * func)
[c_func f].

Definition wrap_call
{width BW word mem locals env ext_spec varname_gen error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
`{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
{t}
(indata : type.for_each_lhs_of_arrow var_data t)
(outdata : base_var_data (type.final_codomain t))
Expand Down
54 changes: 53 additions & 1 deletion src/Bedrock/Field/Synthesis/Examples/p224_64_new.v
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,31 @@ Section Field.
Definition from_mont_string := prefix ++ "from_mont".

(* Call fiat-crypto pipeline on all field operations *)
Instance p224_ops : @word_by_word_Montgomery_ops from_mont_string to_mont_string _ _ _ _ _ _ _ _ _ _ _ (WordByWordMontgomery.n m machine_wordsize) m.
Instance p224_ops : @word_by_word_Montgomery_ops from_mont_string to_mont_string _ _ _ _ _ _ _ _ _ _ _ _ _ (WordByWordMontgomery.n m machine_wordsize) m.
Proof using Type. Time constructor; make_computed_op. Defined.

(* TODO: remove these debugging printouts! *)
Goal False.
(* Import things to make names shorter *)
Require Import IdentifiersBasicGENERATED.
Require Import Rewriter.Language.Language.

Set Printing Depth 100000.

(* The below lines will show the bedrock2 translation of modular
multiplication. Look for "ERROR" to see where in the translation ran into
an expression it couldn't handle. *)
pose (F:=b2_func mul_op).
lazy [b2_func mul_op p224_ops] in F.
clear F.

(* The below lines will show the fiat-crypto pipeline output expression for
modular multiplication. It's a big expression, so printing it takes a few
minutes. *)
pose (X:=res mul_op).
Time lazy [res mul_op p224_ops] in X.

Abort.

(**** Translate each field operation into bedrock2 and apply bedrock2 backend
field pipeline proofs to prove the bedrock2 functions are correct. ****)
Expand Down Expand Up @@ -83,6 +105,8 @@ Section Field.

Derive p224_from_bytes
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_from_bytes
(field_representation:=field_representation_raw m)
(p224_from_bytes :: functions))
Expand All @@ -91,30 +115,44 @@ Section Field.

Derive p224_to_bytes
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_to_bytes
(field_representation:=field_representation_raw m)
(p224_to_bytes :: functions))
As p224_to_bytes_correct.
Proof. Time derive_bedrock2_func to_bytes_op. Qed.

(* TODO: this proof doesn't work because `mul_split` is not yet supported. *)
(*
Derive p224_mul
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_BinOp bin_mul
(field_representation:=field_representation m)
(p224_mul :: functions))
As p224_mul_correct.
Proof. Time derive_bedrock2_func mul_op. Qed.
*)

(* TODO: this proof doesn't work because `mul_split` is not yet supported. *)
(*
Derive p224_square
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_UnOp un_square
(field_representation:=field_representation m)
(p224_square :: functions))
As p224_square_correct.
Proof. Time derive_bedrock2_func square_op. Qed.
*)

Derive p224_add
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_BinOp bin_add
(field_representation:=field_representation m)
(p224_add :: functions))
Expand All @@ -123,15 +161,21 @@ Section Field.

Derive p224_sub
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_BinOp bin_sub
(field_representation:=field_representation m)
(p224_sub :: functions))
As p224_sub_correct.
Proof. Time derive_bedrock2_func sub_op. Qed.

(* TODO: this proof doesn't work because `mul_split` is not yet supported. *)
(*
(*TODO: adapt derive_bedrock2_func to also derive the remaining functions.*)
Derive p224_from_mont
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_UnOp un_from_mont
(field_representation:=field_representation m)
(p224_from_mont :: functions))
Expand All @@ -144,9 +188,14 @@ Section Field.
+ auto.
+ auto.
Qed.
*)

(* TODO: this proof doesn't work because `mul_split` is not yet supported. *)
(*
Derive p224_to_mont
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_UnOp un_to_mont
(field_representation:=field_representation m)
(p224_to_mont :: functions))
Expand All @@ -157,9 +206,12 @@ Section Field.
- eapply Func.valid_func_bool_iff. abstract vm_cast_no_check (eq_refl true).
Unshelve. all: auto.
Qed.
*)

Derive p224_select_znz
SuchThat (forall functions,
Cmd.spec_of_add_carryx (add_carryx:=Defaults.add_carryx) functions ->
Cmd.spec_of_sub_borrowx (sub_borrowx:=Defaults.sub_borrowx) functions ->
spec_of_selectznz
(field_representation:=field_representation m)
(p224_select_znz :: functions))
Expand Down
6 changes: 3 additions & 3 deletions src/Bedrock/Field/Synthesis/New/ComputedOp.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Require Import Crypto.Language.API.
Import API.Compilers.

Record computed_op
{width BW word mem locals env ext_spec varname_gen error}
{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen error}
{width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
{parameters_sentinel : @parameters width BW word mem locals env ext_spec varname_gen add_carryx sub_borrowx error}
{t} {op : Pipeline.ErrorT (API.Expr t)}
{name : String.string}
{insizes outsizes inlengths}
Expand All @@ -17,7 +17,7 @@ Record computed_op
res_eq : op = ErrorT.Success res;
func_eq : b2_func = make_bedrock_func insizes outsizes inlengths res;
}.
Global Arguments computed_op {_ _ _ _ _ _ _ _ _ _ t}.
Global Arguments computed_op {_ _ _ _ _ _ _ _ _ _ _ _ t}.

Ltac make_computed_op :=
eapply Build_computed_op;
Expand Down
Loading