Skip to content

Commit a559efd

Browse files
committed
wrote a version of the rewrite rule that works! need to put it in its own pass now.
1 parent 2144195 commit a559efd

File tree

4 files changed

+126
-81
lines changed

4 files changed

+126
-81
lines changed

fiat-c/src/secp256k1_dettman_64.c

+84-72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PushButtonSynthesis/DettmanMultiplication.v

+3-4
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,14 @@ Section __.
104104
Definition input_bounds : list (ZRange.type.option.interp base.type.Z)
105105
:= fold_left (fun l i => Some r[0 ~> Qceiling (2 * input_magnitude * ((weightf (i + 1) / weightf i) - 1))]%zrange :: l) (seq 0 (n - 1)) [] ++
106106
[Some r[0 ~> Qceiling (2 * input_magnitude * (2^last_limb_width - 1))]%zrange].
107-
(*Definition output_bounds : list (ZRange.type.option.interp base.type.Z)
107+
Definition output_bounds : list (ZRange.type.option.interp base.type.Z)
108108
:= fold_left (fun l i => Some r[0 ~> Qceiling (2 * output_magnitude_first_limbs * ((weightf (i + 1) / weightf i) - 1))]%zrange :: l) (seq 0 (n - 1)) [] ++
109109
[Some r[0 ~> Qceiling (2 * output_magnitude_last_limb * (2^last_limb_width - 1))]%zrange].
110-
*)
111-
Definition output_bounds : list (ZRange.type.option.interp base.type.Z) :=
110+
(*Definition output_bounds : list (ZRange.type.option.interp base.type.Z) :=
112111
match inbounds_multiplier with
113112
| Some _ => [None; None; None; None; None]
114113
| None => [None; None; None; None; None]
115-
end.
114+
end.*)
116115
Local Existing Instance default_translate_to_fancy.
117116
Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize.
118117
Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values.

src/Rewriter/Rules.v

