Library QedkConcrete.altoutputs
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Alt_bn128 MonadVM.Ec.Alt_bn128
QedkConcrete.nativefieldbounds QedkConcrete.nativepointbounds
QedkConcrete.precompileoutputs QedkConcrete.modexpoutputs.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Alt := Ec.Alt_bn128.
Module AltPC := Precompiles.Alt_bn128.
Section AltOutputs.
Context {defaultC2 : Alt.C_2.(Ec.Curve.Make.Make_result.t)}
{defaultC1 : Alt.C_1.(Ec.Curve.Make.Make_result.t)}.
Local Definition prime := @Alt.F_p_fargs .
Local Definition curve := @Alt.C_1_fargs .
Local Definition decoder := @AltPC.opened_module_165_fargs defaultC2 defaultC1.
Local Definition coordBound := @primeFieldBound prime.
Local Definition pointBound := @curvePointBound _ curve coordBound.
Lemma alt_modulus_bound : 0 < @PF.modulus prime < 2^256.
Proof. vm_compute. split; reflexivity. Qed.
Lemma alt_field_closed : fieldClosed (@Curve.F _ curve) coordBound.
Proof.
constructor.
- apply prime_zero_bound. exact (proj1 alt_modulus_bound).
- intro n. apply prime_int_bound. exact (proj1 alt_modulus_bound).
- intros x y Hx Hy. apply prime_add_bound; assumption.
- intros x y Hx Hy. apply prime_sub_bound; assumption.
- intros x y _ _. apply prime_mul_bound. exact (proj1 alt_modulus_bound).
- intros x y _ _. apply prime_div_bound. exact (proj1 alt_modulus_bound).
Qed.
Lemma alt_parameter_bound : coordBound (@Curve.P _ curve).(Ec.Curve.Make_P_signature.a_value).
Proof. apply prime_zero_bound. exact (proj1 alt_modulus_bound). Qed.
Lemma alt_point_parser : nativeEnsures pointBound
(@EP.point_c1 _ _ _ _ decoder).
Proof.
apply (ec_point_parse_bound coordBound).
- intros input output Hout. eapply prime_parse_bound;
[exact (proj1 alt_modulus_bound)|exact Hout].
- intros x y point Hx Hy Hpoint. eapply curve_parse_bound;
[exact Hx|exact Hy|exact Hpoint].
Qed.
Lemma alt_point_encoding point : pointBound point →
OCamlString.length (@EP.delta_1_inv _ _ _ _ decoder point) = 64.
Proof.
intro Hpoint.
pose proof (@curve_coords_bound _ curve coordBound point
(field_zero_closed _ _ alt_field_closed) Hpoint) as [Hx Hy].
change (OCamlString.length (@EP.delta_1_inv _ _ _ _ decoder point) = 2 × 32).
apply (@ec_point_encoding_length _ _ _ _ decoder (Opaque.legacy_witness String.EmptyString) (Opaque.legacy_witness (Ascii.ascii_of_nat 0)) point).
- change (0 ≤ 32 < 2^62). lia.
- change (0 ≤ fst (@Curve.coords _ curve point) < 2^(8×32)).
change (0 ≤ fst (@Curve.coords _ curve point) < @PF.modulus prime) in Hx.
pose proof alt_modulus_bound. lia.
- change (0 ≤ snd (@Curve.coords _ curve point) < 2^(8×32)).
change (0 ≤ snd (@Curve.coords _ curve point) < @PF.modulus prime) in Hy.
pose proof alt_modulus_bound. lia.
Qed.
End AltOutputs.
Lemma alt_add_output_bounded msg :
OCamlString.length (Evmc.Result.t.output_data (@AltPC.add msg)) ≤ 64.
Proof.
unfold AltPC.add. apply native_run_output; [lia|].
apply native_output_bind; intros [].
eapply native_ensures_bind; [apply (@alt_point_parser AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1)|]. intros left Hleft.
eapply native_ensures_bind; [apply (@alt_point_parser AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1)|]. intros right Hright.
apply native_ensures_return. rewrite (@alt_point_encoding AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1); [lia|].
apply curve_add_bound; [apply alt_field_closed|apply alt_parameter_bound|exact Hleft|exact Hright].
Qed.
Lemma alt_mul_output_bounded {UR : Basics.Unreachable Numeric.U256.Repr.t}
{UW : Basics.Unreachable Numeric.U256.t} msg :
OCamlString.length (Evmc.Result.t.output_data (@AltPC.mul msg)) ≤ 64.
Proof.
unfold AltPC.mul. apply native_run_output; [lia|].
apply native_output_bind; intros [].
eapply native_ensures_bind; [apply (@alt_point_parser AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1)|]. intros point Hpoint.
apply native_ensures_any; intro scalar.
apply native_ensures_return. rewrite (@alt_point_encoding AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1); [lia|].
apply curve_mul_bound; [apply alt_field_closed|apply alt_parameter_bound|exact Hpoint].
Qed.
Print Assumptions alt_add_output_bounded.
Print Assumptions alt_mul_output_bounded.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Precompiles.Alt_bn128 MonadVM.Ec.Alt_bn128
QedkConcrete.nativefieldbounds QedkConcrete.nativepointbounds
QedkConcrete.precompileoutputs QedkConcrete.modexpoutputs.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Alt := Ec.Alt_bn128.
Module AltPC := Precompiles.Alt_bn128.
Section AltOutputs.
Context {defaultC2 : Alt.C_2.(Ec.Curve.Make.Make_result.t)}
{defaultC1 : Alt.C_1.(Ec.Curve.Make.Make_result.t)}.
Local Definition prime := @Alt.F_p_fargs .
Local Definition curve := @Alt.C_1_fargs .
Local Definition decoder := @AltPC.opened_module_165_fargs defaultC2 defaultC1.
Local Definition coordBound := @primeFieldBound prime.
Local Definition pointBound := @curvePointBound _ curve coordBound.
Lemma alt_modulus_bound : 0 < @PF.modulus prime < 2^256.
Proof. vm_compute. split; reflexivity. Qed.
Lemma alt_field_closed : fieldClosed (@Curve.F _ curve) coordBound.
Proof.
constructor.
- apply prime_zero_bound. exact (proj1 alt_modulus_bound).
- intro n. apply prime_int_bound. exact (proj1 alt_modulus_bound).
- intros x y Hx Hy. apply prime_add_bound; assumption.
- intros x y Hx Hy. apply prime_sub_bound; assumption.
- intros x y _ _. apply prime_mul_bound. exact (proj1 alt_modulus_bound).
- intros x y _ _. apply prime_div_bound. exact (proj1 alt_modulus_bound).
Qed.
Lemma alt_parameter_bound : coordBound (@Curve.P _ curve).(Ec.Curve.Make_P_signature.a_value).
Proof. apply prime_zero_bound. exact (proj1 alt_modulus_bound). Qed.
Lemma alt_point_parser : nativeEnsures pointBound
(@EP.point_c1 _ _ _ _ decoder).
Proof.
apply (ec_point_parse_bound coordBound).
- intros input output Hout. eapply prime_parse_bound;
[exact (proj1 alt_modulus_bound)|exact Hout].
- intros x y point Hx Hy Hpoint. eapply curve_parse_bound;
[exact Hx|exact Hy|exact Hpoint].
Qed.
Lemma alt_point_encoding point : pointBound point →
OCamlString.length (@EP.delta_1_inv _ _ _ _ decoder point) = 64.
Proof.
intro Hpoint.
pose proof (@curve_coords_bound _ curve coordBound point
(field_zero_closed _ _ alt_field_closed) Hpoint) as [Hx Hy].
change (OCamlString.length (@EP.delta_1_inv _ _ _ _ decoder point) = 2 × 32).
apply (@ec_point_encoding_length _ _ _ _ decoder (Opaque.legacy_witness String.EmptyString) (Opaque.legacy_witness (Ascii.ascii_of_nat 0)) point).
- change (0 ≤ 32 < 2^62). lia.
- change (0 ≤ fst (@Curve.coords _ curve point) < 2^(8×32)).
change (0 ≤ fst (@Curve.coords _ curve point) < @PF.modulus prime) in Hx.
pose proof alt_modulus_bound. lia.
- change (0 ≤ snd (@Curve.coords _ curve point) < 2^(8×32)).
change (0 ≤ snd (@Curve.coords _ curve point) < @PF.modulus prime) in Hy.
pose proof alt_modulus_bound. lia.
Qed.
End AltOutputs.
Lemma alt_add_output_bounded msg :
OCamlString.length (Evmc.Result.t.output_data (@AltPC.add msg)) ≤ 64.
Proof.
unfold AltPC.add. apply native_run_output; [lia|].
apply native_output_bind; intros [].
eapply native_ensures_bind; [apply (@alt_point_parser AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1)|]. intros left Hleft.
eapply native_ensures_bind; [apply (@alt_point_parser AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1)|]. intros right Hright.
apply native_ensures_return. rewrite (@alt_point_encoding AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1); [lia|].
apply curve_add_bound; [apply alt_field_closed|apply alt_parameter_bound|exact Hleft|exact Hright].
Qed.
Lemma alt_mul_output_bounded {UR : Basics.Unreachable Numeric.U256.Repr.t}
{UW : Basics.Unreachable Numeric.U256.t} msg :
OCamlString.length (Evmc.Result.t.output_data (@AltPC.mul msg)) ≤ 64.
Proof.
unfold AltPC.mul. apply native_run_output; [lia|].
apply native_output_bind; intros [].
eapply native_ensures_bind; [apply (@alt_point_parser AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1)|]. intros point Hpoint.
apply native_ensures_any; intro scalar.
apply native_ensures_return. rewrite (@alt_point_encoding AltPC._rocq_fallback_add_0 AltPC._rocq_fallback_add_1); [lia|].
apply curve_mul_bound; [apply alt_field_closed|apply alt_parameter_bound|exact Hpoint].
Qed.
Print Assumptions alt_add_output_bounded.
Print Assumptions alt_mul_output_bounded.