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

refactor: std/math/nonnative -> std/math/emulated #345

Merged
merged 43 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
21c05c5
test: added a shorter path for nonnative. remove plonk fri tests
gbotrel Jul 14, 2022
2d82414
feat: added nonnative snippet in internal/stats
gbotrel Jul 14, 2022
de4d8ba
feat: added example/emulated
gbotrel Jul 14, 2022
79b5b75
fix: added bls24 in plonkfri
gbotrel Jul 14, 2022
2de8c97
refactor: nonnative -> emulated
gbotrel Jul 14, 2022
7295ca5
refactor: checkpoint
gbotrel Jul 14, 2022
a7ceccb
refactor: checkpiont before generic experiment
gbotrel Jul 14, 2022
71fbab3
refactor: checkpoint
gbotrel Jul 16, 2022
729776e
refactor: fix len(limbs) in compact
gbotrel Jul 16, 2022
e0bc6dd
feat: added GnarkInitHook
gbotrel Jul 16, 2022
52f54cf
feat: fixed stats for emulated arithmetic
gbotrel Jul 16, 2022
8af1614
refactor: restored some tests in std/math/emulated
gbotrel Jul 16, 2022
877705c
test: restore more tests in std/emulated/
gbotrel Jul 16, 2022
abf899c
refactor: all tests restored
gbotrel Jul 16, 2022
d2f6990
test: restore emulated pairing test
gbotrel Jul 16, 2022
279b8f7
perf: minor improvments to big.Int test engine
gbotrel Jul 17, 2022
eea7e1c
Merge branch 'feat/enginepool' into refactor/emulated
gbotrel Jul 18, 2022
f5cade6
feat: adjusted emulated params to be on 64bits-6limbs for bls12377fp
gbotrel Jul 18, 2022
2df883d
style: code cleaning
gbotrel Jul 18, 2022
1a91ebb
test: restore composition_test.go
gbotrel Jul 18, 2022
29a335d
build: staticcheck, govet and gosec are done by golanglint ci
gbotrel Jul 18, 2022
cdfb0eb
test: added ops counters in test engine
gbotrel Jul 18, 2022
9254d01
test: checkpoint
gbotrel Jul 18, 2022
8d03234
test: handle nil input in emulated API
gbotrel Jul 18, 2022
755cd00
style: remove redundant code for Element[T] init
gbotrel Jul 18, 2022
e91aa1d
test: isolated inneficient emulated e12 square to start with
gbotrel Jul 18, 2022
334b0fb
style: clean std/emulated/mul
gbotrel Jul 18, 2022
c691476
style: simplified equality hint
gbotrel Jul 18, 2022
d161030
test: added failing constant test
gbotrel Jul 18, 2022
324b185
test: fix previous commit
gbotrel Jul 18, 2022
c3b7eff
style: factorize some emulated hint code
gbotrel Jul 19, 2022
1e9cc44
refactor: checkpoint
gbotrel Jul 19, 2022
3098dc5
refactor: checkpoint
gbotrel Jul 19, 2022
66942fd
refactor: checkpoint
gbotrel Jul 19, 2022
25b5e70
style: clean up
gbotrel Jul 19, 2022
07fbca5
docs: addeed comments in emulated.subPadding
gbotrel Jul 22, 2022
e0dea7a
test: added mont mul test
gbotrel Jul 22, 2022
25c2df2
test: added basic montmul test to ease in-circuit dev
gbotrel Jul 26, 2022
a225184
fix: previous commit call on dangling pointer
gbotrel Jul 26, 2022
ca9ced4
fix: remove montmul_test.go
gbotrel Jul 27, 2022
6ddd173
Merge branch 'develop' into refactor/emulated
gbotrel Jul 27, 2022
b506be8
docs: update NewField doc
gbotrel Jul 27, 2022
7c5dbed
feat: implement PR review fixes
gbotrel Jul 29, 2022
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
10 changes: 0 additions & 10 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ jobs:
run: go install golang.org/x/tools/cmd/goimports@latest && go install github.com/klauspost/asmfmt/cmd/asmfmt@latest
- name: gofmt
run: if [[ -n $(gofmt -l .) ]]; then echo "please run gofmt"; exit 1; fi
- name: go vet
run: go vet ./...
- name: staticcheck
run: |
go install honnef.co/go/tools/cmd/staticcheck@23e1086441d24fed9f668ad1cd4374245118b590
staticcheck ./...
- name: gosec
run: |
go install github.com/securego/gosec/v2/cmd/gosec@latest
gosec -exclude G204 ./...
- name: generated files should not be modified
run: |
go generate ./...
Expand Down
10 changes: 0 additions & 10 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ jobs:
run: go install golang.org/x/tools/cmd/goimports@latest && go install github.com/klauspost/asmfmt/cmd/asmfmt@latest
- name: gofmt
run: if [[ -n $(gofmt -l .) ]]; then echo "please run gofmt"; exit 1; fi
- name: go vet
run: go vet ./...
- name: staticcheck
run: |
go install honnef.co/go/tools/cmd/staticcheck@23e1086441d24fed9f668ad1cd4374245118b590
staticcheck ./...
- name: gosec
run: |
go install github.com/securego/gosec/v2/cmd/gosec@latest
gosec -exclude G204 ./...
- name: generated files should not be modified
run: |
go generate ./...
Expand Down
19 changes: 18 additions & 1 deletion backend/plonkfri/plonkfri.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import (
witness_bn254 "github.com/consensys/gnark/internal/backend/bn254/witness"
witness_bw6633 "github.com/consensys/gnark/internal/backend/bw6-633/witness"
witness_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/witness"

cs_bls24317 "github.com/consensys/gnark/internal/backend/bls24-317/cs"
plonk_bls24317 "github.com/consensys/gnark/internal/backend/bls24-317/plonkfri"
witness_bls24317 "github.com/consensys/gnark/internal/backend/bls24-317/witness"
)

