English

Learning Modular Exponentiation with Transformers

Machine Learning 2025-10-24 v2 Artificial Intelligence Cryptography and Security

Abstract

Modular exponentiation is crucial to number theory and cryptography, yet remains largely unexplored from a mechanistic interpretability standpoint. We train a 4-layer encoder-decoder Transformer model to perform this operation and investigate the emergence of numerical reasoning during training. Utilizing principled sampling strategies, PCA-based embedding analysis, and activation patching, we examine how number-theoretic properties are encoded within the model. We find that reciprocal operand training leads to strong performance gains, with sudden generalization across related moduli. These synchronized accuracy surges reflect grokking-like dynamics, suggesting the model internalizes shared arithmetic structure. We also find a subgraph consisting entirely of attention heads in the final layer sufficient to achieve full performance on the task of regular exponentiation. These results suggest that transformer models learn modular arithmetic through specialized computational circuits, paving the way for more interpretable and efficient neural approaches to modular exponentiation.

Keywords

Cite

@article{arxiv.2506.23679,
  title  = {Learning Modular Exponentiation with Transformers},
  author = {David Demitri Africa and Sara M. Kapoor and Theo Simon Sorg and Challenger Mishra},
  journal= {arXiv preprint arXiv:2506.23679},
  year   = {2025}
}

Comments

Accepted at the 5th MATH-AI Workshop, NeurIPS'25

R2 v1 2026-07-01T03:39:13.789Z