2022-10-01 23:52:12 +00:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
#include <Python.h>
|
|
|
|
#include "c_kzg_4844.h"
|
|
|
|
|
2022-10-02 11:40:36 +00:00
|
|
|
static void free_KZGSettings(PyObject *c) {
|
|
|
|
KZGSettings *s = PyCapsule_GetPointer(c, "KZGSettings");
|
|
|
|
free_trusted_setup(s);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject* load_trusted_setup_wrap(PyObject *self, PyObject *args) {
|
|
|
|
PyObject *f;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "U", &f))
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected a string");
|
|
|
|
|
|
|
|
KZGSettings *s = (KZGSettings*)malloc(sizeof(KZGSettings));
|
|
|
|
|
|
|
|
if (s == NULL) return PyErr_NoMemory();
|
|
|
|
|
2022-12-10 16:07:40 +00:00
|
|
|
if (load_trusted_setup_file(s, fopen(PyUnicode_AsUTF8(f), "r")) != C_KZG_OK) {
|
2022-10-02 17:36:53 +00:00
|
|
|
free(s);
|
2022-10-02 11:40:36 +00:00
|
|
|
return PyErr_Format(PyExc_RuntimeError, "error loading trusted setup");
|
2022-10-02 17:36:53 +00:00
|
|
|
}
|
2022-10-02 11:40:36 +00:00
|
|
|
|
|
|
|
return PyCapsule_New(s, "KZGSettings", free_KZGSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject* blob_to_kzg_commitment_wrap(PyObject *self, PyObject *args) {
|
2022-10-20 07:37:40 +00:00
|
|
|
PyObject *b;
|
|
|
|
PyObject *s;
|
2022-10-02 11:40:36 +00:00
|
|
|
|
2022-10-20 07:37:40 +00:00
|
|
|
if (!PyArg_UnpackTuple(args, "blob_to_kzg_commitment_wrap", 2, 2, &b, &s) ||
|
|
|
|
!PyBytes_Check(b) ||
|
|
|
|
!PyCapsule_IsValid(s, "KZGSettings"))
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected bytes and trusted setup");
|
2022-10-02 11:40:36 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
if (PyBytes_Size(b) != BYTES_PER_BLOB)
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected blobs to be BYTES_PER_BLOB bytes");
|
2022-10-02 11:40:36 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
PyObject *out = PyBytes_FromStringAndSize(NULL, BYTES_PER_COMMITMENT);
|
|
|
|
if (out == NULL) return PyErr_NoMemory();
|
2022-10-02 11:40:36 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
Blob *blob = (Blob *)PyBytes_AsString(b);
|
|
|
|
KZGCommitment *k = (KZGCommitment *)PyBytes_AsString(out);
|
2023-01-09 17:51:05 +00:00
|
|
|
if (blob_to_kzg_commitment(k, blob, PyCapsule_GetPointer(s, "KZGSettings")) != C_KZG_OK) {
|
2023-01-16 20:05:23 +00:00
|
|
|
Py_DECREF(out);
|
2023-01-09 17:51:05 +00:00
|
|
|
return PyErr_Format(PyExc_RuntimeError, "blob_to_kzg_commitment failed");
|
|
|
|
}
|
2022-10-02 11:40:36 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
return out;
|
2022-10-02 11:40:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 07:37:40 +00:00
|
|
|
static PyObject* compute_aggregate_kzg_proof_wrap(PyObject *self, PyObject *args) {
|
|
|
|
PyObject *b, *s;
|
2022-10-02 11:40:36 +00:00
|
|
|
|
2022-10-20 07:37:40 +00:00
|
|
|
if (!PyArg_UnpackTuple(args, "compute_aggregate_kzg_proof", 2, 2, &b, &s) ||
|
|
|
|
!PyBytes_Check(b) ||
|
|
|
|
!PyCapsule_IsValid(s, "KZGSettings"))
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected bytes, trusted setup");
|
2022-10-02 11:40:36 +00:00
|
|
|
|
2022-10-20 07:37:40 +00:00
|
|
|
Py_ssize_t n = PyBytes_Size(b);
|
2023-01-16 20:05:23 +00:00
|
|
|
if (n % BYTES_PER_BLOB != 0)
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected blobs to be a multiple of BYTES_PER_BLOB bytes");
|
|
|
|
n = n / BYTES_PER_BLOB;
|
2022-10-02 11:59:54 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
PyObject *out = PyBytes_FromStringAndSize(NULL, BYTES_PER_PROOF);
|
|
|
|
if (out == NULL) return PyErr_NoMemory();
|
2022-10-02 11:59:54 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
Blob *blobs = (Blob *)PyBytes_AsString(b);
|
|
|
|
KZGProof *k = (KZGProof *)PyBytes_AsString(out);
|
2022-10-20 07:37:40 +00:00
|
|
|
if (compute_aggregate_kzg_proof(k, blobs, n,
|
|
|
|
PyCapsule_GetPointer(s, "KZGSettings")) != C_KZG_OK) {
|
2023-01-16 20:05:23 +00:00
|
|
|
Py_DECREF(out);
|
2022-10-20 07:37:40 +00:00
|
|
|
return PyErr_Format(PyExc_RuntimeError, "compute_aggregate_kzg_proof failed");
|
2022-10-02 11:59:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
return out;
|
2022-10-02 11:59:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 07:37:40 +00:00
|
|
|
static PyObject* verify_aggregate_kzg_proof_wrap(PyObject *self, PyObject *args) {
|
2023-01-16 20:05:23 +00:00
|
|
|
PyObject *b, *c, *p, *s;
|
2022-10-02 12:21:13 +00:00
|
|
|
|
2022-10-20 07:37:40 +00:00
|
|
|
if (!PyArg_UnpackTuple(args, "verify_aggregate_kzg_proof", 4, 4, &b, &c, &p, &s) ||
|
|
|
|
!PyBytes_Check(b) ||
|
2023-01-16 20:05:23 +00:00
|
|
|
!PyBytes_Check(c) ||
|
|
|
|
!PyBytes_Check(p) ||
|
2022-10-02 12:21:13 +00:00
|
|
|
!PyCapsule_IsValid(s, "KZGSettings"))
|
2022-10-20 07:37:40 +00:00
|
|
|
return PyErr_Format(PyExc_ValueError,
|
2023-01-16 20:05:23 +00:00
|
|
|
"expected bytes, bytes, bytes, trusted setup");
|
2022-10-02 12:21:13 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
if (PyBytes_Size(p) != BYTES_PER_PROOF)
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected proof to be BYTES_PER_PROOF bytes");
|
2022-10-02 13:01:33 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
Py_ssize_t n = PyBytes_Size(b);
|
|
|
|
if (n % BYTES_PER_BLOB != 0)
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected blobs to be a multiple of BYTES_PER_BLOB bytes");
|
|
|
|
n = n / BYTES_PER_BLOB;
|
2022-10-02 13:01:33 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
Py_ssize_t m = PyBytes_Size(c);
|
|
|
|
if (m % BYTES_PER_COMMITMENT != 0)
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected commitments to be a multiple of BYTES_PER_COMMITMENT bytes");
|
|
|
|
m = m / BYTES_PER_COMMITMENT;
|
2022-10-02 13:01:33 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
if (m != n)
|
|
|
|
return PyErr_Format(PyExc_ValueError, "expected same number of commitments as polynomials");
|
2022-10-02 13:01:33 +00:00
|
|
|
|
2023-01-16 20:05:23 +00:00
|
|
|
const Blob* blobs = (Blob *)PyBytes_AsString(b);
|
|
|
|
const KZGProof *proof = (KZGProof *)PyBytes_AsString(p);
|
|
|
|
const KZGCommitment *commitments = (KZGCommitment *)PyBytes_AsString(c);
|
2022-10-02 13:01:33 +00:00
|
|
|
|
|
|
|
bool out;
|
2022-10-20 07:37:40 +00:00
|
|
|
if (verify_aggregate_kzg_proof(&out,
|
2023-01-16 20:05:23 +00:00
|
|
|
blobs, commitments, n, proof,
|
2022-10-20 07:37:40 +00:00
|
|
|
PyCapsule_GetPointer(s, "KZGSettings")) != C_KZG_OK) {
|
|
|
|
return PyErr_Format(PyExc_RuntimeError, "verify_aggregate_kzg_proof failed");
|
|
|
|
}
|
2022-10-02 13:01:33 +00:00
|
|
|
|
2022-10-02 21:12:08 +00:00
|
|
|
if (out) Py_RETURN_TRUE; else Py_RETURN_FALSE;
|
2022-10-02 13:01:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 23:52:12 +00:00
|
|
|
static PyMethodDef ckzgmethods[] = {
|
2022-10-20 07:37:40 +00:00
|
|
|
{"load_trusted_setup", load_trusted_setup_wrap, METH_VARARGS, "Load trusted setup from file path"},
|
|
|
|
{"blob_to_kzg_commitment", blob_to_kzg_commitment_wrap, METH_VARARGS, "Create a commitment from a blob"},
|
|
|
|
{"compute_aggregate_kzg_proof", compute_aggregate_kzg_proof_wrap, METH_VARARGS, "Compute aggregate KZG proof"},
|
|
|
|
{"verify_aggregate_kzg_proof", verify_aggregate_kzg_proof_wrap, METH_VARARGS, "Verify aggregate KZG proof"},
|
2022-10-01 23:52:12 +00:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef ckzg = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"ckzg",
|
|
|
|
NULL,
|
|
|
|
-1,
|
|
|
|
ckzgmethods
|
|
|
|
};
|
|
|
|
|
2022-10-02 08:47:43 +00:00
|
|
|
PyMODINIT_FUNC PyInit_ckzg(void) {
|
2022-10-01 23:52:12 +00:00
|
|
|
return PyModule_Create(&ckzg);
|
|
|
|
}
|