Skip to content

Commit

Permalink
Update erlang-tc
Browse files Browse the repository at this point in the history
- Fix erlang-tc module invocations
- Fix specs
  • Loading branch information
vihu committed Apr 24, 2021
1 parent accb003 commit 81b49ff
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 90 deletions.
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[{<<"erlang_tc">>,
{git,"https://github.com/helium/erlang-tc.git",
{ref,"4df790b0b79b2e26e6ba41d177a72253236281d7"}},
{ref,"b3ef1d5541586f5c85b6d231345a921d57be32a3"}},
0}].
36 changes: 18 additions & 18 deletions src/dkg_commitment.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
echoes = #{} :: echoes(),
readies =#{} :: readies(),
proofs =#{} :: ready_proofs(),
commitment_cache_fun :: fun((binary() | {binary(), bicommitment:bicommitment()}) -> bicommitment:bicommitment() | ok)
commitment_cache_fun :: fun((binary() | {binary(), tc_bicommitment:bicommitment()}) -> tc_bicommitment:bicommitment() | ok)
}).

-record(serialized_commitment, {
Expand All @@ -39,22 +39,22 @@
}).

-type commitment() :: #commitment{}.
-type echoes() :: #{pos_integer() => fr:fr()}.
-type readies() :: #{pos_integer() => fr:fr()}.
-type echoes() :: #{pos_integer() => tc_fr:fr()}.
-type readies() :: #{pos_integer() => tc_fr:fr()}.
-type serialized_commitment() :: #serialized_commitment{}.
-type ready_proofs() :: #{pos_integer() => binary()}.

-export_type([commitment/0, ready_proofs/0, serialized_commitment/0]).

-spec new([pos_integer(),...], bicommitment:bicommitment(), binary(), fun()) -> commitment().
-spec new([pos_integer(),...], tc_bicommitment:bicommitment(), binary(), fun()) -> commitment().
new(NodeIDs, Commitment, SerializedCommitment, CacheFun) ->
%% seed the cache since we have both here
ok = CacheFun({SerializedCommitment, Commitment}),
#commitment{nodes=NodeIDs, commitment=SerializedCommitment, commitment_cache_fun=CacheFun}.

-spec new([pos_integer(),...], bicommitment:bicommitment(), fun()) -> commitment().
-spec new([pos_integer(),...], tc_bicommitment:bicommitment(), fun()) -> commitment().
new(NodeIDs, Commitment, CacheFun) ->
SerializedCommitment = bicommitment:serialize(Commitment),
SerializedCommitment = tc_bicommitment:serialize(Commitment),
%% seed the cache since we have both here
ok = CacheFun({SerializedCommitment, Commitment}),
#commitment{nodes=NodeIDs, commitment=SerializedCommitment, commitment_cache_fun=CacheFun}.
Expand All @@ -63,17 +63,17 @@ new(NodeIDs, Commitment, CacheFun) ->
cmp(CommitmentA, CommitmentB) ->
CommitmentA#commitment.commitment == CommitmentB#commitment.commitment.

-spec verify_poly(commitment(), pos_integer(), poly:poly()) -> boolean().
-spec verify_poly(commitment(), pos_integer(), tc_poly:poly()) -> boolean().
verify_poly(Commitment, VerifierID, Poly) ->
bicommitment:verify_poly(gc(Commitment), Poly, VerifierID).
tc_bicommitment:verify_poly(gc(Commitment), Poly, VerifierID).

-spec public_key_share(commitment(), pos_integer()) -> public_key_share:pk_share().
-spec public_key_share(commitment(), pos_integer()) -> tc_public_key_share:pk_share().
public_key_share(Commitment, NodeID) ->
public_key_set:public_key_share(public_key_set:from_commitment(bicommitment:row(gc(Commitment), 0)), NodeID-1).
tc_public_key_set:public_key_share(tc_public_key_set:from_commitment(tc_bicommitment:row(gc(Commitment), 0)), NodeID-1).

-spec public_key_set(commitment()) -> public_key_set:pk_set().
-spec public_key_set(commitment()) -> tc_public_key_set:pk_set().
public_key_set(Commitment) ->
public_key_set:from_commitment(bicommitment:row(gc(Commitment), 0)).
tc_public_key_set:from_commitment(tc_bicommitment:row(gc(Commitment), 0)).

