Skip to content
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

Refactored backend and frontend to avoid use of build tags (curve dependency) #23

Merged
merged 19 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 4 additions & 7 deletions backend/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generated by gnark/internal/generators DO NOT EDIT

package backend

import (
"bufio"
"encoding/csv"
"io"
"math/big"
"os"
"strings"

"github.com/consensys/gnark/curve/fr"
)

// Assignment is used to specify inputs to the Prove and Verify functions
type Assignment struct {
Value fr.Element
Value big.Int
IsPublic bool // default == false (assignemnt is private)
}

Expand All @@ -47,10 +44,10 @@ func (a Assignments) Assign(visibility Visibility, name string, v interface{}) {
}
switch visibility {
case Secret:
a[name] = Assignment{Value: fr.FromInterface(v)}
a[name] = Assignment{Value: FromInterface(v)}
case Public:
a[name] = Assignment{
Value: fr.FromInterface(v),
Value: FromInterface(v),
IsPublic: true,
}
default:
Expand Down
30 changes: 30 additions & 0 deletions backend/assignment_test.backup
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package backend

import (
"testing"

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

while is that file commited? need to restore these tests maybe?

func TestDuplicateAssignment(t *testing.T) {

defer func() {
if r := recover(); r == nil {
t.Fatalf("duplicate assignment will panic.")
}
}()

a := NewAssignment()
a.Assign(Public, "x", 1)
a.Assign(Secret, "x", 1)
}

func TestVisibility(t *testing.T) {
assert := require.New(t)
a := NewAssignment()
a.Assign(Public, "x", 1)
a.Assign(Secret, "y", 1)

assert.True(a["x"].IsPublic)
assert.False(a["y"].IsPublic)
}
41 changes: 23 additions & 18 deletions backend/static/bls377/groth16/fft.go → backend/bls377/fft.go

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

32 changes: 16 additions & 16 deletions backend/groth16/fft_test.go → backend/bls377/fft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ See the License for the specific language governing permissions and
limitations under the License.
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't forget to remove the +build tags :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this file inside bls377 and nowhere else? Should we generate fft_test from template, too?

*/

package groth16
package backend_bls377

import (
"testing"

"github.com/consensys/gnark/curve/fr"
"github.com/consensys/gurvy/bls377/fr"
)

func TestFFT(t *testing.T) {
Expand All @@ -47,7 +47,7 @@ func TestFFT(t *testing.T) {
fftExpected[1].SetString("176691886079129423236139828277131126232163084109021849887887564")
fftExpected[2].SetString("8444461749428370424248824938781546531375899335154063827935233455917408882477")
fftExpected[3].SetString("8444461749428193732362745809358310391547622204027831664851124434067521319365")
fft(poly, w)
FFT(poly, w)

for i := 0; i < 4; i++ {
if !poly[i].Equal(&fftExpected[i]) {
Expand Down Expand Up @@ -90,16 +90,16 @@ func BenchmarkFFT(b *testing.B) {
rootOfUnity.SetString(RootOfUnityStr)

const nbGates = 500000
subGroup := newDomain(rootOfUnity, MaxOrder, nbGates)
subGroup := NewDomain(rootOfUnity, MaxOrder, nbGates)

a := make([]fr.Element, subGroup.cardinality)
a := make([]fr.Element, subGroup.Cardinality)
for i := 0; i < len(a); i++ {
a[i].SetRandom()
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
fft(a, subGroup.generator)
FFT(a, subGroup.Generator)
}
}

Expand All @@ -115,18 +115,18 @@ func TestNewDomain(t *testing.T) {
for i := uint(0); i < uint(25); i++ {
m := 1 << i // m = 2^i

S := newDomain(rootOfUnity, MaxOrder, m)
S := NewDomain(rootOfUnity, MaxOrder, m)

// test S.GeneratorSqRt^2 == S.Generator
var generatorSqRtSq fr.Element
generatorSqRtSq.Mul(&S.generatorSqRt, &S.generatorSqRt)
if generatorSqRtSq != S.generator {
generatorSqRtSq.Mul(&S.GeneratorSqRt, &S.GeneratorSqRt)
if generatorSqRtSq != S.Generator {
t.Error("GeneratorSqRt^2 != Generator")
}

// test order of S.Generator
var generatorPow fr.Element
generatorPow.Set(&S.generator)
generatorPow.Set(&S.Generator)
for j := uint(0); j < i; j++ {
if generatorPow.Equal(&one) {
t.Error("Generator order too small: expected:", m, "received:", 1<<j)
Expand All @@ -141,27 +141,27 @@ func TestNewDomain(t *testing.T) {

// test S.Generator * S.GeneratorInv == 1
var inverseTest fr.Element
inverseTest.Mul(&S.generator, &S.generatorInv)
inverseTest.Mul(&S.Generator, &S.GeneratorInv)
if !inverseTest.Equal(&one) {
t.Error("Generator inverse incorrect: expected: 1 received:", inverseTest.FromMont())
break
}

// test S.GeneratorSqRt * S.GeneratorSqRtInv == 1
inverseTest.Mul(&S.generatorSqRt, &S.generatorSqRtInv)
inverseTest.Mul(&S.GeneratorSqRt, &S.GeneratorSqRtInv)
if !inverseTest.Equal(&one) {
t.Error("GeneratorSqRt inverse incorrect: expected: 1 received:", inverseTest.FromMont())
break
}

// test S.Cardinality, S.CardinalityInv
if S.cardinality != m {
t.Error("Cardinality incorrect: expected:", m, "received:", S.cardinality)
if S.Cardinality != m {
t.Error("Cardinality incorrect: expected:", m, "received:", S.Cardinality)
break
}
var cardinalityelement fr.Element
cardinalityelement.SetUint64(uint64(S.cardinality))
inverseTest.Mul(&cardinalityelement, &S.cardinalityInv)
cardinalityelement.SetUint64(uint64(S.Cardinality))
inverseTest.Mul(&cardinalityelement, &S.CardinalityInv)
if !inverseTest.Equal(&one) {
t.Error("CardinalityInv incorrect: expected: 1 received:", inverseTest.FromMont())
break
Expand Down
138 changes: 138 additions & 0 deletions backend/bls377/groth16/assert.go

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