2023-01-26 17:04:24 +00:00
|
|
|
package cgokzg4844
|
|
|
|
|
|
|
|
// #cgo CFLAGS: -I${SRCDIR}/../../src
|
|
|
|
// #cgo CFLAGS: -I${SRCDIR}/blst_headers
|
|
|
|
// #cgo CFLAGS: -DFIELD_ELEMENTS_PER_BLOB=4096
|
|
|
|
// #include "c_kzg_4844.c"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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 (
|
|
|
|
CKzgRet int
|
|
|
|
Bytes32 [32]byte
|
|
|
|
Bytes48 [48]byte
|
|
|
|
KZGCommitment Bytes48
|
|
|
|
KZGProof Bytes48
|
|
|
|
Blob [BytesPerBlob]byte
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
C_KZG_OK CKzgRet = C.C_KZG_OK
|
|
|
|
C_KZG_BADARGS CKzgRet = C.C_KZG_BADARGS
|
|
|
|
C_KZG_ERROR CKzgRet = C.C_KZG_ERROR
|
|
|
|
C_KZG_MALLOC CKzgRet = C.C_KZG_MALLOC
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
loaded = false
|
|
|
|
settings = C.KZGSettings{}
|
|
|
|
)
|
|
|
|
|
2023-02-10 20:33:32 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Public functions
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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);
|
|
|
|
*/
|
|
|
|
func LoadTrustedSetup(g1Bytes, g2Bytes []byte) CKzgRet {
|
|
|
|
if loaded {
|
|
|
|
panic("trusted setup is already loaded")
|
|
|
|
}
|
|
|
|
if len(g1Bytes)%48 != 0 {
|
|
|
|
panic("len(g1Bytes) is not a multiple of 48")
|
|
|
|
}
|
|
|
|
if len(g2Bytes)%96 != 0 {
|
|
|
|
panic("len(g2Bytes) is not a multiple of 96")
|
|
|
|
}
|
|
|
|
numG1Elements := len(g1Bytes) / 48
|
|
|
|
numG2Elements := len(g2Bytes) / 96
|
|
|
|
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))
|
|
|
|
if CKzgRet(ret) == C_KZG_OK {
|
|
|
|
loaded = true
|
|
|
|
}
|
|
|
|
return CKzgRet(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
LoadTrustedSetupFile is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET load_trusted_setup_file(
|
|
|
|
KZGSettings *out,
|
|
|
|
FILE *in);
|
|
|
|
*/
|
|
|
|
func LoadTrustedSetupFile(trustedSetupFile string) CKzgRet {
|
|
|
|
if loaded {
|
|
|
|
panic("trusted setup is already loaded")
|
|
|
|
}
|
|
|
|
fp := C.fopen(C.CString(trustedSetupFile), C.CString("rb"))
|
|
|
|
if fp == nil {
|
|
|
|
panic("error reading trusted setup")
|
|
|
|
}
|
|
|
|
ret := C.load_trusted_setup_file(&settings, fp)
|
|
|
|
C.fclose(fp)
|
|
|
|
if CKzgRet(ret) == C_KZG_OK {
|
|
|
|
loaded = true
|
|
|
|
}
|
|
|
|
return CKzgRet(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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);
|
|
|
|
*/
|
|
|
|
func BlobToKZGCommitment(blob Blob) (KZGCommitment, CKzgRet) {
|
|
|
|
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)
|
|
|
|
return commitment, CKzgRet(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ComputeKZGProof is the binding for:
|
|
|
|
|
|
|
|
C_KZG_RET compute_kzg_proof(
|
2023-03-08 11:45:54 +00:00
|
|
|
KZGProof *proof_out,
|
|
|
|
Bytes32 *y_out,
|
2023-01-26 17:04:24 +00:00
|
|
|
const Blob *blob,
|
|
|
|
const Bytes32 *z_bytes,
|
|
|
|
const KZGSettings *s);
|
|
|
|
*/
|
2023-03-08 11:45:54 +00:00
|
|
|
func ComputeKZGProof(blob Blob, zBytes Bytes32) (KZGProof, Bytes32, CKzgRet) {
|
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-08 11:45:54 +00:00
|
|
|
return proof, y, CKzgRet(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(
|
|
|
|
KZGProof *out,
|
|
|
|
const Blob *blob,
|
2023-03-08 11:45:54 +00:00
|
|
|
const Bytes48 *commitment_bytes,
|
2023-02-20 16:01:31 +00:00
|
|
|
const KZGSettings *s);
|
|
|
|
*/
|
2023-03-08 11:45:54 +00:00
|
|
|
func ComputeBlobKZGProof(blob Blob, commitmentBytes Bytes48) (KZGProof, CKzgRet) {
|
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)
|
|
|
|
return proof, CKzgRet(ret)
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
*/
|
|
|
|
func VerifyKZGProof(commitmentBytes Bytes48, zBytes, yBytes Bytes32, proofBytes Bytes48) (bool, CKzgRet) {
|
|
|
|
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)
|
|
|
|
return bool(result), CKzgRet(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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-02-20 16:01:31 +00:00
|
|
|
func VerifyBlobKZGProof(blob Blob, commitmentBytes, proofBytes Bytes48) (bool, CKzgRet) {
|
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-02-20 16:01:31 +00:00
|
|
|
return bool(result), CKzgRet(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-02-20 16:01:31 +00:00
|
|
|
func VerifyBlobKZGProofBatch(blobs []Blob, commitmentsBytes, proofsBytes []Bytes48) (bool, CKzgRet) {
|
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-06 10:04:29 +00:00
|
|
|
return false, C_KZG_BADARGS
|
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)
|
|
|
|
return bool(result), CKzgRet(ret)
|
|
|
|
}
|
2023-02-10 20:33:32 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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
|
|
|
|
}
|