Skip to content

Commit

Permalink
src|python: Drop some unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
xdustinface authored and mariano54 committed Feb 17, 2021
1 parent 7240b3c commit 4a1013a
Show file tree
Hide file tree
Showing 12 changed files with 4 additions and 466 deletions.
146 changes: 0 additions & 146 deletions python-bindings/pythonbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,6 @@ using std::vector;

PYBIND11_MODULE(blspy, m)
{
py::class_<BNWrapper>(m, "BNWrapper")
.def(py::init(&BNWrapper::FromByteVector))
.def(py::init([](py::int_ pyint) {
size_t n_bytes = 1 + ((_PyLong_NumBits(pyint.ptr()) + 7) / 8);
std::vector<uint8_t> buffer(n_bytes, 0);
if (_PyLong_AsByteArray(
(PyLongObject *)pyint.ptr(), buffer.data(), n_bytes, 0, 0) <
0) {
throw std::invalid_argument("Failed to cast int to BNWrapper");
}
return BNWrapper::FromByteVector(buffer);
}))
.def(
"__repr__",
[](const BNWrapper &b) {
std::stringstream s;
s << b;
return "<BNWrapper " + s.str() + ">";
})
.def(
"__deepcopy__",
[](const BNWrapper &k, const py::object &memo) {
return BNWrapper(k);
})
.def(
"__str__",
[](const BNWrapper &b) {
std::stringstream s;
s << b;
return s.str();
})
.def("__bytes__", [](const BNWrapper &b) {
std::stringstream s;
int length = bn_size_bin(*b.b);
uint8_t *out = new uint8_t[length];
bn_write_bin(out, length, *b.b);
s << out;
delete[] out;
return py::bytes(s.str());
});

py::implicitly_convertible<py::int_, BNWrapper>();

py::class_<PrivateKey>(m, "PrivateKey")
.def_property_readonly_static(
"PRIVATE_KEY_SIZE",
Expand Down Expand Up @@ -361,7 +318,6 @@ PYBIND11_MODULE(blspy, m)
})
.def("generator", &G1Element::Generator)
.def("from_message", py::overload_cast<const std::vector<uint8_t>&, const uint8_t*, int>(&G1Element::FromMessage))
.def("pair", &G1Element::Pair)
.def("negate", &G1Element::Negate)
.def("infinity", &G1Element::Infinity)
.def("get_fingerprint", &G1Element::GetFingerprint)
Expand All @@ -383,24 +339,12 @@ PYBIND11_MODULE(blspy, m)
return self * (*(bn_t *)&other);
},
py::is_operator())
.def(
"__mul__",
[](G1Element &self, BNWrapper other) { return self * (*other.b); },
py::is_operator())
.def(
"__rmul__",
[](G1Element &self, bn_t other) {
return self * (*(bn_t *)&other);
},
py::is_operator())
.def(
"__rmul__",
[](G1Element &self, BNWrapper other) { return self * (*other.b); },
py::is_operator())
.def(
"__and__",
[](G1Element &self, G2Element &other) { return self & other; },
py::is_operator())
.def(
"__repr__",
[](const G1Element &ele) {
Expand Down Expand Up @@ -474,7 +418,6 @@ PYBIND11_MODULE(blspy, m)
})
.def("generator", &G2Element::Generator)
.def("from_message", py::overload_cast<const std::vector<uint8_t>&, const uint8_t*, int>(&G2Element::FromMessage))
.def("pair", &G2Element::Pair)
.def("negate", &G2Element::Negate)
.def("infinity", &G2Element::Infinity)
.def(
Expand All @@ -495,20 +438,12 @@ PYBIND11_MODULE(blspy, m)
return self * (*(bn_t *)&other);
},
py::is_operator())
.def(
"__mul__",
[](G2Element &self, BNWrapper other) { return self * (*other.b); },
py::is_operator())
.def(
"__rmul__",
[](G2Element &self, bn_t other) {
return self * (*(bn_t *)&other);
},
py::is_operator())
.def(
"__rmul__",
[](G2Element &self, BNWrapper other) { return self * (*other.b); },
py::is_operator())

