Skip to content

Conversation

cypriansakwa
Copy link
Contributor

@cypriansakwa cypriansakwa commented Jul 24, 2025

Description

This pull request introduces a robust, circuit-friendly wrapper for substring search using the noir_string_search library. The circuit, substring_search, validates input lengths with assertions and then always calls the underlying library function, using selector logic to guarantee correct output for all cases. This approach ensures the circuit is safe for zero-knowledge proof systems by avoiding control flow branching and handling all edge cases arithmetically.

  • For an empty needle, the circuit returns (true, 0), following standard library behavior.
  • For a needle longer than the haystack, the circuit returns (false, 0).
  • For all other cases, it returns whether the substring was found and its index.

Selector logic is used to mask the result of the library call as appropriate, ensuring all outputs are circuit-safe and preventing assertion failures or panics.

Problem

Addresses the need for a practical example demonstrating integration with the noir_string_search library (see noir-examples#31). This implementation demonstrates best practices for integrating external Noir libraries into real-world applications, particularly in the context of zero-knowledge circuits.

Summary

  • Adds a wrapper circuit that asserts input validity and uses selector logic to determine output.
  • Always calls the underlying library, but uses selectors to mask outputs for edge cases.
  • Returns (true, 0) for empty needles (matching standard substring search behavior), and (false, 0) for needles longer than the haystack.
  • Maintains a public interface of (bool, u32) for clarity and compatibility with the noir_string_search contract.
  • Ensures all test cases, including edge cases, are handled gracefully with circuit-safe logic.

Additional Context

  • Selector logic ensures there are no control-flow branches, which is important for circuit determinism and security.
  • The approach is robust against future changes in the upstream library and can be easily adapted if the library's behavior changes.
  • All code and tests work without runtime assertion errors, panics, or constant return value warnings.

PR Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@cypriansakwa cypriansakwa marked this pull request as draft July 24, 2025 09:06
@cypriansakwa cypriansakwa marked this pull request as ready for review July 24, 2025 13:33
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.

1 participant