Usage

Table of Contents

Quick Start

The easiest way to use Crypto3.Pubkey library is to use an algorithm with implicit state usage. Following example pubkey with BLS:

#include <nil/crypto3/pubkey/detail/bls/serialization.hpp>
using namespace nil::crypto3::algebra;
using namespace nil::crypto3::pubkey;
using namespace nil::crypto3::hashes;
using namespace nil::crypto3::multiprecision;
using curve_type = curves::bls12_381;
using hash_type = sha2<256>;
using bls_version = bls_mps_ro_version<curve_type, hash_type>;
using scheme_type = bls<bls_default_public_params<>, bls_version, bls_basic_scheme, curve_type>;
using privkey_type = private_key<scheme_type>;
using pubkey_type = public_key<scheme_type>;
using _privkey_type = typename privkey_type::private_key_type;
using _pubkey_type = typename pubkey_type::public_key_type;
using signature_type = typename pubkey_type::signature_type;
using modulus_type = typename _privkey_type::modulus_type;
int main() {
std::string msg_str = "hello world";
std::vector<std::uint8_t> msg(msg_str.begin(), msg_str.end());
privkey_type sk = privkey_type(random_element<typename _privkey_type::field_type>());
signature_type sig = ::nil::crypto3::sign(msg, sk);
pubkey_type &pubkey = sk;
print_field_element(std::cout, sk.get_privkey);
print_curve_element(std::cout, pubkey.get_pubkey);
return !(nil::crypto3::verify(msg, sig, pubkey));
}
SchemeImpl verify(const typename pubkey::public_key< Scheme >::signature_type &proof, const pubkey::public_key< Scheme > &key)
Verification of the proof of possession of the supplied key.
Definition: pubkey/include/nil/crypto3/pubkey/algorithm/verify.hpp:79
bls12< 381 > bls12_381
Definition: curves/bls12.hpp:74
Definition: pair.hpp:33
Definition: accumulators/hash.hpp:53
boost::mpl::apply< AccumulatorSet, tag::sign< ProcessingMode > >::type::result_type sign(const AccumulatorSet &acc)
Definition: accumulators/sign.hpp:113
boost::mpl::apply< AccumulatorSet, tag::pubkey< ProcessingMode > >::type::result_type pubkey(const AccumulatorSet &acc)
Definition: accumulators/pubkey.hpp:106
Definition: pkpad/include/nil/crypto3/pkpad/accumulators/encode.hpp:43

Stateful Processing

In case of public-key scheme source data accumulation necessity is present, following example demonstrates accumulator usage:

#include <nil/crypto3/pubkey/detail/bls/serialization.hpp>
using namespace nil::crypto3::algebra;
using namespace nil::crypto3::pubkey;
using namespace nil::crypto3::hashes;
using namespace nil::crypto3::multiprecision;
using curve_type = curves::bls12_381;
using hash_type = sha2<256>;
using bls_version = bls_mps_ro_version<curve_type, hash_type>;
using scheme_type = bls<bls_default_public_params<>, bls_version, bls_basic_scheme, curve_type>;
using privkey_type = private_key<scheme_type>;
using pubkey_type = public_key<scheme_type>;
using _privkey_type = typename privkey_type::private_key_type;
using _pubkey_type = typename pubkey_type::public_key_type;
using signature_type = typename pubkey_type::signature_type;
using modulus_type = typename _privkey_type::modulus_type;
using signing_isomorphic_mode =
typename ::nil::crypto3::pubkey::modes::isomorphic<scheme_type, ::nil::crypto3::pubkey::nop_padding>::
template bind<::nil::crypto3::pubkey::signing_policy<Scheme>>::type;
using verification_isomorphic_mode =
typename ::nil::crypto3::pubkey::modes::isomorphic<scheme_type, ::nil::crypto3::pubkey::nop_padding>::
template bind<::nil::crypto3::pubkey::verification_policy<scheme_type>>::type;
using verification_acc_set = verification_accumulator_set<verification_isomorphic_mode>;
using verification_acc = typename boost::mpl::front<typename verification_acc_set::features_type>::type;
using signing_acc_set = signing_accumulator_set<signing_isomorphic_mode>;
using signing_acc = typename boost::mpl::front<typename signing_acc_set::features_type>::type;
int main() {
std::string msg_str = "hello world";
std::vector<std::uint8_t> msg(msg_str.begin(), msg_str.end());
privkey_type sk = privkey_type(random_element<typename _privkey_type::field_type>());
pubkey_type &pubkey = sk;
signing_acc_set sign_acc(sk);
nil::crypto3::sign<scheme_type>(msg, sign_acc);
signature_type sig = boost::accumulators::extract_result<signing_acc>(sign_acc);
verification_acc_set verify_acc(pubkey, nil::crypto3::accumulators::signature = sig);
return !(nil::crypto3::verify<scheme_type>(msg, verify_acc));
}