Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Binary generator and logarithm routines for prime residues

Definition
APSCounting893_Main_SmallPrimeResidueOracles

by Shuze Chen · Nov 18, 2026 · Mathlib 0df444a (Lean v4.33.1)

apschinese-remaindercounting-hierarchydiscrete-logarithm

For a prime ppp, a positive residue generator covers every nonzero residue by a power with exponent below p−1p-1p−1. The generator routine chooses the least positive g<pg<pg<p with this coverage property. For a framed input containing p,g,ap,g,ap,g,a, the logarithm routine chooses the least exponent satisfying

0≤e<p−1,ge≡a(modp).0\le e<p-1,\qquad g^e\equiv a\pmod p.0≤e<p−1,ge≡a(modp).

Each routine returns zero when its bounded search has no witness. The searches are defined as actual bounded least-witness functions on finite binary encodings. The modular-power routine uses binary repeated squaring; it reduces every state modulo max⁡(2,p)\max(2,p)max(2,p). These definitions provide uniform short arithmetic values for implicit Chinese remainder multiplication. Their finite-machine hierarchy bounds and their correctness for prime fields are separate theorem statements.

Definition code
import Definitions.Def_APSCounting893_Main_BoundedLeastArithmetic
set_option autoImplicit false
namespace APSCounting893.Main
open Turing.MultiTapeTM
def modularPowerStep (p : ℕ) (state : ℕ × ℕ) (b : Bool) : ℕ × ℕ :=
  (if b then state.1*state.2%max 2 p else state.1%max 2 p,
    state.2*state.2%max 2 p)

def modularPower (a e p : ℕ) : ℕ :=
  (e.bits.foldl (modularPowerStep p) (1%max 2 p,a%max 2 p)).1

def discreteLogInput (p g a : ℕ) : Word := pairWord p.bits (pairWord g.bits a.bits)

def discreteLogPredicate (q : Word) : Bool :=
  let x := Turing.MultiTapeTM.unframe q
  let p := ofBits (Turing.MultiTapeTM.unframe x)
  let g := ofBits (Turing.MultiTapeTM.unframe (Turing.MultiTapeTM.dropFrame x))
  let a := ofBits (Turing.MultiTapeTM.dropFrame (Turing.MultiTapeTM.dropFrame x))
  let e := ofBits (Turing.MultiTapeTM.dropFrame q)
  decide (e < p-1) && decide (modularPower g e p=a)

noncomputable def discreteLogValue : Word → ℕ :=
  boundedLeastValue (fun q => discreteLogPredicate q=true) 1 1


def GeneratorProperty (p g : ℕ) : Prop :=
  0 < g ∧ g < p ∧ ∀ a : ℕ,0 < a → a < p →
    ∃ e : ℕ,e < p-1 ∧ modularPower g e p=a

def generatorLanguage (root : Word) : Prop :=
  GeneratorProperty (ofBits (Turing.MultiTapeTM.unframe root)) (ofBits (Turing.MultiTapeTM.dropFrame root))

def generatorVerifier (z : Word) : Bool :=
  let root := Turing.MultiTapeTM.unframe (Turing.MultiTapeTM.unframe z)
  let p := ofBits (Turing.MultiTapeTM.unframe root)
  let g := ofBits (Turing.MultiTapeTM.dropFrame root)
  let a := ofBits (Turing.MultiTapeTM.dropFrame (Turing.MultiTapeTM.unframe z))
  let e := ofBits (Turing.MultiTapeTM.dropFrame z)
  (decide (0 < g) && decide (g < p)) &&
    ((decide (a=0) || decide (p ≤ a)) ||
      (decide (e < p-1) && decide (modularPower g e p=a)))

noncomputable def generatorValue : Word → ℕ := boundedLeastValue generatorLanguage 1 1

end APSCounting893.Main
Source
Hesse, Allender and Barrington, Uniform Constant-Depth Threshold Circuits for Division and Iterated Multiplication, JCSS 65(4), 2002, author manuscript Section 4, Steps 2–3, pp. 9–11, https://www.cs.rutgers.edu/~allender/papers/division.pdf. The explicit bounded searches and finite-machine interfaces support Agarwal, Hari, Pandey and Shukla, STOC 2027 submission #893, Theorem 6.4, pp. 25–26.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me