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 support for BLS12-381 keys #15

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ erln8.config
/.eqc-info
/coverage/
codecov.json
/current_counterexample.eqc
*.dump
*.crashdump
GPATH
Expand All @@ -22,4 +21,4 @@ log/
data/
src/pb/
gh_pages/
doc/
doc/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cover:
$(REBAR) cover

test:
$(REBAR) as test do eunit, cover
$(REBAR) as test do eunit, ct, cover

ci:
$(REBAR) as test do eunit,cover && $(REBAR) do xref, dialyzer
Expand Down
Binary file added current_counterexample.eqc
Binary file not shown.
60 changes: 60 additions & 0 deletions eqc/key_eqc.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-module(key_eqc).

-include_lib("eqc/include/eqc.hrl").

-export([prop_key_test/0]).

prop_key_test() ->
?FORALL(
{NetType, KeyType, Msg},
{gen_nettype(), gen_keytype(), gen_msg()},
begin
KT = keytype(KeyType),
Keys = #{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(KT),
SigFun = libp2p_crypto:mk_sig_fun(PrivKey),
Signature = SigFun(Msg),

ToBin = libp2p_crypto:keys_to_bin(Keys),
KeysFromBin = libp2p_crypto:keys_from_bin(ToBin),

PubkeyToBin = libp2p_crypto:pubkey_to_bin(NetType, PubKey),
BinToPubkey = libp2p_crypto:bin_to_pubkey(NetType, PubkeyToBin),

SignatureCheck = libp2p_crypto:verify(Msg, Signature, PubKey),
BinCheck = KeysFromBin == Keys,
PubKeyCheck = BinToPubkey == PubKey,

?WHENFAIL(
begin
io:format("KeyType ~p~n", [KeyType]),
io:format("NetType ~p~n", [NetType]),
io:format("Msg ~p~n", [Msg]),
io:format("PubKey ~p~n", [PubKey]),
io:format("PrivKey ~p~n", [PrivKey]),
io:format("SigFun ~p~n", [SigFun]),
io:format("Signature ~p~n", [Signature])
end,
conjunction([
{valid_pubkey, PubKeyCheck},
{valid_bin, BinCheck},
{valid_signature, SignatureCheck}
])
)
end
).

gen_nettype() ->
elements([mainnet, testnet]).

gen_keytype() ->
elements([0, 1, 3]).

gen_msg() ->
binary(32).

keytype(0) ->
ecc_compact;
keytype(1) ->
ed25519;
keytype(3) ->
bls12_381.
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
{ecc_compact, "1.0.5"},
{enacl, "1.1.1"},
{erl_base58, "0.0.1"},
{multiaddr, "1.1.3"}
{multiaddr, "1.1.3"},
{erlang_tc, ".*", {git, "https://github.com/helium/erlang-tc.git", {branch, "rg/serde-sk"}}}
]}.

{erl_opts, [
Expand Down
4 changes: 4 additions & 0 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
{<<"ecc_compact">>,{pkg,<<"ecc_compact">>,<<"1.0.5">>},0},
{<<"enacl">>,{pkg,<<"enacl">>,<<"1.1.1">>},0},
{<<"erl_base58">>,{pkg,<<"erl_base58">>,<<"0.0.1">>},0},
{<<"erlang_tc">>,
{git,"https://github.com/helium/erlang-tc.git",
{ref,"588ca62467a1cbfe43d20e6f681656613dab0622"}},
0},
{<<"multiaddr">>,{pkg,<<"multiaddr">>,<<"1.1.3">>},0},
{<<"multihash">>,{pkg,<<"multihash">>,<<"2.1.0">>},0},
{<<"small_ints">>,{pkg,<<"small_ints">>,<<"0.1.0">>},1}]}.
Expand Down
52 changes: 45 additions & 7 deletions src/libp2p_crypto.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
-define(KEYTYPE_ECC_COMPACT, 0).
-define(KEYTYPE_ED25519, 1).
-define(KEYTYPE_MULTISIG, 2).
-define(KEYTYPE_BLS12_381, 3).
-define(NETTYPE_MAIN, 0).
-define(NETTYPE_TEST, 1).

Expand All @@ -20,7 +21,8 @@

-define(PRIMITIVE_KEY_TYPES, [
ecc_compact,
ed25519
ed25519,
bls12_381
]).

%% used for testing and known/unknown checking
Expand Down Expand Up @@ -52,18 +54,20 @@
]).
-define(MULTI_HASH_TYPES_DEPRECATED, []). %% used ONLY in verify

-type key_type() :: ecc_compact | ed25519.
-type key_type() :: ecc_compact | ed25519 | bls12_381.
-type network() :: mainnet | testnet.
-opaque privkey() ::
{ecc_compact, ecc_compact:private_key()}
| {ed25519, enacl_privkey()}.
| {ed25519, enacl_privkey()}
| {bls12_381, bls12_381_privkey()}.

