feat(crypto): CGo bridge smoke test — FastHash via Keccak-256
Verify CGo link to libcryptonote.a works with a known-vector hash test. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
1416a6714a
commit
b759645724
3 changed files with 65 additions and 0 deletions
25
crypto/crypto.go
Normal file
25
crypto/crypto.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// SPDX-Licence-Identifier: EUPL-1.2
|
||||
|
||||
package crypto
|
||||
|
||||
/*
|
||||
#cgo CPPFLAGS: -I${SRCDIR}/upstream -I${SRCDIR}/compat
|
||||
#cgo LDFLAGS: -L${SRCDIR}/build -lcryptonote -lstdc++ -lssl -lcrypto
|
||||
#include "bridge.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// FastHash computes the CryptoNote fast hash (Keccak-256) of the input.
|
||||
func FastHash(data []byte) [32]byte {
|
||||
var hash [32]byte
|
||||
if len(data) == 0 {
|
||||
C.bridge_fast_hash(nil, 0, (*C.uint8_t)(unsafe.Pointer(&hash[0])))
|
||||
} else {
|
||||
C.bridge_fast_hash((*C.uint8_t)(unsafe.Pointer(&data[0])),
|
||||
C.size_t(len(data)),
|
||||
(*C.uint8_t)(unsafe.Pointer(&hash[0])))
|
||||
}
|
||||
return hash
|
||||
}
|
||||
30
crypto/crypto_test.go
Normal file
30
crypto/crypto_test.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// SPDX-Licence-Identifier: EUPL-1.2
|
||||
|
||||
package crypto_test
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"forge.lthn.ai/core/go-blockchain/crypto"
|
||||
)
|
||||
|
||||
func TestFastHash_Good_KnownVector(t *testing.T) {
|
||||
// Empty input → known Keccak-256 hash.
|
||||
input := []byte{}
|
||||
expected := "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
|
||||
|
||||
got := crypto.FastHash(input)
|
||||
if hex.EncodeToString(got[:]) != expected {
|
||||
t.Fatalf("FastHash(empty)\n got: %x\n want: %s", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFastHash_Good_HelloWorld(t *testing.T) {
|
||||
input := []byte("Hello, World!")
|
||||
got := crypto.FastHash(input)
|
||||
var zero [32]byte
|
||||
if got == zero {
|
||||
t.Fatal("FastHash returned zero hash")
|
||||
}
|
||||
}
|
||||
10
crypto/doc.go
Normal file
10
crypto/doc.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
// SPDX-Licence-Identifier: EUPL-1.2
|
||||
|
||||
// Package crypto provides CryptoNote cryptographic operations via CGo
|
||||
// bridge to the vendored upstream C++ library.
|
||||
//
|
||||
// Build the C++ library before running tests:
|
||||
//
|
||||
// cmake -S crypto -B crypto/build -DCMAKE_BUILD_TYPE=Release
|
||||
// cmake --build crypto/build --parallel
|
||||
package crypto
|
||||
Loading…
Add table
Reference in a new issue