// x^2 - a has a zero in ℤ/pℤ
// defined for p prime > 2 only
proc isquadraticresidue(a:nat,p:nat): Bool
+ ensures result ⇒ (∃i:nat. 0 ≤ i ∧ i < p ∧ (i^2 - a)%p = 0);
{
var res:Bool = false;
var i:nat = 0;
-
- while i < p ∧ res = false do
+ if isprime(p) then
{
- if (i^2-a)%p = 0 then
- res = true;
- i = i+1;
+ while i < p ∧ res = false do
+ invariant ((~res) ∧ ((i = 0) ∨ (∀j:nat. 0 ≤ j ∧ j ≤ i ∧ (j^2 - a)%p ≠ 0))) ∨
+ ((res) ∧ (∃j:nat. 0 ≤ j ∧ j ≤ i ∧ (j^2 - a)%p = 0));
+ decreases p-i;
+ {
+ if (i^2-a)%p = 0 then
+ res = true;
+ i = i+1;
+ }
}
return res;
////////////////////////////////////////////////////////////////////
-// Algorithm: Calcluate x^n by Right-To-Left Exponentation
+// Algorithm: Calculate x^n by Right-To-Left Exponentation
////////////////////////////////////////////////////////////////////
proc r2lexponentation(x:Base,n:Exp): Result
ensures result = x^n;
}
+