Skip to content

refactor(halo2): Decouple circuit from Midnight types - #3076

Merged
hjeljeli32 merged 7 commits into
mainfrom
hjeljeli32/3037-refactor-midnight-decoupling
Mar 11, 2026
Merged

refactor(halo2): Decouple circuit from Midnight types#3076
hjeljeli32 merged 7 commits into
mainfrom
hjeljeli32/3037-refactor-midnight-decoupling

Conversation

@hjeljeli32

@hjeljeli32 hjeljeli32 commented Mar 9, 2026

Copy link
Copy Markdown
Collaborator

Content

This PR improves the Halo2 circuit code structure for by introducing decoupled circuit-local types.

This PR includes:

  • Added a circuit-local field wrapper and shared aliases in types.rs:
    • CircuitBaseField, CircuitBase, CircuitCurve
  • Replaced duplicated local F/C aliases with shared types from types.rs across:
    • circuit.rs, gadgets.rs, golden/helpers.rs
  • Implemented type conversions using From/Into
  • Introduced a test-only adapter module (adapters.rs) for STM Merkle path → Halo2 witness conversion.
  • Removed utils/mod.rs by inlining field-limb split logic into gadgets.rs
  • Unified synthesis error mapping with to_synthesis_error in errors.rs

Pre-submit checklist

  • Branch
    • Tests are provided (if possible)
    • Crates versions are updated (if relevant)
    • CHANGELOG file is updated (if relevant)
    • Commit sequence broadly makes sense
    • Key commits have useful messages
  • PR
    • All check jobs of the CI have succeeded
    • Self-reviewed the diff
    • Useful pull request description
    • Reviewer requested

Issue(s)

Closes #3037

@github-actions

github-actions Bot commented Mar 9, 2026

Copy link
Copy Markdown

Test Results

    5 files  ±0    190 suites  ±0   57m 15s ⏱️ +12s
2 672 tests +1  2 672 ✅ +1  0 💤 ±0  0 ❌ ±0 
9 587 runs  +1  9 587 ✅ +1  0 💤 ±0  0 ❌ ±0 

Results for commit fbf9b8d. ± Comparison against base commit cf9da10.

♻️ This comment has been updated with latest results.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the Halo2 circuit code to decouple it from direct Midnight library types. It introduces a circuit-local wrapper type CircuitBaseField and shared type aliases (CircuitBase, CircuitCurve) in types.rs, replaces duplicated per-file type F/C aliases with these shared types, consolidates error-to-synthesis conversion into to_synthesis_error in errors.rs, moves the STM→Halo2 Merkle path adapter from utils/mod.rs into a new adapters.rs, and inlines the field-limb split utilities from utils/mod.rs directly into gadgets.rs.

Changes:

  • Introduced CircuitBaseField newtype wrapper and CircuitBase/CircuitCurve shared type aliases in types.rs, replacing direct Midnight type re-exports and per-file type F/C aliases
  • Extracted to_synthesis_error from StmCircuit::synthesis_error into errors.rs, sharing the error conversion across circuit.rs and gadgets.rs
  • Moved the STM Merkle path→Halo2 witness adapter from utils/mod.rs to the new adapters.rs test-only module, and inlined field-limb split helpers into gadgets.rs

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
mithril-stm/src/circuits/halo2/types.rs Adds CircuitBaseField wrapper type with arithmetic ops and From/Into conversions; introduces CircuitBase/CircuitCurve aliases; replaces JubjubBase-based type aliases
mithril-stm/src/circuits/halo2/errors.rs Adds GadgetsFieldModulusParse/GadgetsFieldElementConversion variants; extracts to_synthesis_error function
mithril-stm/src/circuits/halo2/gadgets.rs Inlines field-limb split helpers from utils/mod.rs; adopts shared F/C aliases from types.rs; uses to_synthesis_error
mithril-stm/src/circuits/halo2/circuit.rs Adopts shared F/C aliases; inserts .into() conversions at wrapper boundary; uses to_synthesis_error
mithril-stm/src/circuits/halo2/adapters.rs New test-only file housing the STM→Halo2 Merkle path TryFrom adapter
mithril-stm/src/circuits/halo2/mod.rs Registers new adapters module; removes utils sub-module; tightens gadgets visibility to pub(crate)
mithril-stm/src/circuits/halo2/golden/helpers.rs Uses CircuitBaseField via F alias; migrates Midnight type references to use new From/Into path
mithril-stm/src/circuits/halo2/golden/cases/positive.rs Removes unused use ff::Field import
mithril-stm/src/circuits/halo2/golden/cases/negative.rs Removes unused use ff::Field import
mithril-stm/src/circuits/halo2/utils/mod.rs Deleted; content moved to gadgets.rs and adapters.rs

Comment thread mithril-stm/src/circuits/halo2/gadgets.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/errors.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/adapters.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/types.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/types.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/types.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/errors.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/adapters.rs Outdated
@hjeljeli32
hjeljeli32 force-pushed the hjeljeli32/3037-refactor-midnight-decoupling branch 2 times, most recently from 619844f to 4bc9730 Compare March 9, 2026 09:16
Comment thread mithril-stm/src/circuits/halo2/golden/helpers.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/adapters.rs
Comment thread mithril-stm/src/circuits/halo2/adapters.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/circuit.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/gadgets.rs
Comment thread mithril-stm/src/circuits/halo2/types.rs Outdated

@damrobi damrobi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few minor comments.

Comment thread mithril-stm/src/circuits/halo2/circuit.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/types.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/golden/helpers.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/golden/helpers.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/adapters.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/types.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/gadgets.rs Outdated
Comment thread mithril-stm/src/circuits/halo2/circuit.rs
@hjeljeli32
hjeljeli32 force-pushed the hjeljeli32/3037-refactor-midnight-decoupling branch from 4bc9730 to b8657c5 Compare March 10, 2026 02:43

@jpraynaud jpraynaud left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Just few final comments to be addressed before merging.

Comment thread mithril-stm/src/circuits/halo2/mod.rs Outdated
Comment thread rust-clippy-results.sarif Outdated

@damrobi damrobi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@hjeljeli32
hjeljeli32 force-pushed the hjeljeli32/3037-refactor-midnight-decoupling branch from ae490a1 to 4bf9ee0 Compare March 11, 2026 00:54
@hjeljeli32
hjeljeli32 force-pushed the hjeljeli32/3037-refactor-midnight-decoupling branch from b4502f0 to fbf9b8d Compare March 11, 2026 01:07

@curiecrypt curiecrypt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
I think it's better to address the duplicate functionality in the other PR. So, I marked the related conversation as resolved.

It can be merged 🚀

@hjeljeli32
hjeljeli32 merged commit 10f6738 into main Mar 11, 2026
58 checks passed
@hjeljeli32
hjeljeli32 deleted the hjeljeli32/3037-refactor-midnight-decoupling branch March 11, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor SNARK circuit - Midnight library decoupling

5 participants