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

fix #442: use reflectwalk to walk through circuit structures without building a Schema #444

Merged
merged 18 commits into from
Jan 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ var (
// 1. Assignment (ie assigning values to a frontend.Circuit object)
// 2. Witness (this object: an ordered vector of field elements + metadata)
// 3. Serialized (Binary or JSON) using MarshalBinary or MarshalJSON
//
// ! MarshalJSON and UnmarshalJSON are slow, and do not handle all complex circuit structures
type Witness struct {
Vector Vector // TODO @gbotrel the result is an interface for now may change to generic Witness[fr.Element] in an upcoming PR
Schema *schema.Schema // optional, Binary encoding needs no schema
Expand Down Expand Up @@ -148,6 +150,8 @@ func (w *Witness) UnmarshalBinary(data []byte) error {
// MarshalJSON implements json.Marshaler
//
// Only the vector of field elements is marshalled: the curveID and the Schema are omitted.
//
// ! MarshalJSON and UnmarshalJSON are slow, and do not handle all complex circuit structures
func (w *Witness) MarshalJSON() (r []byte, err error) {
if w.Schema == nil {
return nil, errMissingSchema
Expand All @@ -171,6 +175,8 @@ func (w *Witness) MarshalJSON() (r []byte, err error) {
}

// UnmarshalJSON implements json.Unmarshaler
//
// ! MarshalJSON and UnmarshalJSON are slow, and do not handle all complex circuit structures
func (w *Witness) UnmarshalJSON(data []byte) error {
if w.Schema == nil {
return errMissingSchema
Expand Down
4 changes: 2 additions & 2 deletions frontend/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ type Builder interface {

// PublicVariable is called by the compiler when parsing the circuit schema. It panics if
// called inside circuit.Define()
PublicVariable(name *schema.Field) Variable
PublicVariable(schema.LeafInfo) Variable

// SecretVariable is called by the compiler when parsing the circuit schema. It panics if
// called inside circuit.Define()
SecretVariable(field *schema.Field) Variable
SecretVariable(schema.LeafInfo) Variable
}
36 changes: 14 additions & 22 deletions frontend/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,26 @@ func parseCircuit(builder Builder, circuit Circuit) (err error) {
return errors.New("frontend.Circuit methods must be defined on pointer receiver")
}

var countedPublic, countedPrivate int
counterHandler := func(f *schema.Field, tInput reflect.Value) error {
varCount := builder.VariableCount(tInput.Type())
switch f.Visibility {
case schema.Secret:
countedPrivate += varCount
case schema.Public:
countedPublic += varCount
}
return nil
}

s, err := schema.Parse(circuit, tVariable, counterHandler)
s, err := schema.Walk(circuit, tVariable, nil)
if err != nil {
return err
}
s.NbPublic = countedPublic
s.NbSecret = countedPrivate

// we scale the number of secret and public variables by n;
// scs and r1cs builder always return 1. Emulated arithmetic returns number of limbs per variable.
n := builder.VariableCount(nil)
s.Public *= n
s.Secret *= n
log := logger.Logger()
log.Info().Int("nbSecret", s.NbSecret).Int("nbPublic", s.NbPublic).Msg("parsed circuit inputs")
log.Info().Int("nbSecret", s.Secret).Int("nbPublic", s.Public).Msg("parsed circuit inputs")

// leaf handlers are called when encoutering leafs in the circuit data struct
// leafs are Constraints that need to be initialized in the context of compiling a circuit
variableAdder := func(targetVisibility schema.Visibility) func(f *schema.Field, tInput reflect.Value) error {
return func(f *schema.Field, tInput reflect.Value) error {
variableAdder := func(targetVisibility schema.Visibility) func(f schema.LeafInfo, tInput reflect.Value) error {
return func(f schema.LeafInfo, tInput reflect.Value) error {
if tInput.CanSet() {
if f.Visibility == schema.Unset {
return errors.New("can't set val " + f.FullName + " visibility is unset")
return errors.New("can't set val " + f.FullName() + " visibility is unset")
}
if f.Visibility == targetVisibility {
if f.Visibility == schema.Public {
Expand All @@ -109,18 +101,18 @@ func parseCircuit(builder Builder, circuit Circuit) (err error) {

return nil
}
return errors.New("can't set val " + f.FullName)
return errors.New("can't set val " + f.FullName())
}
}

// add public inputs first to compute correct offsets
_, err = schema.Parse(circuit, tVariable, variableAdder(schema.Public))
_, err = schema.Walk(circuit, tVariable, variableAdder(schema.Public))
if err != nil {
return err
}

// add secret inputs
_, err = schema.Parse(circuit, tVariable, variableAdder(schema.Secret))
_, err = schema.Walk(circuit, tVariable, variableAdder(schema.Secret))
if err != nil {
return err
}
Expand Down
17 changes: 6 additions & 11 deletions frontend/cs/r1cs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,24 +624,19 @@ func (builder *builder) Println(a ...frontend.Variable) {

func (builder *builder) printArg(log *constraint.LogEntry, sbb *strings.Builder, a frontend.Variable) {

count := 0
counter := func(f *schema.Field, tValue reflect.Value) error {
count++
return nil
}
// ignoring error, counter() always return nil
_, _ = schema.Parse(a, tVariable, counter)
leafCount, err := schema.Walk(a, tVariable, nil)
count := leafCount.Public + leafCount.Secret

// no variables in nested struct, we use fmt std print function
if count == 0 {
if count == 0 || err != nil {
sbb.WriteString(fmt.Sprint(a))
return
}

sbb.WriteByte('{')
printer := func(f *schema.Field, tValue reflect.Value) error {
printer := func(f schema.LeafInfo, tValue reflect.Value) error {
count--
sbb.WriteString(f.FullName)
sbb.WriteString(f.FullName())
sbb.WriteString(": ")
sbb.WriteString("%s")
if count != 0 {
Expand All @@ -655,7 +650,7 @@ func (builder *builder) printArg(log *constraint.LogEntry, sbb *strings.Builder,
return nil
}
// ignoring error, printer() doesn't return errors
_, _ = schema.Parse(a, tVariable, printer)
_, _ = schema.Walk(a, tVariable, printer)
sbb.WriteByte('}')
}

Expand Down
8 changes: 4 additions & 4 deletions frontend/cs/r1cs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ func (builder *builder) VariableCount(t reflect.Type) int {
}

// PublicVariable creates a new public Variable
func (builder *builder) PublicVariable(f *schema.Field) frontend.Variable {
idx := builder.cs.AddPublicVariable(f.FullName)
func (builder *builder) PublicVariable(f schema.LeafInfo) frontend.Variable {
idx := builder.cs.AddPublicVariable(f.FullName())
return expr.NewLinearExpression(idx, builder.tOne)
}

// SecretVariable creates a new secret Variable
func (builder *builder) SecretVariable(f *schema.Field) frontend.Variable {
idx := builder.cs.AddSecretVariable(f.FullName)
func (builder *builder) SecretVariable(f schema.LeafInfo) frontend.Variable {
idx := builder.cs.AddSecretVariable(f.FullName())
return expr.NewLinearExpression(idx, builder.tOne)
}

Expand Down
17 changes: 6 additions & 11 deletions frontend/cs/scs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,24 +465,19 @@ func (builder *scs) Println(a ...frontend.Variable) {

func (builder *scs) printArg(log *constraint.LogEntry, sbb *strings.Builder, a frontend.Variable) {

count := 0
counter := func(f *schema.Field, tValue reflect.Value) error {
count++
return nil
}
// ignoring error, counter() always return nil
_, _ = schema.Parse(a, tVariable, counter)
leafCount, err := schema.Walk(a, tVariable, nil)
count := leafCount.Public + leafCount.Secret

// no variables in nested struct, we use fmt std print function
if count == 0 {
if count == 0 || err != nil {
sbb.WriteString(fmt.Sprint(a))
return
}

sbb.WriteByte('{')
printer := func(f *schema.Field, tValue reflect.Value) error {
printer := func(f schema.LeafInfo, tValue reflect.Value) error {
count--
sbb.WriteString(f.FullName)
sbb.WriteString(f.FullName())
sbb.WriteString(": ")
sbb.WriteString("%s")
if count != 0 {
Expand All @@ -496,7 +491,7 @@ func (builder *scs) printArg(log *constraint.LogEntry, sbb *strings.Builder, a f
return nil
}
// ignoring error, printer() doesn't return errors
_, _ = schema.Parse(a, tVariable, printer)
_, _ = schema.Walk(a, tVariable, printer)
sbb.WriteByte('}')
}

Expand Down
8 changes: 4 additions & 4 deletions frontend/cs/scs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ func (builder *scs) VariableCount(t reflect.Type) int {
}

// PublicVariable creates a new Public Variable
func (builder *scs) PublicVariable(f *schema.Field) frontend.Variable {
idx := builder.cs.AddPublicVariable(f.FullName)
func (builder *scs) PublicVariable(f schema.LeafInfo) frontend.Variable {
idx := builder.cs.AddPublicVariable(f.FullName())
return expr.NewTermToRefactor(idx, constraint.CoeffIdOne)
}

// SecretVariable creates a new Secret Variable
func (builder *scs) SecretVariable(f *schema.Field) frontend.Variable {
idx := builder.cs.AddSecretVariable(f.FullName)
func (builder *scs) SecretVariable(f schema.LeafInfo) frontend.Variable {
idx := builder.cs.AddSecretVariable(f.FullName())
return expr.NewTermToRefactor(idx, constraint.CoeffIdOne)
}

Expand Down
21 changes: 21 additions & 0 deletions frontend/schema/internal/reflectwalk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 Mitchell Hashimoto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading