feat: define Go interfaces to rust lib
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2024-01-08 04:23:07 +05:30
parent 0fc323f707
commit b443540d61
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 49 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
lib/
main
.ccls-cache/

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module git.batsense.net/mCaptcha/mcaptcha-pow-go
go 1.21.5

45
mcaptcha_pow_go.go Normal file
View file

@ -0,0 +1,45 @@
package "mcaptcha_pow_go"
/*
#cgo LDFLAGS: ./lib/libmcaptcha_pow_ffi.a -ldl
#include "./mcaptcha-pow-ffi/mcaptcha_pow_ffi.h"
#include <stdlib.h>
*/
import "C"
import "fmt"
type ProofOfWork struct {
nonce uint64
result string
}
func ProveWork(salt string, phrase string, difficulty uint) ProofOfWork {
res := C.prove_work(C.CString(salt),
C.CString(phrase), C.uint(difficulty))
return ProofOfWork{
nonce: uint64(res.nonce),
result: C.GoString(res.result),
}
}
func (v ProofOfWork) IsSufficientDifficulty(salt string, difficulty uint) bool {
res := C.is_sufficient_difficulty(
C.ulong(v.nonce),
C.CString(v.result),
C.CString(salt),
C.uint(difficulty))
return bool(res)
}
func (v ProofOfWork) IsValidProof(phrase string, salt string) bool {
res := C.is_valid_proof(
C.ulong(v.nonce),
C.CString(v.result),
C.CString(phrase),
C.CString(salt))
return bool(res)
}