Current section
23 Versions
Jump to
Current section
23 Versions
Compare versions
3
files changed
+24
additions
-11
deletions
| @@ -2,13 +2,13 @@ | |
| 2 2 | [{<<"GitHub">>,<<"https://github.com/mwmiller/ed25519_ex">>}, |
| 3 3 | {<<"Info">>,<<"http://ed25519.cr.yp.to">>}]}. |
| 4 4 | {<<"name">>,<<"ed25519">>}. |
| 5 | - {<<"version">>,<<"1.4.4">>}. |
| 5 | + {<<"version">>,<<"1.5.0">>}. |
| 6 6 | {<<"description">>,<<"Ed25519 signature functions">>}. |
| 7 7 | {<<"elixir">>,<<"~> 1.7">>}. |
| 8 | + {<<"app">>,<<"ed25519">>}. |
| 8 9 | {<<"files">>, |
| 9 10 | [<<"lib">>,<<"lib/hash.ex">>,<<"lib/ed25519.ex">>,<<"mix.exs">>, |
| 10 11 | <<"README.md">>,<<"LICENSE">>]}. |
| 11 | - {<<"app">>,<<"ed25519">>}. |
| 12 12 | {<<"licenses">>,[<<"MIT">>]}. |
| 13 13 | {<<"requirements">>,[]}. |
| 14 14 | {<<"build_tools">>,[<<"mix">>]}. |
| @@ -98,7 +98,7 @@ defmodule Ed25519 do | |
| 98 98 | <<val::little-size(256)>> |
| 99 99 | end |
| 100 100 | |
| 101 | - defp decodepoint(<<n::little-size(256)>>) do |
| 101 | + defp decodepoint!(<<n::little-size(256)>>) do |
| 102 102 | xc = n |> bsr(255) |
| 103 103 | y = n |> band(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) |
| 104 104 | x = xrecover(y) |
| @@ -109,12 +109,25 @@ defmodule Ed25519 do | |
| 109 109 | _ -> {@p - x, y} |
| 110 110 | end |
| 111 111 | |
| 112 | - if isoncurve(point), do: point, else: raise("Point off Edwards curve") |
| 112 | + case is_on_curve?(point) && is_on_main_subgroup?(point) do |
| 113 | + true -> |
| 114 | + point |
| 115 | + |
| 116 | + false -> |
| 117 | + raise("Point is not in the curve main subgroup") |
| 118 | + end |
| 113 119 | end |
| 114 120 | |
| 115 | - defp decodepoint(_), do: raise("Provided value not a key") |
| 121 | + defp decodepoint!(_), do: raise("Provided value not a key") |
| 116 122 | |
| 117 | - defp isoncurve({x, y}), do: (-x * x + y * y - 1 - @d * x * x * y * y) |> mod(@p) == 0 |
| 123 | + defp is_on_curve?({x, y}), do: (-x * x + y * y - 1 - @d * x * x * y * y) |> mod(@p) == 0 |
| 124 | + |
| 125 | + defp is_on_main_subgroup?(point) do |
| 126 | + case scalarmult(@l, point) do |
| 127 | + {0, 1} -> true |
| 128 | + _ -> false |
| 129 | + end |
| 130 | + end |
| 118 131 | |
| 119 132 | @doc """ |
| 120 133 | Returns whether a given `key` lies on the ed25519 curve. |
| @@ -122,7 +135,7 @@ defmodule Ed25519 do | |
| 122 135 | @spec on_curve?(key) :: boolean |
| 123 136 | def on_curve?(key) do |
| 124 137 | try do |
| 125 | - decodepoint(key) |
| 138 | + decodepoint!(key) |
| 126 139 | true |
| 127 140 | rescue |
| 128 141 | _error -> false |
| @@ -179,8 +192,8 @@ defmodule Ed25519 do | |
| 179 192 | @spec valid_signature?(signature, binary, key) :: boolean |
| 180 193 | def valid_signature?(<<for_r::binary-size(32), s::little-size(256)>>, m, pk) |
| 181 194 | when byte_size(pk) == 32 do |
| 182 | - r = decodepoint(for_r) |
| 183 | - a = decodepoint(pk) |
| 195 | + r = decodepoint!(for_r) |
| 196 | + a = decodepoint!(pk) |
| 184 197 | h = hashint(encodepoint(r) <> pk <> m) |
| 185 198 | scalarmult(s, @base) == edwards(r, scalarmult(h, a)) |
| 186 199 | end |
| @@ -236,7 +249,7 @@ defmodule Ed25519 do | |
| 236 249 | def to_curve25519(key, which) |
| 237 250 | |
| 238 251 | def to_curve25519(ed_public_key, :public) do |
| 239 | - {_, y} = decodepoint(ed_public_key) |
| 252 | + {_, y} = decodepoint!(ed_public_key) |
| 240 253 | u = mod((1 + y) * inv(1 - y), @p) |
| 241 254 | <<u::little-size(256)>> |
| 242 255 | end |
| @@ -4,7 +4,7 @@ defmodule Ed25519.Mixfile do | |
| 4 4 | def project do |
| 5 5 | [ |
| 6 6 | app: :ed25519, |
| 7 | - version: "1.4.4", |
| 7 | + version: "1.5.0", |
| 8 8 | elixir: "~> 1.7", |
| 9 9 | name: "Ed25519", |
| 10 10 | source_url: "https://github.com/mwmiller/ed25519_ex", |