Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit 51d8973

Browse files
arvidnmariano54
authored andcommitted
lighter-weight buffer handling
1 parent 09db47d commit 51d8973

File tree

4 files changed

+45
-52
lines changed

4 files changed

+45
-52
lines changed

python-bindings/pythonbindings.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
namespace py = pybind11;
2626
using namespace bls;
27-
using std::vector;
2827

2928

3029
PYBIND11_MODULE(blspy, m)
@@ -46,9 +45,10 @@ PYBIND11_MODULE(blspy, m)
4645
"Length of bytes object not equal to PrivateKey::SIZE");
4746
}
4847
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
49-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
48+
std::array<uint8_t, PrivateKey::PRIVATE_KEY_SIZE> data;
49+
std::copy(data_ptr, data_ptr + PrivateKey::PRIVATE_KEY_SIZE, data.data());
5050
py::gil_scoped_release release;
51-
return PrivateKey::FromByteVector(data);
51+
return PrivateKey::FromBytes(data);
5252
})
5353
.def(
5454
"__bytes__",
@@ -355,9 +355,8 @@ PYBIND11_MODULE(blspy, m)
355355
return G1Element();
356356
}))
357357
.def(py::init(&G1Element::FromByteVector), py::call_guard<py::gil_scoped_release>())
358-
.def(py::init(&G1Element::FromByteVectorUnchecked), py::call_guard<py::gil_scoped_release>())
359358
.def(py::init([](py::int_ pyint) {
360-
std::vector<uint8_t> buffer(G1Element::SIZE, 0);
359+
std::array<uint8_t, G1Element::SIZE> buffer{};
361360
if (_PyLong_AsByteArray(
362361
(PyLongObject *)pyint.ptr(),
363362
buffer.data(),
@@ -367,7 +366,7 @@ PYBIND11_MODULE(blspy, m)
367366
throw std::invalid_argument("Failed to cast int to G1Element");
368367
}
369368
py::gil_scoped_release release;
370-
return G1Element::FromByteVector(buffer);
369+
return G1Element::FromBytes(buffer);
371370
}))
372371
.def(py::init([](py::buffer const b) {
373372
py::buffer_info info = b.request();
@@ -380,9 +379,10 @@ PYBIND11_MODULE(blspy, m)
380379
"Length of bytes object not equal to G1Element::SIZE");
381380
}
382381
auto data_ptr = static_cast<uint8_t *>(info.ptr);
383-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
382+
std::array<uint8_t, G1Element::SIZE> data;
383+
std::copy(data_ptr, data_ptr + G1Element::SIZE, data.data());
384384
py::gil_scoped_release release;
385-
return G1Element::FromByteVector(data);
385+
return G1Element::FromBytes(data);
386386
}))
387387
.def(
388388
"from_bytes",
@@ -397,9 +397,10 @@ PYBIND11_MODULE(blspy, m)
397397
"Length of bytes object not equal to G1Element::SIZE");
398398
}
399399
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
400-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
400+
std::array<uint8_t, G1Element::SIZE> data;
401+
std::copy(data_ptr, data_ptr + G1Element::SIZE, data.data());
401402
py::gil_scoped_release release;
402-
return G1Element::FromByteVector(data);
403+
return G1Element::FromBytes(data);
403404
})
404405
.def(
405406
"from_bytes_unchecked",
@@ -414,9 +415,7 @@ PYBIND11_MODULE(blspy, m)
414415
"Length of bytes object not equal to G1Element::SIZE");
415416
}
416417
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
417-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
418-
py::gil_scoped_release release;
419-
return G1Element::FromByteVectorUnchecked(data);
418+
return G1Element::FromBytesUnchecked({data_ptr, G1Element::SIZE});
420419
})
421420
.def("generator", &G1Element::Generator)
422421
.def("from_message", py::overload_cast<const std::vector<uint8_t>&, const uint8_t*, int>(&G1Element::FromMessage), py::call_guard<py::gil_scoped_release>())
@@ -498,7 +497,6 @@ PYBIND11_MODULE(blspy, m)
498497
return G2Element();
499498
}))
500499
.def(py::init(&G2Element::FromByteVector), py::call_guard<py::gil_scoped_release>())
501-
.def(py::init(&G2Element::FromByteVectorUnchecked), py::call_guard<py::gil_scoped_release>())
502500
.def(py::init([](py::buffer const b) {
503501
py::buffer_info info = b.request();
504502
if (info.format != py::format_descriptor<uint8_t>::format() ||
@@ -510,12 +508,13 @@ PYBIND11_MODULE(blspy, m)
510508
"Length of bytes object not equal to G2Element::SIZE");
511509
}
512510
auto data_ptr = static_cast<uint8_t *>(info.ptr);
513-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
511+
std::array<uint8_t, G2Element::SIZE> data;
512+
std::copy(data_ptr, data_ptr + G2Element::SIZE, data.data());
514513
py::gil_scoped_release release;
515-
return G2Element::FromByteVector(data);
514+
return G2Element::FromBytes(data);
516515
}))
517516
.def(py::init([](py::int_ pyint) {
518-
std::vector<uint8_t> buffer(G2Element::SIZE, 0);
517+
std::array<uint8_t, G2Element::SIZE> buffer{};
519518
if (_PyLong_AsByteArray(
520519
(PyLongObject *)pyint.ptr(),
521520
buffer.data(),
@@ -525,7 +524,7 @@ PYBIND11_MODULE(blspy, m)
525524
throw std::invalid_argument("Failed to cast int to G2Element");
526525
}
527526
py::gil_scoped_release release;
528-
return G2Element::FromByteVector(buffer);
527+
return G2Element::FromBytes(buffer);
529528
}))
530529
.def(
531530
"from_bytes",
@@ -540,9 +539,10 @@ PYBIND11_MODULE(blspy, m)
540539
"Length of bytes object not equal to G2Element::SIZE");
541540
}
542541
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
543-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
542+
std::array<uint8_t, G2Element::SIZE> data;
543+
std::copy(data_ptr, data_ptr + G2Element::SIZE, data.data());
544544
py::gil_scoped_release release;
545-
return G2Element::FromByteVector(data);
545+
return G2Element::FromBytes(data);
546546
})
547547
.def(
548548
"from_bytes_unchecked",
@@ -557,9 +557,7 @@ PYBIND11_MODULE(blspy, m)
557557
"Length of bytes object not equal to G2Element::SIZE");
558558
}
559559
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
560-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
561-
py::gil_scoped_release release;
562-
return G2Element::FromByteVector(data);
560+
return G2Element::FromBytesUnchecked({data_ptr, G2Element::SIZE});
563561
})
564562
.def("generator", &G2Element::Generator)
565563
.def("from_message", py::overload_cast<const std::vector<uint8_t>&, const uint8_t*, int>(&G2Element::FromMessage), py::call_guard<py::gil_scoped_release>())
@@ -642,12 +640,13 @@ PYBIND11_MODULE(blspy, m)
642640
"Length of bytes object not equal to G2Element::SIZE");
643641
}
644642
auto data_ptr = static_cast<uint8_t *>(info.ptr);
645-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
643+
std::array<uint8_t, GTElement::SIZE> data;
644+
std::copy(data_ptr, data_ptr + GTElement::SIZE, data.data());
646645
py::gil_scoped_release release;
647-
return GTElement::FromByteVector(data);
646+
return GTElement::FromBytes(data);
648647
}))
649648
.def(py::init([](py::int_ pyint) {
650-
std::vector<uint8_t> buffer(GTElement::SIZE, 0);
649+
std::array<uint8_t, G1Element::SIZE> buffer{};
651650
if (_PyLong_AsByteArray(
652651
(PyLongObject *)pyint.ptr(),
653652
buffer.data(),
@@ -657,7 +656,7 @@ PYBIND11_MODULE(blspy, m)
657656
throw std::invalid_argument("Failed to cast int to GTElement");
658657
}
659658
py::gil_scoped_release release;
660-
return GTElement::FromByteVector(buffer);
659+
return GTElement::FromBytes(buffer);
661660
}))
662661
.def(
663662
"from_bytes",
@@ -672,9 +671,10 @@ PYBIND11_MODULE(blspy, m)
672671
"Length of bytes object not equal to GTElement::SIZE");
673672
}
674673
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
675-
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
674+
std::array<uint8_t, GTElement::SIZE> data;
675+
std::copy(data_ptr, data_ptr + GTElement::SIZE, data.data());
676676
py::gil_scoped_release release;
677-
return GTElement::FromByteVector(data);
677+
return GTElement::FromBytes(data);
678678
})
679679
.def("unity", &GTElement::Unity)
680680
.def(py::self == py::self)

