MatrixMultiplication #829
Unanswered
sid-alluri
asked this question in
Q&A
Replies: 3 comments 2 replies
-
Hi, you are doing package matmul
import (
"fmt"
"testing"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test"
)
type MatMulCircuit struct {
A [][]frontend.Variable
B [][]frontend.Variable
Y [][]frontend.Variable
}
func MatMul(api frontend.API, A [][]frontend.Variable, B [][]frontend.Variable) [][]frontend.Variable {
output := make([][]frontend.Variable, len(A))
for i := 0; i < len(A); i++ {
output[i] = make([]frontend.Variable, len(B[0]))
for j := 0; j < len(B[0]); j++ {
output[i][j] = frontend.Variable(0)
for k := 0; k < len(A[0]); k++ {
buf := api.Mul(A[i][k], B[k][j])
output[i][j] = api.Add(buf, output[i][j])
}
}
}
return output
}
func (circuit *MatMulCircuit) Define(api frontend.API) error {
output := MatMul(api, circuit.A, circuit.B)
if len(output) != len(circuit.Y) {
return fmt.Errorf("length mismatch")
}
for i := range output {
if len(output[i]) != len(circuit.Y[i]) {
return fmt.Errorf("length mismatch")
}
for j := range output[i] {
api.AssertIsEqual(output[i][j], circuit.Y[i][j])
}
}
return nil
}
func TestMatmul(t *testing.T) {
assert := test.NewAssert(t)
AObj := make([][]frontend.Variable, 3)
BObj := make([][]frontend.Variable, 3)
YObj := make([][]frontend.Variable, 3)
for i := 0; i < 3; i++ {
AObj[i] = make([]frontend.Variable, 3)
}
for i := 0; i < 3; i++ {
BObj[i] = make([]frontend.Variable, 2)
}
for i := 0; i < 3; i++ {
YObj[i] = make([]frontend.Variable, 2)
}
matmul_circuit := MatMulCircuit{
A: AObj,
B: BObj,
Y: YObj,
}
// Correctness
assert.ProverSucceeded(&matmul_circuit, &MatMulCircuit{
A: [][]frontend.Variable{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
B: [][]frontend.Variable{{0, 0}, {0, 0}, {0, 0}},
Y: [][]frontend.Variable{{0, 0}, {0, 0}, {0, 0}},
}, test.WithBackends(backend.PLONK), test.WithCurves(ecc.BN254))
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Additional note - you could try using Freivalds algorithm using |
Beta Was this translation helpful? Give feedback.
2 replies
-
Thank you so much for the help. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I am trying to write a circuit for matrix multiplication.
While testing, I am receiving the following error.
Received unexpected error: parse circuit: [][]frontend.Variable to big.Int not supported frontend.parseCircuit.func2
My test looks like this:
Can anyone kindly help me with where its going wrong?
Beta Was this translation helpful? Give feedback.
All reactions