Native Montgomery arithmetic over eight 32-bit limbs #
Raw word operations for prime moduli below 2 ^ 255, represented as eight 32-bit limbs
carried in the low halves of UInt64 words (Limbs8). This is the multi-limb sibling of
Montgomery/Native32.lean: every operation is a straight-line chain of @[inline] word
helpers, and each helper comes with an existential toNat specification that names its
outputs as fresh naturals. The definitions themselves live in the zero-import module
Montgomery/Native64x8Defs; this module states and proves everything about them.
The limb helpers (adcLo/adcCo, sbbLo/sbbBo, macLo/macHi, montM) are combined by
the two telescoping lemmas carry_chain_sum and borrow_chain_sum, which turn an eight-step
carry or borrow chain into a single identity between recomposed values. Every correctness
statement is generic in the modulus limbs; concrete fields supply their constants through
the Mont64x8Field class of Montgomery/Native64x8Field.
Conditional subtraction, addition, subtraction and negation are proved correct here. The
CIOS multiplication round mulRound is defined in Montgomery/Native64x8Defs; its state
invariant is proved in the sibling module that builds the field carrier.
Main results #
condSub_toNat,add_toNat,sub_toNat,neg_toNat— correctness of the raw operationsaddLimbs_toNat,subLimbs_spec— the underlying carry/borrow chains
Word-level specifications #
Add-with-carry, existential form: the low limb and the carry-out are named as fresh naturals together with their defining equations, so that a chain of them is a linear system over plain variables.
Multiply-accumulate, existential form. The accumulator, both factors and the carry-in
all fit in 32 bits, so t + x * y + c cannot overflow a 64-bit word.
Eight-limb values #
Chain lemmas #
Telescoping identities for an eight-step carry or borrow chain. They are stated over plain
naturals and the y slots are arbitrary, so carry_chain_sum serves both the
add-with-carry chains and the multiply-accumulate chains of CIOS, where yᵢ is a limb
product.
Weighted telescoping of an eight-step carry chain.
Weighted telescoping of an eight-step borrow chain.
Branch lemmas #
The situations that a borrow chain can produce, each as a standalone linear problem over a handful of naturals.