A Greatest Common Divisor (GCD) implementation in Cairo 0 that demonstrates zero-knowledge proof verification of mathematical computations.
This program calculates the GCD of two integers using Python's built-in gcd function and then proves the correctness of the result using Cairo's constraint system.
- Python 3.10 (required due to compatibility issues with Python 3.12+)
- Cairo toolchain
python3.10 -m venv cairo_envsource cairo_env/bin/activatepip install cairo-langCompile the Cairo program:
cairo-compile gcd.cairo --output gcd_compiled.jsonCreate or modify program_input.json with your input values:
{
"a": 144,
"b": -12
}source cairo_env/bin/activate
cairo-run --program=gcd_compiled.json --program_input=program_input.json --layout=recursive --print_outputProgram output:
12
main(): Entry point that reads inputs, ensuresa >= b, and outputs the resultgcd(): Computes GCD using Python's math libraryassert_common_divider(a,b,d): Verifies that d divides both a and bdiv_remainder(): Performs division with remainder and validates the operation
- Divisor check: Verifies the result divides both input numbers
- Coprime check: Proves no greater common divisor exists
- Range validation: Ensures all values are within 64-bit bounds
- Mathematical correctness: Validates division and remainder operations
If you encounter dataclass errors, ensure you're using Python 3.10/11:
python3 --version # Should show 3.10.xAlways activate the virtual environment before running Cairo commands:
source cairo_env/bin/activateThis project demonstrates Cairo 0 programming concepts and is provided for demonstration purposes.