Skip to content

Commit

Permalink
Merge pull request #3 from worldcoin/ewoolsey/gnark-10
Browse files Browse the repository at this point in the history
migration to gnark 10
  • Loading branch information
0xForerunner authored May 27, 2024
2 parents 9221154 + ffeebc4 commit cf6387f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 164 deletions.
32 changes: 0 additions & 32 deletions deserialize/deserializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,6 @@ func TestSerializeR1CS(t *testing.T) {
assert.NoError(err)
}

func TestDeserializePtauConvertPhase1(t *testing.T) {
assert := require.New(t)

input_path := "08.ptau"

ptau, err := ReadPtau(input_path)

if err != nil {
assert.NoError(err)
}

phase1, err := ConvertPtauToPhase1(ptau)

if err != nil {
assert.NoError(err)
}

fmt.Printf("TauG1: %v \n", phase1.tauG1)
fmt.Printf("AlphaTauG1: %v \n", phase1.alphaTauG1)
fmt.Printf("BetaTauG1: %v \n", phase1.betaTauG1)
fmt.Printf("TauG2: %v \n", phase1.tauG2)
fmt.Printf("BetaG2: %v \n", phase1.betaG2)

// Write phase1 to file
err = WritePhase1(phase1, uint8(ptau.Header.Power), "08.ph1")

if err != nil {
assert.NoError(err)
}
}

///////////////////////////////////////////////////////////////////
/// ZKEY ///
///////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -158,7 +127,6 @@ func TestDeserializerZkey(t *testing.T) {
assert := require.New(t)

zkey, err := ReadZkey(input_path)

if err != nil {
assert.NoError(err)
}
Expand Down
90 changes: 8 additions & 82 deletions deserialize/phase1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@ package deserializer
import (
"bufio"
"fmt"
"math"
"os"

"github.com/consensys/gnark-crypto/ecc/bn254"
curve "github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark/backend/groth16/bn254/mpcsetup"
)

type Phase1 struct {
tauG1 []curve.G1Affine
alphaTauG1 []curve.G1Affine
betaTauG1 []curve.G1Affine
tauG2 []curve.G2Affine
betaG2 curve.G2Affine
}

