{"eip":6690,"title":"EVM Modular Arithmetic Extensions","description":"Expanded-width, efficient modular arithmetic operations for the EVM","authors":["Jared Wasinger (@jwasinger)","Alex Beregszaszi (@axic)","Vitalik Buterin (@vbuterin)","Radosław Zagórowicz (@rodiazet)","Paweł Bylica (@chfast)"],"status":"Stagnant","type":"Standards Track","category":"Core","created":"2023-03-15","discussionsTo":"https://ethereum-magicians.org/t/eip-6690-evm-modular-arithmetic-extensions/13322","repo":"EIPs","label":"EIP-6690","officialUrl":"https://eips.ethereum.org/EIPS/eip-6690","sourceUrl":"https://github.com/ethereum/EIPs/blob/master/EIPS/eip-6690.md","url":"https://eips.huatuo.xyz/eip-6690/","tldr":{"en":"EVMMAX: proposed opcodes for fast modular add, subtract and multiply on numbers up to 768 bits, so contracts can implement elliptic-curve and other cryptography without new precompiles.","ja":"EVMMAX: 最大 768 ビットの数に対する高速なモジュラー加算・減算・乗算のために提案された opcode。新しい precompile なしで、コントラクトが楕円曲線などの暗号を実装できるようにする。","ko":"EVMMAX: 최대 768비트 숫자에 대한 빠른 모듈러 덧셈, 뺄셈, 곱셈을 위해 제안된 opcode. 새 precompile 없이도 컨트랙트가 타원곡선 등의 암호를 구현할 수 있게 한다.","zh":"EVMMAX：提议的 opcode，用于对最多 768 位的数进行快速模加、模减和模乘，让合约无需新增 precompile 就能实现椭圆曲线等密码学算法。"},"tags":["evm","opcodes","cryptography","evmmax","core"],"explainers":{"en":{"lang":"en","tldr":"EVMMAX: proposed opcodes for fast modular add, subtract and multiply on numbers up to 768 bits, so contracts can implement elliptic-curve and other cryptography without new precompiles.","tags":["evm","opcodes","cryptography","evmmax","core"],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/eip-6690/","markdown":"## What problem does it solve?\n\nMost cryptography (elliptic curves, pairings, many zero-knowledge verifiers) spends its time on modular arithmetic over large prime fields. The EVM's existing `ADDMOD` / `MULMOD` only work on 256-bit values, accept any input, and re-reduce on every call. Curves like BLS12-381 need 381-bit fields, so contracts end up either very slow or dependent on a dedicated precompile for each new curve.\n\nEIP-6690 (\"EVM Modular Arithmetic Extensions\", often called EVMMAX) proposes general, efficient opcodes for modular arithmetic with moduli up to 768 bits, so new primitives can be written as ordinary contracts.\n\n## How it works\n\nA call frame gets a set of **field contexts**, each identified by an `id` (0–256). A context holds a modulus plus a block of **virtual registers** kept outside normal EVM memory.\n\n| Opcode | Role |\n| --- | --- |\n| `SETMODX` (`0xc0`) | Select context `id`, creating it if needed from a modulus in memory and a register count |\n| `LOADX` (`0xc1`) | Copy register values out to EVM memory |\n| `STOREX` (`0xc2`) | Copy values from EVM memory into registers (each must be \u003c modulus) |\n| `ADDMODX` (`0xc3`), `SUBMODX` (`0xc4`), `MULMODX` (`0xc5`) | Vectorised modular add / sub / mul on registers |\n\nKey rules:\n\n- The modulus must be **odd or a power of two**, between 3 and 2^768 − 1 (at most 96 bytes).\n- Values are stored padded to a multiple of 64 bits. Total register space per call frame is capped at 24,576 bytes, sized to fit in a CPU's L1 cache.\n- Arithmetic opcodes take a 7-byte immediate: `out_idx, out_stride, x_idx, x_stride, y_idx, y_stride, count`, so one instruction can process a strided vector of `count` elements.\n- For odd moduli, implementations are expected to keep values in **Montgomery form** internally; conversion happens in `LOADX`/`STOREX`, which is why those cost one multiplication per value.\n- Gas is cheap and scales with modulus size; for example, multiplication costs 1 gas per element up to 192-bit moduli and 12 gas at 768 bits.\n\n## Sample code\n\nIllustrative pseudo-assembly computing `r = a * b mod p` for a 381-bit prime (layout and immediates simplified):\n\n```text\n// memory[0x00..0x30) holds p (48 bytes); a and b are at 0x40 and 0x70\nPUSH1 3          // alloc_count: registers r0, r1, r2\nPUSH1 48         // modulus_size\nPUSH1 0x00       // modulus_offset\nPUSH1 0          // id\nSETMODX          // create + activate field context 0\n\nPUSH1 2 PUSH1 0x40 PUSH1 0  STOREX   // regs[0..2) \u003c- a, b   (dest=0, source=0x40, count=2)\nMULMODX 0x02 0x01 0x00 0x01 0x01 0x01 0x01  // r2 = r0 * r1 mod p\nPUSH1 1 PUSH1 2 PUSH1 0xa0  LOADX    // memory[0xa0] \u003c- r2   (dest=0xa0, source=2, count=1)\n```\n\n## Things to know\n\n- Status is **Stagnant**; it is not scheduled for any network upgrade.\n- The spec notes only Geth-side tests and an open Geth PR as reference; there are no cross-client tests.\n- The Security Considerations section is empty, and power-of-two multiplication costs are explicitly provisional.\n- Failed checks use `assert` semantics: the call frame consumes all its gas and halts exceptionally (for example, `STOREX` of a value not below the modulus).\n"},"ja":{"lang":"ja","tldr":"EVMMAX: 最大 768 ビットの数に対する高速なモジュラー加算・減算・乗算のために提案された opcode。新しい precompile なしで、コントラクトが楕円曲線などの暗号を実装できるようにする。","tags":["evm","opcodes","cryptography","evmmax","core"],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/ja/eip-6690/","markdown":"## 何を解決するのか\n\nほとんどの暗号（楕円曲線、ペアリング、多くのゼロ知識証明の検証器）は、大きな素体上のモジュラー演算に時間の大半を費やします。EVM の既存の `ADDMOD` / `MULMOD` は 256 ビット値にしか使えず、どんな入力でも受け付け、呼び出しのたびに再度剰余を取ります。BLS12-381 のような曲線は 381 ビットの体を必要とするため、コントラクトは非常に遅くなるか、新しい曲線ごとに専用の precompile に頼ることになります。\n\nEIP-6690（\"EVM Modular Arithmetic Extensions\"、通称 EVMMAX）は、法が最大 768 ビットのモジュラー演算のための汎用的で効率的な opcode を提案します。これにより、新しいプリミティブを通常のコントラクトとして書けるようになります。\n\n## 仕組み\n\nコールフレームには **field context** の集合が与えられ、それぞれ `id`（0〜256）で識別されます。コンテキストは法（modulus）と、通常の EVM メモリの外に置かれる **仮想レジスタ** のブロックを保持します。\n\n| Opcode | 役割 |\n| --- | --- |\n| `SETMODX` (`0xc0`) | コンテキスト `id` を選択する。必要ならメモリ上の法とレジスタ数から作成する |\n| `LOADX` (`0xc1`) | レジスタの値を EVM メモリへコピーする |\n| `STOREX` (`0xc2`) | EVM メモリの値をレジスタへコピーする（各値は法より小さいこと） |\n| `ADDMODX` (`0xc3`)、`SUBMODX` (`0xc4`)、`MULMODX` (`0xc5`) | レジスタに対するベクトル化されたモジュラー加算 / 減算 / 乗算 |\n\n主なルール:\n\n- 法は **奇数または 2 のべき乗** で、3 以上 2^768 − 1 以下（最大 96 バイト）でなければなりません。\n- 値は 64 ビットの倍数にパディングして保存されます。コールフレームごとのレジスタ領域の合計は 24,576 バイトが上限で、CPU の L1 キャッシュに収まる大きさです。\n- 算術 opcode は 7 バイトの即値 `out_idx, out_stride, x_idx, x_stride, y_idx, y_stride, count` を取り、1 命令で `count` 個の要素からなるストライド付きベクトルを処理できます。\n- 奇数の法では、実装は内部で値を **Montgomery 形式** で保持することが想定されています。変換は `LOADX`/`STOREX` で行われるため、これらは値 1 つにつき乗算 1 回分のコストがかかります。\n- gas は安価で、法のサイズに応じて増加します。たとえば乗算のコストは、192 ビット以下の法では要素あたり 1 gas、768 ビットでは 12 gas です。\n\n## サンプルコード\n\n381 ビットの素数に対して `r = a * b mod p` を計算する説明用の疑似アセンブリ（レイアウトと即値は簡略化）:\n\n```text\n// memory[0x00..0x30) holds p (48 bytes); a and b are at 0x40 and 0x70\nPUSH1 3          // alloc_count: registers r0, r1, r2\nPUSH1 48         // modulus_size\nPUSH1 0x00       // modulus_offset\nPUSH1 0          // id\nSETMODX          // create + activate field context 0\n\nPUSH1 2 PUSH1 0x40 PUSH1 0  STOREX   // regs[0..2) \u003c- a, b   (dest=0, source=0x40, count=2)\nMULMODX 0x02 0x01 0x00 0x01 0x01 0x01 0x01  // r2 = r0 * r1 mod p\nPUSH1 1 PUSH1 2 PUSH1 0xa0  LOADX    // memory[0xa0] \u003c- r2   (dest=0xa0, source=2, count=1)\n```\n\n## 知っておくべきこと\n\n- ステータスは **Stagnant** で、どのネットワークアップグレードにも予定されていません。\n- 仕様書が参照実装として挙げているのは Geth 側のテストと未マージの Geth PR のみで、クライアント間テストはありません。\n- Security Considerations セクションは空で、2 のべき乗の乗算コストは暫定であると明記されています。\n- チェックに失敗した場合は `assert` のセマンティクスになり、コールフレームは全 gas を消費して例外的に停止します（例: 法未満でない値の `STOREX`）。\n"},"ko":{"lang":"ko","tldr":"EVMMAX: 최대 768비트 숫자에 대한 빠른 모듈러 덧셈, 뺄셈, 곱셈을 위해 제안된 opcode. 새 precompile 없이도 컨트랙트가 타원곡선 등의 암호를 구현할 수 있게 한다.","tags":["evm","opcodes","cryptography","evmmax","core"],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/ko/eip-6690/","markdown":"## 어떤 문제를 해결하나요?\n\n대부분의 암호(타원곡선, 페어링, 많은 영지식 검증기)는 큰 소수체 위의 모듈러 연산에 대부분의 시간을 씁니다. EVM의 기존 `ADDMOD` / `MULMOD`는 256비트 값에만 쓸 수 있고, 어떤 입력이든 받아들이며, 호출할 때마다 다시 나머지를 구합니다. BLS12-381 같은 곡선은 381비트 체가 필요해서, 컨트랙트가 매우 느리거나 새 곡선마다 전용 precompile에 의존하게 됩니다.\n\nEIP-6690(\"EVM Modular Arithmetic Extensions\", 흔히 EVMMAX라고 부름)은 모듈러스가 최대 768비트인 모듈러 연산을 위한 범용적이고 효율적인 opcode를 제안합니다. 그러면 새로운 프리미티브를 일반 컨트랙트로 작성할 수 있습니다.\n\n## 어떻게 동작하나요?\n\n콜 프레임은 **field context** 집합을 받으며, 각각 `id`(0–256)로 식별됩니다. context는 모듈러스와, 일반 EVM 메모리 밖에 보관되는 **가상 레지스터** 블록을 가집니다.\n\n| Opcode | 역할 |\n| --- | --- |\n| `SETMODX` (`0xc0`) | context `id`를 선택합니다. 필요하면 메모리의 모듈러스와 레지스터 수로 생성합니다 |\n| `LOADX` (`0xc1`) | 레지스터 값을 EVM 메모리로 복사합니다 |\n| `STOREX` (`0xc2`) | EVM 메모리의 값을 레지스터로 복사합니다(각 값은 모듈러스보다 작아야 함) |\n| `ADDMODX` (`0xc3`), `SUBMODX` (`0xc4`), `MULMODX` (`0xc5`) | 레지스터에 대한 벡터화된 모듈러 덧셈 / 뺄셈 / 곱셈 |\n\n핵심 규칙:\n\n- 모듈러스는 **홀수이거나 2의 거듭제곱**이어야 하며, 3 이상 2^768 − 1 이하(최대 96바이트)여야 합니다.\n- 값은 64비트의 배수로 패딩되어 저장됩니다. 콜 프레임당 레지스터 공간 총량은 24,576바이트로 제한되며, CPU의 L1 캐시에 들어가는 크기입니다.\n- 산술 opcode는 7바이트 immediate `out_idx, out_stride, x_idx, x_stride, y_idx, y_stride, count`를 받으므로, 명령 하나로 `count`개 원소의 스트라이드 벡터를 처리할 수 있습니다.\n- 홀수 모듈러스의 경우, 구현은 내부적으로 값을 **Montgomery 형식**으로 유지할 것으로 기대됩니다. 변환은 `LOADX`/`STOREX`에서 일어나므로 이 둘은 값 하나당 곱셈 한 번의 비용이 듭니다.\n- gas는 저렴하며 모듈러스 크기에 따라 늘어납니다. 예를 들어 곱셈은 192비트 이하 모듈러스에서는 원소당 1 gas, 768비트에서는 12 gas입니다.\n\n## 샘플 코드\n\n381비트 소수에 대해 `r = a * b mod p`를 계산하는 설명용 의사 어셈블리(레이아웃과 immediate는 단순화):\n\n```text\n// memory[0x00..0x30) holds p (48 bytes); a and b are at 0x40 and 0x70\nPUSH1 3          // alloc_count: registers r0, r1, r2\nPUSH1 48         // modulus_size\nPUSH1 0x00       // modulus_offset\nPUSH1 0          // id\nSETMODX          // create + activate field context 0\n\nPUSH1 2 PUSH1 0x40 PUSH1 0  STOREX   // regs[0..2) \u003c- a, b   (dest=0, source=0x40, count=2)\nMULMODX 0x02 0x01 0x00 0x01 0x01 0x01 0x01  // r2 = r0 * r1 mod p\nPUSH1 1 PUSH1 2 PUSH1 0xa0  LOADX    // memory[0xa0] \u003c- r2   (dest=0xa0, source=2, count=1)\n```\n\n## 알아둘 점\n\n- 상태는 **Stagnant**이며, 어떤 네트워크 업그레이드에도 예정되어 있지 않습니다.\n- 명세는 참고 자료로 Geth 쪽 테스트와 열려 있는 Geth PR만 언급하며, 클라이언트 간 테스트는 없습니다.\n- Security Considerations 섹션은 비어 있고, 2의 거듭제곱 곱셈 비용은 잠정적이라고 명시되어 있습니다.\n- 검사에 실패하면 `assert` 의미론이 적용되어, 콜 프레임이 모든 gas를 소모하고 예외적으로 중단됩니다(예: 모듈러스보다 작지 않은 값의 `STOREX`).\n"},"zh":{"lang":"zh","tldr":"EVMMAX：提议的 opcode，用于对最多 768 位的数进行快速模加、模减和模乘，让合约无需新增 precompile 就能实现椭圆曲线等密码学算法。","tags":["evm","opcodes","cryptography","evmmax","core"],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/zh/eip-6690/","markdown":"## 它解决什么问题？\n\n大多数密码学（椭圆曲线、配对、许多零知识验证器）的大部分时间都花在大素数域上的模运算。EVM 现有的 `ADDMOD` / `MULMOD` 只支持 256 位的值，接受任意输入，并且每次调用都要重新取模。BLS12-381 之类的曲线需要 381 位的域，因此合约要么非常慢，要么要为每条新曲线依赖专门的 precompile。\n\nEIP-6690（\"EVM Modular Arithmetic Extensions\"，常称 EVMMAX）提议了一组通用、高效的 opcode，用于模数最多 768 位的模运算，这样新的密码学原语就可以写成普通合约。\n\n## 它如何运作\n\n每个调用帧会获得一组 **field context**，每个用 `id`（0–256）标识。一个 context 包含一个模数，以及一块保存在普通 EVM 内存之外的 **虚拟寄存器**。\n\n| Opcode | 作用 |\n| --- | --- |\n| `SETMODX` (`0xc0`) | 选择 context `id`，必要时根据内存中的模数和寄存器数量创建它 |\n| `LOADX` (`0xc1`) | 将寄存器的值复制到 EVM 内存 |\n| `STOREX` (`0xc2`) | 将 EVM 内存中的值复制到寄存器（每个值必须小于模数） |\n| `ADDMODX` (`0xc3`)、`SUBMODX` (`0xc4`)、`MULMODX` (`0xc5`) | 对寄存器进行向量化的模加 / 模减 / 模乘 |\n\n关键规则：\n\n- 模数必须是 **奇数或 2 的幂**，范围在 3 到 2^768 − 1 之间（最多 96 字节）。\n- 值会按 64 位的倍数填充后存储。每个调用帧的寄存器空间总量上限为 24,576 字节，大小设计为可放入 CPU 的 L1 缓存。\n- 算术 opcode 带有 7 字节的立即数：`out_idx, out_stride, x_idx, x_stride, y_idx, y_stride, count`，因此一条指令可以处理由 `count` 个元素组成的带步长向量。\n- 对于奇数模数，实现应在内部以 **Montgomery 形式** 保存值；转换在 `LOADX`/`STOREX` 中进行，因此这两个操作每个值需要一次乘法的开销。\n- gas 很便宜，并随模数大小增长；例如，乘法在模数不超过 192 位时每个元素 1 gas，768 位时为 12 gas。\n\n## 示例代码\n\n用于计算 381 位素数下 `r = a * b mod p` 的示意性伪汇编（布局和立即数已简化）：\n\n```text\n// memory[0x00..0x30) holds p (48 bytes); a and b are at 0x40 and 0x70\nPUSH1 3          // alloc_count: registers r0, r1, r2\nPUSH1 48         // modulus_size\nPUSH1 0x00       // modulus_offset\nPUSH1 0          // id\nSETMODX          // create + activate field context 0\n\nPUSH1 2 PUSH1 0x40 PUSH1 0  STOREX   // regs[0..2) \u003c- a, b   (dest=0, source=0x40, count=2)\nMULMODX 0x02 0x01 0x00 0x01 0x01 0x01 0x01  // r2 = r0 * r1 mod p\nPUSH1 1 PUSH1 2 PUSH1 0xa0  LOADX    // memory[0xa0] \u003c- r2   (dest=0xa0, source=2, count=1)\n```\n\n## 需要注意\n\n- 状态为 **Stagnant**，没有计划纳入任何网络升级。\n- 规范只提到了 Geth 一侧的测试和一个未合并的 Geth PR 作为参考，没有跨客户端测试。\n- Security Considerations 一节为空，2 的幂模乘的 gas 成本也明确说明只是暂定值。\n- 检查失败时采用 `assert` 语义：调用帧会耗尽所有 gas 并异常终止（例如 `STOREX` 写入不小于模数的值）。\n"}}}