.def(
"__repr__",
Expand Down Expand Up @@ -536,87 +471,6 @@ PYBIND11_MODULE(blspy, m)
return G2Element(ele);
});

py::class_<GTElement>(m, "GTElement")
.def_property_readonly_static(
"SIZE", [](py::object self) { return GTElement::SIZE; })
.def(py::init(&GTElement::FromByteVector))
.def(py::init([](py::buffer const b) {
py::buffer_info info = b.request();
if (info.format != py::format_descriptor<uint8_t>::format() ||
info.ndim != 1)
throw std::runtime_error("Incompatible buffer format!");

if ((int)info.size != GTElement::SIZE) {
throw std::invalid_argument(
"Length of bytes object not equal to G2Element::SIZE");
}
auto data_ptr = static_cast<uint8_t *>(info.ptr);
std::vector<uint8_t> data(data_ptr, data_ptr + info.size);
return GTElement::FromByteVector(data);
}))
.def(py::init([](py::int_ pyint) {
std::vector<uint8_t> buffer(GTElement::SIZE, 0);
if (_PyLong_AsByteArray(
(PyLongObject *)pyint.ptr(),
buffer.data(),
GTElement::SIZE,
0,
0) < 0) {
throw std::invalid_argument("Failed to cast int to GTElement");
}
return GTElement::FromByteVector(buffer);
}))
.def(
"from_bytes",
[](py::buffer const b) {
py::buffer_info info = b.request();
if (info.format != py::format_descriptor<uint8_t>::format() ||
info.ndim != 1)
throw std::runtime_error("Incompatible buffer format!");

if ((int)info.size != GTElement::SIZE) {
throw std::invalid_argument(
"Length of bytes object not equal to GTElement::SIZE");
}
auto data_ptr = reinterpret_cast<const uint8_t *>(info.ptr);
return GTElement::FromBytes({data_ptr, GTElement::SIZE});
})
.def("unity", &GTElement::Unity)
.def(py::self == py::self)
.def(py::self != py::self)
.def(
"__deepcopy__",
[](const GTElement &gt, const py::object &memo) {
return GTElement(gt);
})
.def(
"__repr__",
[](const GTElement &ele) {
std::stringstream s;
s << ele;
return "<GTElement " + s.str() + ">";
})
.def(
"__str__",
[](const GTElement &ele) {
std::stringstream s;
s << ele;
return s.str();
})
.def(
"__bytes__",
[](const GTElement &ele) {
uint8_t *out = new uint8_t[GTElement::SIZE];
ele.Serialize(out);
py::bytes ans =
py::bytes(reinterpret_cast<char *>(out), GTElement::SIZE);
delete[] out;
return ans;
})
.def("__deepcopy__", [](const GTElement &ele, const py::object &memo) {
return GTElement(ele);
});

m.attr("PublicKeyMPL") = m.attr("G1Element");
m.attr("SignatureMPL") = m.attr("G2Element");

Expand Down
75 changes: 0 additions & 75 deletions python-bindings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
BasicSchemeMPL,
AugSchemeMPL,
PopSchemeMPL,
BNWrapper,
G1Element,
G2Element,
GTElement,
)
from copy import deepcopy
import binascii
Expand Down Expand Up @@ -71,78 +69,6 @@ def test_schemes():
sigU_child = Scheme.sign(childU, msg)
assert Scheme.verify(childUPk, msg, sigU_child)


def test_elements():
b1 = BNWrapper([1, 2])
b2 = BNWrapper([3, 1, 4, 1, 5, 9])
i1 = int.from_bytes(bytes([1, 2]), byteorder="big")
i2 = int.from_bytes(bytes([3, 1, 4, 1, 5, 9]), byteorder="big")
g1 = G1Element.generator()
g2 = G2Element.generator()
u1 = G1Element.infinity() # unity
u2 = G2Element.infinity()

