Skip to content

Commit

Permalink
refactor: support go 1.24 with newtypes (#518)
Browse files Browse the repository at this point in the history
* refactor: support go 1.24 with newtypes

* fix: use correct proof type in GenerateSDR
  • Loading branch information
Stebalien authored Feb 24, 2025
1 parent e6448b2 commit 2b59ef2
Show file tree
Hide file tree
Showing 6 changed files with 447 additions and 257 deletions.
24 changes: 15 additions & 9 deletions cgo/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,59 @@ package cgo
import "C"

func Hash(message SliceRefUint8) *[96]byte {
resp := C.hash(message)
resp := (*ByteArray96)(C.hash((C.slice_ref_uint8_t)(message)))
defer resp.destroy()
return resp.copyAsArray()
}

func Aggregate(flattenedSignatures SliceRefUint8) *[96]byte {
resp := C.aggregate(flattenedSignatures)
resp := (*ByteArray96)(C.aggregate((C.slice_ref_uint8_t)(flattenedSignatures)))
defer resp.destroy()
return resp.copyAsArray()
}

func Verify(signature SliceRefUint8, flattenedDigests SliceRefUint8, flattenedPublicKeys SliceRefUint8) bool {
resp := C.verify(signature, flattenedDigests, flattenedPublicKeys)
resp := C.verify((C.slice_ref_uint8_t)(signature),
(C.slice_ref_uint8_t)(flattenedDigests),
(C.slice_ref_uint8_t)(flattenedPublicKeys))
return bool(resp)
}

func HashVerify(signature SliceRefUint8, flattenedMessages SliceRefUint8, messageSizes SliceRefUint, flattenedPublicKeys SliceRefUint8) bool {
resp := C.hash_verify(signature, flattenedMessages, messageSizes, flattenedPublicKeys)
resp := C.hash_verify((C.slice_ref_uint8_t)(signature),
(C.slice_ref_uint8_t)(flattenedMessages),
(C.slice_ref_size_t)(messageSizes),
(C.slice_ref_uint8_t)(flattenedPublicKeys))
return bool(resp)
}

func PrivateKeyGenerate() *[32]byte {
resp := C.private_key_generate()
resp := (*ByteArray32)(C.private_key_generate())
defer resp.destroy()
return resp.copyAsArray()
}

func PrivateKeyGenerateWithSeed(rawSeed *ByteArray32) *[32]byte {
resp := C.private_key_generate_with_seed(rawSeed)
resp := (*ByteArray32)(C.private_key_generate_with_seed((*C.uint8_32_array_t)(rawSeed)))
defer resp.destroy()
return resp.copyAsArray()
}

func PrivateKeySign(rawPrivateKey SliceRefUint8, message SliceRefUint8) *[96]byte {
resp := C.private_key_sign(rawPrivateKey, message)
resp := (*ByteArray96)(C.private_key_sign((C.slice_ref_uint8_t)(rawPrivateKey),
(C.slice_ref_uint8_t)(message)))
defer resp.destroy()
return resp.copyAsArray()
}

func PrivateKeyPublicKey(rawPrivateKey SliceRefUint8) *[48]byte {
resp := C.private_key_public_key(rawPrivateKey)
resp := (*ByteArray48)(C.private_key_public_key((C.slice_ref_uint8_t)(rawPrivateKey)))
defer resp.destroy()
return resp.copyAsArray()
}

func CreateZeroSignature() *[96]byte {
resp := C.create_zero_signature()
resp := (*ByteArray96)(C.create_zero_signature())
defer resp.destroy()
return resp.copyAsArray()
}
36 changes: 18 additions & 18 deletions cgo/fvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ package cgo
import "C"

func CreateFvmMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestamp, chainId, baseFeeHi, baseFeeLo, baseCircSupplyHi, baseCircSupplyLo, networkVersion uint64, stateRoot SliceRefUint8, tracing bool, blockstoreId, externsId uint64) (*FvmMachine, error) {
resp := C.create_fvm_machine(
fvmVersion,
resp := (*resultFvmMachine)(C.create_fvm_machine(
(C.FvmRegisteredVersion_t)(fvmVersion),
C.uint64_t(chainEpoch),
C.uint64_t(chainTimestamp),
C.uint64_t(chainId),
Expand All @@ -19,13 +19,13 @@ func CreateFvmMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestam
C.uint64_t(baseCircSupplyHi),
C.uint64_t(baseCircSupplyLo),
C.uint32_t(networkVersion),
stateRoot,
(C.slice_ref_uint8_t)(stateRoot),
C.bool(tracing),
C.uint64_t(blockstoreId),
C.uint64_t(externsId),
)
))
// take out the pointer from the result to ensure it doesn't get freed
executor := resp.value
executor := (*FvmMachine)(resp.value)
resp.value = nil
defer resp.destroy()

Expand All @@ -37,8 +37,8 @@ func CreateFvmMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestam
}

func CreateFvmDebugMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTimestamp, chainId, baseFeeHi, baseFeeLo, baseCircSupplyHi, baseCircSupplyLo, networkVersion uint64, stateRoot SliceRefUint8, actorRedirect SliceRefUint8, tracing bool, blockstoreId, externsId uint64) (*FvmMachine, error) {
resp := C.create_fvm_debug_machine(
fvmVersion,
resp := (*resultFvmMachine)(C.create_fvm_debug_machine(
(C.FvmRegisteredVersion_t)(fvmVersion),
C.uint64_t(chainEpoch),
C.uint64_t(chainTimestamp),
C.uint64_t(chainId),
Expand All @@ -47,14 +47,14 @@ func CreateFvmDebugMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTim
C.uint64_t(baseCircSupplyHi),
C.uint64_t(baseCircSupplyLo),
C.uint32_t(networkVersion),
stateRoot,
actorRedirect,
(C.slice_ref_uint8_t)(stateRoot),
(C.slice_ref_uint8_t)(actorRedirect),
C.bool(tracing),
C.uint64_t(blockstoreId),
C.uint64_t(externsId),
)
))
// take out the pointer from the result to ensure it doesn't get freed
executor := resp.value
executor := (*FvmMachine)(resp.value)
resp.value = nil
defer resp.destroy()

Expand All @@ -66,27 +66,27 @@ func CreateFvmDebugMachine(fvmVersion FvmRegisteredVersion, chainEpoch, chainTim
}

func FvmMachineExecuteMessage(executor *FvmMachine, message SliceRefUint8, chainLen, applyKind uint64) (FvmMachineExecuteResponseGo, error) {
resp := C.fvm_machine_execute_message(
executor,
message,
resp := (*resultFvmMachineExecuteResponse)(C.fvm_machine_execute_message(
(*C.InnerFvmMachine_t)(executor),
(C.slice_ref_uint8_t)(message),
C.uint64_t(chainLen),
C.uint64_t(applyKind),
)
))
defer resp.destroy()

if err := CheckErr(resp); err != nil {
return FvmMachineExecuteResponseGo{}, err
}

return resp.value.copy(), nil
return (FvmMachineExecuteResponse)(resp.value).copy(), nil
}

func FvmMachineFlush(executor *FvmMachine) ([]byte, error) {
resp := C.fvm_machine_flush(executor)
resp := (*resultSliceBoxedUint8)(C.fvm_machine_flush((*C.InnerFvmMachine_t)(executor)))
defer resp.destroy()

if err := CheckErr(resp); err != nil {
return nil, err
}
return resp.value.copy(), nil
return (SliceBoxedUint8)(resp.value).copy(), nil
}
32 changes: 16 additions & 16 deletions cgo/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func AsSliceRefUint64(goBytes []uint64) SliceRefUint64 {
func AllocSliceBoxedUint8(goBytes []byte) SliceBoxedUint8 {
len := len(goBytes)

ptr := C.alloc_boxed_slice(C.size_t(len))
ptr := (SliceBoxedUint8)(C.alloc_boxed_slice(C.size_t(len)))
copy(ptr.slice(), goBytes)

return ptr
Expand Down Expand Up @@ -227,49 +227,49 @@ func CheckErr(resp result) error {

func NewAggregationInputs(commR ByteArray32, commD ByteArray32, sectorId uint64, ticket ByteArray32, seed ByteArray32) AggregationInputs {
return AggregationInputs{
comm_r: commR,
comm_d: commD,
comm_r: (C.uint8_32_array_t)(commR),
comm_d: (C.uint8_32_array_t)(commD),
sector_id: C.uint64_t(sectorId),
ticket: ticket,
seed: seed,
ticket: (C.uint8_32_array_t)(ticket),
seed: (C.uint8_32_array_t)(seed),
}
}

func NewPrivateReplicaInfo(pp RegisteredPoStProof, cacheDirPath string, commR ByteArray32, replicaPath string, sectorId uint64) PrivateReplicaInfo {
return PrivateReplicaInfo{
registered_proof: pp,
cache_dir_path: AllocSliceBoxedUint8([]byte(cacheDirPath)),
replica_path: AllocSliceBoxedUint8([]byte(replicaPath)),
registered_proof: (C.RegisteredPoStProof_t)(pp),
cache_dir_path: (C.slice_boxed_uint8_t)(AllocSliceBoxedUint8([]byte(cacheDirPath))),
replica_path: (C.slice_boxed_uint8_t)(AllocSliceBoxedUint8([]byte(replicaPath))),
sector_id: C.uint64_t(sectorId),
comm_r: commR,
comm_r: (C.uint8_32_array_t)(commR),
}
}

func NewPublicReplicaInfo(pp RegisteredPoStProof, commR ByteArray32, sectorId uint64) PublicReplicaInfo {
return PublicReplicaInfo{
registered_proof: pp,
registered_proof: (C.RegisteredPoStProof_t)(pp),
sector_id: C.uint64_t(sectorId),
comm_r: commR,
comm_r: (C.uint8_32_array_t)(commR),
}
}

func NewPoStProof(pp RegisteredPoStProof, proof []byte) PoStProof {
return PoStProof{
registered_proof: pp,
proof: AllocSliceBoxedUint8(proof),
registered_proof: (C.RegisteredPoStProof_t)(pp),
proof: (C.slice_boxed_uint8_t)(AllocSliceBoxedUint8(proof)),
}
}

func NewPublicPieceInfo(numBytes uint64, commP ByteArray32) PublicPieceInfo {
return PublicPieceInfo{
num_bytes: C.uint64_t(numBytes),
comm_p: commP,
comm_p: (C.uint8_32_array_t)(commP),
}
}

func NewPartitionSnarkProof(pp RegisteredPoStProof, proof []byte) PartitionSnarkProof {
return PartitionSnarkProof{
registered_proof: pp,
proof: AllocSliceBoxedUint8(proof),
registered_proof: (C.RegisteredPoStProof_t)(pp),
proof: (C.slice_boxed_uint8_t)(AllocSliceBoxedUint8(proof)),
}
}
Loading

0 comments on commit 2b59ef2

Please sign in to comment.