github.com/consensys/gnark-crypto@v0.14.0/ecc/bw6-633/fp/hash_to_field/hash_to_field.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 hash_to_field 18 19 import ( 20 "fmt" 21 "hash" 22 23 "github.com/consensys/gnark-crypto/ecc/bw6-633/fp" 24 ) 25 26 type wrappedHashToField struct { 27 domain []byte 28 toHash []byte 29 } 30 31 // New returns a new hasher instance which uses [fp.Hash] to hash all the 32 // written bytes to a field element, returning the byte representation of the 33 // field element. The domain separator is passed as-is to hashing method. 34 func New(domainSeparator []byte) hash.Hash { 35 return &wrappedHashToField{ 36 domain: append([]byte{}, domainSeparator...), // copy in case the argument is modified 37 } 38 } 39 40 func (w *wrappedHashToField) Write(p []byte) (n int, err error) { 41 w.toHash = append(w.toHash, p...) 42 return len(p), nil 43 } 44 45 func (w *wrappedHashToField) Sum(b []byte) []byte { 46 res, err := fp.Hash(w.toHash, w.domain, 1) 47 if err != nil { 48 // we want to follow the interface, cannot return error and have to panic 49 // but by default the method shouldn't return an error internally 50 panic(fmt.Sprintf("native field to hash: %v", err)) 51 } 52 bts := res[0].Bytes() 53 return append(b, bts[:]...) 54 } 55 56 func (w *wrappedHashToField) Reset() { 57 w.toHash = nil 58 } 59 60 func (w *wrappedHashToField) Size() int { 61 return fp.Bytes 62 } 63 64 func (w *wrappedHashToField) BlockSize() int { 65 return fp.Bytes 66 }