Browse Source

add private & public inputs wrapper

pull/5/head
arnaucube 5 years ago
parent
commit
e0f427095e
3 changed files with 105 additions and 45 deletions
  1. +13
    -7
      circuitcompiler/circuit.go
  2. +38
    -35
      circuitcompiler/circuit_test.go
  3. +54
    -3
      circuitcompiler/parser.go

+ 13
- 7
circuitcompiler/circuit.go

@ -13,7 +13,8 @@ type Circuit struct {
NVars int
NPublic int
NSignals int
Inputs []string
PrivateInputs []string
PublicInputs []string
Signals []string
PublicSignals []string
Witness []*big.Int
@ -34,7 +35,8 @@ type Constraint struct {
Out string
Literal string
Inputs []string // in func declaration case
PrivateInputs []string // in func declaration case
PublicInputs []string // in func declaration case
}
func indexInArray(arr []string, e string) int {
@ -99,7 +101,8 @@ func (circ *Circuit) GenerateR1CS() ([][]*big.Int, [][]*big.Int, [][]*big.Int) {
}
used[constraint.Out] = true
if constraint.Op == "in" {
for i := 0; i < len(constraint.Inputs); i++ {
// TODO constraint.PublicInputs
for i := 0; i < len(constraint.PrivateInputs); i++ {
aConstraint[indexInArray(circ.Signals, constraint.Out)] = new(big.Int).Add(aConstraint[indexInArray(circ.Signals, constraint.Out)], big.NewInt(int64(1)))
aConstraint, used = insertVar(aConstraint, circ.Signals, constraint.Out, used)
bConstraint[0] = big.NewInt(int64(1))
@ -154,13 +157,16 @@ type Inputs struct {
// CalculateWitness calculates the Witness of a Circuit based on the given inputs
// witness = [ one, output, publicInputs, privateInputs, ...]
func (circ *Circuit) CalculateWitness(inputs []*big.Int) ([]*big.Int, error) {
if len(inputs) != len(circ.Inputs) {
return []*big.Int{}, errors.New("given inputs != circuit.Inputs")
func (circ *Circuit) CalculateWitness(privateInputs []*big.Int, publicInputs []*big.Int) ([]*big.Int, error) {
if len(privateInputs) != len(circ.PrivateInputs) {
return []*big.Int{}, errors.New("given privateInputs != circuit.PublicInputs")
}
if len(publicInputs) != len(circ.PublicInputs) {
return []*big.Int{}, errors.New("given publicInputs != circuit.PublicInputs")
}
w := r1csqap.ArrayOfBigZeros(len(circ.Signals))
w[0] = big.NewInt(int64(1))
for i, input := range inputs {
for i, input := range privateInputs {
w[i+2] = input
}
for _, constraint := range circ.Constraints {

+ 38
- 35
circuitcompiler/circuit_test.go

@ -23,62 +23,65 @@ func TestCircuitParser(t *testing.T) {
out = m3 + 5
*/
// flat code
// flat code, where er is expected_result
flat := `
func test(x):
func test(private x, public er):
aux = x*x
y = aux*x
z = x + y
out = z + 5
res = z + 5
equals(er, res)
out = 1
`
parser := NewParser(strings.NewReader(flat))
circuit, err := parser.Parse()
assert.Nil(t, err)
fmt.Println(circuit)
fmt.Println("circuit parsed: ", circuit)
// flat code to R1CS
fmt.Println("generating R1CS from flat code")
a, b, c := circuit.GenerateR1CS()
fmt.Print("function with inputs: ")
fmt.Println(circuit.Inputs)
fmt.Println("private inputs: ", circuit.PrivateInputs)
fmt.Println("public inputs: ", circuit.PublicInputs)
fmt.Print("signals: ")
fmt.Println(circuit.Signals)
fmt.Println("signals:", circuit.Signals)
// expected result
b0 := big.NewInt(int64(0))
b1 := big.NewInt(int64(1))
b5 := big.NewInt(int64(5))
aExpected := [][]*big.Int{
[]*big.Int{b0, b0, b1, b0, b0, b0},
[]*big.Int{b0, b0, b0, b1, b0, b0},
[]*big.Int{b0, b0, b1, b0, b1, b0},
[]*big.Int{b5, b0, b0, b0, b0, b1},
}
bExpected := [][]*big.Int{
[]*big.Int{b0, b0, b1, b0, b0, b0},
[]*big.Int{b0, b0, b1, b0, b0, b0},
[]*big.Int{b1, b0, b0, b0, b0, b0},
[]*big.Int{b1, b0, b0, b0, b0, b0},
}
cExpected := [][]*big.Int{
[]*big.Int{b0, b0, b0, b1, b0, b0},
[]*big.Int{b0, b0, b0, b0, b1, b0},
[]*big.Int{b0, b0, b0, b0, b0, b1},
[]*big.Int{b0, b1, b0, b0, b0, b0},
}
assert.Equal(t, aExpected, a)
assert.Equal(t, bExpected, b)
assert.Equal(t, cExpected, c)
// b0 := big.NewInt(int64(0))
// b1 := big.NewInt(int64(1))
// b5 := big.NewInt(int64(5))
// aExpected := [][]*big.Int{
// []*big.Int{b0, b0, b1, b0, b0, b0},
// []*big.Int{b0, b0, b0, b1, b0, b0},
// []*big.Int{b0, b0, b1, b0, b1, b0},
// []*big.Int{b5, b0, b0, b0, b0, b1},
// }
// bExpected := [][]*big.Int{
// []*big.Int{b0, b0, b1, b0, b0, b0},
// []*big.Int{b0, b0, b1, b0, b0, b0},
// []*big.Int{b1, b0, b0, b0, b0, b0},
// []*big.Int{b1, b0, b0, b0, b0, b0},
// }
// cExpected := [][]*big.Int{
// []*big.Int{b0, b0, b0, b1, b0, b0},
// []*big.Int{b0, b0, b0, b0, b1, b0},
// []*big.Int{b0, b0, b0, b0, b0, b1},
// []*big.Int{b0, b1, b0, b0, b0, b0},
// }
//
// assert.Equal(t, aExpected, a)
// assert.Equal(t, bExpected, b)
// assert.Equal(t, cExpected, c)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
b3 := big.NewInt(int64(3))
inputs := []*big.Int{b3}
privateInputs := []*big.Int{b3}
b35 := big.NewInt(int64(35))
publicInputs := []*big.Int{b35}
// Calculate Witness
w, err := circuit.CalculateWitness(inputs)
w, err := circuit.CalculateWitness(privateInputs, publicInputs)
assert.Nil(t, err)
fmt.Println("w", w)
}

+ 54
- 3
circuitcompiler/parser.go

@ -2,7 +2,9 @@ package circuitcompiler
import (
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
)
@ -70,7 +72,41 @@ func (p *Parser) parseLine() (*Constraint, error) {
rgx := regexp.MustCompile(`\((.*?)\)`)
insideParenthesis := rgx.FindStringSubmatch(line)
varsString := strings.Replace(insideParenthesis[1], " ", "", -1)
c.Inputs = strings.Split(varsString, ",")
allInputs := strings.Split(varsString, ",")
// from allInputs, get the private and the public separated
for _, in := range allInputs {
if strings.Contains(in, "private") {
input := strings.Replace(in, "private", "", -1)
c.PrivateInputs = append(c.PrivateInputs, input)
} else if strings.Contains(in, "public") {
input := strings.Replace(in, "public", "", -1)
c.PublicInputs = append(c.PublicInputs, input)
} else {
// TODO give more info about the circuit code error
fmt.Println("error on declaration of public and private inputs")
os.Exit(0)
}
}
return c, nil
}
if c.Literal == "equals" {
// format: `equals(a, b)`
line, err := p.s.r.ReadString(')')
if err != nil {
return c, err
}
// read string inside ( )
rgx := regexp.MustCompile(`\((.*?)\)`)
insideParenthesis := rgx.FindStringSubmatch(line)
varsString := strings.Replace(insideParenthesis[1], " ", "", -1)
params := strings.Split(varsString, ",")
fmt.Println("params", params)
// TODO
return c, nil
}
if c.Literal == "out" {
// TODO
return c, nil
}
@ -124,9 +160,10 @@ func (p *Parser) Parse() (*Circuit, error) {
if err != nil {
break
}
fmt.Println(constraint)
if constraint.Literal == "func" {
// one constraint for each input
for _, in := range constraint.Inputs {
for _, in := range constraint.PrivateInputs {
newConstr := &Constraint{
Op: "in",
Out: in,
@ -134,7 +171,21 @@ func (p *Parser) Parse() (*Circuit, error) {
circuit.Constraints = append(circuit.Constraints, *newConstr)
nInputs++
}
circuit.Inputs = constraint.Inputs
for _, in := range constraint.PublicInputs {
newConstr := &Constraint{
Op: "in",
Out: in,
}
circuit.Constraints = append(circuit.Constraints, *newConstr)
nInputs++
}
circuit.PublicInputs = constraint.PublicInputs
circuit.PrivateInputs = constraint.PrivateInputs
continue
}
if constraint.Literal == "equals" {
// TODO
fmt.Println("circuit.Signals", circuit.Signals)
continue
}
circuit.Constraints = append(circuit.Constraints, *constraint)

Loading…
Cancel
Save