github.com/consensys/gnark@v0.11.0/internal/backend/circuits/xor.go (about) 1 package circuits 2 3 import ( 4 "github.com/consensys/gnark/frontend" 5 ) 6 7 // one input is constant 8 type xorCircuitVarCst struct { 9 Op frontend.Variable 10 XorOne frontend.Variable `gnark:",public"` 11 XorZero frontend.Variable `gnark:",public"` 12 } 13 14 func (circuit *xorCircuitVarCst) Define(api frontend.API) error { 15 a := api.Xor(circuit.Op, 1) 16 b := api.Xor(circuit.Op, 0) 17 c := api.Xor(1, circuit.Op) 18 d := api.Xor(0, circuit.Op) 19 api.AssertIsEqual(a, circuit.XorOne) 20 api.AssertIsEqual(b, circuit.XorZero) 21 api.AssertIsEqual(c, circuit.XorOne) 22 api.AssertIsEqual(d, circuit.XorZero) 23 return nil 24 } 25 26 func init() { 27 28 good := []frontend.Circuit{ 29 &xorCircuitVarCst{ 30 Op: 1, 31 XorOne: 0, 32 XorZero: 1, 33 }, 34 &xorCircuitVarCst{ 35 Op: (0), 36 XorOne: (1), 37 XorZero: (0), 38 }, 39 } 40 41 bad := []frontend.Circuit{ 42 &xorCircuitVarCst{ 43 Op: 0, 44 XorOne: 0, 45 XorZero: 1, 46 }, 47 &xorCircuitVarCst{ 48 Op: (1), 49 XorOne: (1), 50 XorZero: (0), 51 }, 52 } 53 54 addNewEntry("xorCstVar", &xorCircuitVarCst{}, good, bad, nil) 55 56 } 57 58 // both inputs are variable 59 type xorCircuit struct { 60 Op1, Op2, Res frontend.Variable 61 } 62 63 func (circuit *xorCircuit) Define(api frontend.API) error { 64 d := api.Xor(circuit.Op1, circuit.Op2) 65 66 api.AssertIsEqual(d, circuit.Res) 67 return nil 68 } 69 70 func init() { 71 72 good := []frontend.Circuit{ 73 &xorCircuit{ 74 Op1: 1, 75 Op2: 1, 76 Res: 0, 77 }, 78 &xorCircuit{ 79 Op1: 1, 80 Op2: 0, 81 Res: 1, 82 }, 83 &xorCircuit{ 84 Op1: 0, 85 Op2: 1, 86 Res: 1, 87 }, 88 &xorCircuit{ 89 Op1: 0, 90 Op2: 0, 91 Res: 0, 92 }, 93 } 94 95 bad := []frontend.Circuit{ 96 &xorCircuit{ 97 Op1: 1, 98 Op2: 1, 99 Res: 1, 100 }, 101 &xorCircuit{ 102 Op1: 1, 103 Op2: 0, 104 Res: 0, 105 }, 106 &xorCircuit{ 107 Op1: 0, 108 Op2: 1, 109 Res: 0, 110 }, 111 &xorCircuit{ 112 Op1: 0, 113 Op2: 0, 114 Res: 1, 115 }, 116 &xorCircuit{ 117 Op1: (42), 118 Op2: 1, 119 Res: 1, 120 }, 121 &xorCircuit{ 122 Op1: 1, 123 Op2: 1, 124 Res: (42), 125 }, 126 } 127 128 addNewEntry("xor", &xorCircuit{}, good, bad, nil) 129 }