github.com/consensys/gnark@v0.11.0/constraint/term.go (about) 1 // Copyright 2020 ConsenSys AG 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package constraint 16 17 import ( 18 "math" 19 ) 20 21 // ids of the coefficients with simple values in any cs.coeffs slice. 22 const ( 23 CoeffIdZero = iota 24 CoeffIdOne 25 CoeffIdTwo 26 CoeffIdMinusOne 27 CoeffIdMinusTwo 28 ) 29 30 // Term represents a coeff * variable in a constraint system 31 type Term struct { 32 CID, VID uint32 33 } 34 35 func (t *Term) MarkConstant() { 36 t.VID = math.MaxUint32 37 } 38 39 func (t *Term) IsConstant() bool { 40 return t.VID == math.MaxUint32 41 } 42 43 func (t *Term) WireID() int { 44 return int(t.VID) 45 } 46 47 func (t *Term) CoeffID() int { 48 return int(t.CID) 49 } 50 51 func (t Term) String(r Resolver) string { 52 sbb := NewStringBuilder(r) 53 sbb.WriteTerm(t) 54 return sbb.String() 55 } 56 57 // implements constraint.Compressible 58 59 // Compress compresses the term into a slice of uint32 words. 60 // For compatibility with test engine and LinearExpression, the term is encoded as: 61 // 1, CID, VID (i.e a LinearExpression with a single term) 62 func (t Term) Compress(to *[]uint32) { 63 (*to) = append((*to), 1, t.CID, t.VID) 64 }