github.com/ledgerwatch/erigon-lib@v1.0.0/pedersen_hash/hash.go (about) 1 //go:build linux 2 // +build linux 3 4 package hash 5 6 /* 7 #include <stdlib.h> 8 #include "hash.h" 9 */ 10 import "C" 11 12 import ( 13 "encoding/hex" 14 "fmt" 15 ) 16 17 func reverseHexEndianRepresentation(s string) string { 18 rns := []rune(s) 19 for i, j := 0, len(rns)-2; i < j; i, j = i+2, j-2 { 20 rns[i], rns[j] = rns[j], rns[i] 21 rns[i+1], rns[j+1] = rns[j+1], rns[i+1] 22 } 23 return string(rns) 24 } 25 26 func Hash(input1, input2 string) (string, error) { 27 input1Dec, _ := hex.DecodeString(reverseHexEndianRepresentation(input1)) 28 input2Dec, _ := hex.DecodeString(reverseHexEndianRepresentation(input2)) 29 in1 := C.CBytes(input1Dec) 30 in2 := C.CBytes(input2Dec) 31 var o [1024]byte 32 out := C.CBytes(o[:]) 33 upIn1 := in1 34 upIn2 := in2 35 upOut := out 36 defer func() { 37 C.free(upIn1) 38 C.free(upIn2) 39 C.free(upOut) 40 }() 41 res := C.CHash( 42 (*C.char)(upIn1), 43 (*C.char)(upIn2), 44 (*C.char)(upOut)) 45 if res != 0 { 46 return "", fmt.Errorf("Pedersen hash encountered an error: %s\n", C.GoBytes(out, 1024)) 47 } 48 49 hashResult := "0x" + reverseHexEndianRepresentation( 50 hex.EncodeToString(C.GoBytes(out, 32))) 51 52 return hashResult, nil 53 }