Packages

An Elixir cryptocurrency library which contains algorithms used in Bitcoin, Ethereum, and other blockchains. Includes a rich cryptography, number theory, and general mathematics class library.

Current section

22 Versions

Jump to

Compare versions

4 files changed
+14 additions
-9 deletions
  @@ -12,7 +12,7 @@ https://hexdocs.pm/caustic/
12 12 ```elixir
13 13 def deps do
14 14 [
15 - {:caustic, "~> 0.1.3"}
15 + {:caustic, "~> 0.1.7"}
16 16 ]
17 17 end
18 18 ```
  @@ -12,4 +12,4 @@
12 12 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/agro1986/caustic">>}]}.
13 13 {<<"name">>,<<"caustic">>}.
14 14 {<<"requirements">>,[]}.
15 - {<<"version">>,<<"0.1.6">>}.
15 + {<<"version">>,<<"0.1.7">>}.
  @@ -72,6 +72,7 @@ defmodule Caustic.ECPoint do
72 72 {-1, -1, 5, 7}
73 73 iex> Caustic.ECPoint.add(Caustic.ECPoint.make(-1, 1, 5, 7), Caustic.ECPoint.make(-1, -1, 5, 7))
74 74 {nil, nil, 5, 7}
75 +
75 76 iex> p = 223
76 77 iex> a = {0, p}
77 78 iex> b = {7, p}
  @@ -83,6 +84,7 @@ defmodule Caustic.ECPoint do
83 84 iex> p2 = Caustic.ECPoint.make(x2, y2, a, b)
84 85 iex> Caustic.ECPoint.add(p1, p2)
85 86 {{170, 223}, {142, 223}, {0, 223}, {7, 223}}
87 +
86 88 iex> p = 223
87 89 iex> a = {0, p}
88 90 iex> b = {7, p}
  @@ -94,7 +96,6 @@ defmodule Caustic.ECPoint do
94 96 """
95 97 def add(p1, p2 = _inf = {nil, nil, _a, _b}), do: add(p2, p1)
96 98 def add({nil, nil, a, b}, {x, y, a, b}), do: {x, y, a, b}
97 - def add({x, y1, a, b}, {x, y2, a, b}) when y1 == -y2, do: infinity(a, b)
98 99 def add({x, y, a, b}, {x, y, a, b}) do
99 100 s = F.div(F.add(F.mul(F.mul(x, x), 3), a), F.mul(y, 2)) # (3 * x * x + a) / (2 * y)
100 101 x3 = F.sub(F.mul(s, s), F.mul(x, 2)) # s * s - 2 * x
  @@ -102,11 +103,15 @@ defmodule Caustic.ECPoint do
102 103 {x3, y3, a, b}
103 104 end
104 105 def add({x1, y1, a, b}, {x2, y2, a, b}) do
105 - # https://github.com/jimmysong/programmingbitcoin/blob/master/ch02.asciidoc
106 - s = F.div(F.sub(y2, y1), F.sub(x2, x1)) # (y2 - y1) / (x2 - x1)
107 - x3 = F.sub(F.sub(F.mul(s, s), x1), x2) # s * s - x1 - x2
108 - y3 = F.sub(F.mul(s, F.sub(x1, x3)), y1) # s * (x1 - x3) - y1
109 - {x3, y3, a, b}
106 + if F.eq?(y1, F.neg(y2)) do
107 + infinity(a, b)
108 + else
109 + # https://github.com/jimmysong/programmingbitcoin/blob/master/ch02.asciidoc
110 + s = F.div(F.sub(y2, y1), F.sub(x2, x1)) # (y2 - y1) / (x2 - x1)
111 + x3 = F.sub(F.sub(F.mul(s, s), x1), x2) # s * s - x1 - x2
112 + y3 = F.sub(F.mul(s, F.sub(x1, x3)), y1) # s * (x1 - x3) - y1
113 + {x3, y3, a, b}
114 + end
110 115 end
111 116
112 117 @doc """
  @@ -6,7 +6,7 @@ defmodule Caustic.MixProject do
6 6 app: :caustic,
7 7 name: "Caustic",
8 8 source_url: "https://github.com/agro1986/caustic",
9 - version: "0.1.6",
9 + version: "0.1.7",
10 10 elixir: "~> 1.7",
11 11 description: description(),
12 12 build_embedded: Mix.env == :prod,