Skip to content

feat: derive Keystore public key from secret key #1067

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

Merged
merged 10 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
15 changes: 15 additions & 0 deletions lib/bls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ defmodule Bls do
:erlang.nif_error(:nif_not_loaded)
end

@spec derive_pubkey(privkey()) :: {:ok, pubkey()} | {:error, any()}
def derive_pubkey(_private_key) do
:erlang.nif_error(:nif_not_loaded)
end
##### Helpers #####
@doc """
Same as ``Bls.verify``, but treats errors as invalid signatures.
Expand All @@ -77,4 +81,15 @@ defmodule Bls do
{:error, _} -> false
end
end

@doc """
Converts private to public key
"""
@spec derive_pubkey?(privkey()) :: {:ok, pubkey()} | {:error, any()}
def derive_pubkey?(private_key) do
case Bls.derive_pubkey(private_key) do
{:ok, pubkey} -> pubkey
{:error, _} -> false
end
end
end
3 changes: 3 additions & 0 deletions lib/keystore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ defmodule Keystore do
privkey = decrypt!(decoded_json["crypto"], password)
# TODO: derive from privkey and validate with this pubkey
pubkey = Map.fetch!(decoded_json, "pubkey") |> parse_binary!()
if Bls.derive_pubkey(privkey) == pubkey do
raise("Keystore secret and public keys don't form a valid pair")
end
{pubkey, privkey}
end

Expand Down
14 changes: 13 additions & 1 deletion native/bls_nif/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ fn key_validate<'env>(public_key: Binary) -> Result<bool, String> {

Ok(true)
}
#[rustler::nif]
fn derive_pubkey<'env>(env: Env<'env>,private_key:Binary) -> Result<Binary<'env>, String> {
let sk = match SecretKey::deserialize(private_key.as_slice()) {
Ok(sk) => sk,
Err(e) => return Err(format!("{:?}", e)),
};
let public_key = sk.public_key();
let public_key_bytes = public_key.serialize();

Ok(bytes_to_binary(env, &public_key_bytes))
}

rustler::init!(
"Elixir.Bls",
Expand All @@ -172,6 +183,7 @@ rustler::init!(
eth_fast_aggregate_verify,
eth_aggregate_pubkeys,
verify,
key_validate
key_validate,
derive_pubkey
]
);
9 changes: 9 additions & 0 deletions test/unit/bls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ defmodule BlsTest do
assert Bls.key_validate(invalid_public_key) == {:error, "BlstError(BLST_BAD_ENCODING)"}
end
end

describe "Private to public key" do
test "return the correct public key for a private key" do
valid_public_key = Base.decode16!("8abb15ca57942b6225af4710bbb74ce8466e99fdc2264d9ffd3b335c7396667e45f537ff1f75ed5afa00585db274f3b6",case: :mixed)
private_key = Base.decode16!("18363054f52f3f1fdc9ae50d271de853c582c652ebe8dd0f261da3b00cd98984", case: :mixed)
assert Bls.derive_pubkey(private_key)=={:ok, valid_public_key}
end
end

end