Pure natural-number extended GCD.
The result (g, s, t) satisfies g = Nat.gcd a b and
s * a + t * b = g after coercing the inputs to Int. Use
HexArith.Int.extGcd for integer inputs and
HexArith.UInt64.extGcd for UInt64 inputs.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Bezout certificate for the pure Nat extGcd, stated on the returned
triple's projections: the cofactors s = (extGcd a b).2.1 and
t = (extGcd a b).2.2 satisfy s * a + t * b = (extGcd a b).1 after
coercing a and b to Int. The _proj form keeps the right-hand side
as the routine's own returned gcd component; see extGcd_bezout_gcd for
the variant phrased against Nat.gcd a b.
Bezout certificate for the pure Nat extGcd with the gcd resolved to
Nat.gcd a b: the returned cofactors satisfy s * a + t * b = Nat.gcd a b
over Int. Same identity as extGcd_bezout_proj, with the right-hand side
rewritten through extGcd_fst.
Pure Lean reference implementation of extended GCD over integers.
This runs the Euclidean algorithm directly on Int, carrying Bezout
coefficients through the usual quotient/remainder updates.
Equations
Instances For
Pure Lean integer extended GCD.
Hex.pureIntExtGcd is the proof reference and portable fallback for
HexArith.Int.extGcd; callers that want the project public integer API should
use HexArith.Int.extGcd instead.
Equations
- Hex.pureIntExtGcd a b = Hex.pureIntExtGcd.go a b 1 0 0 1
Instances For
The gcd component returned by the pure integer reference implementation is
Lean's Int.gcd.
Bezout certificate for the pure integer reference pureIntExtGcd, stated on
the returned triple's projections: the cofactors (pureIntExtGcd a b).2.1
and (pureIntExtGcd a b).2.2 satisfy s * a + t * b = (pureIntExtGcd a b).1.
Inputs are already Int, so no coercion is involved.
Combined correctness theorem for the pure integer reference implementation.
Use this when a proof needs both the gcd projection and the Bezout certificate without unfolding the recursive reference implementation.
Public extended GCD on integers.
The logical definition retains the signed Euclidean algorithm. A proved
compiler rewrite uses Nat.extendedGcd on nonnegative inputs, preserving
this definition's exact coefficients; negative inputs use the signed algorithm.
Equations
- HexArith.Int.extGcd a b = Hex.pureIntExtGcd a b
Instances For
Projection form of the integer Bezout certificate: the coefficient components,
addressed directly as (extGcd a b).2.1 and (extGcd a b).2.2, combine to the
returned gcd component (extGcd a b).1. Lets callers rewrite a Bezout identity
without first destructuring the (g, s, t) triple.
Bezout certificate stated against Int.gcd a b directly: the coefficient
projections combine to the canonical gcd rather than the returned (extGcd a b).1
component. Lets callers rewrite a Bezout identity straight to Int.gcd, skipping
the intermediate extGcd_fst step.
Combined correctness theorem for the public integer extended GCD.
The proved compiler rewrite uses the GMP-backed natural-number primitive on nonnegative inputs, while preserving the same public triple used by proofs.
For a positive natural modulus, the Bezout coefficient of the zero input in
Int.extGcd 0 p is zero.
Runtime implementation using the natural primitive on nonnegative inputs. Reversing the inputs and coefficients preserves the signed reference's exact zero, equal-input, and coefficient conventions.
Equations
- HexArith.Int.extGcdImpl (Int.ofNat a_2) (Int.ofNat b_2) = ((b_2.extendedGcd a_2).gcd, (b_2.extendedGcd a_2).coeffB, (b_2.extendedGcd a_2).coeffA)
- HexArith.Int.extGcdImpl a b = Hex.pureIntExtGcd a b
Instances For
The native natural-number route returns exactly the signed reference's triple, rather than merely another valid Bezout certificate.
The coefficient components returned by the UInt64 extended-GCD API form a
Bezout certificate for the natural values of the input words.
Bezout certificate for the UInt64 extGcd, stated on the returned triple's
projections: the cofactors satisfy
s * a.toNat + t * b.toNat = (extGcd a b).1.toNat, with each word coerced to
Int via Int.ofNat ·.toNat. The _proj form keeps the right-hand side as
the routine's own returned gcd word; see extGcd_bezout_gcd for the variant
phrased against Nat.gcd a.toNat b.toNat.
Bezout certificate for the UInt64 extGcd with the gcd resolved to
Nat.gcd a.toNat b.toNat: the returned cofactors satisfy
s * a.toNat + t * b.toNat = Nat.gcd a.toNat b.toNat, coerced to Int via
Int.ofNat ·.toNat. Same identity as extGcd_bezout_proj, with the
right-hand side rewritten through extGcd_fst.
Combined correctness theorem for the UInt64 extended-GCD API.
The gcd component is stored as a word, so the gcd equality is stated after
toNat; the Bezout certificate is stated over the natural representatives of
the input words.