func ConvertPtauToPhase1(ptau Ptau) (phase1 Phase1, err error) {
func ConvertPtauToPhase1(ptau Ptau) (phase1 mpcsetup.Phase1, err error) {
tauG1 := make([]curve.G1Affine, len(ptau.PTauPubKey.TauG1))
for i, g1 := range ptau.PTauPubKey.TauG1 {
g1Affine := curve.G1Affine{}
Expand Down Expand Up @@ -112,9 +104,13 @@ func ConvertPtauToPhase1(ptau Ptau) (phase1 Phase1, err error) {
}
}

//fmt.Printf("BetaG2: %v \n", BetaG2)
phase1.Parameters.G1.Tau = tauG1
phase1.Parameters.G1.AlphaTau = alphaTauG1
phase1.Parameters.G1.BetaTau = betaTauG1
phase1.Parameters.G2.Tau = tauG2
phase1.Parameters.G2.Beta = betaG2

return Phase1{tauG1: tauG1, tauG2: tauG2, alphaTauG1: alphaTauG1, betaTauG1: betaTauG1, betaG2: betaG2}, nil
return phase1, nil
}

func WritePhase1FromPtauFile(ptauFile *PtauFile, outputPath string) error {
Expand Down Expand Up @@ -195,73 +191,3 @@ func WritePhase1FromPtauFile(ptauFile *PtauFile, outputPath string) error {
enc.Encode(&betaG2)
return nil
}

func WritePhase1(phase1 Phase1, power byte, outputPath string) error {
// output outputFile
outputFile, err := os.Create(outputPath)
if err != nil {
return err
}
defer outputFile.Close()

var header Header

writer := bufio.NewWriter(outputFile)
defer writer.Flush()

N := int(math.Pow(2, float64(power)))

fmt.Printf("Power %d supports up to %d constraints\n", power, N)

header.Power = power

// can be extracted from ptau.Contributions (7) but hardcoding for now
// ptau link: https://github.com/iden3/snarkjs/tree/master#7-prepare-phase-2
header.Contributions = 54

// Write the header
header.writeTo(outputFile)

// BN254 encoder using compressed representation of points to save storage space
enc := bn254.NewEncoder(writer)

// Taken from https://github.com/worldcoin/semaphore-mtb-setup/blob/main/phase1/phase1.go
// In the initialization, τ = α = β = 1, so we are writing the generators directly
// Write [τ⁰]₁, [τ¹]₁, [τ²]₁, …, [τ²ᴺ⁻²]₁
fmt.Println("1. Writing TauG1")
for i := 0; i < 2*N-1; i++ {
if err := enc.Encode(&phase1.tauG1[i]); err != nil {
return err
}
}

// Write α[τ⁰]₁, α[τ¹]₁, α[τ²]₁, …, α[τᴺ⁻¹]₁
fmt.Println("2. Writing AlphaTauG1")
for i := 0; i < N; i++ {
if err := enc.Encode(&phase1.alphaTauG1[i]); err != nil {
return err
}
}

// Write β[τ⁰]₁, β[τ¹]₁, β[τ²]₁, …, β[τᴺ⁻¹]₁
fmt.Println("3. Writing BetaTauG1")
for i := 0; i < N; i++ {
if err := enc.Encode(&phase1.betaTauG1[i]); err != nil {
return err
}
}

// Write {[τ⁰]₂, [τ¹]₂, [τ²]₂, …, [τᴺ⁻¹]₂}
fmt.Println("4. Writing TauG2")
for i := 0; i < N; i++ {
if err := enc.Encode(&phase1.tauG2[i]); err != nil {
return err
}
}

// Write [β]₂
fmt.Println("5. Writing BetaG2")
enc.Encode(&phase1.betaG2)

return nil
}
32 changes: 6 additions & 26 deletions deserialize/ptau.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ contributions(7) - Ignore contributions, users can verify using snarkjs
// in bytes
const BN254_FIELD_ELEMENT_SIZE = 32

type G1 [2]big.Int
type G2 [4]big.Int
type (
G1 [2]big.Int
G2 [4]big.Int
)

type PtauHeader struct {
N8 uint32
Expand Down Expand Up @@ -97,12 +99,11 @@ type PtauFile struct {

func InitPtau(path string) (*PtauFile, error) {
reader, err := os.Open(path)

if err != nil {
return nil, err
}

var ptauStr = make([]byte, 4)
ptauStr := make([]byte, 4)
_, err = reader.Read(ptauStr)

fmt.Printf("zkeyStr: %s \n", string(ptauStr))
Expand Down Expand Up @@ -142,7 +143,6 @@ func InitPtau(path string) (*PtauFile, error) {

// Read header
header, err := readPtauHeader(reader)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -255,14 +255,13 @@ func (ptauFile *PtauFile) ReadBetaG2() (bn254.G2Affine, error) {

func ReadPtau(zkeyPath string) (Ptau, error) {
reader, err := os.Open(zkeyPath)

if err != nil {
return Ptau{}, err
}

defer reader.Close()

var ptauStr = make([]byte, 4)
ptauStr := make([]byte, 4)
_, err = reader.Read(ptauStr)

fmt.Printf("zkeyStr: %s \n", string(ptauStr))
Expand Down Expand Up @@ -302,7 +301,6 @@ func ReadPtau(zkeyPath string) (Ptau, error) {

// Read header
header, err := readPtauHeader(reader)

if err != nil {
return Ptau{}, err
}
Expand All @@ -317,7 +315,6 @@ func ReadPtau(zkeyPath string) (Ptau, error) {
fmt.Printf("tauG1: \n")

PtauPubKey.TauG1, err = readG1Array(reader, twoToPower*2-1)

if err != nil {
return Ptau{}, err
}
Expand All @@ -328,7 +325,6 @@ func ReadPtau(zkeyPath string) (Ptau, error) {
fmt.Printf("tauG2: \n")

PtauPubKey.TauG2, err = readG2Array(reader, twoToPower)

if err != nil {
return Ptau{}, err
}
Expand All @@ -339,7 +335,6 @@ func ReadPtau(zkeyPath string) (Ptau, error) {
fmt.Printf("alphaTauG1: \n")

PtauPubKey.AlphaTauG1, err = readG1Array(reader, twoToPower)

if err != nil {
return Ptau{}, err
}
Expand All @@ -350,7 +345,6 @@ func ReadPtau(zkeyPath string) (Ptau, error) {
fmt.Printf("betaTauG1: \n")

PtauPubKey.BetaTauG1, err = readG1Array(reader, twoToPower)

if err != nil {
return Ptau{}, err
}
Expand All @@ -361,7 +355,6 @@ func ReadPtau(zkeyPath string) (Ptau, error) {
fmt.Printf("betaG2: \n")

PtauPubKey.BetaG2, err = readG2(reader)

if err != nil {
return Ptau{}, err
}
Expand All @@ -373,23 +366,20 @@ func readPtauHeader(reader io.ReadSeeker) (PtauHeader, error) {
var header PtauHeader

n8, err := readULE32(reader)

if err != nil {
return PtauHeader{}, err
}

header.N8 = n8

prime, err := readBigInt(reader, n8)

if err != nil {
return PtauHeader{}, err
}

header.Prime = prime

power, err := readULE32(reader)

if err != nil {
return PtauHeader{}, err
}
Expand All @@ -403,7 +393,6 @@ func readG1Array(reader io.ReadSeeker, numPoints uint32) ([]G1, error) {
g1s := make([]G1, numPoints)
for i := uint32(0); i < numPoints; i++ {
g1, err := readG1(reader)

if err != nil {
return []G1{}, err
}
Expand All @@ -418,7 +407,6 @@ func readG2Array(reader io.ReadSeeker, numPoints uint32) ([]G2, error) {

for i := uint32(0); i < numPoints; i++ {
g2, err := readG2(reader)

if err != nil {
return []G2{}, err
}
Expand All @@ -431,13 +419,11 @@ func readG2Array(reader io.ReadSeeker, numPoints uint32) ([]G2, error) {

func readTauG2(reader io.ReadSeeker) ([]G2, error) {
tauG2_s, err := readG2(reader)

if err != nil {
return []G2{}, err
}

tauG2_sx, err := readG2(reader)

if err != nil {
return []G2{}, err
}
Expand All @@ -449,15 +435,13 @@ func readG1(reader io.ReadSeeker) (G1, error) {
var g1 G1

x, err := readBigInt(reader, BN254_FIELD_ELEMENT_SIZE)

if err != nil {
return G1{}, err
}

g1[0] = x

y, err := readBigInt(reader, BN254_FIELD_ELEMENT_SIZE)

if err != nil {
return G1{}, err
}
Expand All @@ -471,31 +455,27 @@ func readG2(reader io.ReadSeeker) (G2, error) {
var g2 G2

x0, err := readBigInt(reader, BN254_FIELD_ELEMENT_SIZE)

if err != nil {
return G2{}, err
}

g2[0] = x0

x1, err := readBigInt(reader, BN254_FIELD_ELEMENT_SIZE)

if err != nil {
return G2{}, err
}

g2[1] = x1

y0, err := readBigInt(reader, BN254_FIELD_ELEMENT_SIZE)

if err != nil {
return G2{}, err
}

g2[2] = y0

y1, err := readBigInt(reader, BN254_FIELD_ELEMENT_SIZE)

if err != nil {
return G2{}, err
}
Expand Down
Loading

0 comments on commit cf6387f

Please sign in to comment.