2023-04-10 13:03:36 +00:00
|
|
|
package ckzg4844
|
2023-01-26 17:04:24 +00:00
|
|
|
|
|
|
|
// #cgo CFLAGS: -I${SRCDIR}/../../src
|
|
|
|
// #cgo CFLAGS: -I${SRCDIR}/blst_headers
|
|
|
|
// #include "c_kzg_4844.c"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2023-04-10 13:03:36 +00:00
|
|
|
"encoding/hex"
|
2023-03-30 00:33:15 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-05-25 11:40:21 +00:00
|
|
|
"strings"
|
2023-01-26 17:04:24 +00:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
// So its functions are available during compilation.
|
|
|
|
_ "github.com/supranational/blst/bindings/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BytesPerBlob = C.BYTES_PER_BLOB
|
|
|
|
BytesPerCommitment = C.BYTES_PER_COMMITMENT
|
|
|
|
BytesPerFieldElement = C.BYTES_PER_FIELD_ELEMENT
|
|
|
|
BytesPerProof = C.BYTES_PER_PROOF
|
|
|
|
FieldElementsPerBlob = C.FIELD_ELEMENTS_PER_BLOB
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Bytes32 [32]byte
|
|
|
|
Bytes48 [48]byte
|
|
|
|
KZGCommitment Bytes48
|
|
|
|
KZGProof Bytes48
|
|
|
|
Blob [BytesPerBlob]byte
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-03-30 00:33:15 +00:00
|
|
|
loaded = false
|
|
|
|
settings = C.KZGSettings{}
|
|
|
|
ErrBadArgs = errors.New("bad arguments")
|
|
|
|
ErrError = errors.New("unexpected error")
|
|
|
|
ErrMalloc = errors.New("malloc failed")
|
|
|
|
errorMap = map[C.C_KZG_RET]error{
|
|
|
|
C.C_KZG_OK: nil,
|
|
|
|
C.C_KZG_BADARGS: ErrBadArgs,
|
|
|
|
C.C_KZG_ERROR: ErrError,
|
|
|
|
C.C_KZG_MALLOC: ErrMalloc,
|
|
|
|
}
|
2023-01-26 17:04:24 +00:00
|
|
|
)
|
|
|
|
|
2023-03-30 00:33:15 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2023-04-10 13:03:36 +00:00
|
|
|
// Helper Functions
|
2023-03-30 00:33:15 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// makeErrorFromRet translates an (integral) return value, as reported
|
|
|
|
// by the C library, into a proper Go error. If there is no error, this
|
|
|
|
// will return nil.
|
|
|
|
func makeErrorFromRet(ret C.C_KZG_RET) error {
|
|
|
|
err, ok := errorMap[ret]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("unexpected return value: %v", ret))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:33:32 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2023-04-10 13:03:36 +00:00
|
|
|
// Unmarshal Functions
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
func (b *Bytes32) UnmarshalText(input []byte) error {
|
2023-05-25 11:40:21 +00:00
|
|
|
inputStr := string(input)
|
|
|
|
if strings.HasPrefix(inputStr, "0x") {
|
|
|
|
inputStr = strings.TrimPrefix(inputStr, "0x")
|
2023-04-10 13:03:36 +00:00
|
|
|
}
|
2023-05-25 11:40:21 +00:00
|
|
|
bytes, err := hex.DecodeString(inputStr)
|
2023-04-10 13:03:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(bytes) != len(b) {
|
|
|
|
return ErrBadArgs
|
|
|
|
}
|
|
|
|
copy(b[:], bytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bytes48) UnmarshalText(input []byte) error {
|
2023-05-25 11:40:21 +00:00
|
|
|
inputStr := string(input)
|
|
|
|
if strings.HasPrefix(inputStr, "0x") {
|
|
|
|
inputStr = strings.TrimPrefix(inputStr, "0x")
|
2023-04-10 13:03:36 +00:00
|
|
|
}
|
2023-05-25 11:40:21 +00:00
|
|
|
bytes, err := hex.DecodeString(inputStr)
|
2023-04-10 13:03:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(bytes) != len(b) {
|
|
|
|
return ErrBadArgs
|
|
|
|
}
|
|
|
|
copy(b[:], bytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Blob) UnmarshalText(input []byte) error {
|
2023-05-25 11:40:21 +00:00
|
|
|
inputStr := string(input)
|
|
|
|
if strings.HasPrefix(inputStr, "0x") {
|
|
|
|
inputStr = strings.TrimPrefix(inputStr, "0x")
|
2023-04-10 13:03:36 +00:00
|
|
|
}
|
2023-05-25 11:40:21 +00:00
|
|
|
bytes, err := hex.DecodeString(inputStr)
|
2023-04-10 13:03:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(bytes) != len(b) {
|
|
|
|
return ErrBadArgs
|
|
|
|
}
|
|
|
|
copy(b[:], bytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Interface Functions
|
2023-02-10 20:33:32 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2023-01-26 17:04:24 +00:00
|
|
|
/*
|
|
|
|
LoadTrustedSetup is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET load_trusted_setup(
|
|
|
|
KZGSettings *out,
|
|
|
|
const uint8_t *g1_bytes,
|
|
|
|
size_t n1,
|
|
|
|
const uint8_t *g2_bytes,
|
|
|
|
size_t n2);
|
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func LoadTrustedSetup(g1Bytes, g2Bytes []byte) error {
|
2023-01-26 17:04:24 +00:00
|
|
|
if loaded {
|
|
|
|
panic("trusted setup is already loaded")
|
|
|
|
}
|
2023-04-07 12:57:27 +00:00
|
|
|
if len(g1Bytes)%C.BYTES_PER_G1 != 0 {
|
|
|
|
panic(fmt.Sprintf("len(g1Bytes) is not a multiple of %v", C.BYTES_PER_G1))
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
2023-04-07 12:57:27 +00:00
|
|
|
if len(g2Bytes)%C.BYTES_PER_G2 != 0 {
|
|
|
|
panic(fmt.Sprintf("len(g2Bytes) is not a multiple of %v", C.BYTES_PER_G2))
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
2023-04-07 12:57:27 +00:00
|
|
|
numG1Elements := len(g1Bytes) / C.BYTES_PER_G1
|
|
|
|
numG2Elements := len(g2Bytes) / C.BYTES_PER_G2
|
2023-01-26 17:04:24 +00:00
|
|
|
ret := C.load_trusted_setup(
|
|
|
|
&settings,
|
|
|
|
*(**C.uint8_t)(unsafe.Pointer(&g1Bytes)),
|
|
|
|
(C.size_t)(numG1Elements),
|
|
|
|
*(**C.uint8_t)(unsafe.Pointer(&g2Bytes)),
|
|
|
|
(C.size_t)(numG2Elements))
|
2023-03-30 00:33:15 +00:00
|
|
|
if ret == C.C_KZG_OK {
|
2023-01-26 17:04:24 +00:00
|
|
|
loaded = true
|
|
|
|
}
|
2023-03-30 00:33:15 +00:00
|
|
|
return makeErrorFromRet(ret)
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
LoadTrustedSetupFile is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET load_trusted_setup_file(
|
|
|
|
KZGSettings *out,
|
|
|
|
FILE *in);
|
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func LoadTrustedSetupFile(trustedSetupFile string) error {
|
2023-01-26 17:04:24 +00:00
|
|
|
if loaded {
|
|
|
|
panic("trusted setup is already loaded")
|
|
|
|
}
|
2023-03-24 13:50:15 +00:00
|
|
|
cTrustedSetupFile := C.CString(trustedSetupFile)
|
|
|
|
defer C.free(unsafe.Pointer(cTrustedSetupFile))
|
|
|
|
cMode := C.CString("r")
|
|
|
|
defer C.free(unsafe.Pointer(cMode))
|
|
|
|
fp := C.fopen(cTrustedSetupFile, cMode)
|
2023-01-26 17:04:24 +00:00
|
|
|
if fp == nil {
|
|
|
|
panic("error reading trusted setup")
|
|
|
|
}
|
|
|
|
ret := C.load_trusted_setup_file(&settings, fp)
|
|
|
|
C.fclose(fp)
|
2023-03-30 00:33:15 +00:00
|
|
|
if ret == C.C_KZG_OK {
|
2023-01-26 17:04:24 +00:00
|
|
|
loaded = true
|
|
|
|
}
|
2023-03-30 00:33:15 +00:00
|
|
|
return makeErrorFromRet(ret)
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
FreeTrustedSetup is the binding for:
|
|
|
|
|
|
|
|
void free_trusted_setup(
|
|
|
|
KZGSettings *s);
|
|
|
|
*/
|
|
|
|
func FreeTrustedSetup() {
|
|
|
|
if !loaded {
|
|
|
|
panic("trusted setup isn't loaded")
|
|
|
|
}
|
|
|
|
C.free_trusted_setup(&settings)
|
|
|
|
loaded = false
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
BlobToKZGCommitment is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET blob_to_kzg_commitment(
|
|
|
|
KZGCommitment *out,
|
|
|
|
const Blob *blob,
|
|
|
|
const KZGSettings *s);
|
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func BlobToKZGCommitment(blob Blob) (KZGCommitment, error) {
|
2023-01-26 17:04:24 +00:00
|
|
|
if !loaded {
|
|
|
|
panic("trusted setup isn't loaded")
|
|
|
|
}
|
|
|
|
commitment := KZGCommitment{}
|
|
|
|
ret := C.blob_to_kzg_commitment(
|
|
|
|
(*C.KZGCommitment)(unsafe.Pointer(&commitment)),
|
|
|
|
(*C.Blob)(unsafe.Pointer(&blob)),
|
|
|
|
&settings)
|
2023-03-30 00:33:15 +00:00
|
|
|
return commitment, makeErrorFromRet(ret)
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ComputeKZGProof is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET compute_kzg_proof(
|
2023-05-25 11:40:21 +00:00
|
|
|
KZGProof *proof_out,
|
|
|
|
Bytes32 *y_out,
|
|
|
|
const Blob *blob,
|
|
|
|
const Bytes32 *z_bytes,
|
|
|
|
const KZGSettings *s);
|
2023-01-26 17:04:24 +00:00
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func ComputeKZGProof(blob Blob, zBytes Bytes32) (KZGProof, Bytes32, error) {
|
2023-01-26 17:04:24 +00:00
|
|
|
if !loaded {
|
|
|
|
panic("trusted setup isn't loaded")
|
|
|
|
}
|
|
|
|
proof := KZGProof{}
|
2023-03-08 11:45:54 +00:00
|
|
|
y := Bytes32{}
|
2023-01-26 17:04:24 +00:00
|
|
|
ret := C.compute_kzg_proof(
|
|
|
|
(*C.KZGProof)(unsafe.Pointer(&proof)),
|
2023-03-08 11:45:54 +00:00
|
|
|
(*C.Bytes32)(unsafe.Pointer(&y)),
|
2023-01-26 17:04:24 +00:00
|
|
|
(*C.Blob)(unsafe.Pointer(&blob)),
|
|
|
|
(*C.Bytes32)(unsafe.Pointer(&zBytes)),
|
|
|
|
&settings)
|
2023-03-30 00:33:15 +00:00
|
|
|
return proof, y, makeErrorFromRet(ret)
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 16:01:31 +00:00
|
|
|
/*
|
|
|
|
ComputeBlobKZGProof is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET compute_blob_kzg_proof(
|
2023-05-25 11:40:21 +00:00
|
|
|
KZGProof *out,
|
|
|
|
const Blob *blob,
|
|
|
|
const Bytes48 *commitment_bytes,
|
|
|
|
const KZGSettings *s);
|
2023-02-20 16:01:31 +00:00
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func ComputeBlobKZGProof(blob Blob, commitmentBytes Bytes48) (KZGProof, error) {
|
2023-02-20 16:01:31 +00:00
|
|
|
if !loaded {
|
|
|
|
panic("trusted setup isn't loaded")
|
|
|
|
}
|
|
|
|
proof := KZGProof{}
|
|
|
|
ret := C.compute_blob_kzg_proof(
|
|
|
|
(*C.KZGProof)(unsafe.Pointer(&proof)),
|
|
|
|
(*C.Blob)(unsafe.Pointer(&blob)),
|
2023-03-08 11:45:54 +00:00
|
|
|
(*C.Bytes48)(unsafe.Pointer(&commitmentBytes)),
|
2023-02-20 16:01:31 +00:00
|
|
|
&settings)
|
2023-03-30 00:33:15 +00:00
|
|
|
return proof, makeErrorFromRet(ret)
|
2023-02-20 16:01:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 17:04:24 +00:00
|
|
|
/*
|
|
|
|
VerifyKZGProof is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET verify_kzg_proof(
|
|
|
|
bool *out,
|
|
|
|
const Bytes48 *commitment_bytes,
|
|
|
|
const Bytes32 *z_bytes,
|
|
|
|
const Bytes32 *y_bytes,
|
|
|
|
const Bytes48 *proof_bytes,
|
|
|
|
const KZGSettings *s);
|
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func VerifyKZGProof(commitmentBytes Bytes48, zBytes, yBytes Bytes32, proofBytes Bytes48) (bool, error) {
|
2023-01-26 17:04:24 +00:00
|
|
|
if !loaded {
|
|
|
|
panic("trusted setup isn't loaded")
|
|
|
|
}
|
|
|
|
var result C.bool
|
|
|
|
ret := C.verify_kzg_proof(
|
|
|
|
&result,
|
|
|
|
(*C.Bytes48)(unsafe.Pointer(&commitmentBytes)),
|
|
|
|
(*C.Bytes32)(unsafe.Pointer(&zBytes)),
|
|
|
|
(*C.Bytes32)(unsafe.Pointer(&yBytes)),
|
|
|
|
(*C.Bytes48)(unsafe.Pointer(&proofBytes)),
|
|
|
|
&settings)
|
2023-03-30 00:33:15 +00:00
|
|
|
return bool(result), makeErrorFromRet(ret)
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-02-20 16:01:31 +00:00
|
|
|
VerifyBlobKZGProof is the binding for:
|
2023-01-26 17:04:24 +00:00
|
|
|
|
2023-02-20 16:01:31 +00:00
|
|
|
C_KZG_RET verify_blob_kzg_proof(
|
|
|
|
bool *out,
|
|
|
|
const Blob *blob,
|
|
|
|
const Bytes48 *commitment_bytes,
|
|
|
|
const Bytes48 *proof_bytes,
|
2023-01-26 17:04:24 +00:00
|
|
|
const KZGSettings *s);
|
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func VerifyBlobKZGProof(blob Blob, commitmentBytes, proofBytes Bytes48) (bool, error) {
|
2023-01-26 17:04:24 +00:00
|
|
|
if !loaded {
|
|
|
|
panic("trusted setup isn't loaded")
|
|
|
|
}
|
2023-02-20 16:01:31 +00:00
|
|
|
var result C.bool
|
|
|
|
ret := C.verify_blob_kzg_proof(
|
|
|
|
&result,
|
|
|
|
(*C.Blob)(unsafe.Pointer(&blob)),
|
|
|
|
(*C.Bytes48)(unsafe.Pointer(&commitmentBytes)),
|
|
|
|
(*C.Bytes48)(unsafe.Pointer(&proofBytes)),
|
2023-01-26 17:04:24 +00:00
|
|
|
&settings)
|
2023-03-30 00:33:15 +00:00
|
|
|
return bool(result), makeErrorFromRet(ret)
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-02-20 16:01:31 +00:00
|
|
|
VerifyBlobKZGProofBatch is the binding for:
|
2023-01-26 17:04:24 +00:00
|
|
|
|
2023-02-20 16:01:31 +00:00
|
|
|
C_KZG_RET verify_blob_kzg_proof_batch(
|
2023-01-26 17:04:24 +00:00
|
|
|
bool *out,
|
|
|
|
const Blob *blobs,
|
|
|
|
const Bytes48 *commitments_bytes,
|
2023-02-20 16:01:31 +00:00
|
|
|
const Bytes48 *proofs_bytes,
|
2023-01-26 17:04:24 +00:00
|
|
|
const KZGSettings *s);
|
|
|
|
*/
|
2023-03-30 00:33:15 +00:00
|
|
|
func VerifyBlobKZGProofBatch(blobs []Blob, commitmentsBytes, proofsBytes []Bytes48) (bool, error) {
|
2023-01-26 17:04:24 +00:00
|
|
|
if !loaded {
|
|
|
|
panic("trusted setup isn't loaded")
|
|
|
|
}
|
2023-02-20 16:01:31 +00:00
|
|
|
if len(blobs) != len(commitmentsBytes) || len(blobs) != len(proofsBytes) {
|
2023-03-30 00:33:15 +00:00
|
|
|
return false, ErrBadArgs
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|
2023-02-20 16:01:31 +00:00
|
|
|
|
2023-01-26 17:04:24 +00:00
|
|
|
var result C.bool
|
2023-02-20 16:01:31 +00:00
|
|
|
ret := C.verify_blob_kzg_proof_batch(
|
2023-01-26 17:04:24 +00:00
|
|
|
&result,
|
|
|
|
*(**C.Blob)(unsafe.Pointer(&blobs)),
|
|
|
|
*(**C.Bytes48)(unsafe.Pointer(&commitmentsBytes)),
|
2023-02-20 16:01:31 +00:00
|
|
|
*(**C.Bytes48)(unsafe.Pointer(&proofsBytes)),
|
2023-01-26 17:04:24 +00:00
|
|
|
(C.size_t)(len(blobs)),
|
|
|
|
&settings)
|
2023-03-30 00:33:15 +00:00
|
|
|
return bool(result), makeErrorFromRet(ret)
|
2023-01-26 17:04:24 +00:00
|
|
|
}
|