// Proof represents a Plonk proof generated by plonk.Prove
Expand Down Expand Up @@ -86,6 +90,8 @@ func Setup(ccs frontend.CompiledConstraintSystem) (ProvingKey, VerifyingKey, err
return plonk_bls24315.Setup(tccs)
case *cs_bw6633.SparseR1CS:
return plonk_bw6633.Setup(tccs)
case *cs_bls24317.SparseR1CS:
return plonk_bls24317.Setup(tccs)
default:
panic("unrecognized SparseR1CS curve type")
}
Expand Down Expand Up @@ -147,7 +153,12 @@ func Prove(ccs frontend.CompiledConstraintSystem, pk ProvingKey, fullWitness *wi
return nil, witness.ErrInvalidWitness
}
return plonk_bls24315.Prove(tccs, pk.(*plonk_bls24315.ProvingKey), *w, opt)

case *cs_bls24317.SparseR1CS:
w, ok := fullWitness.Vector.(*witness_bls24317.Witness)
if !ok {
return nil, witness.ErrInvalidWitness
}
return plonk_bls24317.Prove(tccs, pk.(*plonk_bls24317.ProvingKey), *w, opt)
default:
panic("unrecognized SparseR1CS curve type")
}
Expand Down Expand Up @@ -199,6 +210,12 @@ func Verify(proof Proof, vk VerifyingKey, publicWitness *witness.Witness) error
return witness.ErrInvalidWitness
}
return plonk_bls24315.Verify(_proof, vk.(*plonk_bls24315.VerifyingKey), *w)
case *plonk_bls24317.Proof:
w, ok := publicWitness.Vector.(*witness_bls24317.Witness)
if !ok {
return witness.ErrInvalidWitness
}
return plonk_bls24317.Verify(_proof, vk.(*plonk_bls24317.VerifyingKey), *w)

default:
panic("unrecognized proof type")
Expand Down
23 changes: 23 additions & 0 deletions examples/emulated/emulated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package emulated

import (
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/math/emulated"
)

type Circuit struct {
// Limbs of non-native elements X, Y and Res
X, Y, Res emulated.Element[emulated.Secp256k1]
}

func (circuit *Circuit) Define(api frontend.API) error {
// wrap API to work in SECP256k1 scalar field
secp256k1, err := emulated.NewField[emulated.Secp256k1](api)
if err != nil {
return err
}

tmp := secp256k1.Mul(circuit.X, circuit.Y)
secp256k1.AssertIsEqual(tmp, circuit.Res)
return nil
}
23 changes: 23 additions & 0 deletions examples/emulated/emulated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package emulated

import (
"testing"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/std"
"github.com/consensys/gnark/test"
)

