Learning Modular Exponentiation with Transformers

David Demitri Africa, Sara M. Kapoor, Theo Simon Sorg, Challenger Mishra
6/30/2025
cs.LGcs.AIcs.CR

No confident code implementation found for this paper.

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.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{africa2025learning,
  title  = {Learning Modular Exponentiation with Transformers},
  author = {David Demitri Africa and Sara M. Kapoor and Theo Simon Sorg and Challenger Mishra},
  year   = {2025},
  eprint = {2506.23679},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2506.23679v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2506.23679.svg)](https://codeofpaper.com/paper/2506.23679)

Discussion