Skip to content

refactor: guest bindings #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 35 commits into
base: release-v1.2.1-rc.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
77f0aac
Remove explicit calls to setup functions throughout the codebase
Avaneesh-axiom Apr 24, 2025
b506993
Remove guest code and tests from sha256 extension
Avaneesh-axiom Apr 17, 2025
91907b8
Remove guest code and tests from keccak256 extension
Avaneesh-axiom Apr 17, 2025
8266665
Minor macro changes for algebra re-org
Avaneesh-axiom Apr 22, 2025
3f5c64e
Add hint-based square root implementation
Avaneesh-axiom Apr 22, 2025
c034fa7
Fix rebase mistakes
Avaneesh-axiom Apr 25, 2025
1fa8637
Fix bug
Avaneesh-axiom Apr 25, 2025
ab72889
Disambiguate ecc point x() method call
Avaneesh-axiom Apr 28, 2025
cbe407c
Remove re-export of halo2curves in ecc guest
Avaneesh-axiom Apr 28, 2025
969eae5
Make sha256 rust binding public
Avaneesh-axiom Apr 29, 2025
9864f4b
Make keccak256 binding public
Avaneesh-axiom Apr 29, 2025
b89469b
Replace blanket implementation of traits with macro-based manual impl
Avaneesh-axiom Apr 29, 2025
3769aa4
Rename moduli_declare's "prime" argument to "impl_field"
Avaneesh-axiom Apr 29, 2025
2d80694
Update docs and book about impl_field
Avaneesh-axiom Apr 29, 2025
7e239c9
Remove U256 and I256 from bigint guest library
Avaneesh-axiom Apr 30, 2025
f9d6952
Use impl_field = true in k256 and p256 modules
Avaneesh-axiom Apr 30, 2025
84113cf
Remove pairing guest library and tests (moved to new repo)
Avaneesh-axiom Apr 30, 2025
52709c9
Delete bigint tests
Avaneesh-axiom Apr 30, 2025
8d08bca
Remove pairing library's README
Avaneesh-axiom Apr 30, 2025
5beeffd
Remove pairing tests from Cargo.toml
Avaneesh-axiom Apr 30, 2025
8097465
Remove bigint integration tests from Cargo.toml
Avaneesh-axiom Apr 30, 2025
ec62454
Fix rebase
Avaneesh-axiom Apr 30, 2025
c331eeb
Revert "Remove pairing guest library and tests (moved to new repo)"
Avaneesh-axiom Apr 30, 2025
286379c
Delete parts of the pairing guest library
Avaneesh-axiom May 1, 2025
1b5c405
Use rust-runtime feature in openvm-platform
Avaneesh-axiom May 1, 2025
59c7ae5
Fix bugs
Avaneesh-axiom May 1, 2025
6cc31aa
Delete pairing benchmark
Avaneesh-axiom May 1, 2025
a42bc00
Add complex struct name to openvm-pairing-guest
Avaneesh-axiom May 1, 2025
39e371d
Delete unused code
Avaneesh-axiom May 1, 2025
5c760d7
Fix ecc example
Avaneesh-axiom May 1, 2025
908de06
Fix lint in ecc tests
Avaneesh-axiom May 1, 2025
cf43130
Revert "Delete pairing benchmark"
Avaneesh-axiom May 1, 2025
4357ece
chore: Cargo.lock
jonathanpwang May 13, 2025
b1ba5a7
feat: Automatic primality check of moduli in `moduli_declare!` (#1645)
Avaneesh-axiom May 13, 2025
deafd88
feat: Implement ECC point decompression in guest code (#1648)
Avaneesh-axiom May 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 46 additions & 84 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,20 @@ members = [
"extensions/bigint/circuit",
"extensions/bigint/transpiler",
"extensions/bigint/guest",
"extensions/bigint/tests",
"extensions/keccak256/circuit",
"extensions/keccak256/transpiler",
"extensions/keccak256/guest",
"extensions/keccak256/tests",
"extensions/sha256/circuit",
"extensions/sha256/transpiler",
"extensions/sha256/guest",
"extensions/sha256/tests",
"extensions/ecc/circuit",
"extensions/ecc/transpiler",
"extensions/ecc/guest",
"extensions/ecc/sw-macros",
"extensions/ecc/tests",
"extensions/pairing/circuit",
"extensions/pairing/transpiler",
"extensions/pairing/guest",
"extensions/pairing/tests",
"extensions/pairing/transpiler",
]
exclude = ["crates/sdk/example"]
resolver = "2"
Expand Down
7 changes: 6 additions & 1 deletion book/src/custom-extensions/algebra.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ The functional part is provided by the `openvm-algebra-guest` crate, which is a
- `Field` trait:
Provides constants `ZERO` and `ONE` and methods for basic arithmetic operations within a field.

- `Sqrt` trait:
Implements square root in a field using hinting.

## Modular arithmetic

To [leverage](./overview.md) compile-time known moduli for performance, you declare, initialize, and then set up the arithmetic structures:
Expand All @@ -30,7 +33,9 @@ moduli_declare! {
}
```

This creates `Bls12_381Fp` and `Bn254Fp` structs, each implementing the `IntMod` trait. The modulus parameter must be a string literal in decimal or hexadecimal format.
This creates `Bls12_381Fp` and `Bn254Fp` structs, each implementing the `IntMod` trait.
Since both moduli are prime, both structs also implement the `Field` and `Sqrt` traits.
The modulus parameter must be a string literal in decimal or hexadecimal format.

2. **Init**: Use the `init!` macro exactly once in the final binary:

Expand Down
23 changes: 11 additions & 12 deletions docs/specs/ISA.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,7 @@ the same format that is congruent modulo `N` to the respective operation applied

For each instruction, the operand `d` is fixed to be `1` and `e` is fixed to be `2`.
Each instruction performs block accesses with block size `4` in address space `1` and block size `N::BLOCK_SIZE` in
address space `2`, where `N::NUM_LIMBS` is divisible by `N::BLOCK_SIZE`. Recall that `N::BLOCK_SIZE` must be a power of

2.
address space `2`, where `N::NUM_LIMBS` is divisible by `N::BLOCK_SIZE`. Recall that `N::BLOCK_SIZE` must be a power of 2.

| Name | Operands | Description |
| ------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -635,6 +633,16 @@ format with each limb having `LIMB_BITS` bits.
| ISEQMOD_RV32\<N\> | `a,b,c,1,2` | `[a:4]_1 = [r32{0}(b): N::NUM_LIMBS]_2 == [r32{0}(c): N::NUM_LIMBS]_2 (mod N) ? 1 : 0`. Enforces that `[r32{0}(b): N::NUM_LIMBS]_2, [r32{0}(c): N::NUM_LIMBS]_2` are less than `N` and then sets the register value of `[a:4]_1` to `1` or `0` depending on whether the two big integers are equal. |
| SETUP_ISEQMOD_RV32\<N\> | `a,b,c,1,2` | `assert([r32{0}(b): N::NUM_LIMBS]_2 == N)` in the chip that handles modular equality. For the sake of implementation convenience it also writes something (can be anything) into register value of `[a:4]_1` |

#### Phantom Sub-Instructions


| Name | Discriminant | Operands | Description |
| -------------- | ------------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| HintNonQr\<N\> | 0x50 | `_,_,c_upper` | Use `c_upper` to determine the index of the modulus from the list of supported moduli. Reset the hint stream to equal a quadratic nonresidue modulo `N`. |
| HintSqrt\<N\> | 0x51 | `a,_,c_upper` | Use `c_upper` to determine the index of the modulus from the list of supported moduli. Read from memory `x = [r32{0}(a): N::NUM_LIMBS]_2`. If `x` is a quadratic residue modulo `N`, reset the hint stream to `[1u8, 0u8, 0u8, 0u8]` followed by a square root of `x`. If `x` is not a quadratic residue, reset the hint stream to `[0u8; 4]` followed by a square root of `x * non_qr`, where `non_qr` is the quadratic nonresidue returned by `HintNonQr<N>`. |

#

#### Complex Extension Field

A complex extension field `Fp2` is the quadratic extension of a prime field `Fp` with irreducible polynomial `X^2 + 1`.
Expand Down Expand Up @@ -696,15 +704,6 @@ r32_ec_point(a) -> EcPoint {
| EC_DOUBLE\<C\> | `a,b,_,1,2` | Set `r32_ec_point(a) = 2 * r32_ec_point(b)`. This doubles the input point. Assumes that `r32_ec_point(b)` lies on the curve and is not the identity point. |
| SETUP_EC_DOUBLE\<C\> | `a,b,_,1,2` | `assert(r32_ec_point(b).x == C::MODULUS)` in the chip for EC DOUBLE. For the sake of implementation convenience it also writes something (can be anything) into `[r32{0}(a): 2*C::COORD_SIZE]_2`. It is required for proper functionality that `assert(r32_ec_point(b).y != 0 mod C::MODULUS)` |

#### Phantom Sub-Instructions

The elliptic curve extension defines the following phantom sub-instructions.

| Name | Discriminant | Operands | Description |
| -------------- | ------------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| HintDecompress | 0x40 | `a,b,c_upper` | Uses `c_upper = C::IDX` to determine the index of the curve `C`, from the list of enabled curves. Read from memory `x = [r32{0}(a): C::COORD_SIZE]_2` for an element in the coordinate field of `C`. Let `rec_id = [r32{0}(b)]_2` be a byte in memory for the recovery id, where the lowest bit is 1 if and only if the `y` coordinate of the corresponding point is odd. If there exists a unique `y` such that `(x, y)` is a point on `C` and `y` has the same parity as `rec_id`, then the sub-instruction resets the hint stream to `[1, 0, 0, 0]` followed by `y: [_; C::COORD_SIZE]`. Otherwise, it resets the hint stream to `[0, 0, 0, 0]` followed by `sqrt: [_; C::COORD_SIZE]` where `sqrt * sqrt == (x^3 + ax + b) * non_qr` (`non_qr` is a quadratic nonresidue of `C::Fp`). |
| HintNonQr | 0x41 | `_,_,c_upper` | Reset the hint stream to equal `non_qr: [_; C::COORD_SIZE]` where `non_qr` is a quadratic nonresidue of `C::Fp`. |

### Pairing Extension

The pairing extension supports opcodes tailored to accelerate pairing checks using the optimal Ate pairing over certain
Expand Down
Loading
Loading