mirror of
https://github.com/arnaucube/go-snark-study.git
synced 2026-02-02 17:26:41 +01:00
fixed full flow, now works, need to update circuit parser&compiler, and clean the code
This commit is contained in:
@@ -2,6 +2,7 @@ package circuitcompiler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
@@ -16,7 +17,6 @@ type Circuit struct {
|
||||
PrivateInputs []string
|
||||
PublicInputs []string
|
||||
Signals []string
|
||||
PublicSignals []string
|
||||
Witness []*big.Int
|
||||
Constraints []Constraint
|
||||
R1CS struct {
|
||||
@@ -97,12 +97,12 @@ func (circ *Circuit) GenerateR1CS() ([][]*big.Int, [][]*big.Int, [][]*big.Int) {
|
||||
|
||||
// if existInArray(constraint.Out) {
|
||||
if used[constraint.Out] {
|
||||
panic(errors.New("out variable already used: " + constraint.Out))
|
||||
// panic(errors.New("out variable already used: " + constraint.Out))
|
||||
fmt.Println("variable already used")
|
||||
}
|
||||
used[constraint.Out] = true
|
||||
if constraint.Op == "in" {
|
||||
// TODO constraint.PublicInputs
|
||||
for i := 0; i < len(constraint.PrivateInputs); i++ {
|
||||
for i := 0; i <= len(circ.PublicInputs); 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))
|
||||
@@ -166,8 +166,14 @@ func (circ *Circuit) CalculateWitness(privateInputs []*big.Int, publicInputs []*
|
||||
}
|
||||
w := r1csqap.ArrayOfBigZeros(len(circ.Signals))
|
||||
w[0] = big.NewInt(int64(1))
|
||||
for i, input := range publicInputs {
|
||||
fmt.Println(i + 1)
|
||||
fmt.Println(input)
|
||||
w[i+1] = input
|
||||
}
|
||||
for i, input := range privateInputs {
|
||||
w[i+2] = input
|
||||
fmt.Println(i + len(publicInputs) + 1)
|
||||
w[i+len(publicInputs)+1] = input
|
||||
}
|
||||
for _, constraint := range circ.Constraints {
|
||||
if constraint.Op == "in" {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package circuitcompiler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
@@ -21,17 +22,20 @@ func TestCircuitParser(t *testing.T) {
|
||||
m2 = m1 * s1
|
||||
m3 = m2 + s1
|
||||
out = m3 + 5
|
||||
|
||||
*/
|
||||
|
||||
// flat code, where er is expected_result
|
||||
// equals(s5, s1)
|
||||
// s1 = s5 * 1
|
||||
flat := `
|
||||
func test(private x, public er):
|
||||
aux = x*x
|
||||
y = aux*x
|
||||
z = x + y
|
||||
res = z + 5
|
||||
equals(er, res)
|
||||
out = 1
|
||||
func test(private s0, public s1):
|
||||
s2 = s0*s0
|
||||
s3 = s2*s0
|
||||
s4 = s0 + s3
|
||||
s5 = s4 + 5
|
||||
s5 = s1 * one
|
||||
out = 1 * 1
|
||||
`
|
||||
parser := NewParser(strings.NewReader(flat))
|
||||
circuit, err := parser.Parse()
|
||||
@@ -84,4 +88,11 @@ func TestCircuitParser(t *testing.T) {
|
||||
w, err := circuit.CalculateWitness(privateInputs, publicInputs)
|
||||
assert.Nil(t, err)
|
||||
fmt.Println("w", w)
|
||||
|
||||
circuitJson, _ := json.Marshal(circuit)
|
||||
fmt.Println("circuit:", string(circuitJson))
|
||||
|
||||
assert.Equal(t, circuit.NPublic, 1)
|
||||
assert.Equal(t, len(circuit.PublicInputs), 1)
|
||||
assert.Equal(t, len(circuit.PrivateInputs), 1)
|
||||
}
|
||||
|
||||
@@ -105,10 +105,10 @@ func (p *Parser) parseLine() (*Constraint, error) {
|
||||
// TODO
|
||||
return c, nil
|
||||
}
|
||||
if c.Literal == "out" {
|
||||
// TODO
|
||||
return c, nil
|
||||
}
|
||||
// if c.Literal == "out" {
|
||||
// // TODO
|
||||
// return c, nil
|
||||
// }
|
||||
|
||||
_, lit = p.scanIgnoreWhitespace() // skip =
|
||||
c.Literal += lit
|
||||
@@ -197,16 +197,26 @@ func (p *Parser) Parse() (*Circuit, error) {
|
||||
if !isVal {
|
||||
circuit.Signals = addToArrayIfNotExist(circuit.Signals, constraint.V2)
|
||||
}
|
||||
if constraint.Out == "out" {
|
||||
// if Out is "out", put it after first value (one) and before the inputs
|
||||
// fmt.Println("---")
|
||||
// fmt.Println(circuit.PublicInputs[0])
|
||||
// fmt.Println(constraint.Out)
|
||||
// fmt.Println(constraint.Out == circuit.PublicInputs[0])
|
||||
// fmt.Println("---")
|
||||
|
||||
// if constraint.Out == "out" {
|
||||
// if Out is "out", put it after first value (one) and before the inputs
|
||||
// if constraint.Out == circuit.PublicInputs[0] {
|
||||
if existInArray(circuit.PublicInputs, constraint.Out) {
|
||||
// if Out is a public signal, put it after first value (one) and before the private inputs
|
||||
if !existInArray(circuit.Signals, constraint.Out) {
|
||||
// if already don't exists in signal array
|
||||
signalsCopy := copyArray(circuit.Signals)
|
||||
var auxSignals []string
|
||||
auxSignals = append(auxSignals, signalsCopy[0])
|
||||
auxSignals = append(auxSignals, constraint.Out)
|
||||
auxSignals = append(auxSignals, signalsCopy[1:]...)
|
||||
circuit.Signals = auxSignals
|
||||
circuit.PublicSignals = append(circuit.PublicSignals, constraint.Out)
|
||||
// circuit.PublicInputs = append(circuit.PublicInputs, constraint.Out)
|
||||
circuit.NPublic++
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user