|
1 | 1 | defmodule BlsEx do |
| 2 | + @moduledoc """ |
| 3 | + BlsEx provides utility to leverage BLS signatures through WASM |
| 4 | + """ |
2 | 5 | alias __MODULE__.Keystore |
3 | 6 |
|
4 | 7 | @doc """ |
5 | | - Creates a new BLS keystore |
| 8 | + Verifies a single BLS signature |
6 | 9 |
|
7 | 10 | ## Examples |
8 | 11 |
|
9 | | - iex> {:ok, pid} = BlsEx.new_keystore("myseed") |
| 12 | + iex> {:ok, pid} = BlsEx.Keystore.start_link(seed: "myseed") |
10 | 13 | iex> {:ok, public_key} = BlsEx.Keystore.get_public_key(pid) |
11 | 14 | iex> {:ok, signature} = BlsEx.Keystore.sign(pid, "hello") |
12 | | - """ |
13 | | - @spec new_keystore(binary()) :: GenServer.on_start() |
14 | | - def new_keystore(seed) do |
15 | | - Keystore.start_link(seed: seed) |
16 | | - end |
17 | | - |
18 | | - @doc """ |
19 | | - Verifies a single BLS signature |
| 15 | + iex> BlsEx.verify_signature(public_key, "hello", signature) |
| 16 | + {:ok, true} |
20 | 17 | """ |
21 | 18 | @spec verify_signature(public_key :: binary(), message :: binary(), signature :: binary()) :: |
22 | 19 | {:ok, boolean()} | {:error, any()} |
23 | 20 | defdelegate verify_signature(public_key, message, signature), to: __MODULE__.StandaloneWasm |
24 | 21 |
|
25 | 22 | @doc """ |
26 | 23 | Aggregate BLS signatures |
| 24 | +
|
| 25 | + ## Examples |
| 26 | +
|
| 27 | + iex> {:ok, pid} = BlsEx.Keystore.start_link(seed: "myseed") |
| 28 | + iex> {:ok, public_key1} = BlsEx.Keystore.get_public_key(pid) |
| 29 | + iex> {:ok, signature1} = BlsEx.Keystore.sign(pid, "hello") |
| 30 | + iex> {:ok, pid2} = BlsEx.Keystore.start_link(seed: "myseed2") |
| 31 | + iex> {:ok, public_key2} = BlsEx.Keystore.get_public_key(pid2) |
| 32 | + iex> {:ok, signature2} = BlsEx.Keystore.sign(pid2, "hello") |
| 33 | + iex> BlsEx.aggregate_signatures([signature1, signature2], [public_key1, public_key2]) |
27 | 34 | """ |
28 | 35 | @spec aggregate_signatures(signatures :: list(binary()), public_keys :: list(binary())) :: |
29 | 36 | {:ok, binary()} | {:error, any()} |
30 | 37 | defdelegate aggregate_signatures(signatures, public_keys), to: __MODULE__.StandaloneWasm |
31 | 38 |
|
32 | 39 | @doc """ |
33 | 40 | Aggregate BLS public keys |
| 41 | +
|
| 42 | + ## Examples |
| 43 | +
|
| 44 | + iex> {:ok, pid} = BlsEx.Keystore.start_link(seed: "myseed") |
| 45 | + iex> {:ok, public_key1} = BlsEx.Keystore.get_public_key(pid) |
| 46 | + iex> {:ok, pid2} = BlsEx.Keystore.start_link(seed: "myseed2") |
| 47 | + iex> {:ok, public_key2} = BlsEx.Keystore.get_public_key(pid2) |
| 48 | + iex> {:ok, _aggregated_public_key} = BlsEx.aggregated_public_keys([public_key1, public_key2]) |
34 | 49 | """ |
35 | 50 | @spec aggregated_public_keys(public_keys :: list(binary())) :: {:ok, binary()} | {:error, any()} |
36 | 51 | defdelegate aggregated_public_keys(public_keys), to: __MODULE__.StandaloneWasm |
37 | 52 |
|
38 | 53 | @doc """ |
39 | 54 | Verifies an aggregated BLS signature |
| 55 | +
|
| 56 | + ## Examples |
| 57 | +
|
| 58 | + iex> {:ok, pid} = BlsEx.Keystore.start_link(seed: "myseed") |
| 59 | + iex> {:ok, public_key1} = BlsEx.Keystore.get_public_key(pid) |
| 60 | + iex> {:ok, signature1} = BlsEx.Keystore.sign(pid, "hello") |
| 61 | + iex> {:ok, pid2} = BlsEx.Keystore.start_link(seed: "myseed2") |
| 62 | + iex> {:ok, public_key2} = BlsEx.Keystore.get_public_key(pid2) |
| 63 | + iex> {:ok, signature2} = BlsEx.Keystore.sign(pid2, "hello") |
| 64 | + iex> {:ok, aggregated_signature} = BlsEx.aggregate_signatures([signature1, signature2], [public_key1, public_key2]) |
| 65 | + iex> BlsEx.verify_aggregated_signature( [public_key1, public_key2], "hello", aggregated_signature) |
| 66 | + {:ok, true} |
40 | 67 | """ |
41 | 68 | @spec verify_aggregated_signature(list(binary()), binary(), binary()) :: |
42 | 69 | {:ok, boolean()} | {:error, any()} |
43 | 70 | defdelegate verify_aggregated_signature(public_keys, message, signature), |
44 | 71 | to: __MODULE__.StandaloneWasm |
45 | | - |
46 | | - def test() do |
47 | | - {:ok, keystore1} = new_keystore("test") |
48 | | - # {:ok, keystore2} = new_keystore("test2") |
49 | | - # {:ok, public_key1} = Keystore.get_public_key(keystore1) |
50 | | - # {:ok, public_key2} = Keystore.get_public_key(keystore2) |
51 | | - # {:ok, signature1} = Keystore.sign(keystore1, "hello") |
52 | | - # # # IO.inspect(Base.encode16(signature1)) |
53 | | - # {:ok, signature2} = Keystore.sign(keystore2, "hello") |
54 | | - # # IO.inspect(Base.encode16(signature2)) |
55 | | - |
56 | | - # verify_signature(public_key1, "hello", signature1) |
57 | | - # # |
58 | | - # {:ok, agg_signature} = |
59 | | - # aggregate_signatures([signature1, signature2], [public_key1, public_key2]) |
60 | | - |
61 | | - # # # IO.inspect(Base.encode16(agg_signature)) |
62 | | - # verify_aggregated_signature([public_key1, public_key2], "hello", agg_signature) |
63 | | - end |
64 | 72 | end |
0 commit comments