Skip to content

feat: Options for custom hash function and randomness source in MPC setup ceremonies #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

crStiv
Copy link

@crStiv crStiv commented Apr 23, 2025

Description

This PR adds support for custom hash functions and randomness sources in MPC setup ceremonies, as requested in issue #626. These changes enable in-SNARK verification of setup ceremonies and make debugging easier by allowing users to provide their own implementations.

The implementation includes:

  • New interfaces for hash functions and randomness sources
  • Thread-safe global options using the functional options pattern
  • Default implementations that maintain backward compatibility
  • Updated MPC setup templates to use these interfaces
  • Added SetRandomWithSource method to fr.Element
  • Documentation and examples showing how to use custom implementations

Usage Examples

Custom Hash Function

// Create a custom hash function
customHashFunc := func(msg, dst []byte) (curve.G2Affine, error) {
    // Use SHA-256 as an example
    h := sha256.New()
    h.Write(msg)
    h.Write(dst)
    digest := h.Sum(nil)
    
    // Use the built-in HashToG2 with our custom digest
    return curve.HashToG2(digest, []byte{0x01})
}

// Configure MPC with the custom hash function
mpcsetup.ConfigureMPC(mpcsetup.WithHashToG2(customHashFunc))

Custom Randomness Source

// Create a deterministic random source for debugging
deterministicSeed := []byte("fixed seed for deterministic randomness")
customRandomReader := bytes.NewReader(deterministicSeed)

customRandomSource := func() (io.Reader, error) {
    customRandomReader.Reset(deterministicSeed)
    return customRandomReader, nil
}

// Configure MPC with the custom randomness source
mpcsetup.ConfigureMPC(mpcsetup.WithRandomSource(customRandomSource))

Implementation Note

This implementation specifically addresses the feedback from the previous PR (#653):

  1. Uses the functional options pattern as suggested in the feedback
  2. Ensures thread-safety for global hash function and randomness variables with mutex locks
  3. Maintains backward compatibility with existing code

Type of change

[x] New feature (non-breaking change which adds functionality)
[ ] Bug fix (non-breaking change which fixes an issue)
[ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
[ ] This change requires a documentation update

How has this been tested?

  • Added comprehensive test examples that demonstrate using custom hash functions and randomness sources
  • Verified that default implementations maintain backward compatibility
  • Tested thread-safety with concurrent configurations

Fixes #626

@Tabaie
Copy link
Contributor

Tabaie commented Apr 23, 2025

Thank you for the contribution! Am I understanding correctly that the hash and rand settings are global? Since the mpcsetup package is so new, I wouldn't worry about backwards compatibility and change the interface in a way that plays naturally with local settings.

Also, please make sure to run go generate and commit the generated files.

// k is the maximum byte length needed to encode a value < q
// b is the number of bits in the most significant byte of q-1

var bytes [{{mul 8 .NbWords}}]byte
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to use fieldPackage.Bytes instead of the multiplication here.

@crStiv crStiv requested a review from Tabaie April 24, 2025 15:50
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.

Options for custom hash function and randomness source in MPC setup ceremonies
2 participants