erigon-pulse/pedersen_hash/hash.go
TKartist 8b2c02d1fb
Test for kv_mdbx optimized (#528)
* tests for GetOne and Put added

* test configured

* multiple tests added

* conflict resolve

* optimized and tests updated

* conflict fix
2022-07-18 21:36:58 +02:00

55 lines
1.1 KiB
Go

//go:build linux
// +build linux
package hash
/*
#include <stdlib.h>
#include "hash.h"
*/
import "C"
import (
"encoding/hex"
"fmt"
"unsafe"
)
func reverseHexEndianRepresentation(s string) string {
rns := []rune(s)
for i, j := 0, len(rns)-2; i < j; i, j = i+2, j-2 {
rns[i], rns[j] = rns[j], rns[i]
rns[i+1], rns[j+1] = rns[j+1], rns[i+1]
}
return string(rns)
}
func Hash(input1, input2 string) (string, error) {
input1Dec, _ := hex.DecodeString(reverseHexEndianRepresentation(input1))
input2Dec, _ := hex.DecodeString(reverseHexEndianRepresentation(input2))
in1 := C.CBytes(input1Dec)
in2 := C.CBytes(input2Dec)
var o [1024]byte
out := C.CBytes(o[:])
upIn1 := unsafe.Pointer(in1)
upIn2 := unsafe.Pointer(in2)
upOut := unsafe.Pointer(out)
defer func() {
C.free(upIn1)
C.free(upIn2)
C.free(upOut)
}()
res := C.CHash(
(*C.char)(upIn1),
(*C.char)(upIn2),
(*C.char)(upOut))
if res != 0 {
return "", fmt.Errorf("Pedersen hash encountered an error: %s\n", C.GoBytes(unsafe.Pointer(out), 1024))
}
hashResult := "0x" + reverseHexEndianRepresentation(
hex.EncodeToString(C.GoBytes(unsafe.Pointer(out), 32)))
return hashResult, nil
}