Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasPiellard committed Jun 18, 2020
2 parents b1ba91c + 88bec40 commit 9e82268
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 59 deletions.
9 changes: 1 addition & 8 deletions backend/bls377/r1cs.go

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

9 changes: 1 addition & 8 deletions backend/bls381/r1cs.go

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

9 changes: 1 addition & 8 deletions backend/bn256/r1cs.go

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

1 change: 1 addition & 0 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func cmdSetup(cmd *cobra.Command, args []string) {

// check curve ID (TODO is curve.ID necessary now? Because the circuits are serialized with big.Int, here the curve.ID is "unknown")
curveID, err := gob.PeekCurveID(circuitPath)
fmt.Println("test-----" + curveID.String())
if err != nil {
fmt.Println("error:", err)
os.Exit(-1)
Expand Down
11 changes: 6 additions & 5 deletions examples/cubic/cubic.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package main

import (
backend_bn256 "github.com/consensys/gnark/backend/bn256"
"github.com/consensys/gnark/encoding/gob"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gurvy"
)

func main() {
circuit := New()
circuit.Write("circuit.r1cs")
gob.Write("circuit.r1cs", circuit, gurvy.BN256)
}

// New return the circuit implementing
// x**3 + x + 5 == y
func New() *backend_bn256.R1CS {
func New() *frontend.R1CS {
// create root constraint system
circuit := frontend.New()

Expand All @@ -25,6 +26,6 @@ func New() *backend_bn256.R1CS {
x3.Tag("x^3") // we can tag a variable for testing and / or debugging purposes, it has no impact on performances
circuit.MUSTBE_EQ(y, circuit.ADD(x3, x, 5))

r1cs := backend_bn256.New(&circuit)
return &r1cs
r1cs := circuit.ToR1CS()
return r1cs
}
13 changes: 9 additions & 4 deletions examples/cubic/cubic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@ import (
"testing"

"github.com/consensys/gnark/backend"
backend_bn256 "github.com/consensys/gnark/backend/bn256"
"github.com/consensys/gnark/backend/bn256/groth16"
"github.com/consensys/gurvy/bn256/fr"
)

func TestCubicEquation(t *testing.T) {

assert := groth16.NewAssert(t)
r1cs := New() // x**3+x+5 == y

r1cs := New() // x**3+x+5 == y, captures the geometry of the circuit, not tied to a backend

r1csBN256 := backend_bn256.Cast(r1cs) // specifies the circuit to bn256

{
bad := backend.NewAssignment()
bad.Assign(backend.Secret, "x", 42)
bad.Assign(backend.Public, "y", 42)
assert.NotSolved(r1cs, bad)
assert.NotSolved(&r1csBN256, bad)
}

{
bad := backend.NewAssignment()
bad.Assign(backend.Public, "x", 3) // x should be Secret
bad.Assign(backend.Public, "y", 35)
assert.NotSolved(r1cs, bad)
assert.NotSolved(&r1csBN256, bad)
}

{
Expand All @@ -36,7 +41,7 @@ func TestCubicEquation(t *testing.T) {
expectedValues["x^3"] = xcube
x.SetUint64(3)
expectedValues["x"] = x
assert.Solved(r1cs, good, expectedValues)
assert.Solved(&r1csBN256, good, expectedValues)
}

}
11 changes: 6 additions & 5 deletions examples/exponentiate/exponentiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package main
import (
"fmt"

backend_bn256 "github.com/consensys/gnark/backend/bn256"
"github.com/consensys/gnark/encoding/gob"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gurvy"
)

func main() {
circuit := New()
circuit.Write("circuit.r1cs")
gob.Write("circuit.r1cs", circuit, gurvy.BN256)
}

const bitSize = 8 // number of bits of exponent

// New return the circuit implementing
// y == x**e
// only the bitSize least significant bits of e are used
func New() *backend_bn256.R1CS {
func New() *frontend.R1CS {

// create root constraint system
circuit := frontend.New()
Expand Down Expand Up @@ -46,7 +47,7 @@ func New() *backend_bn256.R1CS {

circuit.MUSTBE_EQ(y, output)

r1cs := backend_bn256.New(&circuit)
r1cs := circuit.ToR1CS()

return &r1cs
return r1cs
}
13 changes: 9 additions & 4 deletions examples/exponentiate/exponentiate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ import (
"testing"

"github.com/consensys/gnark/backend"
backend_bn256 "github.com/consensys/gnark/backend/bn256"
"github.com/consensys/gnark/backend/bn256/groth16"
"github.com/consensys/gurvy/bn256/fr"
)

func TestExponentiate(t *testing.T) {

assert := groth16.NewAssert(t)
r1cs := New() // y == x**e

r1cs := New() // y == x**e, captures the geometry of the circuit, not tied to any curve.

r1csBN256 := backend_bn256.Cast(r1cs)

// TODO bigger numbers
{
bad := backend.NewAssignment()
bad.Assign(backend.Public, "x", 2)
bad.Assign(backend.Secret, "e", 12)
bad.Assign(backend.Public, "y", 4095) // y != x**e
assert.NotSolved(r1cs, bad)
assert.NotSolved(&r1csBN256, bad)
}

{
bad := backend.NewAssignment()
bad.Assign(backend.Public, "x", 2)
bad.Assign(backend.Public, "e", 12) // e should be Secret
bad.Assign(backend.Public, "y", 4096)
assert.NotSolved(r1cs, bad)
assert.NotSolved(&r1csBN256, bad)
}

{
Expand All @@ -52,7 +57,7 @@ func TestExponentiate(t *testing.T) {
expectedValues["e[5]"] = bindec[5]
expectedValues["e[6]"] = bindec[6]
expectedValues["e[7]"] = bindec[7]
assert.Solved(r1cs, good, expectedValues)
assert.Solved(&r1csBN256, good, expectedValues)
}

}
4 changes: 2 additions & 2 deletions examples/mimc/input
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
public, h, 3576610639377770372167309049248361867549136162456161943898479697477337767682
secret, pi, 35
public, h,14909111857246927477170742449943584910769272543322217201594632562723849274070
secret, pi,7808462342289447506325013279997289618334122576263655295146895675168642919487
10 changes: 5 additions & 5 deletions examples/mimc/mimc.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package main

import (
backend_bn256 "github.com/consensys/gnark/backend/bn256"
"github.com/consensys/gnark/encoding/gob"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/gadgets/hash/mimc"
"github.com/consensys/gurvy"
)

func main() {
circuit := New()
circuit.Write("circuit.r1cs")
gob.Write("circuit.r1cs", circuit, gurvy.BN256)
}

// New return the circuit implementing
// a pre image check
func New() *backend_bn256.R1CS {
func New() *frontend.R1CS {
// create root constraint system
circuit := frontend.New()

Expand All @@ -29,7 +29,7 @@ func New() *backend_bn256.R1CS {
// mimc(preImage) == hash
circuit.MUSTBE_EQ(hash, mimc.Hash(&circuit, preImage))

r1cs := backend_bn256.New(&circuit)
r1cs := circuit.ToR1CS()

return &r1cs
return r1cs
}
12 changes: 8 additions & 4 deletions examples/mimc/mimc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,38 @@ package main
import (
"testing"

backend_bn256 "github.com/consensys/gnark/backend/bn256"

"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/bn256/groth16"
)

func TestPreimage(t *testing.T) {

assert := groth16.NewAssert(t)
circuit := New()

r1cs := New()
r1csBN256 := backend_bn256.Cast(r1cs)

{
bad := backend.NewAssignment()
bad.Assign(backend.Public, "h", 42)
bad.Assign(backend.Secret, "pi", 42)
assert.NotSolved(circuit, bad)
assert.NotSolved(&r1csBN256, bad)
}

{
bad := backend.NewAssignment()
bad.Assign(backend.Public, "h", 3)
bad.Assign(backend.Public, "pi", 35) // pi should be Secret
assert.NotSolved(circuit, bad)
assert.NotSolved(&r1csBN256, bad)
}

{
good := backend.NewAssignment()
good.Assign(backend.Secret, "pi", 35)
good.Assign(backend.Public, "h", "19226210204356004706765360050059680583735587569269469539941275797408975356275")
assert.Solved(circuit, good, nil)
assert.Solved(&r1csBN256, good, nil)
}

}
2 changes: 2 additions & 0 deletions gadgets/hash/mimc/mimc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package mimc

import (
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -66,6 +67,7 @@ func TestMimcBN256(t *testing.T) {
b := mimcbn256.Sum("seed", databn256.Bytes())
var tmp fr_bn256.Element
tmp.SetBytes(b)
fmt.Println(tmp.String())
expectedValues["res"] = tmp

// provide inputs to the circuit
Expand Down
6 changes: 0 additions & 6 deletions internal/generators/backend/template/representations/r1cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"fmt"
"strconv"
{{ template "import_curve" . }}
{{if ne .Curve "GENERIC"}}
"github.com/consensys/gnark/backend"
{{end}}
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/internal/utils/debug"
"github.com/consensys/gnark/encoding/gob"
)
// R1CS decsribes a set of R1CS constraint
Expand Down Expand Up @@ -402,8 +400,4 @@ func (r1c *R1C) solveR1c(wireInstantiated []bool, wireValues []fr.Element) {
}
}
func (r1cs *R1CS) Write(path string) error {
return gob.Write(path, r1cs, curve.ID)
}
`

0 comments on commit 9e82268

Please sign in to comment.