diff --git a/.idea/go-snark.iml b/.idea/go-snark.iml
deleted file mode 100644
index c956989..0000000
--- a/.idea/go-snark.iml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index 28a804d..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index b9cbca6..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 94a25f7..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index 1d322fe..0000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,806 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- arnaucube
- oneConst
- opera
- getmain
- one
- oneCo
- md
- new
- newpro
- clone
- newProg
- func
- global
- globalin
- newcir
- newprogr
-
-
- mottla
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- file://$PROJECT_DIR$/circuitcompiler/parser.go
- 81
-
-
-
- file://$PROJECT_DIR$/circuitcompiler/parser.go
- 82
-
-
-
- file://$PROJECT_DIR$/circuitcompiler/parser.go
- 83
-
-
-
- file://$PROJECT_DIR$/r1csqap/r1csqap.go
- 228
-
-
-
- file://$PROJECT_DIR$/r1csqap/r1csqap.go
- 229
-
-
-
- file://$PROJECT_DIR$/r1csqap/r1csqap.go
- 83
-
-
-
- file://$PROJECT_DIR$/r1csqap/r1csqap.go
- 74
-
-
-
- file://$PROJECT_DIR$/snark_test.go
- 113
-
-
-
- file://$PROJECT_DIR$/r1csqap/r1csqap.go
- 135
-
-
-
- file://$PROJECT_DIR$/circuitcompiler/Programm.go
- 514
-
-
-
- file://$PROJECT_DIR$/circuitcompiler/Programm.go
- 218
-
-
-
- file://$PROJECT_DIR$/circuitcompiler/Programm.go
- 298
-
-
-
- file://$PROJECT_DIR$/circuitcompiler/Programm.go
- 325
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/README.md b/README.md
index 788cdef..6cbf654 100644
--- a/README.md
+++ b/README.md
@@ -14,27 +14,31 @@ Warning: not finished.
Working example of gate-reduction and code parsing:
```go
def do(x):
- e = x * 5
- b = e * 6
- c = b * 7
- f = c * 1
- d = c * f
- out = d * mul(d,e)
-
- def doSomethingElse(x ,k):
- z = k * x
- out = do(x) + mul(x,z)
-
- def main(x,z):
- out = do(z) + doSomethingElse(x,x)
-
- def mul(a,b):
- out = a * b
+ e = x * 5
+ b = e * 6
+ c = b * 7
+ f = c * 1
+ d = c * f
+ out = d * mul(d,e)
+
+def doSomethingElse(x ,k):
+ z = k * x
+ out = do(x) + mul(x,z)
+
+def main(x,z):
+ out = do(z) + doSomethingElse(x,x)
+
+def mul(a,b):
+ out = a * b
```
R1CS Output:
```go
-[[0 0 210 0 0 0 0 0 0 0 0 0 0 0] [0 0 210 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 1 0 0 0 0 0 0 0 0 0] [0 0 0 1 0 0 0 0 0 0 0 0 0 0] [0 210 0 0 0 0 0 0 0 0 0 0 0 0] [0 210 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 0 1 0 0 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0 0 0]]
-[[0 0 210 0 0 0 0 0 0 0 0 0 0 0] [0 0 210 0 0 0 0 0 0 0 0 0 0 0] [0 0 5 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0 0 0] [0 210 0 0 0 0 0 0 0 0 0 0 0 0] [0 210 0 0 0 0 0 0 0 0 0 0 0 0] [0 5 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 1 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0 0 0 1 0 1 0]]
-[[0 0 0 1 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 1 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 1 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 1 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 1]]
+[[0 0 210 0 0 0 0 0 0 0 0 0] [0 0 0 1 0 0 0 0 0 0 0 0] [0 0 0 1 0 0 0 0 0 0 0 0] [0 210 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 1 0 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0]]
+[[0 0 210 0 0 0 0 0 0 0 0 0] [0 0 5 0 0 0 0 0 0 0 0 0] [0 0 0 0 1 0 0 0 0 0 0 0] [0 210 0 0 0 0 0 0 0 0 0 0] [0 5 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 1 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 1 0 0 1 0 1 0]]
+[[0 0 0 1 0 0 0 0 0 0 0 0] [0 0 0 0 1 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0] [0 0 0 0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 0 0 0 0 1]]
+input
+[7 11]
+witness
+[1 7 11 5336100 293485500 1566067976550000 2160900 75631500 163432108350000 49 343 1729500084900343]
```
-Note that we only need 11 multiplication Gates instead of 16
+Note that we only need 9 multiplication Gates instead of 16
diff --git a/bn128/bn128.go b/bn128/bn128.go
index 77c1b9c..ebef4ee 100644
--- a/bn128/bn128.go
+++ b/bn128/bn128.go
@@ -4,7 +4,7 @@ import (
"errors"
"math/big"
- "github.com/mottla/go-snark/fields"
+ "github.com/arnaucube/go-snark/fields"
)
// Bn128 is the data structure of the BN128
diff --git a/bn128/g1.go b/bn128/g1.go
index e7a8b56..0f5b9e0 100644
--- a/bn128/g1.go
+++ b/bn128/g1.go
@@ -3,7 +3,7 @@ package bn128
import (
"math/big"
- "github.com/mottla/go-snark/fields"
+ "github.com/arnaucube/go-snark/fields"
)
type G1 struct {
diff --git a/bn128/g2.go b/bn128/g2.go
index 9146500..0c05a3a 100644
--- a/bn128/g2.go
+++ b/bn128/g2.go
@@ -3,7 +3,7 @@ package bn128
import (
"math/big"
- "github.com/mottla/go-snark/fields"
+ "github.com/arnaucube/go-snark/fields"
)
type G2 struct {
diff --git a/circuitcompiler/Programm.go b/circuitcompiler/Programm.go
index c122c3b..bfff667 100644
--- a/circuitcompiler/Programm.go
+++ b/circuitcompiler/Programm.go
@@ -3,9 +3,9 @@ package circuitcompiler
import (
"crypto/sha256"
"fmt"
- "github.com/mottla/go-snark/bn128"
- "github.com/mottla/go-snark/fields"
- "github.com/mottla/go-snark/r1csqap"
+ "github.com/arnaucube/go-snark/bn128"
+ "github.com/arnaucube/go-snark/fields"
+ "github.com/arnaucube/go-snark/r1csqap"
"hash"
"math/big"
"sync"
@@ -41,6 +41,10 @@ type Program struct {
computedFactors map[string]string
}
+func (p *Program) GlobalInputCount() int {
+ return len(p.globalInputs)
+}
+
func (p *Program) PrintContraintTrees() {
for k, v := range p.functions {
fmt.Println(k)
@@ -52,6 +56,7 @@ func (p *Program) BuildConstraintTrees() {
mainRoot := p.getMainCircuit().root
+ //if our programs last operation is not a multiplication gate, we need to introduce on
if mainRoot.value.Op&(MINUS|PLUS) != 0 {
newOut := Constraint{Out: "out", V1: "1", V2: "out2", Op: MULTIPLY}
p.getMainCircuit().addConstraint(&newOut)
@@ -64,6 +69,7 @@ func (p *Program) BuildConstraintTrees() {
}
var wg = sync.WaitGroup{}
+ //we build the parse trees concurrently! because we can! go rocks
for _, circuit := range p.functions {
wg.Add(1)
//interesting: if circuit is not passed as argument, the program fails. duno why..
@@ -87,11 +93,10 @@ func (c *Circuit) buildTree(g *gate) {
panic(fmt.Sprintf("undefined variable %s", g.value.Out))
}
if g.OperationType() == FUNC {
- //g.funcInputs = []*gate{}
+
for _, in := range g.value.Inputs {
if gate, ex := c.gateMap[in]; ex {
g.funcInputs = append(g.funcInputs, gate)
- //note that we do repeated work here. the argument
c.buildTree(gate)
} else {
panic(fmt.Sprintf("undefined argument %s", g.value.V1))
@@ -115,7 +120,6 @@ func (c *Circuit) buildTree(g *gate) {
}
func (p *Program) ReduceCombinedTree() (orderedmGates []gate) {
- //mGatesUsed := make(map[string]bool)
orderedmGates = []gate{}
p.computedInContext = make(map[string]map[string]string)
p.computedFactors = make(map[string]string)
@@ -128,7 +132,6 @@ func (p *Program) ReduceCombinedTree() (orderedmGates []gate) {
//recursively walks through the parse tree to create a list of all
//multiplication gates needed for the QAP construction
//Takes into account, that multiplication with constants and addition (= substraction) can be reduced, and does so
-//TODO if the same variable that has been computed in context A, is needed again but from a different context b, will be recomputed and not reused
func (p *Program) r1CSRecursiveBuild(currentCircuit *Circuit, node *gate, hashTraceBuildup []byte, orderedmGates *[]gate, negate bool, invert bool) (facs []factor, hashTraceResult []byte, variableEnd bool) {
if node.OperationType() == CONST {
diff --git a/circuitcompiler/Programm_test.go b/circuitcompiler/Programm_test.go
index 0bdaa1b..1b8960a 100644
--- a/circuitcompiler/Programm_test.go
+++ b/circuitcompiler/Programm_test.go
@@ -32,6 +32,9 @@ var correctnesTest = []TraceCorrectnessTest{
result: bigNumberResult1,
}},
code: `
+ def main( x , z ) :
+ out = do(z) + add(x,x)
+
def do(x):
e = x * 5
b = e * 6
@@ -44,9 +47,7 @@ var correctnesTest = []TraceCorrectnessTest{
z = k * x
out = do(x) + mul(x,z)
- def main(x,z):
- out = do(z) + add(x,x)
-
+
def mul(a,b):
out = a * b
`,
@@ -98,21 +99,24 @@ var correctnesTest = []TraceCorrectnessTest{
out = g * a
`,
},
+ {
+ io: []InOut{{
+ inputs: []*big.Int{big.NewInt(int64(3)), big.NewInt(int64(5)), big.NewInt(int64(7)), big.NewInt(int64(11))},
+ result: big.NewInt(int64(444675)),
+ }},
+ code: `
+ def main(a,b,c,d):
+ e = a * b
+ f = c * d
+ g = e * f
+ h = g / e
+ i = h * 5
+ out = g * i
+ `,
+ },
}
func TestNewProgramm(t *testing.T) {
- //flat := `
- //
- //def doSomething(x ,k):
- // z = k * x
- // out = 6 + mul(x,z)
- //
- //def main(x,z):
- // out = mul(x,z) - doSomething(x,x)
- //
- //def mul(a,b):
- // out = a * b
- //`
for _, test := range correctnesTest {
parser := NewParser(strings.NewReader(test.code))
diff --git a/circuitcompiler/circuit_test.go b/circuitcompiler/circuit_test.go
index 3e69dc9..0a81b93 100644
--- a/circuitcompiler/circuit_test.go
+++ b/circuitcompiler/circuit_test.go
@@ -16,113 +16,3 @@ func TestXor(t *testing.T) {
assert.Equal(t, false, Xor(false, false))
}
-
-func TestCircuitParser(t *testing.T) {
-
- //flat := `
- //func main(a,b):
- // c = a / b
- // d = c * b
- // e = d - c
- // f = c * 55
- // out = f / e
- //`
- //flat := `
- //func test(a,b):
- // d = a / b
- // c = a + d
- // f = a * 55
- // g = a / d
- // h = g + f
- // i = c + h
- // out = i / c
- //`
- //parser := NewParser(strings.NewReader(flat))
- //programm, err := parser.Parse()
- //circuit := programm.getMainCircuit()
- //assert.Nil(t, err)
- //fmt.Println("\n unreduced")
- //fmt.Println(flat)
-
- //fmt.Println("generating R1CS from flat code")
- //a, b, c := circuit.GenerateR1CS()
- //fmt.Println(a)
- //fmt.Println(b)
- //fmt.Println(c)
- //
- //a1 := big.NewInt(int64(6))
- //a2 := big.NewInt(int64(5))
- //inputs := []*big.Int{a1, a2}
- //// Calculate Witness
- //w, err := circuit.CalculateWitness(inputs)
- //assert.Nil(t, err)
- //fmt.Println("w", w)
- //fmt.Printf("inputs %s", circuit.Inputs)
- //fmt.Printf("signals %s", circuit.Signals)
-
- //
- //fmt.Println("Reduced Tree Parsing")
- //r := circuit.BuildConstraintTree()
- //constraintReduced := ReduceTree(r)
- //fmt.Printf("depth %v, mGates %v \n", printDepth(r, 0), CountMultiplicationGates(r))
- //printTree(r, 0)
- //
- //a,b,c,w = circuit.GenerateReducedR1CSandWitness(inputs,constraintReduced)
- //fmt.Println("\ngenerating R1CS from reduced flat code")
- //fmt.Println("\nR1CS:")
- //fmt.Println("a:", a)
- //fmt.Println("b:", b)
- //fmt.Println("c:", c)
- //fmt.Println("w", w)
-
- //// R1CS to QAP
- //alphas, betas, gammas, zxQAP := fields.R1CSToQAP(a, b, c)
- //fmt.Println("qap")
- //fmt.Println("alphas", len(alphas))
- //fmt.Println("alphas", alphas[0])
- //fmt.Println("betas", len(betas))
- //fmt.Println("gammas", len(gammas))
- //fmt.Println("zx length", len(zxQAP))
- //circuit.reduceAdditionGates()
- //
- //fmt.Println(circuit)
- //fmt.Println("generating R1CS from flat code")
- //a, b, c = circuit.GenerateR1CS()
- //
- //fmt.Println(a)
- //fmt.Println(b)
- //fmt.Println(c)
- //
- //// Calculate Witness
- //w, err = circuit.CalculateWitness(inputs)
- //assert.Nil(t, err)
- //fmt.Println("w", w)
-
- //// 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)
-
-}
diff --git a/circuitcompiler/test.py b/circuitcompiler/test.py
index c5d079f..5ce384c 100644
--- a/circuitcompiler/test.py
+++ b/circuitcompiler/test.py
@@ -38,21 +38,21 @@
# g = f + 2
# return g * a
-def doSomething(x ,k):
- z = k * x
- return 6 + mul(x,z)
-
-# def main(x,z):
-# out =
-
-def mul(a,b):
- return a * b
-
-def main():
- x=64341
- z=76548465
+##############################
+# def doSomething(x ,k):
+# z = k * x
+# return 6 + mul(x,z)
+#
+# def mul(a,b):
+# return a * b
+#
+# def main():
+# x=64341
+# z=76548465
+#
+# print(mul(x,z) - doSomething(x,x))
- print(mul(x,z) - doSomething(x,x))
+#######################
#
# def mul(a,b):
# return a * b
@@ -62,7 +62,24 @@ def main():
# c = 4 - b
# d = 5 * c
# return mul(d,c) / mul(b,b)
+############################
+
+def go(a,b,c,d):
+ e = a * b
+ f = c * d
+ g = e * f
+ h = g / e
+ i = h * 5
+ return g * i
+
+def main():
+ print(go(3,5,7,11))
if __name__ == '__main__':
#pascal(8)
- main()
\ No newline at end of file
+ main()
+
+
+ [[0 1 0 0 0 0 0 0 0 0] [0 0 0 1 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1 0 0]]
+ [[0 0 1 0 0 0 0 0 0 0] [0 0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 5 0]]
+ [[0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 0 0 1]]
\ No newline at end of file
diff --git a/cli/main.go b/cli/main.go
index 62aa1d7..0777b46 100644
--- a/cli/main.go
+++ b/cli/main.go
@@ -11,9 +11,9 @@ import (
"math/big"
"os"
- snark "github.com/mottla/go-snark"
- "github.com/mottla/go-snark/circuitcompiler"
- "github.com/mottla/go-snark/r1csqap"
+ snark "github.com/arnaucube/go-snark"
+ "github.com/arnaucube/go-snark/circuitcompiler"
+ "github.com/arnaucube/go-snark/r1csqap"
"github.com/urfave/cli"
)
diff --git a/r1csqap/r1csqap.go b/r1csqap/r1csqap.go
index 8836eec..16f4ec2 100644
--- a/r1csqap/r1csqap.go
+++ b/r1csqap/r1csqap.go
@@ -2,10 +2,9 @@ package r1csqap
import (
"bytes"
- "fmt"
"math/big"
- "github.com/mottla/go-snark/fields"
+ "github.com/arnaucube/go-snark/fields"
)
// Transpose transposes the *big.Int matrix
@@ -173,9 +172,6 @@ func (pf PolynomialField) R1CSToQAP(a, b, c [][]*big.Int) (alphas [][]*big.Int,
aT := Transpose(a)
bT := Transpose(b)
cT := Transpose(c)
- fmt.Println(aT)
- fmt.Println(bT)
- fmt.Println(cT)
for i := 0; i < len(aT); i++ {
alphas = append(alphas, pf.LagrangeInterpolation(aT[i]))
diff --git a/r1csqap/r1csqap_test.go b/r1csqap/r1csqap_test.go
index 184bcca..6be8588 100644
--- a/r1csqap/r1csqap_test.go
+++ b/r1csqap/r1csqap_test.go
@@ -5,7 +5,7 @@ import (
"math/big"
"testing"
- "github.com/mottla/go-snark/fields"
+ "github.com/arnaucube/go-snark/fields"
"github.com/stretchr/testify/assert"
)
@@ -112,65 +112,3 @@ func TestLagrangeInterpolation(t *testing.T) {
assert.Equal(t, aux, int64(0))
}
-
-func TestR1CSToQAP(t *testing.T) {
- // new Finite Field
- r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
- assert.True(nil, ok)
- f := fields.NewFq(r)
- // new Polynomial Field
- pf := NewPolynomialField(f)
-
- b0 := big.NewInt(int64(0))
- b1 := big.NewInt(int64(1))
- b3 := big.NewInt(int64(3))
- b5 := big.NewInt(int64(5))
- b9 := big.NewInt(int64(9))
- b27 := big.NewInt(int64(27))
- b30 := big.NewInt(int64(30))
- b35 := big.NewInt(int64(35))
- a := [][]*big.Int{
- []*big.Int{b0, b1, b0, b0, b0, b0},
- []*big.Int{b0, b0, b0, b1, b0, b0},
- []*big.Int{b0, b1, b0, b0, b1, b0},
- []*big.Int{b5, b0, b0, b0, b0, b1},
- }
- b := [][]*big.Int{
- []*big.Int{b0, b1, b0, b0, b0, b0},
- []*big.Int{b0, b1, b0, b0, b0, b0},
- []*big.Int{b1, b0, b0, b0, b0, b0},
- []*big.Int{b1, b0, b0, b0, b0, b0},
- }
- c := [][]*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, b0, b1, b0, b0, b0},
- }
- alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
- // fmt.Println(alphas)
- // fmt.Println(betas)
- // fmt.Println(gammas)
- // fmt.Print("Z(x): ")
- // fmt.Println(zx)
-
- w := []*big.Int{b1, b3, b35, b9, b27, b30}
- ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
- // fmt.Println(ax)
- // fmt.Println(bx)
- // fmt.Println(cx)
- // fmt.Println(px)
-
- hx := pf.DivisorPolynomial(px, zx)
- // fmt.Println(hx)
-
- // hx==px/zx so px==hx*zx
- assert.Equal(t, px, pf.Mul(hx, zx))
-
- // p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
- abc := pf.Sub(pf.Mul(ax, bx), cx)
- assert.Equal(t, abc, px)
- hz := pf.Mul(hx, zx)
- assert.Equal(t, abc, hz)
-
-}
diff --git a/snark.go b/snark.go
index dca9752..4220d92 100644
--- a/snark.go
+++ b/snark.go
@@ -2,12 +2,13 @@ package snark
import (
"fmt"
+ "github.com/arnaucube/go-snark/circuitcompiler"
"math/big"
"os"
- "github.com/mottla/go-snark/bn128"
- "github.com/mottla/go-snark/fields"
- "github.com/mottla/go-snark/r1csqap"
+ "github.com/arnaucube/go-snark/bn128"
+ "github.com/arnaucube/go-snark/fields"
+ "github.com/arnaucube/go-snark/r1csqap"
)
// Setup is the data structure holding the Trusted Setup data. The Setup.Toxic sub struct must be destroyed after the GenerateTrustedSetup function is completed
@@ -27,7 +28,8 @@ type Setup struct {
// public
G1T [][3]*big.Int // t encrypted in G1 curve, G1T == Pk.H
G2T [][3][2]*big.Int // t encrypted in G2 curve
- Pk struct { // Proving Key pk:=(pkA, pkB, pkC, pkH)
+ Pk struct {
+ // Proving Key pk:=(pkA, pkB, pkC, pkH)
A [][3]*big.Int
B [][3][2]*big.Int
C [][3]*big.Int
@@ -353,3 +355,22 @@ func VerifyProof(setup Setup, proof Proof, publicSignals []*big.Int, debug bool)
return true
}
+
+//TODO this is just a workaround to place the output after the input signals. Will be removed once the handling of private variables is already considered in the lexer
+func RelocateOutput(numberOfInputs int, r1cs circuitcompiler.R1CS, witness []*big.Int) (r circuitcompiler.R1CS, w []*big.Int) {
+ tmpA, tmpB, tmpC := [][]*big.Int{}, [][]*big.Int{}, [][]*big.Int{}
+
+ tmpA = append(tmpA, r1cs.A[len(r1cs.A)-1])
+ tmpA = append(tmpA, r1cs.A[:len(r1cs.A)-1]...)
+
+ tmpB = append(tmpB, r1cs.B[len(r1cs.B)-1])
+ tmpB = append(tmpB, r1cs.B[:len(r1cs.B)-1]...)
+
+ tmpC = append(tmpC, r1cs.C[len(r1cs.C)-1])
+ tmpC = append(tmpC, r1cs.C[:len(r1cs.C)-1]...)
+
+ wtmp := append(witness[:numberOfInputs], witness[len(witness)-1])
+ wtmp = append(wtmp, witness[numberOfInputs:len(witness)-2]...)
+
+ return circuitcompiler.R1CS{A: tmpA, B: tmpB, C: tmpC}, wtmp
+}
diff --git a/snark_test.go b/snark_test.go
index cffbaa4..0e952c2 100644
--- a/snark_test.go
+++ b/snark_test.go
@@ -2,58 +2,14 @@ package snark
import (
"fmt"
- "github.com/mottla/go-snark/circuitcompiler"
- "github.com/mottla/go-snark/r1csqap"
+ "github.com/arnaucube/go-snark/circuitcompiler"
+ "github.com/arnaucube/go-snark/r1csqap"
"github.com/stretchr/testify/assert"
"math/big"
"strings"
"testing"
)
-func TestGenerateProofs(t *testing.T) {
- z := []*big.Int{big.NewInt(int64(1))}
- for i := 1; i < 6; i++ {
- z = Utils.PF.Mul(
- z,
- []*big.Int{
- Utils.PF.F.Neg(
- big.NewInt(int64(i))),
- big.NewInt(int64(1)),
- })
- }
- fmt.Println(z)
- for i := 0; i < 7; i++ {
- fmt.Println(Utils.PF.Eval(z, big.NewInt(int64(i))))
- }
-
- z = []*big.Int{big.NewInt(int64(1))}
- for i := 1; i < 6; i++ {
- z = Utils.PF.Mul(
- z,
- []*big.Int{
- big.NewInt(int64(i)),
- big.NewInt(int64(1)),
- })
-
- }
- fmt.Println(z)
- z = []*big.Int{
- big.NewInt(int64(1)),
- big.NewInt(int64(
- -3)),
- }
- z = Utils.PF.Mul(
- z,
- []*big.Int{
- big.NewInt(int64(1)),
- big.NewInt(int64(3)),
- })
- fmt.Println(z)
- fmt.Println(Utils.PF.F.Neg(
- big.NewInt(int64(1))))
- fmt.Println(Utils.PF.F.Inverse(big.NewInt(int64(1))))
-}
-
func TestNewProgramm(t *testing.T) {
flat := `
@@ -86,27 +42,30 @@ func TestNewProgramm(t *testing.T) {
fmt.Println("generating R1CS")
r1cs := program.GenerateReducedR1CS(gates)
- a, b, c := r1cs.A, r1cs.B, r1cs.C
- fmt.Println(a)
- fmt.Println(b)
- fmt.Println(c)
+ //[[0 1 0 0 0 0 0 0 0 0] [0 0 0 1 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1 0 0]]
+ //[[0 0 1 0 0 0 0 0 0 0] [0 0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 5 0]]
+ //[[0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 1 0 0 0] [0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 0 0 1]]
a1 := big.NewInt(int64(6))
a2 := big.NewInt(int64(5))
inputs := []*big.Int{a1, a2, a1, a2}
w := circuitcompiler.CalculateWitness(inputs, r1cs)
- fmt.Println("witness")
- fmt.Println(w)
+
+ r1csReordered, wReordered := RelocateOutput(program.GlobalInputCount(), r1cs, w)
+
+ a, b, c := r1csReordered.A, r1csReordered.B, r1csReordered.C
+ fmt.Println(a)
+ fmt.Println(b)
+ fmt.Println(c)
// R1CS to QAP
alphas, betas, gammas, domain := Utils.PF.R1CSToQAP(a, b, c)
- fmt.Println("qap")
+ fmt.Println("QAP array lengths")
fmt.Println("alphas", len(alphas))
- fmt.Println("alphas", alphas)
fmt.Println("betas", len(betas))
fmt.Println("gammas", len(gammas))
fmt.Println("domain polynomial ", len(domain))
- ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
+ ax, bx, cx, px := Utils.PF.CombinePolynomials(wReordered, alphas, betas, gammas)
fmt.Println("ax length", len(ax))
fmt.Println("bx length", len(bx))
fmt.Println("cx length", len(cx))
@@ -147,7 +106,7 @@ func TestNewProgramm(t *testing.T) {
// fmt.Println("zxQAP", len(zxQAP))
// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
- proof, err := GenerateProofs(setup, 5, w, px)
+ proof, err := GenerateProofs(setup, program.GlobalInputCount(), wReordered, px)
assert.Nil(t, err)
// fmt.Println("\n proofs:")
@@ -155,11 +114,12 @@ func TestNewProgramm(t *testing.T) {
// fmt.Println("public signals:", proof.PublicSignals)
fmt.Println("\nwitness", w)
+ fmt.Println("\nwitness Reordered ", wReordered)
// b1 := big.NewInt(int64(1))
//b35 := big.NewInt(int64(35))
//// publicSignals := []*big.Int{b1, b35}
//publicSignals := []*big.Int{b35}
//before := time.Now()
- assert.True(t, VerifyProof(setup, proof, w[:5], true))
+ assert.True(t, VerifyProof(setup, proof, wReordered[:program.GlobalInputCount()], true))
//fmt.Println("verify proof time elapsed:", time.Since(before))
}