-opaque pubkey_multi() ::
{multisig, pos_integer(), pos_integer(), binary()}.

-opaque pubkey_single() ::
{ecc_compact, ecc_compact:public_key()}
| {ed25519, enacl_pubkey()}.
| {ed25519, enacl_pubkey()}
| {bls12_381, bls12_381_pubkey()}.

-opaque pubkey() ::
pubkey_single() | pubkey_multi().
Expand All @@ -74,6 +78,8 @@
-type key_map() :: #{secret => privkey(), public => pubkey(), network => network()}.
-type enacl_privkey() :: <<_:256>>.
-type enacl_pubkey() :: <<_:256>>.
-type bls12_381_privkey() :: <<_:256>>.
-type bls12_381_pubkey() :: <<_:384>>.

-export_type([
privkey/0,
Expand Down Expand Up @@ -161,6 +167,14 @@ generate_keys(Network, ed25519) ->
secret => {ed25519, PrivKey},
public => {ed25519, PubKey},
network => Network
};
generate_keys(Network, bls12_381) ->
SK = tc_secret_key:random(),
PK = tc_secret_key:public_key(SK),
#{
secret => {bls12_381, tc_secret_key:serialize(SK)},
public => {bls12_381, tc_pubkey:serialize(PK)},
network => Network
}.

%% @doc Load the private key from a pem encoded given filename.
Expand All @@ -181,7 +195,9 @@ load_keys(FileName) ->
mk_sig_fun({ecc_compact, PrivKey}) ->
fun(Bin) -> public_key:sign(Bin, sha256, PrivKey) end;
mk_sig_fun({ed25519, PrivKey}) ->
fun(Bin) -> enacl:sign_detached(Bin, PrivKey) end.
fun(Bin) -> enacl:sign_detached(Bin, PrivKey) end;
mk_sig_fun({bls12_381, PrivKey}) ->
fun(Bin) -> tc_signature:serialize(tc_secret_key:sign(tc_secret_key:deserialize(PrivKey), Bin)) end.

