forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
div.go
47 lines (37 loc) · 893 Bytes
/
div.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package circuits
import (
"math/big"
"github.com/consensys/gnark-crypto/ecc"
"github.com/airchains-network/gnark/frontend"
)
type divCircuit struct {
A, B frontend.Variable
C frontend.Variable `gnark:",public"`
}
func (circuit *divCircuit) Define(api frontend.API) error {
c := api.DivUnchecked(circuit.A, circuit.B)
d := api.DivUnchecked(2387287246, circuit.B)
e := api.DivUnchecked(circuit.A, 987342642)
api.AssertIsEqual(c, circuit.C)
api.AssertIsEqual(d, circuit.C)
api.AssertIsEqual(e, circuit.C)
return nil
}
func init() {
var good, bad divCircuit
a := big.NewInt(2387287246)
b := big.NewInt(987342642)
m := ecc.BN254.ScalarField()
var c big.Int
c.ModInverse(b, m).Mul(&c, a)
c.Mod(&c, m)
// good.A = a
good.A = a
good.B = b
good.C = c
// bad.A = a
bad.A = a
bad.B = b
bad.C = 1
addEntry("div", &divCircuit{}, &good, &bad, []ecc.ID{ecc.BN254})
}