Making Hard Problems Easier with Custom Data Distributions and Loss Regularization: A Case Study in Modular Arithmetic

Eshika Saxena, Alberto Alfarano, Emily Wenger, Kristin E. Lauter

Neural networks have historically found modular arithmetic bafflingly difficult. While modern transformers can solve complex symbolic calculus, they often fail to sum even a few numbers if they have to "wrap around" a modulus (e.g., $10 + 5 \pmod{12} = 3$). This failure isn't just a mathematical curiosity; it is a critical security bottleneck. The Learning with Errors (LWE) problem, which secures much of our future "post-quantum" digital infrastructure, relies heavily on the hardness of modular arithmetic.

Recent attempts to use machine learning to break LWE have stalled because models couldn't scale to the number of terms required for real-world secrets. Standard gradients struggle with the jump from $q-1$ back to $0$, and in high-dimensional spaces, models rarely see the "simple" examples (sums with many zeros) needed to build an internal logic of the operation.

In a recent breakthrough, researchers have unlocked a massive jump in the ability of neural networks to perform modular arithmetic. By combining a "Goldilocks" data distribution that favors sparse examples with a custom loss function that prevents model collapse, the authors scaled modular addition from summing 6 terms to 128 terms. This effectively doubles the strength of machine-learning-based attacks against LWE.

Rethinking the Training Pipeline

To overcome the traditional limitations of deep learning on modular arithmetic, the authors introduce two key innovations to the training pipeline for encoder-only transformers:

1. Sparse Vector Augmentation ($f_{inv\_sqrt}$) Instead of training on purely random vectors, the researchers utilize a custom sampling distribution. This distribution ensures the model sees many "sparse" vectors (containing many zeros) alongside dense ones. This acts as an implicit, static curriculum, allowing the model to learn how to add 1 or 2 numbers before tackling 128.

Probability distribution of non-zero elements, highlighting the custom $f_{inv\_sqrt}$ distribution.

There is a "sweet spot" for training data. If the training distribution is too similar to the dense test distribution, the model never learns. If it's too sparse, it fails to generalize. The $f_{inv\_sqrt}$ distribution provides the perfect mid-range KL divergence. Unlike standard Curriculum Learning, which requires complex scheduling, this sparse data approach is static, task-agnostic, and significantly more stable across different trials. Visualizations of the learning order confirm that models naturally learn to solve sparse examples (low Hamming weight) early in training before building up to dense ones.

2. Angular Loss Regularization The authors represent numbers as points on a unit circle (angular embedding). However, they noticed that in hard tasks, models often "give up" and predict $(0,0)$—the center of the circle—to minimize standard Mean Squared Error (MSE). Because traditional loss functions fail to respect the circular topology of modular fields, the authors added a penalty term that pushes predictions away from the origin and toward the unit circle. Forcing the model to stay on the "rim" of the circle via regularization proved essential for preventing model collapse in high-dimensional settings.

Massive Scaling and Key Results

The study primarily uses synthetically generated datasets for modular addition (summing $N$ elements modulo $q$), LWE cryptanalysis (ignoring noise for this proof-of-concept), and general synthetic benchmarks like the Copy task, Associative Recall, and Parity.

The results demonstrate a massive leap in scaling. Previous state-of-the-art models struggled to sum more than $N=6$ terms. With the new training pipeline, the models successfully sum $N=128$ terms modulo $q \le 974,269$ with near-perfect accuracy.

Model accuracy scaling up to N=128 terms and large moduli using the proposed techniques.

In LWE secret recovery, these techniques allowed the recovery of secrets with a Hamming weight of 6, whereas prior ML attacks were limited to a weight of 3. This effectively doubles the "hardness" of secrets that ML can currently crack. Beyond arithmetic, the sparse data technique also yielded 100% accuracy on difficult synthetic tasks like Parity and Associative Recall, where standard training distributions resulted in models performing no better than random guessing.

Limitations and Future Work

While these results are a significant step forward, the LWE application currently ignores the "Error" (noise) component to isolate the arithmetic challenge. Real-world LWE is inherently noisy, and future work must determine if these models remain robust when the sums are intentionally jittered.

Additionally, while reaching $N=128$ is a huge leap, real-world cryptographic standards like CRYSTALS-Kyber use $N=512$. The authors suggest that further scaling might require even larger models or pre-training on general modular addition before fine-tuning on specific cryptographic instances. Nevertheless, this work provides a powerful new toolkit for applying deep learning to structured, hard mathematical problems.

Summary updated

BibTeX
@inproceedings{saxena2025making,
  author = {Eshika Saxena and Alberto Alfarano and Emily Wenger and Kristin E. Lauter},
  title = {{Making Hard Problems Easier with Custom Data Distributions and Loss Regularization: A Case Study in Modular Arithmetic}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=le8hVvWi6Q},
}
Figure