gnark-crypto icon indicating copy to clipboard operation
gnark-crypto copied to clipboard

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

Open crStiv opened this issue 8 months ago • 1 comments

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

crStiv avatar Apr 23 '25 12:04 crStiv

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.

Tabaie avatar Apr 23 '25 19:04 Tabaie