func TestEmulatedArithmetic(t *testing.T) {
assert := test.NewAssert(t)
std.RegisterHints()

var circuit, witness Circuit

witness.X.Assign("26959946673427741531515197488526605382048662297355296634326893985793")
witness.Y.Assign("53919893346855483063030394977053210764097324594710593268653787971586")
witness.Res.Assign("485279052387156144224396168012515269674445015885648619762653195154800")

assert.ProverSucceeded(&circuit, &witness, test.WithCurves(ecc.BN254), test.WithBackends(backend.GROTH16), test.NoSerialization())
}
9 changes: 9 additions & 0 deletions frontend/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type Schema struct {
// LeafHandler is the handler function that will be called when Visit reaches leafs of the struct
type LeafHandler func(field *Field, tValue reflect.Value) error

// An object implementing an init hook knows how to "init" itself
// when parsed at compile time
type InitHook interface {
GnarkInitHook() // TODO @gbotrel find a better home for this
}

// Parse filters recursively input data struct and keeps only the fields containing slices, arrays of elements of
// type frontend.Variable and return the corresponding Slices are converted to arrays.
//
Expand Down Expand Up @@ -278,6 +284,9 @@ func parse(r []Field, input interface{}, target reflect.Type, parentFullName, pa

if fValue.CanAddr() && fValue.Addr().CanInterface() {
value := fValue.Addr().Interface()
if ih, hasInitHook := value.(InitHook); hasInitHook {
ih.GnarkInitHook()
}
var err error
subFields, err = parse(subFields, value, target, getFullName(parentFullName, name, nameTag), name, nameTag, visibility, handler, nbPublic, nbSecret)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/leanovate/gopter v0.2.9
github.com/rs/zerolog v1.26.1
github.com/stretchr/testify v1.7.1
golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75 h1:x03zeu7B2B11ySp+daztnwM5oBJ/8wGUSqrwcw9L0RA=
golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
Binary file modified internal/stats/latest.stats
Binary file not shown.
20 changes: 20 additions & 0 deletions internal/stats/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/consensys/gnark/std/algebra/sw_bls24315"
"github.com/consensys/gnark/std/hash/mimc"
"github.com/consensys/gnark/std/math/bits"
"github.com/consensys/gnark/std/math/emulated"
)

var (
Expand Down Expand Up @@ -78,6 +79,25 @@ func initSnippets() {
mimc.Write(newVariable())
_ = mimc.Sum()
})
registerSnippet("math/emulated/secp256k1_32", func(api frontend.API, newVariable func() frontend.Variable) {
secp256k1, _ := emulated.NewField[emulated.Secp256k1](api)

newElement := func() emulated.Element[emulated.Secp256k1] {
r := emulated.NewElement[emulated.Secp256k1](nil)
for i := 0; i < len(r.Limbs); i++ {
r.Limbs[i] = newVariable()
}
return r
}

x13 := secp256k1.Mul(newElement(), newElement(), newElement())
fx2 := secp256k1.Mul(5, newElement())
nom := secp256k1.Sub(fx2, x13)
denom := secp256k1.Add(newElement(), newElement(), newElement(), newElement())
free := secp256k1.Div(nom, denom)
res := secp256k1.Add(x13, fx2, free)
secp256k1.AssertIsEqual(res, newElement())
})

registerSnippet("pairing_bls12377", func(api frontend.API, newVariable func() frontend.Variable) {

Expand Down
12 changes: 3 additions & 9 deletions internal/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/frontend/cs/scs"
"github.com/consensys/gnark/logger"
)

const nbCurves = 7
Expand Down Expand Up @@ -46,7 +45,7 @@ func init() {

func NewGlobalStats() *globalStats {
return &globalStats{
Stats: make(map[string][backend.PLONK + 1][nbCurves + 1]snippetStats),
Stats: make(map[string][backend.PLONKFRI + 1][nbCurves + 1]snippetStats),
}
}

Expand Down Expand Up @@ -80,7 +79,7 @@ func NewSnippetStats(curve ecc.ID, backendID backend.ID, circuit frontend.Circui
switch backendID {
case backend.GROTH16:
newCompiler = r1cs.NewBuilder
case backend.PLONK:
case backend.PLONK, backend.PLONKFRI:
newCompiler = scs.NewBuilder
default:
panic("not implemented")
Expand All @@ -101,11 +100,6 @@ func NewSnippetStats(curve ecc.ID, backendID backend.ID, circuit frontend.Circui
func (s *globalStats) Add(curve ecc.ID, backendID backend.ID, cs snippetStats, circuitName string) {
s.Lock()
defer s.Unlock()
if backendID == backend.PLONKFRI {
log := logger.Logger()
log.Warn().Msg("ignoring plonk_fri circuit")
return
}
rs := s.Stats[circuitName]
rs[backendID][CurveIdx(curve)] = cs
s.Stats[circuitName] = rs
Expand All @@ -118,7 +112,7 @@ type Circuit struct {

type globalStats struct {
sync.RWMutex
Stats map[string][backend.PLONK + 1][nbCurves + 1]snippetStats
Stats map[string][backend.PLONKFRI + 1][nbCurves + 1]snippetStats
}

type snippetStats struct {
Expand Down
4 changes: 0 additions & 4 deletions internal/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ func TestCircuitStatistics(t *testing.T) {
for _, b := range backend.Implemented() {
curve := curve
backendID := b
if backendID == backend.PLONKFRI {
// TODO
continue
}
name := name
// copy the circuit now in case assert calls t.Parallel()
circuit := c.Circuit
Expand Down
4 changes: 2 additions & 2 deletions std/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/consensys/gnark/std/algebra/sw_bls12377"
"github.com/consensys/gnark/std/algebra/sw_bls24315"
"github.com/consensys/gnark/std/math/bits"
"github.com/consensys/gnark/std/math/nonnative"
"github.com/consensys/gnark/std/math/emulated"
)

var registerOnce sync.Once
Expand All @@ -30,5 +30,5 @@ func registerHints() {
hint.Register(bits.NNAF)
hint.Register(bits.IthBit)
hint.Register(bits.NBits)
hint.Register(nonnative.GetHints()...)
hint.Register(emulated.GetHints()...)
}
Loading