forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_assertions.go
185 lines (142 loc) · 5.01 KB
/
api_assertions.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright © 2021 ConsenSys Software Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package r1cs
import (
"fmt"
"math/big"
"github.com/nume-crypto/gnark/frontend"
"github.com/nume-crypto/gnark/frontend/compiled"
"github.com/nume-crypto/gnark/internal/utils"
"github.com/nume-crypto/gnark/std/math/bits"
)
// AssertIsEqual adds an assertion in the constraint system (i1 == i2)
func (system *r1cs) AssertIsEqual(i1, i2 frontend.Variable) {
// encoded 1 * i1 == i2
r := system.toVariable(i1).(compiled.LinearExpression)
o := system.toVariable(i2).(compiled.LinearExpression)
debug := system.AddDebugInfo("assertIsEqual", r, " == ", o)
system.addConstraint(newR1C(system.one(), r, o), debug)
}
// AssertIsDifferent constrain i1 and i2 to be different
func (system *r1cs) AssertIsDifferent(i1, i2 frontend.Variable) {
system.Inverse(system.Sub(i1, i2))
}
// AssertIsBoolean adds an assertion in the constraint system (v == 0 ∥ v == 1)
func (system *r1cs) AssertIsBoolean(i1 frontend.Variable) {
vars, _ := system.toVariables(i1)
v := vars[0]
if c, ok := system.ConstantValue(v); ok {
if !(c.IsUint64() && (c.Uint64() == 0 || c.Uint64() == 1)) {
panic(fmt.Sprintf("assertIsBoolean failed: constant(%s)", c.String()))
}
return
}
if system.IsBoolean(v) {
return // compiled.LinearExpression is already constrained
}
system.MarkBoolean(v)
debug := system.AddDebugInfo("assertIsBoolean", v, " == (0|1)")
o := system.toVariable(0)
// ensure v * (1 - v) == 0
_v := system.Sub(1, v)
system.addConstraint(newR1C(v, _v, o), debug)
}
// AssertIsLessOrEqual adds assertion in constraint system (v ⩽ bound)
//
// bound can be a constant or a Variable
//
// derived from:
// https://github.com/zcash/zips/blob/main/protocol/protocol.pdf
func (system *r1cs) AssertIsLessOrEqual(_v frontend.Variable, bound frontend.Variable) {
v, _ := system.toVariables(_v)
switch b := bound.(type) {
case compiled.LinearExpression:
assertIsSet(b)
system.mustBeLessOrEqVar(v[0], b)
default:
system.mustBeLessOrEqCst(v[0], utils.FromInterface(b))
}
}
func (system *r1cs) mustBeLessOrEqVar(a, bound compiled.LinearExpression) {
debug := system.AddDebugInfo("mustBeLessOrEq", a, " <= ", bound)
nbBits := system.BitLen()
aBits := bits.ToBinary(system, a, bits.WithNbDigits(nbBits), bits.WithUnconstrainedOutputs())
boundBits := system.ToBinary(bound, nbBits)
p := make([]frontend.Variable, nbBits+1)
p[nbBits] = system.toVariable(1)
zero := system.toVariable(0)
for i := nbBits - 1; i >= 0; i-- {
// if bound[i] == 0
// p[i] = p[i+1]
// t = p[i+1]
// else
// p[i] = p[i+1] * a[i]
// t = 0
v := system.Mul(p[i+1], aBits[i])
p[i] = system.Select(boundBits[i], v, p[i+1])
t := system.Select(boundBits[i], zero, p[i+1])
// (1 - t - ai) * ai == 0
var l frontend.Variable
l = system.one()
l = system.Sub(l, t, aBits[i])
// note if bound[i] == 1, this constraint is (1 - ai) * ai == 0
// → this is a boolean constraint
// if bound[i] == 0, t must be 0 or 1, thus ai must be 0 or 1 too
system.MarkBoolean(aBits[i].(compiled.LinearExpression)) // this does not create a constraint
system.addConstraint(newR1C(l, aBits[i], zero), debug)
}
}
func (system *r1cs) mustBeLessOrEqCst(a compiled.LinearExpression, bound big.Int) {
nbBits := system.BitLen()
// ensure the bound is positive, it's bit-len doesn't matter
if bound.Sign() == -1 {
panic("AssertIsLessOrEqual: bound must be positive")
}
if bound.BitLen() > nbBits {
panic("AssertIsLessOrEqual: bound is too large, constraint will never be satisfied")
}
// debug info
debug := system.AddDebugInfo("mustBeLessOrEq", a, " <= ", system.toVariable(bound))
// note that at this stage, we didn't boolean-constraint these new variables yet
// (as opposed to ToBinary)
aBits := bits.ToBinary(system, a, bits.WithNbDigits(nbBits), bits.WithUnconstrainedOutputs())
// t trailing bits in the bound
t := 0
for i := 0; i < nbBits; i++ {
if bound.Bit(i) == 0 {
break
}
t++
}
p := make([]frontend.Variable, nbBits+1)
// p[i] == 1 → a[j] == c[j] for all j ⩾ i
p[nbBits] = system.toVariable(1)
for i := nbBits - 1; i >= t; i-- {
if bound.Bit(i) == 0 {
p[i] = p[i+1]
} else {
p[i] = system.Mul(p[i+1], aBits[i])
}
}
for i := nbBits - 1; i >= 0; i-- {
if bound.Bit(i) == 0 {
// (1 - p(i+1) - ai) * ai == 0
l := system.Sub(1, p[i+1])
l = system.Sub(l, aBits[i])
system.addConstraint(newR1C(l, aBits[i], system.toVariable(0)), debug)
system.MarkBoolean(aBits[i].(compiled.LinearExpression))
} else {
system.AssertIsBoolean(aBits[i])
}
}
}