github.com/consensys/gnark@v0.11.0/constraint/field.go (about) 1 package constraint 2 3 import ( 4 "encoding/binary" 5 "math/big" 6 ) 7 8 // Element represents a term coefficient data. It is instantiated by the concrete 9 // constraint system implementation. 10 // Most of the scalar field used in gnark are on 4 uint64, so we have a clear memory overhead here. 11 type Element [6]uint64 12 13 // IsZero returns true if coefficient == 0 14 func (z *Element) IsZero() bool { 15 return (z[5] | z[4] | z[3] | z[2] | z[1] | z[0]) == 0 16 } 17 18 // Bytes return the Element as a big-endian byte slice 19 func (z *Element) Bytes() [48]byte { 20 var b [48]byte 21 binary.BigEndian.PutUint64(b[40:48], z[0]) 22 binary.BigEndian.PutUint64(b[32:40], z[1]) 23 binary.BigEndian.PutUint64(b[24:32], z[2]) 24 binary.BigEndian.PutUint64(b[16:24], z[3]) 25 binary.BigEndian.PutUint64(b[8:16], z[4]) 26 binary.BigEndian.PutUint64(b[0:8], z[5]) 27 return b 28 } 29 30 // SetBytes sets the Element from a big-endian byte slice 31 func (z *Element) SetBytes(b [48]byte) { 32 z[0] = binary.BigEndian.Uint64(b[40:48]) 33 z[1] = binary.BigEndian.Uint64(b[32:40]) 34 z[2] = binary.BigEndian.Uint64(b[24:32]) 35 z[3] = binary.BigEndian.Uint64(b[16:24]) 36 z[4] = binary.BigEndian.Uint64(b[8:16]) 37 z[5] = binary.BigEndian.Uint64(b[0:8]) 38 } 39 40 // Field capability to perform arithmetic on Coeff 41 type Field interface { 42 FromInterface(interface{}) Element 43 ToBigInt(Element) *big.Int 44 Mul(a, b Element) Element 45 Add(a, b Element) Element 46 Sub(a, b Element) Element 47 Neg(a Element) Element 48 Inverse(a Element) (Element, bool) 49 One() Element 50 IsOne(Element) bool 51 String(Element) string 52 Uint64(Element) (uint64, bool) 53 }