Using other libraries in circuits #671
Replies: 5 comments 3 replies
-
Unfortunately not. In-circuit all operations have to be performed using the given circuit API. So instead of func fn(a, b int) int {
res := b+c
res = res + 3
return res
} you have to define a circuit type Circuit struct {
A, B frontend.Variable `gnark:",public"`
Res frontend.Variable
}
func (c *Circuit) Define(api frontend.API) error {
res := api.Add(c.A, c.B)
res = api.Add(res, 3)
api.AssertIsEqual(res, c.Res)
return nil
} You also have to keep in mind that if out-circuit int is usually 64 bits then in-circuit it is a large prime field element. So the operation results may be unexpected. To overcome this, we have implemented field emulation, but this comes at a performance cost. Sometimes you can quite easily adapt out-circuit implementations into the circuit (for example, for Keccak hash function it was quite straightforward), but sometimes not. For example, for matrix multiplication it makes more sense to implement Freivalds algorithm in-circuit for efficiency. |
Beta Was this translation helpful? Give feedback.
-
FYI - converted issue to discussion as is more suitable. |
Beta Was this translation helpful? Give feedback.
-
Thanks! So I cannot use any libraries. All the functions have to be implemented using the circuit API.
|
Beta Was this translation helpful? Give feedback.
-
Thanks! Can I call this permute function or some other function inside a circuit? |
Beta Was this translation helpful? Give feedback.
-
Also I have the code in Golang. Is it possible to directly get a circuit from this code? Are there any libraries which can help me? Or Do I have to write the circuit using frontend myself? |
Beta Was this translation helpful? Give feedback.
-
Hi there,
I have some Golang code that I wish to create a GNARK circuit for. This code uses a priority queue and some custom data structs created by me. For priority queue, I am using a heap library that already exists in golang. I am also using the mat library for matrix and vector operations. Can I use these data structs and libraries in gnark as is?
Beta Was this translation helpful? Give feedback.
All reactions