%% @doc Constructs an ECDH exchange function from a given private key.
%%
Expand Down Expand Up @@ -226,7 +242,10 @@ keys_to_bin(Keys = #{secret := {ecc_compact, PrivKey}, public := {ecc_compact, _
end;
keys_to_bin(Keys = #{secret := {ed25519, PrivKey}, public := {ed25519, PubKey}}) ->
NetType = from_network(maps:get(network, Keys, mainnet)),
<<NetType:4, ?KEYTYPE_ED25519:4, PrivKey:64/binary, PubKey:32/binary>>.
<<NetType:4, ?KEYTYPE_ED25519:4, PrivKey:64/binary, PubKey:32/binary>>;
keys_to_bin(Keys = #{secret := {bls12_381, PrivKey}, public := {bls12_381, PubKey}}) ->
NetType = from_network(maps:get(network, Keys, mainnet)),
<<NetType:4, ?KEYTYPE_BLS12_381:4, PrivKey:32/binary, PubKey:48/binary>>.

%% @doc Convers a given binary to a key map
-spec keys_from_bin(binary()) -> key_map().
Expand Down Expand Up @@ -281,6 +300,12 @@ keys_from_bin(<<NetType:4, ?KEYTYPE_ED25519:4, PrivKey:64/binary, PubKey:32/bina
secret => {ed25519, PrivKey},
public => {ed25519, PubKey},
network => to_network(NetType)
};
keys_from_bin(<<NetType:4, ?KEYTYPE_BLS12_381:4, PrivKey:32/binary, PubKey:48/binary>>) ->
#{
secret => {bls12_381, PrivKey},
public => {bls12_381, PubKey},
network => to_network(NetType)
}.

%% @doc Convertsa a given tagged public key to its binary form on the current
Expand All @@ -301,6 +326,8 @@ pubkey_to_bin(Network, {ecc_compact, PubKey}) ->
end;
pubkey_to_bin(Network, {ed25519, PubKey}) ->
<<(from_network(Network)):4, ?KEYTYPE_ED25519:4, PubKey/binary>>;
pubkey_to_bin(Network, {bls12_381, PubKey}) ->
<<(from_network(Network)):4, ?KEYTYPE_BLS12_381:4, PubKey/binary>>;
pubkey_to_bin(Network, {multisig, M, N, KeysDigest}) ->
<<
(from_network(Network)):4,
Expand Down Expand Up @@ -329,6 +356,11 @@ bin_to_pubkey(Network, <<NetType:4, ?KEYTYPE_ED25519:4, PubKey:32/binary>>) ->
true -> {ed25519, PubKey};
false -> erlang:error({bad_network, NetType})
end;
bin_to_pubkey(Network, <<NetType:4, ?KEYTYPE_BLS12_381:4, PubKey:48/binary>>) ->
case NetType == from_network(Network) of
true -> {bls12_381, PubKey};
false -> erlang:error({bad_network, NetType})
end;
bin_to_pubkey(
Network,
<<
Expand Down Expand Up @@ -394,6 +426,8 @@ key_size_bytes(?KEYTYPE_ED25519) ->
32;
key_size_bytes(?KEYTYPE_ECC_COMPACT) ->
32;
key_size_bytes(?KEYTYPE_BLS12_381) ->
48;
key_size_bytes(KeyType) ->
error({bad_key_type, KeyType}).

Expand All @@ -405,7 +439,9 @@ verify(Bin, MultiSignature, {multisig, M, N, KeysDigest}) ->
verify(Bin, Signature, {ecc_compact, PubKey}) ->
public_key:verify(Bin, sha256, Signature, PubKey);
verify(Bin, Signature, {ed25519, PubKey}) ->
enacl:sign_verify_detached(Signature, Bin, PubKey).
enacl:sign_verify_detached(Signature, Bin, PubKey);
verify(Bin, Signature, {bls12_381, PubKey}) ->
tc_pubkey:verify(tc_pubkey:deserialize(PubKey), tc_signature:deserialize(Signature), Bin).

-spec verify_multisig(binary(), binary(), pubkey_multi(), [atom()]) -> boolean().
verify_multisig(Bin, MultiSignature, {multisig, M, N, KeysDigest}, HashTypes) ->
Expand Down Expand Up @@ -562,6 +598,8 @@ pubkey_is_multisig({multisig, _, _, _}) ->
pubkey_is_multisig({ecc_compact, _}) ->
false;
pubkey_is_multisig({ed25519, _}) ->
false;
pubkey_is_multisig({bls12_381, _}) ->
false.

%% @doc The binary form of this multisig-pubkey can be optained with
Expand Down
92 changes: 92 additions & 0 deletions test/key_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
-module(key_SUITE).

-include_lib("common_test/include/ct.hrl").

-export([all/0,
init_per_testcase/2,
end_per_testcase/2,
init_per_group/2,
end_per_group/2,
groups/0]).

-export([sig_test/1, roundtrip_test/1]).

test_cases() -> [sig_test, roundtrip_test].

all() ->
[{group, ecc_compact}, {group, ed25519}, {group, bls12_381}].

groups() ->
[
{ecc_compact, [], test_cases()},
{ed25519, [], test_cases()},
{bls12_381, [], test_cases()}
].

init_per_group(ecc_compact, Config) ->
[{key_type, ecc_compact} | Config];
init_per_group(ed25519, Config) ->
[{key_type, ed25519} | Config];
init_per_group(bls12_381, Config) ->
[{key_type, bls12_381} | Config].

end_per_group(_, _Config) ->
ok.

init_per_testcase(_, Config) ->
Msg = <<"Rip and tear until it's done">>,
[{total, 43}, {msg, Msg} | Config].

end_per_testcase(_, Config) ->
Config.

%% Test Cases

roundtrip_test(Config) ->
true = run_roundtrip_test(gen_keys(Config)),
true = run_roundtrip_test(gen_keys(Config)),
true = run_roundtrip_test(gen_keys(Config)),
ok.

sig_test(Config) ->
true = run_sig_test(Config, gen_keys(Config)),
true = run_sig_test(Config, gen_keys(Config)),
true = run_sig_test(Config, gen_keys(Config)),
ok.

%% Helpers

run_roundtrip_test(Keys) ->
Bins = lists:map(fun libp2p_crypto:keys_to_bin/1, Keys),
B58s = lists:map(fun libp2p_crypto:bin_to_b58/1, Bins),
Bins1 = lists:map(fun libp2p_crypto:b58_to_bin/1, B58s),
Bins == Bins1.

gen_keys(Config) ->
Tot = ?config(total, Config),
KeyType = ?config(key_type, Config),
[libp2p_crypto:generate_keys(KeyType) || _ <- lists:seq(1, Tot)].

run_sig_test(Config, Keys) ->
Msg = ?config(msg, Config),
SigsAndPKs = lists:map(
fun(#{public := PK, secret := SK}) ->
SigFun = libp2p_crypto:mk_sig_fun(SK),
{SigFun(Msg), PK}
end,
Keys),

ct:pal("SigsAndPKs: ~p", [SigsAndPKs]),

F = fun(SignaturesAndPubkeys) ->
true = lists:all(
fun({Sig, PK}) ->
libp2p_crypto:verify(Msg, Sig, PK)
end,
SignaturesAndPubkeys
)
end,

{Time, Res} = timer:tc(F, [SigsAndPKs]),
ct:pal("Time: ~pms", [Time / 1000]),
Res.