Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the go bindings for the chiavdf c wrapper #108

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Get latest chiavdf libs
run: |
apt-get update && apt-get install -y unzip
LATEST_CHIAVDF=$(curl -s https://latest.cmm.io/chiavdf)
cmmarslender marked this conversation as resolved.
Show resolved Hide resolved
echo "Latest chiavdf is: $LATEST_CHIAVDF"
mkdir chiavdfc
cd chiavdfc || exit 1
wget https://github.com/Chia-Network/chiavdf/releases/download/$LATEST_CHIAVDF/chiavdfc-ubuntu-intel.zip
unzip chiavdfc-ubuntu-intel.zip
echo "CGO_CFLAGS=-I${GITHUB_WORKSPACE}/chiavdfc/chiavdfc-ubuntu-intel" >> "$GITHUB_ENV"

- name: Test
run: make test
2 changes: 2 additions & 0 deletions .repo-content-updater.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var_overrides:
CGO_ENABLED: "1"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ all: fmt lint vet build

.PHONY: build
build: $(BIN) ; $(info $(M) building executable…) @ ## Build program binary
$Q CGO_ENABLED=0 $(GO) build \
$Q CGO_ENABLED=1 $(GO) build \
-tags release \
-o $(BIN)/$(notdir $(basename $(MODULE)))$(binext) main.go
# Tools
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpc/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// GetNetworkInfoOptions options for the get_network_info rpc calls
type GetNetworkInfoOptions struct {}
type GetNetworkInfoOptions struct{}

// GetNetworkInfoResponse common get_network_info response from all RPC services
type GetNetworkInfoResponse struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/vdf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.py
venv
20 changes: 20 additions & 0 deletions pkg/vdf/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Go Bindings for chiavdf

This module depends on cgo and c bindings from chiavdf. To compile, you will need to provide paths to the libraries to
link against.

This example links gmp statically so the destination system doesn't need to have it installed:

```shell
# This path needs to include the c_wrapper.h file from chiavdf's c_bindings
export CGO_CFLAGS="-I/path/to/chiavdf/src/c_bindings"
export CGO_LDFLAGS="-L/path/to/chiavdf/build/lib/static -L/opt/homebrew/opt/gmp/lib /opt/homebrew/opt/gmp/lib/libgmp.a"
cmmarslender marked this conversation as resolved.
Show resolved Hide resolved
```

You can also dynamically link against gmp if the target system has it installed and available at runtime

```shell
# This path needs to include the c_wrapper.h file from chiavdf's c_bindings
export CGO_CFLAGS="-I/path/to/chiavdf/src/c_bindings"
export CGO_LDFLAGS="-L/path/to/chiavdf/build/lib/static -L/opt/homebrew/opt/gmp/lib -lgmp"
```
60 changes: 60 additions & 0 deletions pkg/vdf/vdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package vdf

/*
#cgo CXXFLAGS: -std=c++17
#cgo LDFLAGS: -lstdc++ -lchiavdfc
#include "c_wrapper.h"
#include <stdlib.h>
*/
import "C"
import (
"unsafe"
)

// CreateDiscriminant Creates discriminant
func CreateDiscriminant(seed []byte, length int) string {
cSeed := C.CBytes(seed)
defer C.free(cSeed)

cResultStr := C.create_discriminant_wrapper((*C.uint8_t)(cSeed), C.size_t(len(seed)), C.int(length))
defer C.free(unsafe.Pointer(cResultStr))

// Convert the C-string to a Go string
resultStr := C.GoString(cResultStr)

return resultStr
}

// Prove generates a proof
func Prove(challengeHash []byte, initialEL []byte, discriminantSizeBits int, numIterations uint64) []byte {
cChallengeHash := C.CBytes(challengeHash)
defer C.free(cChallengeHash)

cInitialEL := C.CBytes(initialEL)
defer C.free(cInitialEL)

cResult := C.prove_wrapper((*C.uint8_t)(cChallengeHash), C.size_t(len(challengeHash)), (*C.uint8_t)(cInitialEL), C.size_t(len(initialEL)), C.int(discriminantSizeBits), C.uint64_t(numIterations))
defer C.free(unsafe.Pointer(cResult.data))

// Convert C.ByteArray to Go []byte
length := int(cResult.length)
resultSlice := C.GoBytes(unsafe.Pointer(cResult.data), C.int(length))

return resultSlice
}

// VerifyNWesolowski checks an N Wesolowski proof.
func VerifyNWesolowski(discriminant string, xS, proofBlob []byte, numIterations, discSizeBits, recursion uint64) bool {
cDiscriminant := C.CString(discriminant)
defer C.free(unsafe.Pointer(cDiscriminant))

cXS := C.CBytes(xS)
defer C.free(cXS)

cProofBlob := C.CBytes(proofBlob)
defer C.free(cProofBlob)

result := C.verify_n_wesolowski_wrapper((*C.char)(cDiscriminant), C.size_t(len(discriminant)), (*C.char)(cXS), C.size_t(len(xS)), (*C.char)(cProofBlob), C.size_t(len(proofBlob)), C.uint64_t(numIterations), C.uint64_t(discSizeBits), C.uint64_t(recursion))

return result == 1
}