-
Notifications
You must be signed in to change notification settings - Fork 85
refactor: swap Goldilocks re-export to the Felt type unified for off-chain and on-chain code
#819
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
Merged
+1,909
−598
Merged
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ba5aa2b
refactor: add `Felt` type unified for off-chain and on-chain code,
greenhat 1519870
refactor: move `Word` and derived types from `miden-crypto` to `miden…
greenhat 2340896
chore: add `make build_target_miden` task and `targe-miden` CI job in…
greenhat d852c32
fix: bring wasm+miden Felt API on par with native
greenhat 06d007b
fix: build `miden-field` for wasm+miden target
greenhat c5ae258
fix: imports for `LexicographicWord`, remove `AsRef` impl for `Word`
greenhat 0e06cae
refactor: change the shape of the `Word` type from a tuple to named f…
greenhat d0a0ae6
chore: formatting
greenhat 2374a2b
refactor: make the inner type private in `Felt`
greenhat 9ab51d9
chore: make `miden-field` crate a workspace dependency
greenhat 2adaca4
chore: add README.md for the `miden-field` crate
greenhat d973a9b
refactor: use `format!`
greenhat b818ccc
fix: re-export `miden_field::word` module from `miden-crypto`
greenhat f3f7bda
refactor: rename `miden_field::WORD_SIZE_FELT` to `WORD_SIZE_FELTS`
greenhat 81f9ce6
fix: `cargo check --all-targets --no-default-features` for the `miden…
greenhat a08abee
test: implement `Arbitrary` for `Felt`
greenhat ad0c03f
refactor: `native.rs` -> `native/mod.rs`
greenhat ea183ce
chore: add tests to ensure equivalence of `Felt` vs `Goldilocks`
greenhat 2d67e4c
chore: convert `Word` tests to proptests
greenhat 21199a3
refactor: re-export `p3-field` traits in `miden-field`
greenhat 04f5af6
fix: gate `word!` to be available in the off-chain target only
greenhat a897b5b
test: add the roundtrip test for `Word::reversed`
greenhat daab99c
fix: don't clobber `RUSTFLAGS` env var `build-target-miden` task
greenhat f3e1a5a
chore: add doc comment for `Word::reversed`
greenhat 440b59e
refactor: define `align(16)` for `Word` only for miden target
greenhat 59dadc1
test: add a compile-time check that `Word` has the same layout as `[F…
greenhat 7e5a2cb
fix: check that `MIDENC_TARGET_IS_MIDEN_VM` env var is not empty
greenhat 12a70dc
chore: add comment explaining why `Word` fields have to be public
greenhat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| categories.workspace = true | ||
| description = "A unified field element type for on-chain and off-chain Miden Rust code" | ||
| documentation = "https://docs.rs/miden-field" | ||
| edition.workspace = true | ||
| keywords.workspace = true | ||
| license.workspace = true | ||
| name = "miden-field" | ||
| readme = "README.md" | ||
| repository.workspace = true | ||
| rust-version.workspace = true | ||
| version.workspace = true | ||
|
|
||
| [lib] | ||
| crate-type = ["rlib"] | ||
|
|
||
| # dependendies for both off-chain and on-chain targets | ||
| [dependencies] | ||
| thiserror = { default-features = false, version = "2.0" } | ||
|
|
||
| # dependendies for the off-chain target only | ||
| [target.'cfg(not(all(target_family = "wasm", miden)))'.dependencies] | ||
| miden-serde-utils = { workspace = true } | ||
| num-bigint = { default-features = false, version = "0.4" } | ||
| p3-challenger = { default-features = false, version = "0.4.2" } | ||
| p3-field = { default-features = false, version = "0.4.2" } | ||
| p3-goldilocks = { default-features = false, version = "0.4.2" } | ||
| paste = { version = "1.0.15" } | ||
| proptest = { default-features = false, features = ["alloc", "std"], optional = true, version = "1.7" } | ||
| rand = { default-features = false, features = ["small_rng"], version = "0.9.0" } | ||
| serde = { default-features = false, features = ["derive"], version = "1.0" } | ||
|
|
||
| [features] | ||
| default = [] | ||
| testing = ["dep:proptest"] | ||
|
|
||
| [dev-dependencies] | ||
| rand = { default-features = false, version = "0.9" } | ||
| rstest = { version = "0.26" } | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # `miden-field` | ||
|
|
||
| A unified field element type for Miden Rust code that needs to run in two very different | ||
| environments: | ||
|
|
||
| - **Off-chain** (native, or regular Wasm): `Felt` is a thin wrapper around Plonky3’s | ||
| `Goldilocks` field element. | ||
| - **On-chain** (Wasm compiled for the Miden VM): `Felt` is represented using Miden compiler | ||
| intrinsics. | ||
|
|
||
| ## Motivation | ||
|
|
||
| In the Miden on-chain execution environment, field elements are currently represented by the | ||
| compiler using a *Wasm primitive type* (`f32`) that the compiler “reinterprets” as a felt. | ||
| That works for on-chain code generation, but it means the on-chain `Felt` is not the same type | ||
| as the off-chain `Felt` used throughout the Rust ecosystem. | ||
|
|
||
| The result is that any code meant to be shared between on-chain and off-chain ends up either: | ||
|
|
||
| - duplicated, or | ||
| - littered with `#[cfg(...)]` gates and wrapper types to bridge the two representations. | ||
|
|
||
| `miden-field` exists to provide a single `miden_field::Felt` API surface that compiles in both | ||
| contexts without forcing downstream crates to pick a side. | ||
|
|
||
| ## How it works | ||
|
|
||
| `miden-field` uses conditional compilation to select the backing implementation: | ||
|
|
||
| - `cfg(all(target_family = "wasm", miden))` (on-chain): `Felt` is a `#[repr(transparent)]` | ||
| record with an `inner: f32` field (matching the WIT shape expected by bindings). Arithmetic | ||
| and conversions are implemented by calling Miden compiler intrinsics (e.g. | ||
| `intrinsics::felt::add`), and `f32` is never treated as a floating-point number. | ||
| - otherwise (off-chain): `Felt` is `#[repr(transparent)]` over `p3_goldilocks::Goldilocks` and | ||
| implements the usual field traits. The modulus is the Goldilocks prime `2^64 - 2^32 + 1`. | ||
|
|
||
| The rest of the crate (e.g. `Word`) builds on top of `Felt` and therefore works in both | ||
| environments as well. | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.