# Does not allow construction
try:
i = G1Element()
assert False
except Exception:
pass
try:
i = G2Element()
assert False
except Exception:
pass

x1 = g1 * b1
x2 = g1 * b2
y1 = g2 * b1
y2 = g2 * b2

# Implicit conversion from python ints to BNWrapperWrapperWrapper
assert x1 == g1 * i1 == i1 * g1
assert x2 == g1 * i2 == i2 * g1
assert y1 == g2 * i1 == i1 * g2
assert y2 == g2 * i2 == i2 * g2

# G1
assert x1 != x2
assert x1 * b1 == b1 * x1
assert x1 * b1 != x1 * b2
assert x1 + u1 == x1
assert x1 + x2 == x2 + x1
assert x1 + x1.negate() == u1
assert x1 == G1Element(bytes(x1))
copy = deepcopy(x1)
assert x1 == copy
x1 += x2
assert x1 != copy

# G2
assert y1 != y2
assert y1 * b1 == b1 * y1
assert y1 * b1 != y1 * b2
assert y1 + u2 == y1
assert y1 + y2 == y2 + y1
assert y1 + y1.negate() == u2
assert y1 == G2Element(bytes(y1))
copy = deepcopy(y1)
assert y1 == copy
y1 += y2
assert y1 != copy

# pairing operation
pair = x1 & y1
assert pair != x1 & y2
assert pair != x2 & y1
assert pair == x1.pair(y1)
assert pair == GTElement(bytes(pair))
copy = deepcopy(pair)
assert pair == copy
pair = None
assert pair != copy


def test_vectors_invalid():
# Invalid inputs from https://github.com/algorand/bls_sigs_ref/blob/master/python-impl/serdesZ.py
invalid_inputs_1 = [
Expand Down Expand Up @@ -394,7 +320,6 @@ def test_aggregate_verify_zero_items():


test_schemes()
test_elements()
test_vectors_invalid()
test_vectors_valid()
test_readme()
Expand Down
6 changes: 0 additions & 6 deletions python-impl/impl-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,6 @@ def test_elements():
y1 = g2 * b1
y2 = g2 * b2

# Implicit conversion from python ints to BNWrapper
assert x1 == g1 * i1 == i1 * g1
assert x2 == g1 * i2 == i2 * g1
assert y1 == g2 * i1 == i1 * g2
assert y2 == g2 * i2 == i2 * g2

# G1
assert x1 != x2
assert x1 * b1 == b1 * x1
Expand Down
8 changes: 0 additions & 8 deletions src/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <algorithm>
#include <cstring>
#include <set>
#include <string>

#include "bls.hpp"

#if BLSALLOC_SODIUM
Expand All @@ -25,9 +20,6 @@

namespace bls {

const char BLS::GROUP_ORDER[] =
"73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001";

bool BLSInitResult = BLS::Init();

Util::SecureAllocCallback Util::secureAllocCallback;
Expand Down
18 changes: 0 additions & 18 deletions src/bls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,13 @@
#ifndef SRC_BLS_HPP_
#define SRC_BLS_HPP_

#include <vector>
#include <map>
#include <string>
#include <stdexcept>

#include "relic_conf.h"

#if defined GMP && ARITH == GMP
#include <gmp.h>
#endif

#include "privatekey.hpp"
#include "util.hpp"
#include "schemes.hpp"
#include "elements.hpp"
#include "hkdf.hpp"
#include "hdkeys.hpp"

extern "C" {
#include "relic.h"
}
#include "relic_test.h"

namespace bls {

/*
Expand All @@ -46,8 +30,6 @@ namespace bls {
*/
class BLS {
public:
// Order of g1, g2, and gt. Private keys are in {0, GROUP_ORDER}.
static const char GROUP_ORDER[];
static const size_t MESSAGE_HASH_LEN = 32;

// Initializes the BLS library (called automatically)
Expand Down
Loading

0 comments on commit 4a1013a

Please sign in to comment.