Machine learning for modular multiplication

Kristin E. Lauter, Cathy Yuanchen Li, Krystal Maughan, Rachel Newton, Megha SrivastavaarXiv 2024 · DOI

Abstract

This paper investigates the ability of neural networks to learn modular arithmetic, a cornerstone of modern cryptography. We evaluate circular regression and sequence-to-sequence transformers on modular multiplication tasks, specifically the 1-dimensional Learning With Errors (LWE) problem. Our results indicate that while models can memorize small-scale modular operations, they fail to generalize to unseen data, reinforcing the perceived hardness of these mathematical structures for current AI architectures.

Introduction

Modern cryptography, including RSA and Diffie-Hellman, relies on the assumption that modular arithmetic operations are easy to perform in one direction but computationally difficult to reverse. Recent studies, such as the "SALSA" papers, have suggested that transformer models might be capable of attacking certain cryptographic schemes. This research seeks to isolate the problem: can machine learning models actually perform modular multiplication and division, or are they simply identifying statistical noise? We focus on the 1-dimensional Learning With Errors (LWE) problem, attempting to recover a secret $s$ from noisy modular products $b_i \equiv a_i \cdot s + e_i \pmod p$.

Methodology

Circular Regression

Because modular arithmetic "wraps around" a finite field, we mapped integers modulo $p$ onto a unit circle. We utilized the von Mises distribution—a circular analogue of the Gaussian distribution—to define the loss function. A significant challenge arose because standard gradient descent often fails when the gradient magnitude is largest far from the target. To overcome this, we employed the reciprocal of the gradient, which provides a more stable optimization landscape for identifying the correct integer solution.

The reciprocal of the gradient provides a more favorable optimization landscape for circular regression by smoothing the path to the target solution.

Sequence-to-Sequence Transformers

We also treated modular arithmetic as a language translation task. Numbers were converted into sequences of tokens based on various bases (e.g., base 10). We tested several architectural tweaks, including weighted loss functions to prioritize significant digits and different positional encoding schemes to help the model understand place-value relationships.

Results and Key Insights

The Generalization Gap

Our experiments revealed a stark contrast between memorization and learning. While transformers achieved near 100% accuracy in memorizing modular multiplication tables for small primes, they exhibited 0% accuracy on held-out test sets. This suggests that the models fail to "grok" the underlying algebraic rules of multiplication.

Arithmetic Distance and Training Dynamics

We monitored the "arithmetic distance"— the numerical difference between the model's prediction and the ground truth. As shown in the training curves, while both test loss and arithmetic distance decrease during training, they do not reach the zero-error threshold required for a successful cryptographic attack.

Training progress over 500 epochs shows that while test loss and arithmetic distance decrease, they do not reach levels indicative of true generalization.

Optimization Challenges

The choice of base representation (e.g., base 8 vs. base 11) significantly impacted memorization performance, indicating that input formatting is a major bottleneck. Furthermore, the non-differentiable nature of the modulo function creates sharp jumps in the loss landscape, posing a fundamental difficulty for gradient-based optimization methods.

Conclusion

Our findings highlight a "modular wall" that current machine learning architectures struggle to penetrate. While circular regression can solve small instances through specialized optimization and transformers can memorize finite tables, neither demonstrates true algebraic generalization. Future research should explore smoother approximations of the modulo function and determine if significantly larger models can discover the underlying principles of finite fields.

Summary updated

BibTeX
@misc{lauter2024machine,
  author = {Kristin E. Lauter and Cathy Yuanchen Li and Krystal Maughan and Rachel Newton and Megha Srivastava},
  title = {{Machine learning for modular multiplication}},
  year = {2024},
  eprint = {2402.19254},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.19254},
}
Figure