Skip to content

Conversation

ymekuria
Copy link

@ymekuria ymekuria commented Sep 2, 2025

Summary

This PR fixes the issue where zkProgram.compile() with cache writing hangs indefinitely in web browsers. The problem was that caml_fp_srs_get_lagrange_basis and caml_fq_srs_get_lagrange_basis functions return WasmVector types that cannot be transferred over the web worker synchronization API.

Changes

Added new pointer-based functions for web compatibility:

Fp Functions

  • caml_fp_srs_get_lagrange_basis_ptr - Returns pointer for worker communication
  • caml_fp_srs_get_lagrange_basis_read_from_ptr - Extracts data from pointer

Fq Functions

  • caml_fq_srs_get_lagrange_basis_ptr - Returns pointer for worker communication
  • caml_fq_srs_get_lagrange_basis_read_from_ptr - Extracts data from pointer

Implementation Details

  • Zero breaking changes - Original functions remain unchanged
  • Simplified approach - New functions reuse existing implementations
  • Proven pattern - Follows same approach as lagrange_commitments_whole_domain_ptr
  • Memory safe - Uses Box::into_raw() and Box::from_raw() for pointer management

Related PRs

This is part 1 of 3 for the complete fix:

  1. This PR: proof-systems (Rust functions)
  2. mina#17707 (submodule reference update)
  3. o1js#2404 (worker spec + TypeScript integration)

Files Changed

  • plonk-wasm/src/srs.rs - Added 4 new pointer-based functions

- Add caml_fp_srs_get_lagrange_basis_ptr: Returns pointer for web worker communication
- Add caml_fp_srs_get_lagrange_basis_read_from_ptr: Extracts data from pointer
- Follows existing working pattern from lagrange_commitments_whole_domain functions
- Enables zkProgram.compile() with cache writing in web environments
- Part 1 of 2: Fp functions (Fq functions to follow)
- Add caml_fq_srs_get_lagrange_basis_ptr: Returns pointer for web worker communication
- Add caml_fq_srs_get_lagrange_basis_read_from_ptr: Extracts data from pointer
- Completes pair with Fp functions added in previous commit
- Uses same proven pattern as existing lagrange_commitments_whole_domain functions
- Part 2 of 2: Now both Fp and Fq are ready for web environments
@ymekuria ymekuria changed the title Add pointer functions for lagrange basis web compatibility Fix lagrange basis functions for web environments Sep 2, 2025
querolita
querolita previously approved these changes Sep 11, 2025
Copy link
Member

@querolita querolita left a comment

Choose a reason for hiding this comment

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

What a fix! I am sure this one bug was hard to find, well done 💯

Copy link
Member

@querolita querolita left a comment

Choose a reason for hiding this comment

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

🆗

@@ -320,6 +320,33 @@ pub mod fp {
});
basis.iter().map(Into::into).collect()
}

/// Web-compatible version that returns pointer for worker communication
Copy link
Member

Choose a reason for hiding this comment

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

Both ways are web compatible, it just doesn't work with our current worker implementation. For clarity, I would change this comment to something that better reflects what it does: Calculates and returns a pointer to the lagrange basis

@@ -400,4 +427,31 @@ pub mod fq {
});
basis.iter().map(Into::into).collect()
}

/// Web-compatible version that returns pointer for worker communication
Copy link
Member

Choose a reason for hiding this comment

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

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants