02loveslollipop

sagemath

1
0
# Install this skill:
npx skills add 02loveslollipop/ctf-toolkit-bootstrap --skill "sagemath"

Install specific skill from multi-skill repository

# Description

Run SageMath computations through the existing Anaconda environment named `sage`. Use when Codex needs Sage for CTF and cryptography work such as asymmetric cryptography, finite fields, elliptic curves, lattices, polynomial algebra, modular arithmetic, discrete logs, small-root attacks, or PRNG cryptanalysis, and when execution should happen from inline code or a `.sage` file instead of plain Python.

# SKILL.md


name: sagemath
description: Run SageMath computations through the existing Anaconda environment named sage. Use when Codex needs Sage for CTF and cryptography work such as asymmetric cryptography, finite fields, elliptic curves, lattices, polynomial algebra, modular arithmetic, discrete logs, small-root attacks, or PRNG cryptanalysis, and when execution should happen from inline code or a .sage file instead of plain Python.


SageMath

Use this skill to execute SageMath reliably from the local conda environment sage.

Quick Start

Use the bundled runner:

python /home/zerotwo/.codex/skills/sagemath/scripts/run_sage.py --code 'print(factor(2^20 - 1))'

Or execute an existing .sage file:

python /home/zerotwo/.codex/skills/sagemath/scripts/run_sage.py --file /absolute/path/to/script.sage

Workflow

  1. Decide whether the task is best expressed as inline code or a .sage file.
  2. For small one-off computations, pass the code with --code.
  3. For longer programs or reusable work, create a .sage file and pass it with --file.
  4. For CTF crypto tasks, prefer Sage when the problem involves polynomial rings, finite fields, matrices over modular domains, lattices, or symbolic number theory operations.
  5. Review stdout/stderr from SageMath and report the meaningful result back to the user.

Runner Notes

  • The runner always invokes conda run -n sage sage.
  • For --code, the runner writes the code into a temporary .sage file first. This avoids quoting issues and keeps behavior aligned with file execution.
  • Pass --timeout SECONDS when a computation may hang or run too long.
  • Pass --keep-temp only when debugging generated Sage code.

Patterns

For inline calculations:

python /home/zerotwo/.codex/skills/sagemath/scripts/run_sage.py --code '
R.<x> = QQ[]
f = x^4 - 1
print(f.factor())
'

For file-based workflows:

  1. Create a .sage file in the workspace.
  2. Run it with the bundled runner.
  3. If the script generates files, verify the outputs before replying.

For CTF cryptography workflows:

  • Use Sage integer and modular arithmetic for RSA attacks, CRT reconstruction, inverses, and exponent relations.
  • Use polynomial rings and small_roots() for Coppersmith-style attacks when the instance is suitable.
  • Use GF(p) or extension fields for ECC, finite-field equations, and structured algebraic recovery.
  • Use Matrix, vector, and LLL for lattice-based recovery problems and hidden-number style attacks.
  • Use recurrence solving, modular equations, and matrix lifting for LCG, xorshift, and related PRNG state recovery when Sage algebra is useful.

References

  • For CTF-oriented patterns and starter snippets, read references/ctf-crypto.md when the task involves RSA, ECC, lattices, or PRNG cryptanalysis.

Templates

Resource

scripts/run_sage.py

Use this script instead of calling SageMath manually unless there is a specific reason not to. It standardizes environment selection, timeout handling, and inline-code execution.

# Supported AI Coding Agents

This skill is compatible with the SKILL.md standard and works with all major AI coding agents:

Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.