Skip to content

Conversation

@ivokub
Copy link
Collaborator

@ivokub ivokub commented Jul 11, 2025

Description

Previously we had mechanisms for being able to call hints in non-native gadget where either the inputs xor the outputs are in native field. But this wasn't sufficient for all use cases we had, sometimes we had to be able to provide the inputs from two different fields or we would have outputs in different fields. And the fields could be either native or non-native.

This PR refactors the hint calling convention to be more generic, allowing for all possible use cases. It additionally adds an option to be able to have inputs over multiple emulated fields. As we use type parametrization, then it is currently limited to 2 emulated fields, but the wrapping/unwrapping in principle supports arbitrary number of emulated fields.

I refactored current wrapping/unwrapping to use the new mechanism. All changes are backwards compatible, so any internal/external uses should keep working as is.

This also enables #1303 as now we can also pass in A, B to the scalarmul hint nicely and just do generic SW scalar multiplication.

Finally, it also allowed to change the hints in EC arithmetic where we previously called similar hints multiple times to return the emulated values and signs.

Type of change

  • New feature (non-breaking change which adds functionality)

How has this been tested?

  • TestGenericHintNativeInNativeOut
  • TestGenericHintNativeInEmulatedeOut
  • TestGenericHintEmulatedInEmulatedOut
  • TestGenericHintEmulatedInNativeOut
  • TestCrossFieldHint
  • TestMatchingFieldHint

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • golangci-lint does not output errors locally
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@ivokub ivokub requested a review from Copilot July 11, 2025 10:49
@ivokub ivokub self-assigned this Jul 11, 2025
@ivokub ivokub added the type: consolidate strengthen an existing feature label Jul 11, 2025
Copy link
Contributor

Copilot AI left a comment

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 hint-calling convention to introduce a generic, multi-field hint API and migrates all existing hint methods and tests to use it.

  • Adds NewHintGeneric and NewVarGenericHint with a HintContext wrapper.
  • Updates UnwrapHint*, NewHint, NewHintWithNativeOutput, and NewHintWithNativeInput to delegate to the new generic mechanism.
  • Refactors all native and emulated hint implementations and their tests to use the new context-based API.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
std/math/emulated/field_test.go Removed legacy multi-hint tests, retained only sub-constant test
std/math/emulated/field_hint_test.go Updated unit tests to call generic hints and renamed package for internal access
std/math/emulated/field_hint_example_test.go Added an example demonstrating the new generic hint usage
std/math/emulated/field_hint.go Introduced HintContext, NewHintGeneric, NewVarGenericHint, removed old wrap
std/algebra/native/sw_bls12377/hints.go Migrated halfGCDEisenstein to the new context-based API, removed sign-only hint
std/algebra/native/sw_bls12377/g1.go Switched GLV/fake-GLV hint calls to use NewHintGeneric
std/algebra/emulated/sw_emulated/point.go Updated scalar‐mul GLV hints to use the generic API
std/algebra/emulated/sw_emulated/hints.go Refactored all SW emulated hints to use HintContext
std/algebra/emulated/sw_bls12381/hints.go Refactored BLS12-381 decomposition hints to the new API
std/algebra/emulated/sw_bls12381/g2.go Updated G2 GLV decomposition calls to use NewHintGeneric
Comments suppressed due to low confidence (5)

std/math/emulated/field_hint_test.go:309

  • [nitpick] There's a typo in the function name: Emulatede should be Emulated. Consider renaming to testGenericHintNativeInEmulatedOut for clarity.
func testGenericHintNativeInEmulatedeOut[T FieldParams](t *testing.T) {

std/math/emulated/field_hint_test.go:327

  • [nitpick] Typo in the test name: Emulatede should be Emulated. Rename to TestGenericHintNativeInEmulatedOut to match the function under test.
func TestGenericHintNativeInEmulatedeOut(t *testing.T) {

std/math/emulated/field_hint_test.go:655

  • Consider adding unit tests for Field.NewHintWithNativeOutput and Field.NewHintWithNativeInput to verify those code paths are covered by the test suite.
}

std/math/emulated/field_hint.go:326

  • This loop uses range nbEmulated1Outputs, but nbEmulated1Outputs is an integer, not a slice. It will not compile. Consider iterating over the slice emulated1Outputs (e.g., for i := range emulated1Outputs) or using for i := 0; i < nbEmulated1Outputs; i++.
		for i := range nbEmulated1Outputs {

std/math/emulated/field_hint.go:336

  • Similarly, nbEmulated2Outputs is an integer, so range nbEmulated2Outputs is invalid. Update this to iterate by index or over the emulated2Outputs slice.
		for i := range nbEmulated2Outputs {

cursor[bot]

This comment was marked as outdated.

@ivokub ivokub requested a review from gbotrel July 11, 2025 11:49
@ivokub ivokub merged commit a9f014f into master Jul 15, 2025
7 checks passed
@ivokub ivokub deleted the refactor/nonnative-crossfield-hint branch July 15, 2025 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: consolidate strengthen an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants