diff --git a/crypto/crypto.go b/crypto/crypto.go new file mode 100644 index 0000000..650693f --- /dev/null +++ b/crypto/crypto.go @@ -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 +} diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go new file mode 100644 index 0000000..f084460 --- /dev/null +++ b/crypto/crypto_test.go @@ -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") + } +} diff --git a/crypto/doc.go b/crypto/doc.go new file mode 100644 index 0000000..de442c0 --- /dev/null +++ b/crypto/doc.go @@ -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