mirror of
https://github.com/arnaucube/go-snark-study.git
synced 2026-02-02 17:26:41 +01:00
minimal clean & update tests
This commit is contained in:
@@ -2,7 +2,6 @@ package circuitcompiler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
@@ -96,10 +95,9 @@ func (circ *Circuit) GenerateR1CS() ([][]*big.Int, [][]*big.Int, [][]*big.Int) {
|
||||
cConstraint := r1csqap.ArrayOfBigZeros(len(circ.Signals))
|
||||
|
||||
// if existInArray(constraint.Out) {
|
||||
if used[constraint.Out] {
|
||||
// panic(errors.New("out variable already used: " + constraint.Out))
|
||||
fmt.Println("variable already used")
|
||||
}
|
||||
// if used[constraint.Out] {
|
||||
// panic(errors.New("out variable already used: " + constraint.Out))
|
||||
// }
|
||||
used[constraint.Out] = true
|
||||
if constraint.Op == "in" {
|
||||
for i := 0; i <= len(circ.PublicInputs); i++ {
|
||||
@@ -152,7 +150,7 @@ func grabVar(signals []string, w []*big.Int, vStr string) *big.Int {
|
||||
|
||||
type Inputs struct {
|
||||
Private []*big.Int
|
||||
Publics []*big.Int
|
||||
Public []*big.Int
|
||||
}
|
||||
|
||||
// CalculateWitness calculates the Witness of a Circuit based on the given inputs
|
||||
@@ -167,12 +165,9 @@ 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 {
|
||||
fmt.Println(i + len(publicInputs) + 1)
|
||||
w[i+len(publicInputs)+1] = input
|
||||
}
|
||||
for _, constraint := range circ.Constraints {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package circuitcompiler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -11,74 +9,62 @@ import (
|
||||
)
|
||||
|
||||
func TestCircuitParser(t *testing.T) {
|
||||
/*
|
||||
input:
|
||||
def test():
|
||||
y = x**3
|
||||
return x + y + 5
|
||||
|
||||
flattened:
|
||||
m1 = s1 * s1
|
||||
m2 = m1 * s1
|
||||
m3 = m2 + s1
|
||||
out = m3 + 5
|
||||
|
||||
*/
|
||||
|
||||
// flat code, where er is expected_result
|
||||
// equals(s5, s1)
|
||||
// s1 = s5 * 1
|
||||
// y = x^3 + x + 5
|
||||
flat := `
|
||||
func test(private s0, public s1):
|
||||
s2 = s0*s0
|
||||
s3 = s2*s0
|
||||
s4 = s0 + s3
|
||||
s2 = s0 * s0
|
||||
s3 = s2 * s0
|
||||
s4 = s3 + s0
|
||||
s5 = s4 + 5
|
||||
s5 = s1 * one
|
||||
equals(s1, s5)
|
||||
out = 1 * 1
|
||||
`
|
||||
parser := NewParser(strings.NewReader(flat))
|
||||
circuit, err := parser.Parse()
|
||||
assert.Nil(t, err)
|
||||
fmt.Println("circuit parsed: ", circuit)
|
||||
|
||||
// flat code to R1CS
|
||||
fmt.Println("generating R1CS from flat code")
|
||||
a, b, c := circuit.GenerateR1CS()
|
||||
fmt.Println("private inputs: ", circuit.PrivateInputs)
|
||||
fmt.Println("public inputs: ", circuit.PublicInputs)
|
||||
assert.Equal(t, "s0", circuit.PrivateInputs[0])
|
||||
assert.Equal(t, "s1", circuit.PublicInputs[0])
|
||||
|
||||
fmt.Println("signals:", circuit.Signals)
|
||||
assert.Equal(t, []string{"one", "s1", "s0", "s2", "s3", "s4", "s5", "out"}, 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)
|
||||
fmt.Println(a)
|
||||
fmt.Println(b)
|
||||
fmt.Println(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, b0, b0},
|
||||
[]*big.Int{b0, b0, b0, b1, b0, b0, b0, b0},
|
||||
[]*big.Int{b0, b0, b1, b0, b1, b0, b0, b0},
|
||||
[]*big.Int{b5, b0, b0, b0, b0, b1, b0, b0},
|
||||
[]*big.Int{b0, b0, b0, b0, b0, b0, b1, b0},
|
||||
[]*big.Int{b0, b1, b0, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
|
||||
}
|
||||
bExpected := [][]*big.Int{
|
||||
[]*big.Int{b0, b0, b1, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b0, b0, b1, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b1, b0, b0, b0, b0, b0, b0, b0},
|
||||
}
|
||||
cExpected := [][]*big.Int{
|
||||
[]*big.Int{b0, b0, b0, b1, b0, b0, b0, b0},
|
||||
[]*big.Int{b0, b0, b0, b0, b1, b0, b0, b0},
|
||||
[]*big.Int{b0, b0, b0, b0, b0, b1, b0, b0},
|
||||
[]*big.Int{b0, b0, b0, b0, b0, b0, b1, b0},
|
||||
[]*big.Int{b0, b1, b0, b0, b0, b0, b0, b0},
|
||||
[]*big.Int{b0, b0, b0, b0, b0, b0, b1, b0},
|
||||
[]*big.Int{b0, b0, b0, b0, b0, b0, b0, b1},
|
||||
}
|
||||
|
||||
assert.Equal(t, aExpected, a)
|
||||
assert.Equal(t, bExpected, b)
|
||||
assert.Equal(t, cExpected, c)
|
||||
|
||||
b3 := big.NewInt(int64(3))
|
||||
privateInputs := []*big.Int{b3}
|
||||
@@ -87,10 +73,14 @@ func TestCircuitParser(t *testing.T) {
|
||||
// Calculate Witness
|
||||
w, err := circuit.CalculateWitness(privateInputs, publicInputs)
|
||||
assert.Nil(t, err)
|
||||
fmt.Println("w", w)
|
||||
b9 := big.NewInt(int64(9))
|
||||
b27 := big.NewInt(int64(27))
|
||||
b30 := big.NewInt(int64(30))
|
||||
wExpected := []*big.Int{b1, b35, b3, b9, b27, b30, b35, b1}
|
||||
assert.Equal(t, wExpected, w)
|
||||
|
||||
circuitJson, _ := json.Marshal(circuit)
|
||||
fmt.Println("circuit:", string(circuitJson))
|
||||
// circuitJson, _ := json.Marshal(circuit)
|
||||
// fmt.Println("circuit:", string(circuitJson))
|
||||
|
||||
assert.Equal(t, circuit.NPublic, 1)
|
||||
assert.Equal(t, len(circuit.PublicInputs), 1)
|
||||
|
||||
@@ -101,8 +101,6 @@ func (p *Parser) parseLine() (*Constraint, error) {
|
||||
insideParenthesis := rgx.FindStringSubmatch(line)
|
||||
varsString := strings.Replace(insideParenthesis[1], " ", "", -1)
|
||||
params := strings.Split(varsString, ",")
|
||||
fmt.Println("params", params)
|
||||
// TODO
|
||||
c.V1 = params[0]
|
||||
c.V2 = params[1]
|
||||
return c, nil
|
||||
@@ -162,7 +160,6 @@ 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.PublicInputs {
|
||||
@@ -189,8 +186,6 @@ func (p *Parser) Parse() (*Circuit, error) {
|
||||
continue
|
||||
}
|
||||
if constraint.Literal == "equals" {
|
||||
// TODO
|
||||
fmt.Println("circuit.Signals", circuit.Signals)
|
||||
constr1 := &Constraint{
|
||||
Op: "*",
|
||||
V1: constraint.V2,
|
||||
|
||||
Reference in New Issue
Block a user