github.com/consensys/gnark-crypto@v0.14.0/internal/generator/hash_to_field/template/hash_to_field.go.tmpl (about) 1 import ( 2 "fmt" 3 "hash" 4 5 "{{ .FieldPackagePath }}" 6 ) 7 8 type wrappedHashToField struct { 9 domain []byte 10 toHash []byte 11 } 12 // New returns a new hasher instance which uses [{{ .FieldPackageName}}.Hash] to hash all the 13 // written bytes to a field element, returning the byte representation of the 14 // field element. The domain separator is passed as-is to hashing method. 15 func New(domainSeparator []byte) hash.Hash { 16 return &wrappedHashToField{ 17 domain: append([]byte{}, domainSeparator...), // copy in case the argument is modified 18 } 19 } 20 21 func (w *wrappedHashToField) Write(p []byte) (n int, err error) { 22 w.toHash = append(w.toHash, p...) 23 return len(p), nil 24 } 25 26 func (w *wrappedHashToField) Sum(b []byte) []byte { 27 res, err := {{ .FieldPackageName }}.Hash(w.toHash, w.domain, 1) 28 if err != nil { 29 // we want to follow the interface, cannot return error and have to panic 30 // but by default the method shouldn't return an error internally 31 panic(fmt.Sprintf("native field to hash: %v", err)) 32 } 33 bts := res[0].Bytes() 34 return append(b, bts[:]...) 35 } 36 37 func (w *wrappedHashToField) Reset() { 38 w.toHash = nil 39 } 40 41 func (w *wrappedHashToField) Size() int { 42 return {{ .FieldPackageName}}.Bytes 43 } 44 45 func (w *wrappedHashToField) BlockSize() int { 46 return {{ .FieldPackageName}}.Bytes 47 }