# Sagemath

> Auth/lab ref: Computer algebra and number-theory environment for cryptanalysis scripting.

- Skill: `aeondave/sagemath` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add aeondave/sagemath`
- Raw SKILL.md: https://api.skillmd.com/api/skills/aeondave/sagemath/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: GPL-3.0
- Author: AeonDave (https://skillmd.com/u/aeondave)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/aeondave/sagemath

---


# SageMath

Sage is the go-to environment for **mathematical cryptanalysis** and attack modeling.
Use it when standard scripting is not enough for algebraic structures and lattice workflows.

## Typical use cases

- Finite field computations (`GF`, extension fields).
- Polynomial rings and elimination/resultants.
- Elliptic curve arithmetic and discrete-log style tasks.
- Lattice modeling (`matrix`, `LLL`) for partial leakage recovery.
- Fast prototyping of attack math before operationalizing in Python.

## Invocation modes

```bash
# REPL
sage

# Run .sage script
sage solver.sage

# Run Python file with Sage runtime
sage -python solver.py

# Jupyter with Sage kernel
sage -n jupyter
```

## Practical workflow

1. Parse input values (files, network output, pubkeys).
2. Model algebra in Sage-native structures (`GF`, rings, curves, matrices).
3. Validate with small synthetic tests.
4. Execute full solve, export recovered secrets to plain Python bytes/ints.
5. Integrate with `pwntools` or service scripts for secret recovery.

## Common cryptanalysis patterns

From real-world cryptanalysis work, common patterns include:

- `from sage.all import *` alongside `pwntools` remote interaction.
- Factoring and formatting factorization answers for oracle protocols.
- Elliptic curve order / point lifting / discrete-log solves.
- Lattice assembly via identity/augment/stack + `LLL()`.
- Finite-field polynomial construction and resultant-based elimination.
- `.sage` solvers combining bitwise transforms, polynomial operations over `GF(2^N)`, and iterative remote interaction rounds.

## Output hygiene

- Convert Sage integers to Python ints before serialization.
- Convert numeric secrets to bytes with explicit endianness/length handling.
- Keep deterministic seeds/options when random components appear.

## Resources

- `references/python-sage-scripts.md` — authoritative patterns for `.sage` and `sage -python` workflows, imports, interoperability, and pitfalls.
- `references/cryptanalysis-patterns.md` — practical cryptanalysis templates (GF, EC, lattice, polynomial elimination) grounded in real solver style.

