Current section

23 Versions

Jump to

Compare versions

3 files changed
+19 additions
-26 deletions
  @@ -3,8 +3,8 @@
3 3 {<<"description">>,<<"Ed25519 signature functions">>}.
4 4 {<<"elixir">>,<<"~> 1.5">>}.
5 5 {<<"files">>,
6 - [<<"lib/ed25519.ex">>,<<"lib/hash.ex">>,<<"mix.exs">>,<<"README.md">>,
7 - <<"LICENSE">>]}.
6 + [<<"lib">>,<<"lib/ed25519.ex">>,<<"lib/hash.ex">>,<<"mix.exs">>,
7 + <<"README.md">>,<<"LICENSE">>]}.
8 8 {<<"licenses">>,[<<"MIT">>]}.
9 9 {<<"links">>,
10 10 [{<<"GitHub">>,<<"https://github.com/mwmiller/ed25519_ex">>},
  @@ -12,4 +12,4 @@
12 12 {<<"maintainers">>,[<<"Matt Miller">>]}.
13 13 {<<"name">>,<<"ed25519">>}.
14 14 {<<"requirements">>,[]}.
15 - {<<"version">>,<<"1.2.0">>}.
15 + {<<"version">>,<<"1.3.0">>}.
  @@ -65,7 +65,7 @@ defmodule Ed25519 do
65 65 # __using__ Macro generates the hash function at compile time, which allows the
66 66 # hashing function to be configurable without runtime overhead
67 67 use Ed25519.Hash
68 - defp hashint(m), do: m |> hash |> decodeint
68 + defp hashint(m), do: m |> hash |> :binary.decode_unsigned(:little)
69 69
70 70 # :crypto.mod_pow chokes on negative inputs, so we feed it positive values
71 71 # only and patch up the result if necessary
  @@ -93,21 +93,18 @@ defmodule Ed25519 do
93 93 {mod(x, @p), mod(y, @p)}
94 94 end
95 95
96 - defp encodeint(x), do: x |> :binary.encode_unsigned(:little)
97 -
98 96 defp encodepoint({x, y}) do
99 - y
100 - |> band(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
101 - |> bor((x &&& 1) <<< 255)
102 - |> encodeint
97 + val =
98 + y
99 + |> band(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
100 + |> bor((x &&& 1) <<< 255)
101 +
102 + <<val::little-size(256)>>
103 103 end
104 104
105 - defp decodeint(x), do: x |> :binary.decode_unsigned(:little)
106 -
107 - defp decodepoint(n) do
108 - decoded = n |> :binary.decode_unsigned(:little)
109 - xc = decoded |> bsr(255)
110 - y = decoded |> band(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
105 + defp decodepoint(<<n::little-size(256)>>) do
106 + xc = n |> bsr(255)
107 + y = n |> band(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
111 108 x = xrecover(y)
112 109
113 110 point =
  @@ -121,9 +118,6 @@ defmodule Ed25519 do
121 118
122 119 defp isoncurve({x, y}), do: (-x * x + y * y - 1 - @d * x * x * y * y) |> mod(@p) == 0
123 120
124 - defp rightsize(n, s) when byte_size(n) == s, do: n
125 - defp rightsize(n, s) when byte_size(n) < s, do: rightsize(n <> <<0>>, s)
126 -
127 121 @doc """
128 122 Sign a message
129 123
  @@ -140,12 +134,12 @@ defmodule Ed25519 do
140 134 r = hashint(:binary.part(h, 32, 32) <> m)
141 135 bigr = r |> scalarmult(@base) |> encodepoint
142 136 s = mod(r + hashint(bigr <> pk <> m) * a, @l)
143 - (bigr <> encodeint(s)) |> rightsize(64)
137 + bigr <> <<s::little-size(256)>>
144 138 end
145 139
146 - defp a_from_hash(h) do
140 + defp a_from_hash(<<h::little-size(256), _rest::binary>>) do
147 141 @t254 +
148 - (h |> :binary.part(0, 32) |> :binary.decode_unsigned(:little)
142 + (h
149 143 |> band(0xF3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8))
150 144 end
151 145
  @@ -165,11 +159,10 @@ defmodule Ed25519 do
165 159 validate a signed message
166 160 """
167 161 @spec valid_signature?(signature, binary, key) :: boolean
168 - def valid_signature?(s, m, pk) when byte_size(s) == 64 and byte_size(pk) == 32 do
169 - <<for_r::binary-size(32), for_s::binary-size(32)>> = s
162 + def valid_signature?(<<for_r::binary-size(32), s::little-size(256)>>, m, pk)
163 + when byte_size(pk) == 32 do
170 164 r = decodepoint(for_r)
171 165 a = decodepoint(pk)
172 - s = decodeint(for_s)
173 166 h = hashint(encodepoint(r) <> pk <> m)
174 167 scalarmult(s, @base) == edwards(r, scalarmult(h, a))
175 168 end
  @@ -4,7 +4,7 @@ defmodule Ed25519.Mixfile do
4 4 def project do
5 5 [
6 6 app: :ed25519,
7 - version: "1.2.0",
7 + version: "1.3.0",
8 8 elixir: "~> 1.5",
9 9 name: "Ed25519",
10 10 source_url: "https://github.com/mwmiller/ed25519_ex",