Skip to content

Commit

Permalink
final exponentiation: select optimisation (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
shramee authored Nov 26, 2024
1 parent 15328ce commit bc78c54
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 8 deletions.
5 changes: 1 addition & 4 deletions std/algebra/emulated/sw_bls12381/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ func (pr Pairing) finalExponentiation(e *GTEl, unsafe bool) *GTEl {
// the case, the result is 1 in the torus. We assign a dummy value (1) to e.C1
// and proceed further.
selector1 = pr.Ext6.IsZero(&e.C1)
e = &fields_bls12381.E12{
C0: e.C0,
C1: *pr.Ext6.Select(selector1, _dummy, &e.C1),
}
e.C1.B0.A0 = *pr.curveF.Select(selector1, pr.curveF.One(), &e.C1.B0.A0)
}

// Torus compression absorbed:
Expand Down
61 changes: 61 additions & 0 deletions std/algebra/emulated/sw_bls12381/pairing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,64 @@ func BenchmarkPairing(b *testing.B) {
}
})
}

func BenchmarkFinalExponentiation(b *testing.B) {
// e(a,2b) * e(-2a,b) == 1
var gt bls12381.GT
gt.SetRandom()
res := bls12381.FinalExponentiation(&gt)
witness := FinalExponentiationCircuit{
InGt: NewGTEl(gt),
Res: NewGTEl(res),
}
w, err := frontend.NewWitness(&witness, ecc.BN254.ScalarField())
if err != nil {
b.Fatal(err)
}
var ccs constraint.ConstraintSystem
b.Run("compile scs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ccs, err = frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &FinalExponentiationCircuit{}); err != nil {
b.Fatal(err)
}
}
})
var buf bytes.Buffer
_, err = ccs.WriteTo(&buf)
if err != nil {
b.Fatal(err)
}
b.Logf("scs size: %d (bytes), nb constraints %d, nbInstructions: %d", buf.Len(), ccs.GetNbConstraints(), ccs.GetNbInstructions())
b.Run("solve scs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := ccs.Solve(w); err != nil {
b.Fatal(err)
}
}
})
b.Run("compile r1cs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ccs, err = frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &FinalExponentiationCircuit{}); err != nil {
b.Fatal(err)
}
}
})
buf.Reset()
_, err = ccs.WriteTo(&buf)
if err != nil {
b.Fatal(err)
}
b.Logf("r1cs size: %d (bytes), nb constraints %d, nbInstructions: %d", buf.Len(), ccs.GetNbConstraints(), ccs.GetNbInstructions())

b.Run("solve r1cs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := ccs.Solve(w); err != nil {
b.Fatal(err)
}
}
})
}
5 changes: 1 addition & 4 deletions std/algebra/emulated/sw_bn254/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ func (pr Pairing) finalExponentiation(e *GTEl, unsafe bool) *GTEl {
// the case, the result is 1 in the torus. We assign a dummy value (1) to e.C1
// and proceed further.
selector1 = pr.Ext6.IsZero(&e.C1)
e = &fields_bn254.E12{
C0: e.C0,
C1: *pr.Ext6.Select(selector1, _dummy, &e.C1),
}
e.C1.B0.A0 = *pr.curveF.Select(selector1, pr.curveF.One(), &e.C1.B0.A0)
}

// Torus compression absorbed:
Expand Down
62 changes: 62 additions & 0 deletions std/algebra/emulated/sw_bn254/pairing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,65 @@ func BenchmarkPairing(b *testing.B) {
}
})
}

// bench
func BenchmarkFinalExponentiation(b *testing.B) {
// e(a,2b) * e(-2a,b) == 1
var gt bn254.GT
gt.SetRandom()
res := bn254.FinalExponentiation(&gt)
witness := FinalExponentiationCircuit{
InGt: NewGTEl(gt),
Res: NewGTEl(res),
}
w, err := frontend.NewWitness(&witness, ecc.BN254.ScalarField())
if err != nil {
b.Fatal(err)
}
var ccs constraint.ConstraintSystem
b.Run("compile scs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ccs, err = frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &FinalExponentiationCircuit{}); err != nil {
b.Fatal(err)
}
}
})
var buf bytes.Buffer
_, err = ccs.WriteTo(&buf)
if err != nil {
b.Fatal(err)
}
b.Logf("scs size: %d (bytes), nb constraints %d, nbInstructions: %d", buf.Len(), ccs.GetNbConstraints(), ccs.GetNbInstructions())
b.Run("solve scs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := ccs.Solve(w); err != nil {
b.Fatal(err)
}
}
})
b.Run("compile r1cs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ccs, err = frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &FinalExponentiationCircuit{}); err != nil {
b.Fatal(err)
}
}
})
buf.Reset()
_, err = ccs.WriteTo(&buf)
if err != nil {
b.Fatal(err)
}
b.Logf("r1cs size: %d (bytes), nb constraints %d, nbInstructions: %d", buf.Len(), ccs.GetNbConstraints(), ccs.GetNbInstructions())

b.Run("solve r1cs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := ccs.Solve(w); err != nil {
b.Fatal(err)
}
}
})
}

0 comments on commit bc78c54

Please sign in to comment.