PV-Bench / benchmarks /Algorithms /modular_inverse /lean /solution_annotated.c
cielseven's picture
add benchmark corpora, groundtruth, symexec scripts and coverage docs
bff4ec7
Raw History Blame Contribute Delete
1.22 kB
/*@ Import Lean
import AUXLib.Arithmetic
import SimpleC.SL.SeparationLogic
open scoped SimpleC
*/
/*@ Extern Coq (Zgcd: Z -> Z -> Z) */
/*
* The verified exgcd case supplies this interface. In particular, x and y
* are Bezout coefficients when the call returns.
*/
int exgcd(int a, int b, int *x, int *y)
/*@ Require
INT_MIN < a && a <= INT_MAX &&
INT_MIN < b && b <= INT_MAX &&
has_int_permission(x) * has_int_permission(y)
Ensure
__return == Zgcd(a, b) &&
a * (*x) + b * (*y) == Zgcd(a, b)
*/;
/*
* Return the canonical representative of the inverse of a modulo modulus.
* The precondition restricts this small example to the usual coprime,
* positive inputs for which an inverse exists.
*/
int modular_inverse(int a, int modulus)
/*@ Require
1 < modulus && 0 < a && a < modulus && Zgcd(a, modulus) == 1 && emp
Ensure
0 <= __return && __return < modulus &&
exists k, a * __return + modulus * k == 1 && emp
*/
{
int x;
int y;
int g = exgcd(a, modulus, &x, &y);
/* The precondition and the exgcd contract imply g == 1. */
int inverse = x % modulus;
if (inverse < 0) {
inverse += modulus;
}
return inverse;
}