Files
icicle/wrappers/golang/curves/bls12377/poseidon/poseidon.go
ChickenLover 7fd9ed1b49 Feat/roman/tree builder (#525)
# Updates:

## Hashing

 - Added SpongeHasher class
 - Can be used to accept any hash function as an argument
 - Absorb and squeeze are now separated
- Memory management is now mostly done by SpongeHasher class, each hash
function only describes permutation kernels

## Tree builder

 - Tree builder is now hash-agnostic. 
 - Tree builder now supports 2D input (matrices)
- Tree builder can now use two different hash functions for layer 0 and
compression layers

## Poseidon1

 - Interface changed to classes
 - Now allows for any alpha
 - Now allows passing constants not in a single vector
 - Now allows for any domain tag
 - Constants are now released upon going out of scope
 - Rust wrappers changed to Poseidon struct
 
 ## Poseidon2
 
 - Interface changed to classes
 - Constants are now released upon going out of scope
 - Rust wrappers changed to Poseidon2 struct
 
## Keccak

 - Added Keccak class which inherits SpongeHasher
 - Now doesn't use gpu registers for storing states
 
 To do:
- [x] Update poseidon1 golang bindings
- [x] Update poseidon1 examples
- [x] Fix poseidon2 cuda test
- [x] Fix poseidon2 merkle tree builder test
- [x] Update keccak class with new design
- [x] Update keccak test
- [x] Check keccak correctness
- [x] Update tree builder rust wrappers
- [x] Leave doc comments

Future work:  
- [ ] Add keccak merkle tree builder externs
- [ ] Add keccak rust tree builder wrappers
- [ ] Write docs
- [ ] Add example
- [ ] Fix device output for tree builder

---------

Co-authored-by: Jeremy Felder <jeremy.felder1@gmail.com>
Co-authored-by: nonam3e <71525212+nonam3e@users.noreply.github.com>
2024-07-11 13:46:25 +07:00

88 lines
3.4 KiB
Go

package poseidon
// #cgo CFLAGS: -I./include/
// #include "poseidon.h"
import "C"
import (
"runtime"
"unsafe"
"github.com/ingonyama-zk/icicle/v2/wrappers/golang/core"
cr "github.com/ingonyama-zk/icicle/v2/wrappers/golang/cuda_runtime"
bls12_377 "github.com/ingonyama-zk/icicle/v2/wrappers/golang/curves/bls12377"
)
type PoseidonHandler = C.struct_PoseidonInst
type Poseidon struct {
width uint32
handle *PoseidonHandler
}
func Create(arity uint32, alpha uint32, fullRoundsHalf uint32, partialRounds uint32, scalars core.HostOrDeviceSlice, mdsMatrix core.HostOrDeviceSlice, nonSparseMatrix core.HostOrDeviceSlice, sparseMatrices core.HostOrDeviceSlice, domainTag bls12_377.ScalarField, ctx *cr.DeviceContext) (*Poseidon, core.IcicleError) {
var poseidon *PoseidonHandler
cArity := (C.uint)(arity)
cAlpha := (C.uint)(alpha)
cFullRoundsHalf := (C.uint)(fullRoundsHalf)
cPartialRounds := (C.uint)(partialRounds)
cScalars := (*C.scalar_t)(scalars.AsUnsafePointer())
cMdsMatrix := (*C.scalar_t)(mdsMatrix.AsUnsafePointer())
cNonSparseMatrix := (*C.scalar_t)(nonSparseMatrix.AsUnsafePointer())
cSparseMatrices := (*C.scalar_t)(sparseMatrices.AsUnsafePointer())
cDomainTag := (*C.scalar_t)(unsafe.Pointer(&domainTag))
cCtx := (*C.DeviceContext)(unsafe.Pointer(ctx))
__ret := C.bls12_377_poseidon_create_cuda(&poseidon, cArity, cAlpha, cFullRoundsHalf, cPartialRounds, cScalars, cMdsMatrix, cNonSparseMatrix, cSparseMatrices, cDomainTag, cCtx)
err := core.FromCudaError((cr.CudaError)(__ret))
if err.IcicleErrorCode != core.IcicleSuccess {
return nil, err
}
p := Poseidon{handle: poseidon, width: arity + 1}
runtime.SetFinalizer(&p, func(p *Poseidon) {
p.Delete()
})
return &p, err
}
func Load(arity uint32, ctx *cr.DeviceContext) (*Poseidon, core.IcicleError) {
var poseidon *PoseidonHandler
cArity := (C.uint)(arity)
cCtx := (*C.DeviceContext)(unsafe.Pointer(ctx))
__ret := C.bls12_377_poseidon_load_cuda(&poseidon, cArity, cCtx)
err := core.FromCudaError((cr.CudaError)(__ret))
if err.IcicleErrorCode != core.IcicleSuccess {
return nil, err
}
p := Poseidon{handle: poseidon, width: arity + 1}
runtime.SetFinalizer(&p, func(p *Poseidon) {
p.Delete()
})
return &p, err
}
func (poseidon *Poseidon) HashMany(inputs core.HostOrDeviceSlice, output core.HostOrDeviceSlice, numberOfStates uint32, inputBlockLen uint32, outputLen uint32, cfg *core.SpongeConfig) core.IcicleError {
core.SpongeInputCheck(inputs, numberOfStates, inputBlockLen, cfg.InputRate, &cfg.Ctx)
core.SpongeOutputsCheck(output, numberOfStates, outputLen, poseidon.width, false, &cfg.Ctx)
cInputs := (*C.scalar_t)(inputs.AsUnsafePointer())
cOutput := (*C.scalar_t)(output.AsUnsafePointer())
cNumberOfStates := (C.uint)(numberOfStates)
cInputBlockLen := (C.uint)(inputBlockLen)
cOutputLen := (C.uint)(outputLen)
cCfg := (*C.SpongeConfig)(unsafe.Pointer(cfg))
__ret := C.bls12_377_poseidon_hash_many_cuda(poseidon.handle, cInputs, cOutput, cNumberOfStates, cInputBlockLen, cOutputLen, cCfg)
err := (cr.CudaError)(__ret)
return core.FromCudaError(err)
}
func (poseidon *Poseidon) Delete() core.IcicleError {
__ret := C.bls12_377_poseidon_delete_cuda(poseidon.handle)
err := (cr.CudaError)(__ret)
return core.FromCudaError(err)
}
func (poseidon *Poseidon) GetDefaultSpongeConfig() core.SpongeConfig {
cfg := core.GetDefaultSpongeConfig()
cfg.InputRate = poseidon.width - 1
cfg.OutputRate = poseidon.width
return cfg
}