forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_expression.go
63 lines (54 loc) · 1.5 KB
/
linear_expression.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
package expr
import (
"github.com/aakash4dev/gnark2/constraint"
)
type LinearExpression []Term
// NewLinearExpression helper to initialize a linear expression with one term
func NewLinearExpression(vID int, cID constraint.Element) LinearExpression {
return LinearExpression{Term{Coeff: cID, VID: vID}}
}
func (l LinearExpression) Clone() LinearExpression {
res := make(LinearExpression, len(l))
copy(res, l)
return res
}
// Len return the length of the Variable (implements Sort interface)
func (l LinearExpression) Len() int {
return len(l)
}
// Equals returns true if both SORTED expressions are the same
//
// pre conditions: l and o are sorted
func (l LinearExpression) Equal(o LinearExpression) bool {
if len(l) != len(o) {
return false
}
if (l == nil) != (o == nil) {
return false
}
for i := 0; i < len(l); i++ {
if l[i] != o[i] {
return false
}
}
return true
}
// Swap swaps terms in the Variable (implements Sort interface)
func (l LinearExpression) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// Less returns true if variableID for term at i is less than variableID for term at j (implements Sort interface)
func (l LinearExpression) Less(i, j int) bool {
iID := l[i].WireID()
jID := l[j].WireID()
return iID < jID
}
// HashCode returns a fast-to-compute but NOT collision resistant hash code identifier for the linear
// expression
func (l LinearExpression) HashCode() uint64 {
h := uint64(17)
for _, val := range l {
h = h*23 + val.HashCode() // TODO @gbotrel revisit
}
return h
}