Skip to content

Conversation

@ludamad
Copy link
Collaborator

@ludamad ludamad commented Oct 23, 2025

Move BN254 point operations to a dedicated module following the grumpkin pattern. This restructures the crypto module to have ECC operations delegated to barretenberg/cpp/src/barretenberg/bbapi/bbapi_ecc.hpp, which will be exposed through the @aztec/bb.js TypeScript bindings after running barretenberg/ts/bootstrap.sh. This improves code organization and maintains consistency across crypto implementations.

ludamad and others added 3 commits October 23, 2025 13:04
Move BN254 G1/G2 point operations from crypto/bls/bn254_point.ts to crypto/bn254/index.ts following the pattern established by the Grumpkin module. This creates a dedicated module for BN254 operations that can be expanded with barretenberg bbapi operations in the future.

Changes:
- Create new crypto/bn254/index.ts with Bn254 class
- Maintain backwards compatibility via re-exports in crypto/bls/index.ts
- Update tests to import from new location
- Remove old crypto/bls/bn254_point.ts

The new structure uses @noble/curves for now, with a clear path to migrate to barretenberg bbapi operations once they are implemented in bbapi_ecc.hpp.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Add BN254 G1 and G2 point multiplication operations to the barretenberg API:
- Bn254G1Mul: Multiply a BN254 G1 point by a scalar
- Bn254G2Mul: Multiply a BN254 G2 point by a scalar

These operations enable TypeScript clients to perform BN254 elliptic curve
operations via bb.js after running barretenberg/ts/bootstrap.sh.

Changes:
- Add struct definitions in bbapi_ecc.hpp
- Implement execution methods in bbapi_ecc.cpp
- Register commands in bbapi_execute.hpp

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@ludamad ludamad force-pushed the ad/refactor/reorganize-bn254-crypto-module branch from 9d647e5 to e95e7ff Compare October 23, 2025 19:08
ludamad and others added 13 commits October 23, 2025 19:36
- Add Bn254G1Mul operation to bbapi_ecc.hpp/cpp
- Add MSGPACK_FIELDS to field2 for future G2 support
- Register Bn254G1Mul in bbapi command registry
- G2 operations deferred pending msgpack serialization work
Updates the BN254 crypto module to use barretenberg's efficient C++
implementation for G1 point operations instead of @noble/curves. G2
operations continue to use @noble/curves pending msgpack serialization
support for field extensions (Fq2).

Changes:
- Make computeG1PublicKey and computeG1PublicKeyCompressed async to support barretenberg calls
- Update all callers to handle async operations (await/Promise)
- Update buildValidatorEntries and related functions to be async
- Fix test cases to handle async operations
- Re-export deriveBlsPrivateKey for tests

All 22 existing tests pass, verifying barretenberg integration works correctly.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Complete reorganization of BN254 crypto module to forward all ECC operations to barretenberg:

- Add three new bbapi operations:
  - Bn254G1GeneratorScalarMul for G1 generator multiplication
  - Bn254G2GeneratorScalarMul for G2 generator multiplication
  - Bn254G1IsOnCurve for curve validation

- Implement msgpack serialization for field2 (G2 points) in affine_element.hpp
  - Uses existing serialize::write/read infrastructure for correct byte ordering
  - Supports both regular fields (32 bytes) and field2 (64 bytes)

- Reorganize yarn-project/foundation/src/crypto/bn254 module:
  - Follows Grumpkin pattern
  - Eliminates @noble/curves from production code (kept in tests for verification)
  - All operations now async and properly awaited
  - Maintains backward compatibility through bls/index.ts re-exports

All 22 tests passing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Removes unnecessary re-exports and wrapper functions from the BLS module.
The BLS module now focuses solely on BLS key derivation, while all BN254
curve operations are accessed directly through the Bn254 class.

Changes:
- Remove all curve operation re-exports from bls/index.ts
- Remove wrapper functions (computeBn254G1PublicKey, etc.)
- Update tests to use Bn254 class instance directly
- Maintain only BLS key derivation logic in bls/index.ts

All 22 tests passing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Simplify the BN254 module by removing the class-based architecture
and using free functions that directly call bb.js operations.

Changes:
- Remove Bn254 class from bn254/index.ts
- Convert all methods to exported free functions:
  - computeG1PublicKey
  - computeG2PublicKey
  - compressG1Point
  - decompressG1Point
  - isOnCurve
- Make helper functions internal (bigintToBuffer, modPow, modularSqrt)
- Simplify bls/index.ts to re-export bn254 functions directly
- Update tests to use free functions instead of class instance

All 22 tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Add a new barretenberg API endpoint to retrieve BN254 curve constants
including Fr and Fq moduli and G1/G2 generator points.

