Library QedkConcrete.blssquareroots
The BLS base-field SWU square-root branch cannot fail
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Bls12_381 MonadVM.Ec.Curve
QedkConcrete.primesqrt QedkConcrete.blsprimality
QedkConcrete.legendretests QedkConcrete.nativefieldbounds.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module SWU := Ec.Curve.SWU_method.
Section BlsSquareRoots.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
{UI : Basics.Unreachable Numeric.Integer.t}
{UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
{UF : Z} {UO : Basics.Unreachable (option Z)}
{US : Basics.Unreachable (Z → option Z)}.
Local Definition field := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition sqrt := @Bls.F_p.sqrt_opt .
Lemma bls_sqrt_available : @PF.sqrt_opt field = Some sqrt.
Proof.
destruct (@prime_sqrt_available field
(@bls_modulus_three_mod_four)) as [root Hroot].
change (@PF.sqrt_opt field =
Some (@MonadVM.Option.get (Z → option Z) Bls.F_p._rocq_fallback_sqrt_opt_1 (@PF.sqrt_opt field))).
rewrite Hroot. reflexivity.
Qed.
Lemma bls_sqrt_correct value root : sqrt value = Some root →
0 ≤ root < @PF.modulus field ∧
(root × root) mod (@PF.modulus field) = value mod (@PF.modulus field).
Proof.
apply (@prime_sqrt_correct field sqrt value root);
[|exact (@bls_modulus_three_mod_four)|exact bls_sqrt_available].
pose proof (@bls_generated_modulus_prime). apply Z.prime_ge_2 in H.
change (2 ≤ @PF.modulus field) in H. lia.
Qed.
Lemma bls_sqrt_alternative value : 0 ≤ value < @PF.modulus field →
sqrt value = None → ∃ root,
sqrt ((11 × value) mod (@PF.modulus field)) = Some root ∧
0 ≤ root < @PF.modulus field.
Proof.
intros Hvalue Hnone.
destruct (@prime_sqrt_swu_alternative field sqrt value 11
(@bls_generated_modulus_prime) (@bls_modulus_three_mod_four)
(@bls_swu_parameter_euler) Hvalue bls_sqrt_available Hnone)
as [root [Hroot [Hbound Hsquare]]].
∃ root. auto.
Qed.
End BlsSquareRoots.
Section BlsRatio.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
{UI : Basics.Unreachable Numeric.Integer.t}
{UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
{UF : Z} {UO : Basics.Unreachable (option Z)}
{US : Basics.Unreachable (Z → option Z)}.
Local Definition ratioField := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition ratioSqrt := @Bls.F_p.sqrt_opt .
Theorem bls_swu_sqrt_ratio_bound u v :
let swu := @Bls.F_p_SWU_fargs in
let result := @SWU.sqrt_ratio _ _ swu UF u v in
0 ≤ snd result < @PF.modulus ratioField.
Proof.
cbv zeta.
change (0 ≤ snd
(let q := @Bls.F_p_SWU_curve.Underlying.op_div u v in
match ratioSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UF (ratioSqrt ((11 × q) mod (@PF.modulus ratioField))))
end) < @PF.modulus ratioField).
set (q := @Bls.F_p_SWU_curve.Underlying.op_div u v).
cbv zeta.
assert (Hq : 0 ≤ q < @PF.modulus ratioField).
{ apply (@prime_div_bound ratioField). pose proof (@bls_generated_modulus_prime).
apply Z.prime_ge_2 in H. change (2 ≤ @PF.modulus ratioField) in H. lia. }
destruct (ratioSqrt q) as [root|] eqn:Hroot.
- exact (proj1 (@bls_sqrt_correct q root Hroot)).
- destruct (@bls_sqrt_alternative q Hq Hroot)
as [root [Halternative Hbound]].
change (ratioSqrt ((11 × q) mod (@PF.modulus ratioField)) = Some root) in Halternative.
rewrite Halternative. exact Hbound.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Ec.Bls12_381 MonadVM.Ec.Curve
QedkConcrete.primesqrt QedkConcrete.blsprimality
QedkConcrete.legendretests QedkConcrete.nativefieldbounds.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module Bls := Ec.Bls12_381.
Module SWU := Ec.Curve.SWU_method.
Section BlsSquareRoots.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
{UI : Basics.Unreachable Numeric.Integer.t}
{UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
{UF : Z} {UO : Basics.Unreachable (option Z)}
{US : Basics.Unreachable (Z → option Z)}.
Local Definition field := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition sqrt := @Bls.F_p.sqrt_opt .
Lemma bls_sqrt_available : @PF.sqrt_opt field = Some sqrt.
Proof.
destruct (@prime_sqrt_available field
(@bls_modulus_three_mod_four)) as [root Hroot].
change (@PF.sqrt_opt field =
Some (@MonadVM.Option.get (Z → option Z) Bls.F_p._rocq_fallback_sqrt_opt_1 (@PF.sqrt_opt field))).
rewrite Hroot. reflexivity.
Qed.
Lemma bls_sqrt_correct value root : sqrt value = Some root →
0 ≤ root < @PF.modulus field ∧
(root × root) mod (@PF.modulus field) = value mod (@PF.modulus field).
Proof.
apply (@prime_sqrt_correct field sqrt value root);
[|exact (@bls_modulus_three_mod_four)|exact bls_sqrt_available].
pose proof (@bls_generated_modulus_prime). apply Z.prime_ge_2 in H.
change (2 ≤ @PF.modulus field) in H. lia.
Qed.
Lemma bls_sqrt_alternative value : 0 ≤ value < @PF.modulus field →
sqrt value = None → ∃ root,
sqrt ((11 × value) mod (@PF.modulus field)) = Some root ∧
0 ≤ root < @PF.modulus field.
Proof.
intros Hvalue Hnone.
destruct (@prime_sqrt_swu_alternative field sqrt value 11
(@bls_generated_modulus_prime) (@bls_modulus_three_mod_four)
(@bls_swu_parameter_euler) Hvalue bls_sqrt_available Hnone)
as [root [Hroot [Hbound Hsquare]]].
∃ root. auto.
Qed.
End BlsSquareRoots.
Section BlsRatio.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
{UI : Basics.Unreachable Numeric.Integer.t}
{UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
{UF : Z} {UO : Basics.Unreachable (option Z)}
{US : Basics.Unreachable (Z → option Z)}.
Local Definition ratioField := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition ratioSqrt := @Bls.F_p.sqrt_opt .
Theorem bls_swu_sqrt_ratio_bound u v :
let swu := @Bls.F_p_SWU_fargs in
let result := @SWU.sqrt_ratio _ _ swu UF u v in
0 ≤ snd result < @PF.modulus ratioField.
Proof.
cbv zeta.
change (0 ≤ snd
(let q := @Bls.F_p_SWU_curve.Underlying.op_div u v in
match ratioSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UF (ratioSqrt ((11 × q) mod (@PF.modulus ratioField))))
end) < @PF.modulus ratioField).
set (q := @Bls.F_p_SWU_curve.Underlying.op_div u v).
cbv zeta.
assert (Hq : 0 ≤ q < @PF.modulus ratioField).
{ apply (@prime_div_bound ratioField). pose proof (@bls_generated_modulus_prime).
apply Z.prime_ge_2 in H. change (2 ≤ @PF.modulus ratioField) in H. lia. }
destruct (ratioSqrt q) as [root|] eqn:Hroot.
- exact (proj1 (@bls_sqrt_correct q root Hroot)).
- destruct (@bls_sqrt_alternative q Hq Hroot)
as [root [Halternative Hbound]].
change (ratioSqrt ((11 × q) mod (@PF.modulus ratioField)) = Some root) in Halternative.
rewrite Halternative. exact Hbound.
Qed.
The returned flag distinguishes a root of q from a root of 11*q.
This equation, not only the coordinate bound, is needed to justify
the following curve-membership check.
Theorem bls_swu_sqrt_ratio_correct u v :
let swu := @Bls.F_p_SWU_fargs in
let q := @Bls.F_p_SWU_curve.Underlying.op_div u v in
let '(square, root) := @SWU.sqrt_ratio _ _ swu UF u v in
(root×root) mod (@PF.modulus ratioField) =
(if square then q else 11×q) mod (@PF.modulus ratioField).
Proof.
cbv zeta.
set (q := @Bls.F_p_SWU_curve.Underlying.op_div u v).
change (let '(square, root) :=
match ratioSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UF
(ratioSqrt ((11×q) mod (@PF.modulus ratioField))))
end in
(root×root) mod (@PF.modulus ratioField) =
(if square then q else 11×q) mod (@PF.modulus ratioField)).
assert (Hq : 0 ≤ q < @PF.modulus ratioField).
{ apply (@prime_div_bound ratioField).
pose proof (@bls_generated_modulus_prime) as Hp.
apply Z.prime_ge_2 in Hp. change (2 ≤ @PF.modulus ratioField) in Hp. lia. }
destruct (ratioSqrt q) as [root|] eqn:Hroot.
- exact (proj2 (@bls_sqrt_correct q root Hroot)).
- destruct (@bls_sqrt_alternative q Hq Hroot)
as [root [Hr Hbound]].
change (ratioSqrt ((11×q) mod (@PF.modulus ratioField)) = Some root) in Hr.
rewrite Hr.
pose proof (@bls_sqrt_correct
((11×q) mod (@PF.modulus ratioField)) root Hr) as [_ Hsquare].
rewrite Z.mod_mod in Hsquare by lia. exact Hsquare.
Qed.
End BlsRatio.
let swu := @Bls.F_p_SWU_fargs in
let q := @Bls.F_p_SWU_curve.Underlying.op_div u v in
let '(square, root) := @SWU.sqrt_ratio _ _ swu UF u v in
(root×root) mod (@PF.modulus ratioField) =
(if square then q else 11×q) mod (@PF.modulus ratioField).
Proof.
cbv zeta.
set (q := @Bls.F_p_SWU_curve.Underlying.op_div u v).
change (let '(square, root) :=
match ratioSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UF
(ratioSqrt ((11×q) mod (@PF.modulus ratioField))))
end in
(root×root) mod (@PF.modulus ratioField) =
(if square then q else 11×q) mod (@PF.modulus ratioField)).
assert (Hq : 0 ≤ q < @PF.modulus ratioField).
{ apply (@prime_div_bound ratioField).
pose proof (@bls_generated_modulus_prime) as Hp.
apply Z.prime_ge_2 in Hp. change (2 ≤ @PF.modulus ratioField) in Hp. lia. }
destruct (ratioSqrt q) as [root|] eqn:Hroot.
- exact (proj2 (@bls_sqrt_correct q root Hroot)).
- destruct (@bls_sqrt_alternative q Hq Hroot)
as [root [Hr Hbound]].
change (ratioSqrt ((11×q) mod (@PF.modulus ratioField)) = Some root) in Hr.
rewrite Hr.
pose proof (@bls_sqrt_correct
((11×q) mod (@PF.modulus ratioField)) root Hr) as [_ Hsquare].
rewrite Z.mod_mod in Hsquare by lia. exact Hsquare.
Qed.
End BlsRatio.
The translator supplies distinct fallback parameters at distinct call
sites. Neither parameter is used: if the first root is absent, the
second root exists. Thus the complete map may use either parameter.
Section Fallback.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
{UI : Basics.Unreachable Numeric.Integer.t}
{UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
{UF : Z} {UO : Basics.Unreachable (option Z)}
{US : Basics.Unreachable (Z → option Z)}.
Local Definition fallbackSwu := @Bls.F_p_SWU_fargs .
Local Definition fallbackField := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition fallbackSqrt := @Bls.F_p.sqrt_opt .
Lemma bls_swu_ratio_fallback_independent {UX : Z} u v :
@SWU.sqrt_ratio _ _ fallbackSwu UX u v = @SWU.sqrt_ratio _ _ fallbackSwu UF u v.
Proof.
set (q := @Bls.F_p_SWU_curve.Underlying.op_div u v).
change ((match fallbackSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UX (fallbackSqrt ((11×q) mod (@PF.modulus fallbackField))))
end) =
(match fallbackSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UF (fallbackSqrt ((11×q) mod (@PF.modulus fallbackField))))
end)).
destruct (fallbackSqrt q) as [root|] eqn:Hroot; [reflexivity|].
assert (Hq : 0 ≤ q < @PF.modulus fallbackField).
{ apply (@prime_div_bound fallbackField).
pose proof (@bls_generated_modulus_prime) as Hp.
apply Z.prime_ge_2 in Hp. change (2 ≤ @PF.modulus fallbackField) in Hp. lia. }
destruct (@bls_sqrt_alternative q Hq Hroot)
as [root [Hr _]].
change (fallbackSqrt ((11×q) mod (@PF.modulus fallbackField)) = Some root) in Hr.
rewrite Hr. reflexivity.
Qed.
Lemma bls_swu_map_fallback_independent {UX : Z}
{UP : (@SWU.C _ _ fallbackSwu).(Ec.Curve.SIG.t)} u :
@SWU.map_to_curve_simple_swu _ _ fallbackSwu UX UP u =
@SWU.map_to_curve_simple_swu _ _ fallbackSwu UF UP u.
Proof.
unfold SWU.map_to_curve_simple_swu. cbn beta iota zeta.
rewrite bls_swu_ratio_fallback_independent. reflexivity.
Qed.
End Fallback.
Context {UZ : Basics.Unreachable Z} {UU : Basics.Unreachable Support.UintN.t}
{UI : Basics.Unreachable Numeric.Integer.t}
{UB : Basics.Unreachable Numeric.IntegerBase.(Numeric.Make.Make_result.t)}
{UF : Z} {UO : Basics.Unreachable (option Z)}
{US : Basics.Unreachable (Z → option Z)}.
Local Definition fallbackSwu := @Bls.F_p_SWU_fargs .
Local Definition fallbackField := @Bls.F_p.MonadVM_Ec_Algebra_Prime_field_include_fargs .
Local Definition fallbackSqrt := @Bls.F_p.sqrt_opt .
Lemma bls_swu_ratio_fallback_independent {UX : Z} u v :
@SWU.sqrt_ratio _ _ fallbackSwu UX u v = @SWU.sqrt_ratio _ _ fallbackSwu UF u v.
Proof.
set (q := @Bls.F_p_SWU_curve.Underlying.op_div u v).
change ((match fallbackSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UX (fallbackSqrt ((11×q) mod (@PF.modulus fallbackField))))
end) =
(match fallbackSqrt q with
| Some root ⇒ (true, root)
| None ⇒ (false, @MonadVM.Option.get Z UF (fallbackSqrt ((11×q) mod (@PF.modulus fallbackField))))
end)).
destruct (fallbackSqrt q) as [root|] eqn:Hroot; [reflexivity|].
assert (Hq : 0 ≤ q < @PF.modulus fallbackField).
{ apply (@prime_div_bound fallbackField).
pose proof (@bls_generated_modulus_prime) as Hp.
apply Z.prime_ge_2 in Hp. change (2 ≤ @PF.modulus fallbackField) in Hp. lia. }
destruct (@bls_sqrt_alternative q Hq Hroot)
as [root [Hr _]].
change (fallbackSqrt ((11×q) mod (@PF.modulus fallbackField)) = Some root) in Hr.
rewrite Hr. reflexivity.
Qed.
Lemma bls_swu_map_fallback_independent {UX : Z}
{UP : (@SWU.C _ _ fallbackSwu).(Ec.Curve.SIG.t)} u :
@SWU.map_to_curve_simple_swu _ _ fallbackSwu UX UP u =
@SWU.map_to_curve_simple_swu _ _ fallbackSwu UF UP u.
Proof.
unfold SWU.map_to_curve_simple_swu. cbn beta iota zeta.
rewrite bls_swu_ratio_fallback_independent. reflexivity.
Qed.
End Fallback.