Skip to content
Open
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
18 changes: 10 additions & 8 deletions lib/lightning_network/invoice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ defmodule Bitcoinex.LightningNetwork.Invoice do
defp validate_and_parse_signature_data(destination, hrp, invoice_data, signature_data)
when is_list(invoice_data) and is_list(signature_data) do
with {:ok, signature_data_in_byte} <- Bech32.convert_bits(signature_data, 5, 8),
{signature, [recoveryId]} = split_at(signature_data_in_byte, -1),
{signature, [recovery_id]} = split_at(signature_data_in_byte, -1),
{:ok, invoice_data_in_byte} <- Bech32.convert_bits(invoice_data, 5, 8) do
to_sign = (hrp |> :erlang.binary_to_list()) ++ invoice_data_in_byte
signature = signature |> byte_list_to_binary
Expand All @@ -151,7 +151,7 @@ defmodule Bitcoinex.LightningNetwork.Invoice do
# TODO if destination exist from tagged field, we dun need to recover but to verify it with signature
# but that require convert lg sig before using secp256k1 to verify it
# TODO refactor too nested
case Bitcoinex.Secp256k1.Ecdsa.ecdsa_recover_compact(hash, signature, recoveryId) do
case Bitcoinex.Secp256k1.Ecdsa.ecdsa_recover_compact(hash, signature, recovery_id) do
{:ok, pubkey} ->
if is_nil(destination) or destination == pubkey do
{:ok, pubkey}
Expand Down Expand Up @@ -401,10 +401,11 @@ defmodule Bitcoinex.LightningNetwork.Invoice do

17 ->
case Bech32.convert_bits(rest, 5, 8, false) do
{:ok, pubKeyHash} ->
{:ok, pub_key_hash} ->
{:ok,
Bitcoinex.Address.encode(
pubKeyHash |> :binary.list_to_bin(),
pub_key_hash
|> :binary.list_to_bin()
|> Bitcoinex.Address.encode(
network,
:p2pkh
)}
Expand All @@ -415,10 +416,11 @@ defmodule Bitcoinex.LightningNetwork.Invoice do

18 ->
case Bech32.convert_bits(rest, 5, 8, false) do
{:ok, scriptHash} ->
{:ok, script_hash} ->
{:ok,
Bitcoinex.Address.encode(
scriptHash |> :binary.list_to_bin(),
script_hash
|> :binary.list_to_bin()
|> Bitcoinex.Address.encode(
network,
:p2sh
)}
Expand Down
56 changes: 28 additions & 28 deletions lib/secp256k1/math.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,29 @@ defmodule Bitcoinex.Secp256k1.Math do
"""
def multiply(p, n) when is_point(p) and is_integer(n) do
p
|> toJacobian()
|> jacobianMultiply(n)
|> fromJacobian()
|> to_jacobian()
|> jacobian_multiply(n)
|> from_jacobian()
end

@doc """
add accepts points p and q and,
does jacobian addition to return resulting point.
"""
def add(p, q) when is_point(p) and is_point(q) do
jacobianAdd(toJacobian(p), toJacobian(q))
|> fromJacobian()
p
|> to_jacobian()
|> jacobian_add(to_jacobian(q))
|> from_jacobian()
end

# Convert our point P to jacobian coordinates.
defp toJacobian(p) do
defp to_jacobian(p) do
%Point{x: p.x, y: p.y, z: 1}
end

# Convert our jacobian coordinates to a point P on secp256k1 curve.
defp fromJacobian(p) do
defp from_jacobian(p) do
z = inv(p.z, Params.curve().p)

%Point{
Expand All @@ -110,7 +112,7 @@ defmodule Bitcoinex.Secp256k1.Math do
# double Point P to get point P + P
# We use the dbl-1998-cmo-2 doubling formula.
# For reference, http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html.
defp jacobianDouble(p) do
defp jacobian_double(p) do
if p.y == 0 do
%Point{x: 0, y: 0, z: 0}
else
Expand Down Expand Up @@ -159,7 +161,7 @@ defmodule Bitcoinex.Secp256k1.Math do
# add points P and Q to get P + Q
# We use the add-1998-cmo-2 addition formula.
# For reference, http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html.
defp jacobianAdd(p, q) do
defp jacobian_add(p, q) do
if p.y == 0 do
q
else
Expand Down Expand Up @@ -190,7 +192,7 @@ defmodule Bitcoinex.Secp256k1.Math do
if s1 != s2 do
%Point{x: 0, y: 0, z: 1}
else
jacobianDouble(p)
jacobian_double(p)
end
else
# H = U2 - U1
Expand Down Expand Up @@ -236,19 +238,15 @@ defmodule Bitcoinex.Secp256k1.Math do
end

# multply point P with scalar n
defp jacobianMultiply(_p, n) when n == 0 do
%Point{x: 0, y: 0, z: 1}
end
defp jacobian_multiply(_p, 0), do: %Point{x: 0, y: 0, z: 1}

defp jacobianMultiply(p, n) when n == 1 do
if p.y == 0 do
%Point{x: 0, y: 0, z: 1}
else
p
end
defp jacobian_multiply(p, 1) do
if p.y == 0,
do: %Point{x: 0, y: 0, z: 1},
else: p
end

defp jacobianMultiply(p, n)
defp jacobian_multiply(p, n)
# This integer is n, the integer order of G for secp256k1.
# Unfortunately cannot call Params.curve.n to get the curve order integer,
# so instead, it is pasted it here.
Expand All @@ -259,26 +257,28 @@ defmodule Bitcoinex.Secp256k1.Math do
if p.y == 0 do
%Point{x: 0, y: 0, z: 1}
else
jacobianMultiply(p, modulo(n, Params.curve().n))
jacobian_multiply(p, modulo(n, Params.curve().n))
end
end

defp jacobianMultiply(p, n) when rem(n, 2) == 0 do
defp jacobian_multiply(p, n) when rem(n, 2) == 0 do
if p.y == 0 do
%Point{x: 0, y: 0, z: 1}
else
jacobianMultiply(p, div(n, 2))
|> jacobianDouble()
p
|> jacobian_multiply(div(n, 2))
|> jacobian_double()
end
end

defp jacobianMultiply(p, n) do
defp jacobian_multiply(p, n) do
if p.y == 0 do
%Point{x: 0, y: 0, z: 1}
else
jacobianMultiply(p, div(n, 2))
|> jacobianDouble()
|> jacobianAdd(p)
p
|> jacobian_multiply(div(n, 2))
|> jacobian_double()
|> jacobian_add(p)
end
end
end