Changes:
- Add Bn254GetCurveConstants command struct to bbapi_ecc.hpp
- Implement curve constant serialization in bbapi_ecc.cpp
- Register command in bbapi_execute.hpp
- Create CurveConstants singleton in bb.js for lazy initialization
- Moduli are returned as big-endian 32-byte arrays
- Generator points returned as affine elements

This enables TypeScript code to query field moduli and generators
directly from barretenberg instead of hardcoding constants.

BLS Note: Barretenberg has HMAC and SHA256 but not SHA512. The BLS
key derivation (HMAC-SHA512) should remain in TypeScript using
@noble/hashes for now until SHA512 is added to barretenberg.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Replace Bn254G1GeneratorScalarMul and Bn254G2GeneratorScalarMul with
generic Bn254G1Mul and Bn254G2Mul operations that accept arbitrary
points. Add Bn254GetCurveConstants endpoint to retrieve curve constants
(Fr/Fq moduli and G1/G2 generators) from barretenberg.

Changes:
- Add Bn254G1Mul (replaces Bn254G1GeneratorScalarMul)
- Add Bn254G2Mul (replaces Bn254G2GeneratorScalarMul)
- Add Bn254GetCurveConstants bbapi endpoint
- Create CurveConstants singleton in bb.js for lazy initialization
- Update foundation bn254 to use new multiply operations
- Export CurveConstants from bb.js

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Changes:
- Add `bb msgpack curve_constants` CLI command to serialize all curve constants (BN254, Grumpkin, Secp256k1, Secp256r1)
- Generate TypeScript curve_constants.ts from msgpack output during `yarn generate`
- Create Field subclasses for each curve (Bn254Fr/Fq, GrumpkinFr/Fq, Secp256k1Fr/Fq, Secp256r1Fr/Fq)
- Replace hardcoded moduli in fields.ts with generated constants
- Remove Bn254GetCurveConstants from bbapi (no longer needed)

Benefits:
- Single source of truth for curve constants from C++ implementation
- No hardcoded values in TypeScript
- Build-time generation instead of runtime initialization
- Type-safe Field classes for all curves
- Backward compatible with existing Fr/Fq usage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Simplify cli_curve_constants.cpp by leveraging uint256_t's built-in msgpack serialization instead of manually converting field moduli to byte arrays.

Changes:
- Replace std::array<uint8_t, 32> with uint256_t for all moduli fields
- Remove manual modulus_to_bytes() helper function
- Directly construct uint256_t from field::modulus

Benefits:
- Cleaner code using existing msgpack infrastructure
- uint256_t serializes as bin32 (32-byte binary)
- No manual byte conversion needed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Optimize MsgpackRawAffineElement to use uint256_t directly instead of byte vectors, with separate handling for field2 types.

C++ Changes (affine_element.hpp):
- Replace std::vector<uint8_t> with conditional type:
  - uint256_t for regular fields (BN254 G1, Grumpkin, Secp256k1, Secp256r1)
  - std::array<uint256_t, 2> for field2 types (BN254 G2)
- Use compile-time branching (if constexpr) for pack/unpack
- Leverage uint256_t's built-in msgpack serialization (bin32)
- Remove manual field_to_bytes/bytes_to_field helpers

TypeScript Changes (generate.ts):
- Add serializeCoordinate() helper to handle both:
  - Single Uint8Array for regular fields
  - Array of two Uint8Arrays for field2
- Update all generator serialization to use helper

Benefits:
- More efficient: uint256_t msgpack vs byte vectors
- Type-safe: compile-time dispatch for field vs field2
- Cleaner: leverages existing msgpack infrastructure
- Matches base PR pattern more closely

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Add private __brand property to all field types (Bn254Fr, Bn254Fq, GrumpkinFr, GrumpkinFq, Secp256k1Fr, Secp256k1Fq, Secp256r1Fr, Secp256r1Fq) to prevent TypeScript structural typing from mixing them up
- Add trivial conversion methods between GrumpkinFr ↔ Bn254Fq and GrumpkinFq ↔ Bn254Fr (since Grumpkin uses swapped moduli from BN254)
- Remove deprecated CurveConstants singleton class from curve_constants.ts
- Remove legacy Fr/Fq aliases from curve_fields.ts
- Remove CurveConstants export from index.ts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Consolidate all field types (BN254, Grumpkin, Secp256k1, Secp256r1) into single fields.ts file
- Keep backward compatibility with Fr and Fq aliases for BN254
- Add both value and type exports for Fr to support type annotations
- Remove redundant curve_fields.ts file
- All field types retain branding and conversion methods from previous commit

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@ludamad ludamad marked this pull request as ready for review October 25, 2025 15:09
ludamad and others added 6 commits October 25, 2025 15:12
- Enhanced Point class with full Grumpkin curve support
  - Added point compression/decompression
  - Added fromXAndSign and YFromX methods for curve equation solving
  - Added isOnGrumpkin curve validation
  - Added poseidon2 hash computation for points
