Small fixes

This commit is contained in:
Sean Hinde 2023-01-13 11:31:14 +01:00
parent 55cb5526eb
commit 4f8b06713f
4 changed files with 10 additions and 15 deletions

View File

@ -1,3 +1,6 @@
// Very specific NIF just for key recovery.
// Partly from https://github.com/mbrix/libsecp256k1
#include "erl_nif.h"
#include "secp256k1_recovery.h"
@ -17,7 +20,7 @@ static ERL_NIF_TERM ok_result(ErlNifEnv* env, ERL_NIF_TERM *r)
static ERL_NIF_TERM
recover(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ERL_NIF_TERM r;
ERL_NIF_TERM r;
ErlNifBinary message, csignature;
int result;
int compressed = SECP256K1_EC_UNCOMPRESSED;
@ -36,11 +39,11 @@ recover(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
}
if (!enif_get_int(env, argv[2], &recid)) {
return error_result(env, "Recovery id invalid 0-3");
return error_result(env, "Recovery id invalid not integer 0-3");
}
if (recid < 0 || recid > 3) {
error_result(env, "Recovery id invalid 0-3x");
error_result(env, "Recovery id invalid 0-3");
}
result = secp256k1_ecdsa_recoverable_signature_parse_compact(ctx, &signature, csignature.data, recid);

View File

@ -11,10 +11,6 @@ in {
stable.stdenv
## erlang
stable.erlangR21 # OTP 21.3.5.2
## rust, required for building the NIF
stable.rustc
stable.cargo
stable.cmake
];
};
}

View File

@ -4,7 +4,8 @@
{registered, []},
{applications,
[kernel,
stdlib
stdlib,
sha3
]},
{env,[]},
{modules, []},

View File

@ -1,7 +1,5 @@
-module(ecrecover).
%% https://github.com/mbrix/libsecp256k1
%% API
-export([recover/2, recover/3]).
@ -11,10 +9,6 @@
%%=============================================================================
%% NIF API
%% GoodMsg1 = <<71,23,50,133,168,215,52,30,94,151,47,198,119,40,99,132,248,2,248,239,66,165,236,95,3,187,250,37,76,176,31,173>>.
%% GoodSig1_v = <<27,101,10,207,157,63,95,10,44,121,151,118,161,37,67,85,213,244,6,23,98,162,55,57,106,153,160,224,227,252,43,205,103,41,81,74,13,172,178,230,35,172,74,189,21,124,177,129,99,255,148,34,128,219,77,92,170,214,109,223,148,27,161,46,3>>.
%% ecrecover:recover(<<71,23,50,133,168,215,52,30,94,151,47,198,119,40,99,132,248,2,248,239,66,165,236,95,3,187,250,37,76,176,31,173>>, <<27,101,10,207,157,63,95,10,44,121,151,118,161,37,67,85,213,244,6,23,98,162,55,57,106,153,160,224,227,252,43,205,103,41,81,74,13,172,178,230,35,172,74,189,21,124,177,129,99,255,148,34,128,219,77,92,170,214,109,223,148,27,161,46,3>>).
%%
load() ->
EbinDir = filename:dirname(code:which(?MODULE)),
@ -29,6 +23,7 @@ not_loaded(Line) ->
%%=============================================================================
%% External API
-spec recover(<<_:(32*8)>>, <<_:(65*8)>>) -> <<_:(32*8)>>.
recover(Hash, <<V, Sig:64/binary>>) when V == 27; V == 28 ->
RecId = V - 27,
case recover(Hash, Sig, RecId) of
@ -41,7 +36,7 @@ recover(Hash, <<V, Sig:64/binary>>) when V == 27; V == 28 ->
recover(_Hash, _VSig) ->
<<0:256>>.
-spec recover(<<_:(32*8)>>, <<_:(65*8)>>, integer()) -> <<_:(32*8)>>.
-spec recover(<<_:(32*8)>>, <<_:(64*8)>>, integer()) -> <<_:(32*8)>>.
recover(_Hash, _Sig, _RecId) ->
not_loaded(?LINE).