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

Golang poseidon docs #644

Merged
merged 3 commits into from
Nov 10, 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
39 changes: 38 additions & 1 deletion docs/docs/icicle/golang-bindings/hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Using the Hash package requires `go` version 1.22

The ICICLE library provides Golang bindings for hashing using a variety of cryptographic hash functions. These hash functions are optimized for both general-purpose data and cryptographic operations such as multi-scalar multiplication, commitment generation, and Merkle tree construction.

This guide will show you how to use the ICICLE hashing API in Golang with examples for common hash algorithms, such as Keccak-256, Keccak-512, SHA3-256, SHA3-512, and Blake2s.
This guide will show you how to use the ICICLE hashing API in Golang with examples for common hash algorithms, such as Keccak-256, Keccak-512, SHA3-256, SHA3-512, Blake2s, and Poseidon.

## Importing Hash Functions

Expand Down Expand Up @@ -72,3 +72,40 @@ fmt.Println!("Hash(`", input_str, "`) =", &outputAsHexStr)
```

Using other hash algorithms is similar and only requires replacing the Hasher constructor with the relevant hashing algorithm.

### 3. Poseidon Example (field elements) and batch hashing

The Poseidon hash is designed for cryptographic field elements and curves, making it ideal for use cases such as zero-knowledge proofs (ZKPs). Poseidon hash using babybear field:

:::note

Since poseidon is designed for use with field elements and curves, it is located within the field or curve packages and not in the Hash package though it does rely on using the Hash package.

:::

```go
import (
"github.com/ingonyama-zk/icicle/v3/wrappers/golang/core"
babybear "github.com/ingonyama-zk/icicle/v3/wrappers/golang/fields/babybear"
"github.com/ingonyama-zk/icicle/v3/wrappers/golang/fields/babybear/poseidon"
)

batch := 1 << 4
t := 3 // Currently support arity of 3, 5, 9, 12
// (t - 1) is due to domainTag being non nil
// if domainTag is nil, then the input size should be `batch * t`
// See more in our tests: https://github.com/ingonyama-zk/icicle/blob/docs/v3/golang/poseidon/wrappers/golang/curves/bn254/tests/poseidon_test.go#L23-L27
inputsSize = batch * (t - 1)
jeremyfelder marked this conversation as resolved.
Show resolved Hide resolved
inputs := babybear.GenerateScalars(inputsSize)
domainTag := babybear.GenerateScalars(1)[0]

outputsRef := make([]babybear.ScalarField, batch)
poseidonHasherRef, _ := poseidon.NewHasher(uint64(t), &domainTag)
poseidonHasherRef.Hash(
core.HostSliceFromElements(inputs),
core.HostSliceFromElements(outputsRef),
core.GetDefaultHashConfig(),
)

poseidonHasherRef.Delete()
```
2 changes: 1 addition & 1 deletion docs/docs/icicle/primitives/hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ Currently the poseidon sponge function (Sec 2.1 of https://eprint.iacr.org/2019/
### Supported Bindings

- [Rust](../rust-bindings/hash)
- Go bindings soon
- [Go](../golang-bindings/hash)
Loading