- Added poseidon2 crypto module with hash functions
  - poseidon2Hash, poseidon2HashWithSeparator
  - poseidon2HashAccumulate, poseidon2Permutation
  - poseidon2HashBytes
- Added sqrt() method to Bn254Fr for square root computation

These additions provide barretenberg/ts with cryptographic primitives
that were previously only available in yarn-project/foundation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
… aliases

- Replace Fq class with alias to Bn254Fq (matching Fr pattern)
- Create dedicated point classes for each curve type:
  - GrumpkinPoint (y^2 = x^3 - 17)
  - Bn254G1Point (y^2 = x^3 + 3)
  - Secp256k1Point (y^2 = x^3 + 7)
  - Secp256r1Point (y^2 = x^3 - 3x + b)
- Update poseidon2 Fieldable type to explicitly support Bn254Fr and GrumpkinFq
  - Added comment explaining they share the same underlying field modulus
  - Use explicit type checking instead of broad toBuffer() duck typing
- Maintain Point as backward compatibility alias to GrumpkinPoint

This provides type safety while allowing seamless interop between
Bn254Fr and GrumpkinFq in poseidon hashing operations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
…licit

- Remove Point → GrumpkinPoint alias from points.ts
- Update point.ts to be legacy re-export with deprecation notice
- Forces explicit GrumpkinPoint usage throughout codebase

Point was less widely used than Fr/Fq, making this refactor feasible.
This improves type clarity by making curve types explicit.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Move all barretenberg bbapi-calling code from foundation/crypto to barretenberg/ts
to avoid multiple abstraction layers building directly on bb.js in yarn-project.

Changes:
- Add crypto modules to barretenberg/ts:
  - grumpkin: Grumpkin class with mul, add, batchMul, getRandomFr, reduce512BufferToFr
  - schnorr: Schnorr class and SchnorrSignature for Grumpkin-based signatures
  - ecdsa: Ecdsa class and EcdsaSignature for secp256k1/secp256r1 signatures
  - pedersen: pedersenCommit, pedersenHash, pedersenHashBuffer functions

- Update barretenberg/ts package.json:
  - Add subpath exports for new crypto modules and types
  - Include TypeScript type definitions for all subpaths

- Update foundation/crypto to delegate to barretenberg/ts:
  - grumpkin, schnorr, ecdsa: Create adapter classes maintaining foundation API
  - pedersen: Update to call barretenberg/ts implementations
  - bn254: Add computeBn254G1PublicKeyCompressed function, fix G2 generator usage

- Export bn254 module from foundation/crypto index

This consolidates the canonical barretenberg implementations in one place while
maintaining backward compatibility for yarn-project consumers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Move all remaining standard cryptographic operations from foundation/crypto
to barretenberg/ts, continuing the consolidation started in previous commits.

Moved modules:
- AES-128-CBC encryption/decryption
- BN254 G1/G2 operations with point compression
- Secp256k1 elliptic curve operations
- Verification key utilities (vkAsFieldsMegaHonk)

Foundation modules now serve as thin adapters that:
- Re-export or delegate to @aztec/bb.js/crypto/* subpaths
- Maintain backward compatibility with Buffer return types
- Convert between foundation Fr and barretenberg Bn254Fr types where needed

This completes the crypto consolidation effort, with all standard
cryptographic primitives now centralized in barretenberg/ts.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Moved all field methods and arithmetic operations from foundation's Fr/Fq classes into barretenberg's Bn254Fr/Bn254Fq classes. Foundation now simply re-exports these classes with aliases for backward compatibility.

Key changes:
- Enhanced BaseField in barretenberg with lazy Buffer/BigInt conversion for performance
- Added comprehensive methods: toBigInt(), toNumber(), equals(), cmp(), isZero(), etc.
- Implemented arithmetic operations in Bn254Fr: add(), sub(), mul(), div(), square(), negate()
- Added Bn254Fq-specific methods: lo, hi getters, fromHighLow(), toFields()
- Fixed fromString() to handle numeric strings as decimal, hex strings as hex
- Added proper fromHexString() to always interpret input as hexadecimal
- Made fromBuffer() signatures use 'any' to avoid BufferReader type conflicts between packages
- Foundation fields.ts reduced from 518 lines to 43 lines (re-exports only)
- Created separate FrSchema/FqSchema constants instead of inline class properties

This consolidation follows "Option A" to move all standard cryptographic primitives into bb.js where they belong, eliminating code duplication and establishing barretenberg as the canonical source for field implementations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@ludamad
Copy link
Collaborator Author

ludamad commented Oct 25, 2025

going a different way

@ludamad ludamad closed this Oct 25, 2025
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