Library QedkConcrete.primecertcompute
Computational witnesses for primecert. The certificate checker uses
logarithmic modular exponentiation; its soundness uses only Stdlib.
From Stdlib Require Import ZArith Lia Bool.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.primecert QedkConcrete.legendrearithmetic.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition certificatePower a exponent n :=
match exponent with
| Z.pos p ⇒ OCamlZ.powm_positive a p n
| Z0 ⇒ 1 mod n
| Z.neg _ ⇒ 0
end.
Lemma certificatePower_spec a exponent n : n ≠ 0 → 0 ≤ exponent →
certificatePower a exponent n = a ^ exponent mod n.
Proof.
intros Hn He. destruct exponent; [reflexivity|now apply powm_positive_spec|lia].
Qed.
Definition factorCheck n q a :=
let exponent := (n - 1) / q in
(1 <? n) && (0 <? exponent) && (n - 1 =? q × exponent) &&
(certificatePower a (n - 1) n =? 1) &&
(Z.gcd (certificatePower a exponent n - 1) n =? 1).
Lemma factorCheck_sound n q a : Z.prime q → factorCheck n q a = true →
∀ p, Z.prime p → (p | n) → (q | p - 1).
Proof.
intros Hq Hcheck. unfold factorCheck in Hcheck.
rewrite !andb_true_iff, !Z.ltb_lt, !Z.eqb_eq in Hcheck.
destruct Hcheck as [[[[Hn He] Hfactor] Hpower] Hcoprime].
rewrite !certificatePower_spec in Hpower, Hcoprime by lia.
exact (pocklington_factor n q ((n - 1) / q) a Hn Hq He Hfactor Hpower Hcoprime).
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.primecert QedkConcrete.legendrearithmetic.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition certificatePower a exponent n :=
match exponent with
| Z.pos p ⇒ OCamlZ.powm_positive a p n
| Z0 ⇒ 1 mod n
| Z.neg _ ⇒ 0
end.
Lemma certificatePower_spec a exponent n : n ≠ 0 → 0 ≤ exponent →
certificatePower a exponent n = a ^ exponent mod n.
Proof.
intros Hn He. destruct exponent; [reflexivity|now apply powm_positive_spec|lia].
Qed.
Definition factorCheck n q a :=
let exponent := (n - 1) / q in
(1 <? n) && (0 <? exponent) && (n - 1 =? q × exponent) &&
(certificatePower a (n - 1) n =? 1) &&
(Z.gcd (certificatePower a exponent n - 1) n =? 1).
Lemma factorCheck_sound n q a : Z.prime q → factorCheck n q a = true →
∀ p, Z.prime p → (p | n) → (q | p - 1).
Proof.
intros Hq Hcheck. unfold factorCheck in Hcheck.
rewrite !andb_true_iff, !Z.ltb_lt, !Z.eqb_eq in Hcheck.
destruct Hcheck as [[[[Hn He] Hfactor] Hpower] Hcoprime].
rewrite !certificatePower_spec in Hpower, Hcoprime by lia.
exact (pocklington_factor n q ((n - 1) / q) a Hn Hq He Hfactor Hpower Hcoprime).
Qed.