+13-3
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,14 @@ Definition arith_with_casts_rewrite_rulesT (adc_no_carry_to_add : bool) : list (
337337
0 ∈ rland -> 0 ∈ r0
338338
-> cstZ rland (Z.land (cstZ r0 ('0)) (cstZ rv v)) = cstZ r0 ('0))
339339
(* try to use lower bounds = 0, rland.upper divides rv.upper to remove double cast. *)
340-
; (forall rland rm1 rv v,
340+
(*; (forall rland rm1 rv v,
341341
rland.(upper) ∈ rm1
342342
-> rland.(upper) = Z.ones (Z.succ (Z.log2 rland.(upper)))
343343
-> 0 = rland.(lower)
344344
-> 0 = rv.(lower)
345345
-> 0 <= rv.(upper)
346346
-> (rv.(upper) + 1) mod (rland.(upper) + 1) = 0
347-
-> cstZ rland (Z.land (cstZ rv v) (cstZ rm1 ('rland.(upper)))) = cstZ rland v)
347+
-> cstZ rland (Z.land (cstZ rv v) (cstZ rm1 ('rland.(upper)))) = cstZ rland v)*)
348348
; (forall rland rm1 rv v,
349349
(rv <= rland)%zrange -> -1 ∈ rm1
350350
-> cstZ rland (Z.land (cstZ rv v) (cstZ rm1 ('-1))) = cstZ rv v)
@@ -1060,7 +1060,17 @@ Section with_bitwidth.
10601060
[mymap dont_do_again []
10611061
; mymap
10621062
do_again
1063-
[(forall A B x y, @fst A B (x, y) = x)
1063+
[
1064+
(* owen put this here, and he needs to remove it. *)
1065+
(forall rland rm1 rv v,
1066+
rland.(upper) ∈ rm1
1067+
-> rland.(upper) = Z.ones (Z.succ (Z.log2 rland.(upper)))
1068+
-> 0 = rland.(lower)
1069+
-> 0 = rv.(lower)
1070+
-> 0 <= rv.(upper)
1071+
-> (rv.(upper) + 1) mod (rland.(upper) + 1) = 0
1072+
-> cstZ rland (Z.land (cstZ rv v) (cstZ rm1 ('rland.(upper)))) = cstZ rland v)
1073+
; (forall A B x y, @fst A B (x, y) = x)
10641074
; (forall A B x y, @snd A B (x, y) = y)
10651075
(** In order to avoid tautological compares, we need to deal with carry/borrows being 0 *)
10661076
; (forall r0 s x y r1 r2,

src/Rewriter/RulesProofs.v

+26-2
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ Lemma arith_with_casts_rewrite_rules_proofs (adc_no_carry_to_add : bool)
570570
Proof using Type.
571571
start_proof; auto; intros; try lia.
572572
all: repeat interp_good_t_step_related.
573-
11: { replace (ident.cast rland v) with (ident.cast rland (ident.cast rv v)).
573+
(*11: { replace (ident.cast rland v) with (ident.cast rland (ident.cast rv v)).
574574
- interp_good_t_step_arith. interp_good_t_step_arith. rewrite Z.land_ones.
575575
+ replace (2 ^ Z.succ (Z.log2 (upper rland))) with (upper rland + 1).
576576
-- rewrite <- ident.cast_out_of_bounds_simple_0_mod.
@@ -592,7 +592,7 @@ Proof using Type.
592592
+ lia.
593593
+ Search Z.ones. rewrite H0. apply Ones.Z.ones_nonneg.
594594
remember (Z.log2_nonneg (upper)). lia.
595-
}
595+
}*)
596596
(* Search Z.ones. rewrite H0. apply Ones.Z.ones_nonneg.
597597
remember (Z.log2_nonneg (upper)). lia.
598598
}
@@ -861,6 +861,30 @@ Proof using Type.
861861
by (intros; apply Z.pow_gt_lin_r; auto with zarith).
862862

863863
start_proof; auto; intros; try lia.
864+
1: {
865+
replace (ident.cast rland v) with (ident.cast rland (ident.cast rv v)).
866+
- interp_good_t_step_arith. interp_good_t_step_arith. rewrite Z.land_ones.
867+
+ replace (2 ^ Z.succ (Z.log2 (upper rland))) with (upper rland + 1).
868+
-- rewrite <- ident.cast_out_of_bounds_simple_0_mod.
869+
++ destruct rland. simpl in *. subst. apply ident.cast_idempotent.
870+
++ rewrite H2. apply Ones.Z.ones_nonneg. remember (Z.log2_nonneg (upper rland)). lia.
871+
-- remember (Z.log2 _) as x. rewrite H2. subst. rewrite Z.ones_equiv. lia.
872+
+ remember (Z.log2_nonneg (upper rland)). lia.
873+
- Search ident.cast. destruct rland. destruct rv. simpl in *. subst.
874+
(*Search ident.cast. Search ZRange.normalize.
875+
repeat rewrite <- (ident.cast_normalize r[0~>upper]).
876+
repeat rewrite <- (ident.cast_normalize r[0~>upper0]).*)
877+
Check ident.cast_out_of_bounds_simple_0_mod.
878+
repeat rewrite ident.cast_out_of_bounds_simple_0_mod.
879+
+ Search ((_ mod _) mod _). rewrite <- Z.mod_div_mod_full.
880+
-- reflexivity.
881+
-- Search Z.divide. rewrite <- Z.mod_divide_full. assumption.
882+
+ Search Z.ones. rewrite H2. apply Ones.Z.ones_nonneg.
883+
remember (Z.log2_nonneg (upper)). lia.
884+
+ lia.
885+
+ Search Z.ones. rewrite H2. apply Ones.Z.ones_nonneg.
886+
remember (Z.log2_nonneg (upper)). lia.
887+
}
864888
all: repeat interp_good_t_step_related.
865889
all: systematically_handle_casts; autorewrite with zsimplify_fast; try reflexivity.
866890
all: subst; rewrite !ident.platform_specific_cast_0_is_mod, ?Z.sub_add, ?Z.mod_mod by lia; try reflexivity.

0 commit comments

Comments
 (0)