Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from ingonyama-zk/update-golang
Browse files Browse the repository at this point in the history
refactor: update golang
  • Loading branch information
ImmanuelSegol authored Jan 2, 2024
2 parents 435937f + 157e338 commit f9abc9d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 127 deletions.
86 changes: 1 addition & 85 deletions docs/icicle/golang-bindings.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,3 @@
# Golang bindings

Golang bindings allow you to use ICICLE as a Golang library.

The `curves` directory contains implementations for every supported curve. Each curve is its own package and includes all the required primitives and methods. There is no need to import all curves into your project, only the ones your project requires.

`goicicle.go` contains wrappers for CUDA memory allocation methods.

`templates` contains scripts to generate new packages for new curves, so if you ever want to add support for a new curve you can generate it based off these templates.

## Using ICICLE Golang bindings in your project

Too add ICICLE to your `go.mod` file.

```
go get github.com/ingonyama-zk/icicle/goicicle
```

If you want to specify a specific branch

```
go get github.com/ingonyama-zk/icicle/goicicle@<branch_name>
```

For a specific commit

```
go get github.com/ingonyama-zk/icicle/goicicle@<commit_id>
```

While this will add the ICICLE library to your project you sill need to build the static library, using our [make file](https://github.com/ingonyama-zk/icicle/blob/main/goicicle/Makefile).

```
make libbn254.so
```

The current supported options are `libbn254.so`, `libbls12_381.so`, `libbls12_377.so`, `libbw6_671.so` and all to compile all curves. The resulting .so files are the compiled static libraries for each curve.


Once the static library has been built

```
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH/<path_to_shared_libs>
```

Now you should be able to run your ICICLE program as usual.

# How do the Golang bindings work?

The static libraries produced from the CUDA code compilation are used to bind Golang to ICICLE's CUDA code.

1. These static libraries (`libbn254.so`, `libbls12_381.so`, `libbls12_377.so`, `libbw6_671.so`) can be imported in your Go project to leverage the GPU accelerated functionalities provided by ICICLE.

2. In your Go project, you can use `cgo` to link these static libraries. Here's a basic example on how you can use `cgo` to link these libraries:

```go
/*
#cgo LDFLAGS: -L/path/to/shared/libs -lbn254 -lbls12_381 -lbls12_377 -lbw6_671
#include "icicle.h" // make sure you use the correct header file(s)
*/
import "C"

func main() {
// Now you can call the C functions from the ICICLE libraries.
// Note that C function calls are prefixed with 'C.' in Go code.
}
```

Replace `/path/to/shared/libs` with the actual path where the static libraries are located on your system.

## Common issues

### Cannot find static library

In some cases you may encounter the following error, despite exporting the correct `LD_LIBRARY_PATH`.

```
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: cannot find -lbn254: No such file or directory
/usr/bin/ld: cannot find -lbn254: No such file or directory
/usr/bin/ld: cannot find -lbn254: No such file or directory
/usr/bin/ld: cannot find -lbn254: No such file or directory
/usr/bin/ld: cannot find -lbn254: No such file or directory
collect2: error: ld returned 1 exit status
```

This is normally fixed by exporting the path to the static library location in the following way: `export CGO_LDFLAGS="-L/<path_to_shared_lib>/"`
Golang is WIP in v1, coming soon. Please checkout a previous [release v0.1.0](https://github.com/ingonyama-zk/icicle/releases/tag/v0.1.0) for golang bindings.
41 changes: 3 additions & 38 deletions docs/icicle/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Based on this dependency architecture, the ICICLE repository has three main sect

[ICICLE core](https://github.com/ingonyama-zk/icicle/tree/main/icicle) contains all the low level CUDA code implementing primitives such as [points](https://github.com/ingonyama-zk/icicle/tree/main/icicle/primitives) and [MSM](https://github.com/ingonyama-zk/icicle/tree/main/icicle/appUtils/msm). There also exists higher level C++ wrappers to expose the low level CUDA primitives ([example](https://github.com/ingonyama-zk/icicle/blob/main/icicle/curves/bn254/msm.cu)).

ICICLE Core would typically be compiled into a static library and used in a third party language such as Rust, Golang or Python.
ICICLE Core would typically be compiled into a static library and used in a third party language such as Rust or Golang.

### ICICLE Rust and Golang bindings

Expand Down Expand Up @@ -169,42 +169,7 @@ cargo bench

#### ICICLE Golang

The Golang bindings are a bit more of a manual process. We start by compiling a static library for a curve we want to use in this case `bn254`.

```
cd goicicle
```

Now lets build our static library

```
make libbn254.so
```

The current supported options are `libbn254.so`, `libbls12_381.so`, `libbls12_377.so`, `libbw6_671.so` and `all` to compile all curves. The resulting `.so` files are the compiled static libraries for each curve.

`make clean` will remove compiled static libraries.

Before using the static libraries we need to make our OS aware of them

```
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH/<path_to_shared_libs>
```

:::tip

On some systems, despite exporting `LD_LIBRARY_PATH`, the system still won't be able to locate our static libraries. In this case, try the following steps.

```
export CGO_LDFLAGS="-L/<path_to_shared_lib>/"
```
:::

To run test for a specific curve

```
go test ./goicicle/curves/bn254 -count=1
```
Golang is WIP in v1, coming soon. Please checkout a previous [release v0.1.0](https://github.com/ingonyama-zk/icicle/releases/tag/v0.1.0) for golang bindings.

### Running ICICLE examples

Expand Down Expand Up @@ -259,7 +224,7 @@ You can now experiment with our other examples, perhaps try to run a rust or gol

## Writing new bindings for ICICLE

Since ICICLE Core is written in CUDA / C++ its really simple to generate static libraries. These static libraries can be installed on any system and called by higher level languages such as Golang or Python.
Since ICICLE Core is written in CUDA / C++ its really simple to generate static libraries. These static libraries can be installed on any system and called by higher level languages such as Golang.

static libraries can be loaded into memory once and used by multiple programs, reducing memory usage and potentially improving performance. They also allow you to separate functionality into distinct modules so your static library may need to compile only specific features that you want to use.

Expand Down
9 changes: 6 additions & 3 deletions docs/icicle/rust-bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ Simply add the following to your `Cargo.toml`.

```
# GPU Icicle integration
icicle = { git = "https://github.com/ingonyama-zk/icicle.git" }
icicle-cuda-runtime = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.0.0" }
icicle-core = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.0.0" }
icicle-bn254 = { git = "https://github.com/ingonyama-zk/icicle.git", tag = "v1.0.0" }
```

If you wish to point to a specific ICICLE branch add `branch = "<name_of_branch>"` to the ICICLE dependency.
For a specific commit add `rev = "<commit_id>"`.
`icicle-bn254` being the curve you wish to use and `icicle-core` and `icicle-cuda-runtime` contain ICICLE utilities and CUDA wrappers.

If you wish to point to a specific ICICLE branch add `branch = "<name_of_branch>"` or `tag = "<name_of_tag>"` to the ICICLE dependency. For a specific commit add `rev = "<commit_id>"`.

When you build your project ICICLE will be built as part of the build command.

Expand Down
2 changes: 1 addition & 1 deletion docs/icicle/supporting-additional-curves.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ Here you would need to replace `bn254NTTCuda` with `<curve_name>NTTCuda`. Most o

### Golang

TODO: based on jeremies changes
Golang is WIP in v1, coming soon. Please checkout a previous [release v0.1.0](https://github.com/ingonyama-zk/icicle/releases/tag/v0.1.0) for golang bindings.

0 comments on commit f9abc9d

Please sign in to comment.