-spec verify_point(commitment(), pos_integer(), pos_integer(), binary()) -> boolean().
verify_point(Commitment, SenderID, VerifierID, Point) ->
Expand All @@ -82,22 +82,22 @@ verify_point(Commitment, SenderID, VerifierID, Point) ->
true ->
true;
false ->
bicommitment:validate_point(gc(Commitment), SenderID, VerifierID, fr:deserialize(Point))
tc_bicommitment:validate_point(gc(Commitment), SenderID, VerifierID, tc_fr:deserialize(Point))
end.

-spec interpolate(commitment(), pos_integer(), echo | ready) -> poly:poly().
-spec interpolate(commitment(), pos_integer(), echo | ready) -> tc_poly:poly().
interpolate(Commitment, T, EchoOrReady) ->
Map = case EchoOrReady of
echo -> Commitment#commitment.echoes;
ready -> Commitment#commitment.readies
end,
Received = [
{fr:into(Index), fr:deserialize(Val)}
{tc_fr:into(Index), tc_fr:deserialize(Val)}
|| {Index, Val} <- lists:sublist(maps:to_list(Map), T+1)
],
poly:interpolate_from_fr(Received).
tc_poly:interpolate_from_fr(Received).

-spec add_echo(commitment(), pos_integer(), fr:fr()) -> {true | false, commitment()}.
-spec add_echo(commitment(), pos_integer(), tc_fr:fr()) -> {true | false, commitment()}.
add_echo(Commitment = #commitment{nodes=Nodes, echoes=Echoes}, NodeID, Echo) when NodeID /= 0 ->
case lists:member(NodeID, Nodes) of
true ->
Expand All @@ -112,7 +112,7 @@ add_echo(Commitment = #commitment{nodes=Nodes, echoes=Echoes}, NodeID, Echo) whe
{false, Commitment}
end.

-spec add_ready(commitment(), pos_integer(), fr:fr()) -> {true | false, commitment()}.
-spec add_ready(commitment(), pos_integer(), tc_fr:fr()) -> {true | false, commitment()}.
add_ready(Commitment = #commitment{nodes=Nodes, readies=Readies}, NodeID, Ready) when NodeID /= 0 ->
case lists:member(NodeID, Nodes) of
true ->
Expand Down
18 changes: 9 additions & 9 deletions src/dkg_hybriddkg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
-type leader_vote_counts() :: #{Leader :: pos_integer() => [{Sender :: pos_integer(), signed_leader_change()}]}.
-type shares_map() :: #{pos_integer() => dkg_hybridvss:vss()}.
-type serialized_shares_map() :: #{pos_integer() => #{atom() => binary() | map()}}.
-type shares_results() :: #{pos_integer() => {C :: dkg_commitment:commitment(), Si :: fr:fr()}}.
-type shares_results() :: #{pos_integer() => {C :: dkg_commitment:commitment(), Si :: tc_fr:fr()}}.
-type dkg() :: #dkg{}.

-export_type([dkg/0]).
Expand Down Expand Up @@ -126,7 +126,7 @@ handle_msg(DKG=#dkg{await_vss = true}, Sender, {{vss, SharesId}, SharesMsg}) ->
{send, dkg_util:wrap({vss, SharesId}, ToSend)}};
{NewShares, {result, {_Session, Commitment, Si}}} ->
NewDKG = DKG#dkg{shares_map = maps:put(SharesId, NewShares, DKG#dkg.shares_map),
shares_results = maps:put(SharesId, {dkg_commitment:serialize(Commitment), fr:serialize(Si)}, DKG#dkg.shares_results),
shares_results = maps:put(SharesId, {dkg_commitment:serialize(Commitment), tc_fr:serialize(Si)}, DKG#dkg.shares_results),
shares_seen = [SharesId | DKG#dkg.shares_seen]
},
case output_ready(NewDKG, NewDKG#dkg.shares_acked) of
Expand Down Expand Up @@ -160,7 +160,7 @@ handle_msg(DKG=#dkg{leader = Leader}, Sender, {{vss, SharesId}, SharesMsg}) ->
%% delay ← delay(T); start timer(delay)

NewDKG = DKG#dkg{shares_map = maps:put(SharesId, NewShares, DKG#dkg.shares_map),
shares_results = maps:put(SharesId, {dkg_commitment:serialize(Commitment), fr:serialize(Si)}, DKG#dkg.shares_results),
shares_results = maps:put(SharesId, {dkg_commitment:serialize(Commitment), tc_fr:serialize(Si)}, DKG#dkg.shares_results),
shares_seen = [SharesId | DKG#dkg.shares_seen]
},
case length(NewDKG#dkg.shares_seen) == NewDKG#dkg.t + 1 andalso length(NewDKG#dkg.shares_acked) == 0 of
Expand Down Expand Up @@ -397,19 +397,19 @@ output(DKG, Shares) ->
Shard = shard(DKG, Shares),
tc_key_share:new(DKG#dkg.id - 1, PublicKeySet, Shard).

-spec output_public_key_set(#dkg{}, node_set()) -> public_key_set:pk_set().
-spec output_public_key_set(#dkg{}, node_set()) -> tc_public_key_set:pk_set().
output_public_key_set(DKG=#dkg{shares_results=R0}, Shares) ->
{[Head|Commitments], _Shares} = lists:unzip(maps:values(maps:with(Shares, R0))),
lists:foldl(fun(Commitment, Acc) ->
public_key_set:combine(Acc, dkg_commitment:public_key_set(dkg_commitment:deserialize(Commitment, DKG#dkg.commitment_cache_fun)))
tc_public_key_set:combine(Acc, dkg_commitment:public_key_set(dkg_commitment:deserialize(Commitment, DKG#dkg.commitment_cache_fun)))
end, dkg_commitment:public_key_set(dkg_commitment:deserialize(Head, DKG#dkg.commitment_cache_fun)), Commitments).

-spec shard(#dkg{}, node_set()) -> secret_key_share:sk_share().
-spec shard(#dkg{}, node_set()) -> tc_secret_key_share:sk_share().
shard(_DKG=#dkg{shares_results=R0}, Shares) ->
{_Commitments, [Head|Keys]} = lists:unzip(maps:values(maps:with(Shares, R0))),
lists:foldl(fun(Si, Acc) ->
secret_key_share:combine(Acc, secret_key_share:from_fr(fr:deserialize(Si)))
end, secret_key_share:from_fr(fr:deserialize(Head)), Keys).
tc_secret_key_share:combine(Acc, tc_secret_key_share:from_fr(tc_fr:deserialize(Si)))
end, tc_secret_key_share:from_fr(tc_fr:deserialize(Head)), Keys).

-spec leader_cap(pos_integer(), pos_integer()) -> pos_integer().
leader_cap(L, N) ->
Expand Down Expand Up @@ -673,4 +673,4 @@ status(DKG) ->
leader_cap => DKG#dkg.leader_cap}.

default_commitment_cache_fun({_Ser, _DeSer}) -> ok;
default_commitment_cache_fun(Ser) -> bicommitment:deserialize(Ser).
default_commitment_cache_fun(Ser) -> tc_bicommitment:deserialize(Ser).
30 changes: 15 additions & 15 deletions src/dkg_hybridvss.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
-type send_msg() :: {unicast, pos_integer(), {send, {session(), dkg_commitment:serialized_commitment(), binary()}}}.
-type echo_msg() :: {unicast, pos_integer(), {echo, {session(), dkg_commitment:serialized_commitment(), binary()}}}.
-type ready_msg() :: {unicast, pos_integer(), {ready, {session(), dkg_commitment:serialized_commitment(), binary()}}}.
-type result() :: {result, {session(), dkg_commitment:commitment(), fr:fr()}}.
-type result() :: {result, {session(), dkg_commitment:commitment(), tc_fr:fr()}}.
-type vss() :: #vss{}.
-type signfun() :: fun((Msg :: binary()) -> Signature :: binary()).
-type verifyfun() :: fun((Sender :: pos_integer(), Msg :: binary(), Signature :: binary()) -> boolean()).
Expand Down Expand Up @@ -70,20 +70,20 @@ init(Id, N, F, T, Session, Callback, SignFun, VerifyFun, CCacheFun) ->
%% aj(y) ← φ(j,y); send the message (Pd, τ, send, C, aj) to Pj
-spec input(VSS :: vss(), Secret :: integer()) -> {vss(), {send, [send_msg()]} | ok}.
input(VSS = #vss{session=Session={Dealer,_}, id=Id, t=T, n=N, callback=CB}, Secret) when Dealer == Id ->
BiPoly = bipoly:with_secret(Secret, T),
Commitment = dkg_commitment:new(dkg_util:allnodes(N), bipoly:commitment(BiPoly), VSS#vss.commitment_cache_fun),
BiPoly = tc_bipoly:with_secret(Secret, T),
Commitment = dkg_commitment:new(dkg_util:allnodes(N), tc_bipoly:commitment(BiPoly), VSS#vss.commitment_cache_fun),
%% only serialize this once, not in the loop below
SerializedCommitmentMatrix = dkg_commitment:matrix(Commitment),

case CB of
true ->
Msgs = lists:map(fun(Node) ->
poly:serialize(bipoly:row(BiPoly, Node))
tc_poly:serialize(tc_bipoly:row(BiPoly, Node))
end, dkg_util:allnodes(N)),
{commitment_sent(SerializedCommitmentMatrix, dkg_util:allnodes(N), store_commitment(Commitment, Id, VSS)), {send, [{callback, {send, {Session, SerializedCommitmentMatrix, Msgs}}}]}};
false ->
Msgs = lists:map(fun(Node) ->
Aj = poly:serialize(bipoly:row(BiPoly, Node)),
Aj = tc_poly:serialize(tc_bipoly:row(BiPoly, Node)),
{unicast, Node, {send, {Session, SerializedCommitmentMatrix, Aj}}}
end, dkg_util:allnodes(N)),
{commitment_sent(SerializedCommitmentMatrix, dkg_util:allnodes(N), store_commitment(Commitment, Id, VSS)), {send, Msgs}}
Expand All @@ -99,16 +99,16 @@ input(VSS, _Secret) ->
handle_msg(VSS=#vss{n=N, id=Id, session=Session, received_commitment=false, callback=CB}, Sender, {send, {Session = {Sender, _}, SerializedCommitmentMatrix0, SA}}) ->
case get_commitment(SerializedCommitmentMatrix0, VSS) of
{ok, Commitment} ->
A = poly:deserialize(SA),
A = tc_poly:deserialize(SA),
case dkg_commitment:verify_poly(Commitment, Id, A) of
true when CB == true ->
Msgs = lists:map(fun(Node) ->
fr:serialize(poly:eval(A, Node))
tc_fr:serialize(tc_poly:eval(A, Node))
end, dkg_util:allnodes(N)),
{commitment_sent(SerializedCommitmentMatrix0, dkg_util:allnodes(N), store_commitment(Commitment, Sender, echo, VSS#vss{received_commitment=true})), {send, [{callback, {echo, {Session, SerializedCommitmentMatrix0, Msgs}}}]}};
true ->
Msgs = lists:map(fun(Node) ->
{unicast, Node, {echo, {Session, maybe_send_commitment(SerializedCommitmentMatrix0, N, VSS), fr:serialize(poly:eval(A, Node))}}}
{unicast, Node, {echo, {Session, maybe_send_commitment(SerializedCommitmentMatrix0, N, VSS), tc_fr:serialize(tc_poly:eval(A, Node))}}}
end, dkg_util:allnodes(N)),
{commitment_sent(SerializedCommitmentMatrix0, dkg_util:allnodes(N), store_commitment(Commitment, Sender, echo, VSS#vss{received_commitment=true})), {send, Msgs}};
false ->
Expand Down Expand Up @@ -142,15 +142,15 @@ handle_msg(VSS=#vss{id=Id, n=N, t=T, session=Session, done=false, callback=CB},
SubShares = dkg_commitment:interpolate(NewCommitment, T, echo),
ReadyProof = construct_proof(VSS),
Msgs = lists:map(fun(Node) ->
fr:serialize(poly:eval(SubShares, Node))
tc_fr:serialize(tc_poly:eval(SubShares, Node))
end, dkg_util:allnodes(N)),
{store_commitment(NewCommitment, Sender, ready, VSS), {send, [{callback, {ready, {Session, SerializedCommitmentMatrix0, Msgs, ReadyProof}}}]}};
true ->
%% not in callback mode
SubShares = dkg_commitment:interpolate(NewCommitment, T, echo),
ReadyProof = construct_proof(VSS),
Msgs = lists:map(fun(Node) ->
{unicast, Node, {ready, {Session, maybe_send_commitment(SerializedCommitmentMatrix0, Node, VSS), fr:serialize(poly:eval(SubShares, Node)), ReadyProof}}}
{unicast, Node, {ready, {Session, maybe_send_commitment(SerializedCommitmentMatrix0, Node, VSS), tc_fr:serialize(tc_poly:eval(SubShares, Node)), ReadyProof}}}
end, dkg_util:allnodes(N)),
{store_commitment(NewCommitment, Sender, ready, VSS), {send, Msgs}};
false ->
Expand Down Expand Up @@ -191,7 +191,7 @@ handle_msg(VSS=#vss{n=N, t=T, f=F, id=Id, done=false, callback=CB}, Sender, {rea
SubShares = dkg_commitment:interpolate(NewCommitment, T, ready),
MyReadyProof = construct_proof(VSS),
Msgs = lists:map(fun(Node) ->
fr:serialize(poly:eval(SubShares, Node))
tc_fr:serialize(tc_poly:eval(SubShares, Node))
end, dkg_util:allnodes(N)),
NewVSS = store_commitment(NewCommitment, ready, VSS),
{NewVSS, {send, [{callback, {ready, {Session, SerializedCommitmentMatrix0, Msgs, MyReadyProof}}}]}};
Expand All @@ -200,7 +200,7 @@ handle_msg(VSS=#vss{n=N, t=T, f=F, id=Id, done=false, callback=CB}, Sender, {rea
SubShares = dkg_commitment:interpolate(NewCommitment, T, ready),
MyReadyProof = construct_proof(VSS),
Msgs = lists:map(fun(Node) ->
{unicast, Node, {ready, {Session, maybe_send_commitment(SerializedCommitmentMatrix0, Node, VSS), fr:serialize(poly:eval(SubShares, Node)), MyReadyProof}}}
{unicast, Node, {ready, {Session, maybe_send_commitment(SerializedCommitmentMatrix0, Node, VSS), tc_fr:serialize(tc_poly:eval(SubShares, Node)), MyReadyProof}}}
end, dkg_util:allnodes(N)),
NewVSS = store_commitment(NewCommitment, ready, VSS),
{NewVSS, {send, Msgs}};
Expand All @@ -209,7 +209,7 @@ handle_msg(VSS=#vss{n=N, t=T, f=F, id=Id, done=false, callback=CB}, Sender, {rea
maps:size(dkg_commitment:ready_proofs(NewCommitment)) == (N-T-F) of
true->
SubShares = dkg_commitment:interpolate(NewCommitment, T, ready),
SubShare = poly:eval(SubShares, 0),
SubShare = tc_poly:eval(SubShares, 0),
%% clear the commitments out of our state and return the winning one
{VSS#vss{done=true, commitments=#{}}, {result, {Session, NewCommitment, SubShare}}};
false ->
Expand Down Expand Up @@ -318,9 +318,9 @@ get_commitment(SerializedMatrix, VSS = #vss{n=N, t=T, commitment_cache_fun=Fun})
{ok, Commitment} ->
{ok, Commitment};
error ->
try bicommitment:deserialize(SerializedMatrix) of
try tc_bicommitment:deserialize(SerializedMatrix) of
BiCommitment ->
case bicommitment:degree(BiCommitment) == T of
case tc_bicommitment:degree(BiCommitment) == T of
true ->
{ok, dkg_commitment:new(dkg_util:allnodes(N), BiCommitment, SerializedMatrix, Fun)};
false ->
Expand Down
2 changes: 1 addition & 1 deletion src/dkg_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ commitment_cache_fun() ->
Self(Ser) ->
case ets:lookup(T, erlang:phash2(Ser)) of
[] ->
DeSer = bicommitment:deserialize(Ser),
DeSer = tc_bicommitment:deserialize(Ser),
ok = Self({Ser, DeSer}),
DeSer;
[{_, Res}] ->
Expand Down
Loading

0 comments on commit 81b49ff

Please sign in to comment.