m2pool_core/internal/server/grs/hash_grs.go

67 lines
1.7 KiB
Go

// hash_grs.go
package grs
/*
#cgo CFLAGS : -I../include
#cgo LDFLAGS: -L../lib -lx1x
#include <stdio.h>
#include <stdlib.h>
#include "groestlcoin.h"
*/
import "C"
import (
"bytes"
//"encoding/hex"
"log"
"unsafe"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)
// GRS
func Build_GrsPowHash(h wire.BlockHeader) chainhash.Hash {
outputs := make([]byte, 32)
var powhash chainhash.Hash
buf := bytes.NewBuffer(make([]byte, 0, wire.MaxBlockHeaderPayload))
err := h.Serialize(buf)
if err != nil {
log.Println("[grs] powhash: Serialize error:", err)
return powhash
}
//test_grs_pow := "0000002029c8da051a0f5282d35bbe97ddf4e587cc68175d6bd3c07c620e000000000000335d5efbac0f20cbf4a7c04676857b299b668623df39ac73522e7aa858c63480f8770767907b251ae0b26421"
//test_grs_bytes, _ := hex.DecodeString(test_grs_pow)
//log.Println("[grs]in", hex.EncodeToString(buf.Bytes()))
in := (*C.char)(C.CBytes(buf.Bytes()))
//in := (*C.char)(C.CBytes(test_grs_bytes))
defer C.free(unsafe.Pointer(in))
output := (*C.char)(C.malloc(32))
if output == nil {
log.Println("[grs] powhash: malloc failed")
return powhash
}
defer C.free(unsafe.Pointer(output))
C.GroestlCoinHash(unsafe.Pointer(in), C.int(wire.MaxBlockHeaderPayload), unsafe.Pointer(output))
//outputs := C.GoBytes((*C.uint8_t)((unsafe.Pointer)(output)), 32)
/*p := uintptr(unsafe.Pointer(output))
for i := 0; i < 32; i++ {
j := *(*byte)(unsafe.Pointer(p))
outputs[i] = j
p += unsafe.Sizeof(j)
}*/
outputs = C.GoBytes(unsafe.Pointer(output), 32)
//log.Println("[grs]out", outputs)
err = powhash.SetBytes(outputs)
if err != nil {
log.Println("[grs] powhash: SetBytes error:", err)
return powhash
}
return powhash
}