Download benchmarks/Algorithms/modular_inverse/lean/solution_annotated.c from PV-SJTU/PV-Bench: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/datasets/PV-SJTU/PV-Bench/resolve/main/benchmarks/Algorithms/modular_inverse/lean/solution_annotated.c
- Command line
-
hf download hf://datasets/PV-SJTU/PV-Bench/benchmarks/Algorithms/modular_inverse/lean/solution_annotated.c
-
curl -L -o solution_annotated.c https://huggingface.co/datasets/PV-SJTU/PV-Bench/resolve/main/benchmarks/Algorithms/modular_inverse/lean/solution_annotated.c
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; | |
| } | |