Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new BLS host function support and switch to affine #252

Merged
merged 6 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions libraries/eosiolib/capi/eosio/crypto_bls_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ __attribute__((eosio_wasm_import))
int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len);
int32_t bls_g1_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);
int32_t bls_g2_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len);
Expand All @@ -34,6 +28,12 @@ int32_t bls_g2_map(const char* e, uint32_t e_len, char* res, uint32_t res_len);
__attribute__((eosio_wasm_import))
int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_fp_mul(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_fp_exp(const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, char* res, uint32_t res_len);

#ifdef __cplusplus
}
#endif
Expand Down
143 changes: 58 additions & 85 deletions libraries/eosiolib/core/eosio/crypto_bls_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ namespace eosio {
int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len);
int32_t bls_g1_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);
int32_t bls_g2_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len);
Expand All @@ -40,121 +34,100 @@ namespace eosio {

__attribute__((eosio_wasm_import))
int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_fp_mul(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len);

__attribute__((eosio_wasm_import))
int32_t bls_fp_exp(const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, char* res, uint32_t res_len);
}
}

using bls_scalar = std::array<char, 32>;
using bls_fp = std::array<char, 48>;
using bls_s = std::array<char, 64>;
using bls_fp2 = std::array<bls_fp, 2>;
using bls_g1 = std::array<char, 144>;
using bls_g2 = std::array<char, 288>;
using bls_gt = std::array<char, 576>;
using bls_g1 = std::array<char, 96>;
using bls_g2 = std::array<char, 192>;
using bls_gt = std::array<char, 576>; // group fp12

int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) {
inline int32_t bls_g1_add(const bls_g1& op1, const bls_g1& op2, bls_g1& res) {
return internal_use_do_not_use::bls_g1_add(
op1.data(),
sizeof(bls_g1),
op2.data(),
sizeof(bls_g1),
res.data(),
sizeof(bls_g1)
op1.data(), sizeof(bls_g1),
op2.data(), sizeof(bls_g1),
res.data(), sizeof(bls_g1)
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
);
}

int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) {
inline int32_t bls_g2_add(const bls_g2& op1, const bls_g2& op2, bls_g2& res) {
return internal_use_do_not_use::bls_g2_add(
op1.data(),
sizeof(bls_g2),
op2.data(),
sizeof(bls_g2),
res.data(),
sizeof(bls_g2)
);
}

int32_t bls_g1_mul(const bls_g1& point, const bls_scalar& scalar, bls_g1& res) {
return internal_use_do_not_use::bls_g1_mul(
point.data(),
sizeof(bls_g1),
scalar.data(),
sizeof(bls_scalar),
res.data(),
sizeof(bls_g1)
op1.data(), sizeof(bls_g2),
op2.data(), sizeof(bls_g2),
res.data(), sizeof(bls_g2)
);
}

int32_t bls_g2_mul(const bls_g2& point, const bls_scalar& scalar, bls_g2& res) {
return internal_use_do_not_use::bls_g2_mul(
point.data(),
sizeof(bls_g2),
scalar.data(),
sizeof(bls_scalar),
res.data(),
sizeof(bls_g2)
);
}

int32_t bls_g1_exp(const bls_g1 points[], const bls_scalar scalars[], const uint32_t num, bls_g1& res) {
return internal_use_do_not_use::bls_g1_exp(
num ? points[0].data() : nullptr,
num * sizeof(bls_g1),
num ? scalars[0].data() : nullptr,
num * sizeof(bls_scalar),
inline int32_t bls_g1_weighted_sum(const bls_g1 g1_points[], const bls_scalar scalars[], uint32_t num, bls_g1& res) {
return internal_use_do_not_use::bls_g1_weighted_sum(
num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1),
num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar),
num,
res.data(),
sizeof(bls_g1)
res.data(), sizeof(bls_g1)
);
}

int32_t bls_g2_exp(const bls_g2 points[], const bls_scalar scalars[], const uint32_t num, bls_g2& res) {
return internal_use_do_not_use::bls_g2_exp(
num ? points[0].data() : nullptr,
num * sizeof(bls_g2),
num ? scalars[0].data() : nullptr,
num * sizeof(bls_scalar),
inline int32_t bls_g2_weighted_sum(const bls_g2 g2_points[], const bls_scalar scalars[], uint32_t num, bls_g2& res) {
return internal_use_do_not_use::bls_g2_weighted_sum(
num ? g2_points[0].data() : nullptr, num * sizeof(bls_g2),
num ? scalars[0].data() : nullptr, num * sizeof(bls_scalar),
num,
res.data(),
sizeof(bls_g2)
res.data(), sizeof(bls_g2)
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
);
}

int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) {
inline int32_t bls_pairing(const bls_g1 g1_points[], const bls_g2 g2_points[], const uint32_t num, bls_gt& res) {
return internal_use_do_not_use::bls_pairing(
num ? g1_points[0].data() : nullptr,
num * sizeof(bls_g1),
num ? g2_points[0].data() : nullptr,
num * sizeof(bls_g2),
num ? g1_points[0].data() : nullptr, num * sizeof(bls_g1),
num ? g2_points[0].data() : nullptr, num * sizeof(bls_g2),
num,
res.data(),
sizeof(bls_gt)
res.data(), sizeof(bls_gt)
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
);
}

int32_t bls_g1_map(const bls_fp& e, bls_g1& res) {
inline int32_t bls_g1_map(const bls_fp& e, bls_g1& res) {
return internal_use_do_not_use::bls_g1_map(
e.data(),
sizeof(bls_fp),
res.data(),
sizeof(bls_g1)
e.data(), sizeof(bls_fp),
res.data(), sizeof(bls_g1)
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
);
}

int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) {
inline int32_t bls_g2_map(const bls_fp2& e, bls_g2& res) {
return internal_use_do_not_use::bls_g2_map(
e[0].data(),
sizeof(bls_fp2),
res.data(),
sizeof(bls_g2)
e[0].data(), sizeof(bls_fp2),
res.data(), sizeof(bls_g2)
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
);
}

int32_t bls_fp_mod(const bls_s& s, bls_fp& res) {
inline int32_t bls_fp_mod(const bls_s& s, bls_fp& res) {
return internal_use_do_not_use::bls_fp_mod(
s.data(),
sizeof(bls_s),
res.data(),
sizeof(bls_fp)
s.data(), sizeof(bls_s),
res.data(), sizeof(bls_fp)
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
);
}

inline int32_t bls_fp_mul(const bls_fp& op1, const bls_fp& op2, bls_fp& res) {
return internal_use_do_not_use::bls_fp_mul(
op1.data(), sizeof(bls_fp),
op2.data(), sizeof(bls_fp),
res.data(), sizeof(bls_fp)
);
}

inline int32_t bls_fp_exp(const bls_fp& base, const bls_s& exp, bls_fp& res) {
return internal_use_do_not_use::bls_fp_exp(
base.data(), sizeof(bls_fp),
exp.data(), sizeof(bls_s),
res.data(), sizeof(bls_fp)
greg7mdp marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
Expand Down
28 changes: 14 additions & 14 deletions libraries/native/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,24 +947,14 @@ int32_t bls_g2_add(const char* op1, uint32_t op1_len, const char* op2, uint32_t
return intrinsics::get().call<intrinsics::bls_g2_add>(op1, op1_len, op2, op2_len, res, res_len);
}

int32_t bls_g1_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len)
int32_t bls_g1_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len)
{
return intrinsics::get().call<intrinsics::bls_g1_mul>(point, point_len, scalar, scalar_len, res, res_len);
return intrinsics::get().call<intrinsics::bls_g1_weighted_sum>(points, points_len, scalars, scalars_len, n, res, res_len);
}

int32_t bls_g2_mul(const char* point, uint32_t point_len, const char* scalar, uint32_t scalar_len, char* res, uint32_t res_len)
int32_t bls_g2_weighted_sum(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len)
{
return intrinsics::get().call<intrinsics::bls_g2_mul>(point, point_len, scalar, scalar_len, res, res_len);
}

int32_t bls_g1_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len)
{
return intrinsics::get().call<intrinsics::bls_g1_exp>(points, points_len, scalars, scalars_len, n, res, res_len);
}

int32_t bls_g2_exp(const char* points, uint32_t points_len, const char* scalars, uint32_t scalars_len, uint32_t n, char* res, uint32_t res_len)
{
return intrinsics::get().call<intrinsics::bls_g2_exp>(points, points_len, scalars, scalars_len, n, res, res_len);
return intrinsics::get().call<intrinsics::bls_g2_weighted_sum>(points, points_len, scalars, scalars_len, n, res, res_len);
}

int32_t bls_pairing(const char* g1_points, uint32_t g1_points_len, const char* g2_points, uint32_t g2_points_len, uint32_t n, char* res, uint32_t res_len)
Expand All @@ -986,3 +976,13 @@ int32_t bls_fp_mod(const char* s, uint32_t s_len, char* res, uint32_t res_len)
{
return intrinsics::get().call<intrinsics::bls_fp_mod>(s, s_len, res, res_len);
}

int32_t bls_fp_mul(const char* op1, uint32_t op1_len, const char* op2, uint32_t op2_len, char* res, uint32_t res_len)
{
return intrinsics::get().call<intrinsics::bls_fp_mul>(op1, op1_len, op2, op2_len, res, res_len);
}

int32_t bls_fp_exp(const char* base, uint32_t base_len, const char* exp, uint32_t exp_len, char* res, uint32_t res_len)
{
return intrinsics::get().call<intrinsics::bls_fp_exp>(base, base_len, exp, exp_len, res, res_len);
}
10 changes: 5 additions & 5 deletions libraries/native/native/eosio/intrinsics_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ intrinsic_macro(alt_bn128_pair) \
intrinsic_macro(mod_exp) \
intrinsic_macro(bls_g1_add) \
intrinsic_macro(bls_g2_add) \
intrinsic_macro(bls_g1_mul) \
intrinsic_macro(bls_g2_mul) \
intrinsic_macro(bls_g1_exp) \
intrinsic_macro(bls_g2_exp) \
intrinsic_macro(bls_g1_weighted_sum) \
intrinsic_macro(bls_g2_weighted_sum) \
intrinsic_macro(bls_pairing) \
intrinsic_macro(bls_g1_map) \
intrinsic_macro(bls_g2_map) \
intrinsic_macro(bls_fp_mod)
intrinsic_macro(bls_fp_mod) \
intrinsic_macro(bls_fp_mul) \
intrinsic_macro(bls_fp_exp)



Expand Down
Loading