github.com/consensys/gnark-crypto@v0.14.0/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go (about) 1 // Copyright 2020 Consensys Software Inc. 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 // Code generated by consensys/gnark-crypto DO NOT EDIT 16 17 package test_vector_utils 18 19 import ( 20 "fmt" 21 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" 22 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/polynomial" 23 "hash" 24 "reflect" 25 "strings" 26 ) 27 28 func ToElement(i int64) *fr.Element { 29 var res fr.Element 30 res.SetInt64(i) 31 return &res 32 } 33 34 type HashDescription map[string]interface{} 35 36 func HashFromDescription(d HashDescription) (hash.Hash, error) { 37 if _type, ok := d["type"]; ok { 38 switch _type { 39 case "const": 40 startState := int64(d["val"].(float64)) 41 return &MessageCounter{startState: startState, step: 0, state: startState}, nil 42 default: 43 return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) 44 } 45 } 46 return nil, fmt.Errorf("hash description missing type") 47 } 48 49 type MessageCounter struct { 50 startState int64 51 state int64 52 step int64 53 } 54 55 func (m *MessageCounter) Write(p []byte) (n int, err error) { 56 inputBlockSize := (len(p)-1)/fr.Bytes + 1 57 m.state += int64(inputBlockSize) * m.step 58 return len(p), nil 59 } 60 61 func (m *MessageCounter) Sum(b []byte) []byte { 62 inputBlockSize := (len(b)-1)/fr.Bytes + 1 63 resI := m.state + int64(inputBlockSize)*m.step 64 var res fr.Element 65 res.SetInt64(int64(resI)) 66 resBytes := res.Bytes() 67 return resBytes[:] 68 } 69 70 func (m *MessageCounter) Reset() { 71 m.state = m.startState 72 } 73 74 func (m *MessageCounter) Size() int { 75 return fr.Bytes 76 } 77 78 func (m *MessageCounter) BlockSize() int { 79 return fr.Bytes 80 } 81 82 func NewMessageCounter(startState, step int) hash.Hash { 83 transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} 84 return transcript 85 } 86 87 func NewMessageCounterGenerator(startState, step int) func() hash.Hash { 88 return func() hash.Hash { 89 return NewMessageCounter(startState, step) 90 } 91 } 92 93 type ListHash []fr.Element 94 95 func (h *ListHash) Write(p []byte) (n int, err error) { 96 return len(p), nil 97 } 98 99 func (h *ListHash) Sum(b []byte) []byte { 100 res := (*h)[0].Bytes() 101 *h = (*h)[1:] 102 return res[:] 103 } 104 105 func (h *ListHash) Reset() { 106 } 107 108 func (h *ListHash) Size() int { 109 return fr.Bytes 110 } 111 112 func (h *ListHash) BlockSize() int { 113 return fr.Bytes 114 } 115 func SetElement(z *fr.Element, value interface{}) (*fr.Element, error) { 116 117 // TODO: Put this in element.SetString? 118 switch v := value.(type) { 119 case string: 120 121 if sep := strings.Split(v, "/"); len(sep) == 2 { 122 var denom fr.Element 123 if _, err := z.SetString(sep[0]); err != nil { 124 return nil, err 125 } 126 if _, err := denom.SetString(sep[1]); err != nil { 127 return nil, err 128 } 129 denom.Inverse(&denom) 130 z.Mul(z, &denom) 131 return z, nil 132 } 133 134 case float64: 135 asInt := int64(v) 136 if float64(asInt) != v { 137 return nil, fmt.Errorf("cannot currently parse float") 138 } 139 z.SetInt64(asInt) 140 return z, nil 141 } 142 143 return z.SetInterface(value) 144 } 145 146 func SliceToElementSlice[T any](slice []T) ([]fr.Element, error) { 147 elementSlice := make([]fr.Element, len(slice)) 148 for i, v := range slice { 149 if _, err := SetElement(&elementSlice[i], v); err != nil { 150 return nil, err 151 } 152 } 153 return elementSlice, nil 154 } 155 156 func SliceEquals(a []fr.Element, b []fr.Element) error { 157 if len(a) != len(b) { 158 return fmt.Errorf("length mismatch %d≠%d", len(a), len(b)) 159 } 160 for i := range a { 161 if !a[i].Equal(&b[i]) { 162 return fmt.Errorf("at index %d: %s ≠ %s", i, a[i].String(), b[i].String()) 163 } 164 } 165 return nil 166 } 167 168 func SliceSliceEquals(a [][]fr.Element, b [][]fr.Element) error { 169 if len(a) != len(b) { 170 return fmt.Errorf("length mismatch %d≠%d", len(a), len(b)) 171 } 172 for i := range a { 173 if err := SliceEquals(a[i], b[i]); err != nil { 174 return fmt.Errorf("at index %d: %w", i, err) 175 } 176 } 177 return nil 178 } 179 180 func PolynomialSliceEquals(a []polynomial.Polynomial, b []polynomial.Polynomial) error { 181 if len(a) != len(b) { 182 return fmt.Errorf("length mismatch %d≠%d", len(a), len(b)) 183 } 184 for i := range a { 185 if err := SliceEquals(a[i], b[i]); err != nil { 186 return fmt.Errorf("at index %d: %w", i, err) 187 } 188 } 189 return nil 190 } 191 192 func ElementToInterface(x *fr.Element) interface{} { 193 if i := x.BigInt(nil); i != nil { 194 return i 195 } 196 return x.Text(10) 197 } 198 199 func ElementSliceToInterfaceSlice(x interface{}) []interface{} { 200 if x == nil { 201 return nil 202 } 203 204 X := reflect.ValueOf(x) 205 206 res := make([]interface{}, X.Len()) 207 for i := range res { 208 xI := X.Index(i).Interface().(fr.Element) 209 res[i] = ElementToInterface(&xI) 210 } 211 return res 212 } 213 214 func ElementSliceSliceToInterfaceSliceSlice(x interface{}) [][]interface{} { 215 if x == nil { 216 return nil 217 } 218 219 X := reflect.ValueOf(x) 220 221 res := make([][]interface{}, X.Len()) 222 for i := range res { 223 res[i] = ElementSliceToInterfaceSlice(X.Index(i).Interface()) 224 } 225 226 return res 227 }