src/elements.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ namespace bls {
2020

2121
const size_t G1Element::SIZE;
2222

23-
G1Element G1Element::FromBytes(const Bytes& bytes) {
23+
G1Element G1Element::FromBytes(Bytes const bytes) {
2424
G1Element ele = G1Element::FromBytesUnchecked(bytes);
2525
ele.CheckValid();
2626
return ele;
2727
}
2828

29-
G1Element G1Element::FromBytesUnchecked(const Bytes& bytes)
29+
G1Element G1Element::FromBytesUnchecked(Bytes const bytes)
3030
{
3131
if (bytes.size() != SIZE) {
3232
throw std::invalid_argument("G1Element::FromBytes: Invalid size");
@@ -73,11 +73,6 @@ G1Element G1Element::FromByteVector(const std::vector<uint8_t>& bytevec)
7373
return G1Element::FromBytes(Bytes(bytevec));
7474
}
7575

76-
G1Element G1Element::FromByteVectorUnchecked(const std::vector<uint8_t>& bytevec)
77-
{
78-
return G1Element::FromBytesUnchecked(Bytes(bytevec));
79-
}
80-
8176
G1Element G1Element::FromNative(const g1_t element)
8277
{
8378
G1Element ele;
@@ -212,13 +207,13 @@ G1Element operator*(const bn_t& k, const G1Element& a) { return a * k; }
212207

213208
const size_t G2Element::SIZE;
214209

215-
G2Element G2Element::FromBytes(const Bytes& bytes) {
210+
G2Element G2Element::FromBytes(Bytes const bytes) {
216211
G2Element ele = G2Element::FromBytesUnchecked(bytes);
217212
ele.CheckValid();
218213
return ele;
219214
}
220215

221-
G2Element G2Element::FromBytesUnchecked(const Bytes& bytes)
216+
G2Element G2Element::FromBytesUnchecked(Bytes const bytes)
222217
{
223218
if (bytes.size() != SIZE) {
224219
throw std::invalid_argument("G2Element::FromBytes: Invalid size");
@@ -270,12 +265,6 @@ G2Element G2Element::FromByteVector(const std::vector<uint8_t>& bytevec)
270265
return G2Element::FromBytes(Bytes(bytevec));
271266
}
272267

273-
G2Element G2Element::FromByteVectorUnchecked(const std::vector<uint8_t>& bytevec)
274-
{
275-
return G2Element::FromBytesUnchecked(Bytes(bytevec));
276-
}
277-
278-
279268
G2Element G2Element::FromNative(const g2_t element)
280269
{
281270
G2Element ele;

src/elements.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ class G1Element {
4040
g1_set_infty(p);
4141
}
4242

43-
static G1Element FromBytes(const Bytes& bytes);
44-
static G1Element FromBytesUnchecked(const Bytes& bytes);
43+
static G1Element FromBytes(Bytes bytes);
44+
static G1Element FromBytesUnchecked(Bytes bytes);
4545
static G1Element FromByteVector(const std::vector<uint8_t> &bytevec);
46-
static G1Element FromByteVectorUnchecked(const std::vector<uint8_t> &bytevec);
4746
static G1Element FromNative(const g1_t element);
4847
static G1Element FromMessage(const std::vector<uint8_t> &message,
4948
const uint8_t *dst,
@@ -82,10 +81,9 @@ class G2Element {
8281
g2_set_infty(q);
8382
}
8483

85-
static G2Element FromBytes(const Bytes& bytes);
86-
static G2Element FromBytesUnchecked(const Bytes& bytes);
84+
static G2Element FromBytes(Bytes bytes);
85+
static G2Element FromBytesUnchecked(Bytes bytes);
8786
static G2Element FromByteVector(const std::vector<uint8_t> &bytevec);
88-
static G2Element FromByteVectorUnchecked(const std::vector<uint8_t> &bytevec);
8987
static G2Element FromNative(const g2_t element);
9088
static G2Element FromMessage(const std::vector<uint8_t>& message,
9189
const uint8_t* dst,

src/util.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sstream>
2121
#include <string>
2222
#include <vector>
23+
#include <array>
2324

2425
namespace bls {
2526

@@ -30,14 +31,19 @@ class Bytes {
3031
const size_t nSize;
3132

3233
public:
33-
explicit Bytes(const uint8_t* pDataIn, const size_t nSizeIn)
34+
Bytes(const uint8_t* pDataIn, const size_t nSizeIn)
3435
: pData(pDataIn), nSize(nSizeIn)
3536
{
3637
}
37-
explicit Bytes(const std::vector<uint8_t>& vecBytes)
38+
Bytes(const std::vector<uint8_t>& vecBytes)
3839
: pData(vecBytes.data()), nSize(vecBytes.size())
3940
{
4041
}
42+
template <size_t N>
43+
Bytes(const std::array<uint8_t, N>& a)
44+
: pData(a.data()), nSize(N)
45+
{
46+
}
4147

4248
inline const uint8_t* begin() const { return pData; }
4349
inline const uint8_t* end() const { return pData + nSize; }

0 commit comments

Comments
 (0)