Limb arrays: value semantics of the accessors #
The accessors of Core.Runtime have value-level contracts in terms of the natural number toNat
of a limb array. The contracts use three groups of lemmas.
segmentlemmas:segment_eq_ofDigitsandtoNat_eq_ofDigitsidentify the denotation with Mathlib's little-endian base-2^32evaluation. Its append, bound, prefix, and injectivity theorems apply to limb segments, including zero padding beyond the stored size.- Digit and bit extraction:
limb_toNat_eqreads limbias a base-2^32digit oftoNat, andlimb_testBitreads a bit of a limb as a bit oftoNat. Together withNat.eq_of_testBit_eqthese turn every bitwise accessor into a statement aboutNatbits. - The accessor theorems:
toNat_ofNat,ofNat_toNat,testBit_eq,bitsAt32_toNat,toNat_lowBits,toNat_resize,anyBelow_eq_true_iff,toNat_orLowBit,compare_eq,isZero_eq_true_iff, andlog2_eq.
The compiler certificate toNat_eq_toNatImpl replaces the proof-facing toNat by its Horner
loop. limb_toNat_eq and limb_testBit connect limb access to value-level proofs.
Limbs and segments #
A segment evaluates its zero-padded limb digits in Mathlib's little-endian convention.
The value #
A limb array's value is the base-2^32 evaluation of its stored digits.
Horner evaluation #
Compile the proof-facing front recursion of toNat as its tail-recursive Horner loop.
Construction from a natural number #
Zero #
Leading limb, zero test, and logarithm #
Native UInt32.log2 has the natural-number value of Nat.log2.
Bit access #
Masking and resizing #
Sticky bits #
The sticky scan reports true exactly when some limb below the cut is nonzero.
Comparison #
Lexicographic comparison from the top limb down agrees with comparing the denoted segments.
Limb comparison compares the values.