|
1 | 1 | package poseidon |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "testing" |
6 | 5 |
|
| 6 | + "github.com/PolyhedraZK/ExpanderCompilerCollection" |
7 | 7 | "github.com/PolyhedraZK/ExpanderCompilerCollection/field/m31" |
8 | | - "github.com/consensys/gnark-crypto/ecc" |
| 8 | + "github.com/PolyhedraZK/ExpanderCompilerCollection/test" |
9 | 9 | "github.com/consensys/gnark/constraint" |
10 | 10 | "github.com/consensys/gnark/frontend" |
11 | | - "github.com/consensys/gnark/frontend/cs/r1cs" |
12 | | - "github.com/consensys/gnark/test" |
13 | 11 | ) |
14 | 12 |
|
15 | 13 | type MockPoseidonCircuit struct { |
16 | | - State []frontend.Variable `gnark:",public"` |
17 | | - Output frontend.Variable `gnark:",public"` |
| 14 | + State [16]frontend.Variable `gnark:",public"` |
| 15 | + Output frontend.Variable `gnark:",public"` |
18 | 16 | } |
19 | 17 |
|
20 | 18 | func (c *MockPoseidonCircuit) Define(api frontend.API) (err error) { |
21 | | - // Define the circuit |
22 | 19 | param := NewPoseidonParams() |
23 | 20 | engine := m31.Field{} |
24 | | - t := PoseidonCircuit(api, engine, param, c.State, false) |
| 21 | + t := PoseidonCircuit(api, engine, param, c.State[:], false) |
25 | 22 | api.AssertIsEqual(t, c.Output) |
26 | 23 |
|
27 | 24 | return |
28 | 25 | } |
29 | 26 |
|
30 | 27 | func TestPoseidonCircuit(t *testing.T) { |
31 | | - assert := test.NewAssert(t) |
32 | | - |
33 | 28 | param := NewPoseidonParams() |
34 | 29 |
|
35 | | - state := make([]constraint.Element, 16) |
36 | | - stateVar := make([]frontend.Variable, 16) |
37 | | - var internalStateVars PoseidonInternalStateVar |
| 30 | + var states [16]constraint.Element |
| 31 | + var stateVars [16]frontend.Variable |
| 32 | + var outputVar frontend.Variable |
38 | 33 |
|
39 | | - for i := 0; i < 16; i++ { |
40 | | - state[i] = constraint.Element{uint64(i)} |
41 | | - stateVar[i] = frontend.Variable(uint64(i)) |
| 34 | + for j := 0; j < 16; j++ { |
| 35 | + states[j] = constraint.Element{uint64(j)} |
| 36 | + stateVars[j] = frontend.Variable(uint64(j)) |
42 | 37 | } |
43 | | - internalState, output := PoseidonM31WithInternalStates(param, state, true) |
44 | | - outputVar := frontend.Variable(output[0]) |
| 38 | + output := PoseidonM31(param, states[:]) |
| 39 | + outputVar = frontend.Variable(output[0]) |
45 | 40 |
|
46 | | - fmt.Println("internal state", internalState) |
47 | | - |
48 | | - for j := 0; j < 16; j++ { |
49 | | - internalStateVars.AfterHalfFullRound[j] = frontend.Variable(internalState.AfterHalfFullRound[j][0]) |
50 | | - internalStateVars.AfterHalfPartialRound[j] = frontend.Variable(internalState.AfterHalfPartialRound[j][0]) |
51 | | - internalStateVars.AfterPartialRound[j] = frontend.Variable(internalState.AfterPartialRound[j][0]) |
| 41 | + assignment := &MockPoseidonCircuit{ |
| 42 | + State: stateVars, |
| 43 | + Output: outputVar, |
52 | 44 | } |
53 | 45 |
|
54 | | - c := MockPoseidonCircuit{ |
55 | | - stateVar, |
56 | | - outputVar, |
| 46 | + // Gnark test disabled as it does not support randomness and custom gates |
| 47 | + // err := test.IsSolved(&MockPoseidonCircuit{}, assignment, m31.ScalarField) |
| 48 | + // if err != nil { |
| 49 | + // panic(err) |
| 50 | + // } |
| 51 | + // fmt.Println("Gnark test passed") |
| 52 | + |
| 53 | + // Ecc test |
| 54 | + circuit, err := ExpanderCompilerCollection.Compile(m31.ScalarField, &MockPoseidonCircuit{}, frontend.WithCompressThreshold(32)) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
57 | 57 | } |
58 | 58 |
|
59 | | - w, _ := frontend.NewWitness(&c, m31.ScalarField) |
60 | | - fmt.Println("witness", w) |
| 59 | + layered_circuit := circuit.GetLayeredCircuit() |
| 60 | + // circuit.GetCircuitIr().Print() |
61 | 61 |
|
62 | | - err := test.IsSolved(&c, &c, m31.ScalarField) |
63 | | - assert.NoError(err) |
| 62 | + inputSolver := circuit.GetInputSolver() |
| 63 | + witness, err := inputSolver.SolveInputAuto(assignment) |
| 64 | + if err != nil { |
| 65 | + panic(err) |
| 66 | + } |
64 | 67 |
|
65 | | - r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &c) |
66 | | - assert.NoError(err) |
67 | | - fmt.Println("num constraints:", r1cs.GetNbConstraints()) |
68 | | - fmt.Println("num coefficients:", r1cs.GetNbCoefficients()) |
69 | | - i, p, s := r1cs.GetNbVariables() |
70 | | - fmt.Println("num variables:", i, p, s) |
| 68 | + if !test.CheckCircuit(layered_circuit, witness) { |
| 69 | + panic("verification failed") |
| 70 | + } |
71 | 71 | } |
0 commit comments