|
| 1 | +# Optimism-geth with NTT Precompiles (liboqs Implementation) |
| 2 | + |
| 3 | +Fork of `op-geth` with precompiled contracts for Number Theoretic Transform (NTT) operations using open-quantum-safe liboqs via CGO bindings, enabling efficient on-chain post-quantum cryptographic operations. |
| 4 | + |
| 5 | +## Precompiled Contracts |
| 6 | + |
| 7 | +| Address | Name | Description | |
| 8 | +| ------- | --------- | ---------------------------------------------------------------------- | |
| 9 | +| `0x12` | NTT_FW | Forward NTT using liboqs (Falcon-512/1024, ML-DSA) | |
| 10 | +| `0x13` | NTT_INV | Inverse NTT using liboqs (same schemes as NTT_FW) | |
| 11 | +| `0x14` | VECMULMOD | Element-wise modular multiplication: `result[i] = (a[i] * b[i]) mod q` | |
| 12 | +| `0x15` | VECADDMOD | Element-wise modular addition: `result[i] = (a[i] + b[i]) mod q` | |
| 13 | + |
| 14 | +### Supported Schemes |
| 15 | + |
| 16 | +| Scheme | Ring Degree | Modulus | Element Size | |
| 17 | +| ----------- | ----------- | ------- | ---------------- | |
| 18 | +| Falcon-512 | 512 | 12289 | uint16 (2 bytes) | |
| 19 | +| Falcon-1024 | 1024 | 12289 | uint16 (2 bytes) | |
| 20 | +| ML-DSA | 256 | 8380417 | int32 (4 bytes) | |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +### 1. Install liboqs with NTT CGO Bindings |
| 25 | + |
| 26 | +```bash |
| 27 | +# Clone and install dependencies |
| 28 | +git clone -b feature/ntt-cgo-bindings https://github.com/yhl125/liboqs.git |
| 29 | +sudo apt install astyle cmake gcc ninja-build libssl-dev python3-pytest python3-pytest-xdist unzip xsltproc doxygen graphviz python3-yaml valgrind pkg-config |
| 30 | + |
| 31 | +# Build liboqs and Go bindings |
| 32 | +cd liboqs/bindings/go |
| 33 | +make all |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Configure Environment |
| 37 | + |
| 38 | +```bash |
| 39 | +# Set paths (adjust to your installation directory) |
| 40 | +export PKG_CONFIG_PATH=/path/to/liboqs/bindings/go/.config:$PKG_CONFIG_PATH |
| 41 | +export DYLD_LIBRARY_PATH=/path/to/liboqs/build/lib:$DYLD_LIBRARY_PATH # macOS |
| 42 | +export LD_LIBRARY_PATH=/path/to/liboqs/build/lib:$LD_LIBRARY_PATH # Linux |
| 43 | +``` |
| 44 | + |
| 45 | +### 3. Build op-geth |
| 46 | + |
| 47 | +```bash |
| 48 | +make geth |
| 49 | +``` |
| 50 | + |
| 51 | +## API Reference |
| 52 | + |
| 53 | +### NTT_FW (0x12) - Forward Transform |
| 54 | + |
| 55 | +Transforms coefficients into NTT domain using liboqs. |
| 56 | + |
| 57 | +**Input:** |
| 58 | + |
| 59 | +``` |
| 60 | +[0:4] ring_degree (uint32, big-endian) |
| 61 | +[4:12] modulus (uint64, big-endian) |
| 62 | +[12:*] coefficients (Falcon: uint16×N, ML-DSA: int32×N, big-endian) |
| 63 | +``` |
| 64 | + |
| 65 | +**Output:** NTT-transformed coefficients (same format as input) |
| 66 | + |
| 67 | +**Gas Cost:** |
| 68 | + |
| 69 | +- Falcon-512: 500 gas (~9.4μs, 53 mgas/s) |
| 70 | +- Falcon-1024: 1,080 gas (~20.4μs, 53 mgas/s) |
| 71 | +- ML-DSA: 256 gas (~4.8μs, 53 mgas/s) |
| 72 | + |
| 73 | +### NTT_INV (0x13) - Inverse Transform |
| 74 | + |
| 75 | +Transforms NTT domain coefficients back to standard representation. |
| 76 | + |
| 77 | +**Input:** Same as NTT_FW (coefficients in NTT domain) |
| 78 | + |
| 79 | +**Output:** Coefficients in standard representation |
| 80 | + |
| 81 | +**Gas Cost:** |
| 82 | + |
| 83 | +- Falcon-512: 500 gas (~9.4μs, 53 mgas/s) |
| 84 | +- Falcon-1024: 1,080 gas (~20.3μs, 53 mgas/s) |
| 85 | +- ML-DSA: 340 gas (~6.4μs, 53 mgas/s) |
| 86 | + |
| 87 | +### VECMULMOD (0x14) - Vector Multiplication |
| 88 | + |
| 89 | +Element-wise modular multiplication in NTT domain. |
| 90 | + |
| 91 | +**Input:** |
| 92 | + |
| 93 | +``` |
| 94 | +[0:4] ring_degree (uint32, big-endian) |
| 95 | +[4:12] modulus (uint64, big-endian) |
| 96 | +[12:12+n*size] vector_a |
| 97 | +[12+n*size:*] vector_b |
| 98 | +``` |
| 99 | + |
| 100 | +**Output:** Element-wise product `(a[i] * b[i]) mod q` |
| 101 | + |
| 102 | +**Gas Cost:** `ceil(0.32 × n)` |
| 103 | + |
| 104 | +- Falcon-512: 164 gas (~2.9μs, 56 mgas/s) |
| 105 | +- Falcon-1024: 328 gas (~6.0μs, 55 mgas/s) |
| 106 | +- ML-DSA: 82 gas (~2.0μs, 42 mgas/s) |
| 107 | + |
| 108 | +### VECADDMOD (0x15) - Vector Addition |
| 109 | + |
| 110 | +Element-wise modular addition. |
| 111 | + |
| 112 | +**Input:** Same as VECMULMOD |
| 113 | + |
| 114 | +**Output:** Element-wise sum `(a[i] + b[i]) mod q` |
| 115 | + |
| 116 | +**Gas Cost:** `ceil(0.3 × n)` |
| 117 | + |
| 118 | +- Falcon-512: 154 gas (~2.8μs, 55 mgas/s) |
| 119 | +- Falcon-1024: 308 gas (~5.8μs, 53 mgas/s) |
| 120 | +- ML-DSA: 77 gas (~1.6μs, 47 mgas/s) |
| 121 | + |
| 122 | +## Testing |
| 123 | + |
| 124 | +```bash |
| 125 | +cd core/vm |
| 126 | + |
| 127 | +# All NTT tests |
| 128 | +go test -v -run TestPrecompiled.*NTT |
| 129 | + |
| 130 | +# Specific tests |
| 131 | +go test -v -run TestPrecompiledNTT_FW |
| 132 | +go test -v -run TestPrecompiledNTT_VECMULMOD |
| 133 | + |
| 134 | +# Benchmarks |
| 135 | +go test -bench=BenchmarkPrecompiledNTT -benchtime=5s |
| 136 | +``` |
| 137 | + |
| 138 | +**Test Coverage:** |
| 139 | + |
| 140 | +- Scheme detection (Falcon-512/1024, ML-DSA) |
| 141 | +- Input validation (malformed inputs, invalid parameters) |
| 142 | +- Round-trip verification (`INTT(NTT(x)) = x`) |
| 143 | +- Cross-scheme isolation |
| 144 | +- Performance benchmarks with crypto standards |
| 145 | + |
| 146 | +### Benchmark Results |
| 147 | + |
| 148 | +Benchmarks were run on an Intel(R) Xeon(R) CPU @ 2.20GHz. For detailed results, please see the files below: |
| 149 | + |
| 150 | +- [Ecrecover Benchmark Test Results](./benchmark_results/BenchmarkPrecompiledEcrecover) |
| 151 | +- [NTT & NTT Vector Operations Benchmark Test Results](./benchmark_results/BenchmarkPrecompiledNTT) |
0 commit comments