Skip to content

feat: add configurable options for Phase2 MPC setup #1475

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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 118 additions & 31 deletions backend/groth16/bn254/mpcsetup/phase2.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions backend/groth16/bn254/mpcsetup/phase2_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mpcsetup

import (
"crypto/rand"
"io"
)

// Phase2Options contains configuration options for Phase2
type Phase2Options struct {
// RandomSource is the source of randomness for contribution generation
RandomSource io.Reader
}

// DefaultPhase2Options returns default options for Phase2
func DefaultPhase2Options() *Phase2Options {
return &Phase2Options{
RandomSource: rand.Reader,
}
}

// WithRandomSource sets a custom random source for Phase2Options
func (o *Phase2Options) WithRandomSource(source io.Reader) *Phase2Options {
o.RandomSource = source
return o
}
51 changes: 51 additions & 0 deletions backend/groth16/bn254/mpcsetup/phase2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mpcsetup

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPhase2WithCustomRandomSource(t *testing.T) {
// Create a test random source
testRandomSource := bytes.NewReader(make([]byte, 32))

// Create options with custom random source
options := DefaultPhase2Options().WithRandomSource(testRandomSource)

// Create a new Phase2 instance with options
phase2 := NewPhase2(options)

// Check that options are set correctly
assert.NotNil(t, phase2.options)
assert.Equal(t, testRandomSource, phase2.options.RandomSource)

// Generate contribution
err := phase2.GenerateContribution()
require.NoError(t, err)

// Check that contribution was generated
assert.NotNil(t, phase2.Parameters)
assert.NotNil(t, phase2.Parameters.G1.Delta)
assert.NotNil(t, phase2.Parameters.G2.Delta)
}

func TestPhase2WithDefaultOptions(t *testing.T) {
// Create Phase2 with default options
phase2 := NewPhase2(nil)

// Check that default options are set
assert.NotNil(t, phase2.options)
assert.NotNil(t, phase2.options.RandomSource)

// Generate contribution
err := phase2.GenerateContribution()
require.NoError(t, err)

// Check that contribution was generated
assert.NotNil(t, phase2.Parameters)
assert.NotNil(t, phase2.Parameters.G1.Delta)
assert.NotNil(t, phase2.Parameters.G2.Delta)
}
82 changes: 82 additions & 0 deletions backend/groth16/bn254/mpcsetup/unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2020-2025 Consensys Software Inc.
// Licensed under the Apache License, Version 2.0. See the LICENSE file for details.

package mpcsetup

import (
"encoding/binary"
"io"

curve "github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/mpcsetup"
gIo "github.com/consensys/gnark/io"
)

// UnsafeReadFrom implements io.UnsafeReaderFrom
func (p *Phase2) UnsafeReadFrom(reader io.Reader) (n int64, err error) {
var nbCommitments uint16

if err = binary.Read(reader, binary.BigEndian, &nbCommitments); err != nil {
return -1, err
}
n = 2

p.Sigmas = make([]mpcsetup.UpdateProof, nbCommitments)
p.Parameters.G1.SigmaCKK = make([][]curve.G1Affine, nbCommitments)
p.Parameters.G2.Sigma = make([]curve.G2Affine, nbCommitments)

dec := curve.NewDecoder(reader)
for _, v := range p.refsSlice() {
if err = dec.Decode(v); err != nil {
return n + dec.BytesRead(), err
}
}

if err = dec.Decode(&p.Delta); err != nil {
return n + dec.BytesRead(), err
}

for i := range p.Sigmas {
if err = dec.Decode(&p.Sigmas[i]); err != nil {
return n + dec.BytesRead(), err
}
}

challenge, dn, err := gIo.ReadBytesShort(reader)
if err != nil {
return n + dec.BytesRead() + dn, err
}
p.Challenge = challenge
return n + dec.BytesRead() + dn, nil
}

// BinaryDump implements io.BinaryDumper
func (p *Phase2) BinaryDump(writer io.Writer) (n int64, err error) {
if err = binary.Write(writer, binary.BigEndian, uint16(len(p.Parameters.G2.Sigma))); err != nil {
return -1, err
}
n = 2

enc := curve.NewEncoder(writer)
for _, v := range p.refsSlice() {
if err = enc.Encode(v); err != nil {
return n + enc.BytesWritten(), err
}
}

if err = enc.Encode(&p.Delta); err != nil {
return n + enc.BytesWritten(), err
}

for i := range p.Sigmas {
if err = enc.Encode(&p.Sigmas[i]); err != nil {
return n + enc.BytesWritten(), err
}
}

dn, err := gIo.WriteBytesShort(p.Challenge, writer)
if err != nil {
return n + enc.BytesWritten() + dn, err
}
return n + enc.BytesWritten() + dn, nil
}