[Go] Clean things up a bit (#282)
This commit is contained in:
parent
89c1c3a811
commit
64af10e37b
|
@ -1,4 +1,4 @@
|
|||
# cgo-kzg-4844
|
||||
# Go bindings
|
||||
|
||||
This package implements Go bindings (using [Cgo](https://go.dev/blog/cgo)) for the
|
||||
exported functions in [C-KZG-4844](https://github.com/ethereum/c-kzg-4844).
|
||||
|
@ -21,8 +21,8 @@ For reference, see the `example` module in this directory. You can test it out w
|
|||
|
||||
```
|
||||
user@system ~/c-kzg-4844/bindings/go/example $ go run .
|
||||
go: downloading github.com/ethereum/c-kzg-4844 v0.0.0-20230321204456-577d146c0a5a
|
||||
go: downloading github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2
|
||||
go: downloading github.com/ethereum/c-kzg-4844 v0.0.0-20230407130613-fd0a51aa35bc
|
||||
go: downloading github.com/supranational/blst v0.3.11-0.20230406105308-e9dfc5ee724b
|
||||
88f1aea383b825371cb98acfbae6c81cce601a2e3129461c3c2b816409af8f3e5080db165fd327db687b3ed632153a62
|
||||
```
|
||||
|
||||
|
|
|
@ -2,6 +2,6 @@ module example
|
|||
|
||||
go 1.19
|
||||
|
||||
require github.com/ethereum/c-kzg-4844 v0.0.0-20230330220301-966e40d70e72
|
||||
require github.com/ethereum/c-kzg-4844 v0.0.0-20230407130613-fd0a51aa35bc
|
||||
|
||||
require github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2 // indirect
|
||||
require github.com/supranational/blst v0.3.11-0.20230406105308-e9dfc5ee724b // indirect
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/ethereum/c-kzg-4844 v0.0.0-20230330220301-966e40d70e72 h1:oBooxFAmy0BLUXYlKK0ecyivDlvqUc9OMVxyG0yXi1A=
|
||||
github.com/ethereum/c-kzg-4844 v0.0.0-20230330220301-966e40d70e72/go.mod h1:RILWWKloPYRjowfacYdBBTg56GdU2nzxU7hAZ6bwWsw=
|
||||
github.com/ethereum/c-kzg-4844 v0.0.0-20230407130613-fd0a51aa35bc h1:bm2sLc9OcgiHkN6+ZlnwIMIXOBMLcPto7SeGyocFQhA=
|
||||
github.com/ethereum/c-kzg-4844 v0.0.0-20230407130613-fd0a51aa35bc/go.mod h1:WI2Nd82DMZAAZI1wV2neKGost9EKjvbpQR9OqE5Qqa8=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2 h1:wh1wzwAhZBNiZO37uWS/nDaKiIwHz4mDo4pnA+fqTO0=
|
||||
github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
|
||||
github.com/supranational/blst v0.3.11-0.20230406105308-e9dfc5ee724b h1:u49mjRnygnB34h8OKbnNJFVUtWSKIKb1KukdV8bILUM=
|
||||
github.com/supranational/blst v0.3.11-0.20230406105308-e9dfc5ee724b/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package cgokzg4844
|
||||
package ckzg4844
|
||||
|
||||
// #cgo CFLAGS: -I${SRCDIR}/../../src
|
||||
// #cgo CFLAGS: -I${SRCDIR}/blst_headers
|
||||
|
@ -9,6 +9,7 @@ package cgokzg4844
|
|||
import "C"
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
@ -48,7 +49,7 @@ var (
|
|||
)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Helper functions
|
||||
// Helper Functions
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// makeErrorFromRet translates an (integral) return value, as reported
|
||||
|
@ -63,7 +64,56 @@ func makeErrorFromRet(ret C.C_KZG_RET) error {
|
|||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Public functions
|
||||
// Unmarshal Functions
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func (b *Bytes32) UnmarshalText(input []byte) error {
|
||||
if string(input[:2]) == "0x" {
|
||||
input = input[2:]
|
||||
}
|
||||
bytes, err := hex.DecodeString(string(input))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(bytes) != len(b) {
|
||||
return ErrBadArgs
|
||||
}
|
||||
copy(b[:], bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bytes48) UnmarshalText(input []byte) error {
|
||||
if string(input[:2]) == "0x" {
|
||||
input = input[2:]
|
||||
}
|
||||
bytes, err := hex.DecodeString(string(input))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(bytes) != len(b) {
|
||||
return ErrBadArgs
|
||||
}
|
||||
copy(b[:], bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Blob) UnmarshalText(input []byte) error {
|
||||
if string(input[:2]) == "0x" {
|
||||
input = input[2:]
|
||||
}
|
||||
bytes, err := hex.DecodeString(string(input))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(bytes) != len(b) {
|
||||
return ErrBadArgs
|
||||
}
|
||||
copy(b[:], bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Interface Functions
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
|
@ -286,24 +336,3 @@ func VerifyBlobKZGProofBatch(blobs []Blob, commitmentsBytes, proofsBytes []Bytes
|
|||
&settings)
|
||||
return bool(result), makeErrorFromRet(ret)
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Private functions
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
sha256 is the binding for:
|
||||
|
||||
void blst_sha256(
|
||||
byte out[32],
|
||||
const byte *msg,
|
||||
size_t msg_len);
|
||||
*/
|
||||
func sha256(msg []byte) Bytes32 {
|
||||
var out Bytes32
|
||||
C.blst_sha256(
|
||||
(*C.byte)(unsafe.Pointer(&out)),
|
||||
*(**C.byte)(unsafe.Pointer(&msg)),
|
||||
(C.size_t)(len(msg)))
|
||||
return out
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package cgokzg4844
|
||||
package ckzg4844
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
@ -27,43 +25,7 @@ func TestMain(m *testing.M) {
|
|||
// Helper Functions
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func (f *Bytes32) UnmarshalText(input []byte) error {
|
||||
bytes, err := hex.DecodeString(string(input[2:]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(bytes) != len(f) {
|
||||
return errors.New("invalid Bytes32")
|
||||
}
|
||||
copy(f[:], bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Bytes48) UnmarshalText(input []byte) error {
|
||||
bytes, err := hex.DecodeString(string(input[2:]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(bytes) != len(f) {
|
||||
return errors.New("invalid Bytes48")
|
||||
}
|
||||
copy(f[:], bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Blob) UnmarshalText(input []byte) error {
|
||||
blobBytes, err := hex.DecodeString(string(input[2:]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(blobBytes) != len(b) {
|
||||
return errors.New("invalid Blob")
|
||||
}
|
||||
copy(b[:], blobBytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetRandFieldElement(seed int64) Bytes32 {
|
||||
func getRandFieldElement(seed int64) Bytes32 {
|
||||
rand.Seed(seed)
|
||||
bytes := make([]byte, 31)
|
||||
_, err := rand.Read(bytes)
|
||||
|
@ -78,34 +40,17 @@ func GetRandFieldElement(seed int64) Bytes32 {
|
|||
return fieldElementBytes
|
||||
}
|
||||
|
||||
func GetRandBlob(seed int64) Blob {
|
||||
func getRandBlob(seed int64) Blob {
|
||||
var blob Blob
|
||||
for i := 0; i < BytesPerBlob; i += BytesPerFieldElement {
|
||||
fieldElementBytes := GetRandFieldElement(seed + int64(i))
|
||||
fieldElementBytes := getRandFieldElement(seed + int64(i))
|
||||
copy(blob[i:i+BytesPerFieldElement], fieldElementBytes[:])
|
||||
}
|
||||
return blob
|
||||
}
|
||||
|
||||
/*
|
||||
HumanBytes will convert an integer to a human-readable value. Adapted from:
|
||||
https://programming.guide/go/formatting-byte-size-to-human-readable-format.html
|
||||
*/
|
||||
func HumanBytes(b int64) string {
|
||||
const unit = 1024
|
||||
if b < unit {
|
||||
return fmt.Sprintf("%dB", b)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := b / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%v%cB", float64(b)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Tests
|
||||
// Reference Tests
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
var (
|
||||
|
@ -454,7 +399,7 @@ func Benchmark(b *testing.B) {
|
|||
proofs := [length]Bytes48{}
|
||||
fields := [length]Bytes32{}
|
||||
for i := 0; i < length; i++ {
|
||||
blob := GetRandBlob(int64(i))
|
||||
blob := getRandBlob(int64(i))
|
||||
commitment, err := BlobToKZGCommitment(blob)
|
||||
require.NoError(b, err)
|
||||
proof, err := ComputeBlobKZGProof(blob, Bytes48(commitment))
|
||||
|
@ -463,13 +408,9 @@ func Benchmark(b *testing.B) {
|
|||
blobs[i] = blob
|
||||
commitments[i] = Bytes48(commitment)
|
||||
proofs[i] = Bytes48(proof)
|
||||
fields[i] = GetRandFieldElement(int64(i))
|
||||
fields[i] = getRandFieldElement(int64(i))
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Public functions
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
b.Run("BlobToKZGCommitment", func(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
BlobToKZGCommitment(blobs[0])
|
||||
|
@ -507,19 +448,4 @@ func Benchmark(b *testing.B) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Private functions
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
for i := 2; i <= 20; i += 2 {
|
||||
numBytes := int64(1 << i)
|
||||
bytes := make([]byte, numBytes)
|
||||
b.Run(fmt.Sprintf("sha256(size=%v)", HumanBytes(numBytes)), func(b *testing.B) {
|
||||
b.SetBytes(numBytes)
|
||||
for n := 0; n < b.N; n++ {
|
||||
sha256(bytes)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue