Skip to content

Commit

Permalink
add zkllvm-compatible aggregated verifiaction into bls_basic_functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CblPOK-git committed Dec 22, 2023
1 parent 7b5279b commit 4d7c57d
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions include/nil/crypto3/pubkey/detail/bls/bls_basic_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,68 @@ namespace nil {
namespace detail {
#ifdef __ZKLLVM__
namespace bls_basic_functions {
static inline bool verify(
bool verify(
typename algebra::fields::bls12_base_field<381>::value_type hashed_msg,
typename algebra::curves::bls12<381>::template g2_type<>::value_type pubkey,
typename algebra::curves::bls12<381>::template g1_type<>::value_type sig) {
typename algebra::curves::bls12<381>::template g1_type<>::value_type sig
) {

typename algebra::curves::bls12<381>::template g1_type<>::value_type msg_point = __builtin_assigner_hash_to_curve(hashed_msg);
typename algebra::curves::bls12<381>::template g1_type<>::value_type msg_point = __builtin_assigner_hash_to_curve(hashed_msg);

// __builtin_assigner_exit_check(__builtin_assigner_is_in_g1_check(sig));
// __builtin_assigner_exit_check(__builtin_assigner_is_in_g2_check(pubkey));
// __builtin_assigner_exit_check(__builtin_assigner_is_in_g1_check(sig));
// __builtin_assigner_exit_check(__builtin_assigner_is_in_g2_check(pubkey));

typename algebra::curves::bls12<381>::template g2_type<>::value_type g2_group_generator = algebra::curves::bls12<381>::template g2_type<>::one();
typename algebra::curves::bls12<381>::template g2_type<>::value_type g2_group_generator = algebra::curves::bls12<381>::template g2_type<>::one();

typename algebra::curves::bls12<381>::gt_type::value_type pairing1 = algebra::pair<algebra::curves::bls12<381>>(sig, g2_group_generator);
typename algebra::curves::bls12<381>::gt_type::value_type pairing2 = algebra::pair<algebra::curves::bls12<381>>(msg_point, pubkey);
typename algebra::curves::bls12<381>::gt_type::value_type pairing1 = algebra::pair<algebra::curves::bls12<381>>(sig, g2_group_generator);
typename algebra::curves::bls12<381>::gt_type::value_type pairing2 = algebra::pair<algebra::curves::bls12<381>>(msg_point, pubkey);

bool are_equal = 0;
for (std::size_t i = 0; i < 12; i++) {
are_equal = are_equal && (pairing1[i] == pairing2[i]);
}
// __builtin_assigner_exit_check(are_equal);

return are_equal;
}


template<std::size_t validators_amount>
bool aggregate_verify(
typename algebra::fields::bls12_base_field<381>::value_type hashed_msg,
std::array<typename algebra::curves::bls12<381>::template g2_type<>::value_type, validators_amount> pubkeys,
typename algebra::curves::bls12<381>::template g1_type<>::value_type aggregated_signature
) {

// __builtin_assigner_exit_check(__builtin_assigner_is_in_g1_check(aggregated_signature));

bool are_equal = 0;
for (std::size_t i = 0; i < 12; i++) {
are_equal = are_equal && (pairing1[i] == pairing2[i]);
}
// __builtin_assigner_exit_check(are_equal);

return are_equal;
typename algebra::curves::bls12<381>::template g2_type<>::value_type g2_group_generator = algebra::curves::bls12<381>::template g2_type<>::one();
typename algebra::curves::bls12<381>::gt_type::value_type pairing1 =
algebra::pair<algebra::curves::bls12<381>>(aggregated_signature, g2_group_generator);

typename algebra::curves::bls12<381>::template g1_type<>::value_type msg_point = __builtin_assigner_hash_to_curve(hashed_msg);

// __builtin_assigner_exit_check(__builtin_assigner_is_in_g2_check(pubkeys[0]));
typename algebra::curves::bls12<381>::gt_type::value_type pairing2 =
algebra::pair<algebra::curves::bls12<381>>(msg_point, pubkeys[0]);

typename algebra::curves::bls12<381>::gt_type::value_type current_pairing;

for (std::size_t i = 1; i < validators_amount; i++) {
// __builtin_assigner_exit_check(__builtin_assigner_is_in_g2_check(pubkeys[i]));
current_pairing = algebra::pair<algebra::curves::bls12<381>>(msg_point, pubkeys[i]);
pairing2 = __builtin_assigner_gt_multiplication(pairing2, current_pairing);
}

bool are_equal = 0;
for (std::size_t i = 0; i < 12; i++) {
are_equal = are_equal && (pairing1[i] == pairing2[i]);
}
// __builtin_assigner_exit_check(are_equal);

return are_equal;
}
}
#else
template<typename policy_type>
Expand Down

0 comments on commit 4d7c57d

Please sign in to comment.