-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module to generate transition tables
- Loading branch information
1 parent
3c902aa
commit 47218fa
Showing
4 changed files
with
65 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule Nilsimsa.Transition do | ||
@moduledoc """ | ||
A module for generating transition tables. | ||
A transition table provides a deterministic, yet pseudorandom, way of mapping | ||
input bytes to output hash chunks. | ||
""" | ||
|
||
import Bitwise | ||
|
||
@doc """ | ||
Generate a transition table. | ||
iex> Transition.generate(53) |> Enum.take(3) | ||
[2, 214, 158] | ||
""" | ||
def generate(target), | ||
do: build(target, 0, 0, []) | ||
|
||
# Private | ||
|
||
defp build(_target, 256, _j, tran), | ||
do: Enum.reverse(tran) | ||
|
||
defp build(target, i, j, tran) do | ||
j = band(j * target + 1, 255) | ||
j = if j * 2 > 255, do: j * 2 - 255, else: j * 2 | ||
j = handle_collision(j, tran, 0) | ||
|
||
build(target, i + 1, j, [j | tran]) | ||
end | ||
|
||
defp handle_collision(j, _tran, 256), | ||
do: j | ||
|
||
defp handle_collision(j, tran, i) do | ||
case Enum.at(tran, i) do | ||
^j -> handle_collision(band(j + 1, 255), tran, 0) | ||
_ -> handle_collision(j, tran, i + 1) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Nilsimsa.TransitionTest do | ||
use ExUnit.Case, async: true | ||
|
||
import Bitwise | ||
|
||
alias Nilsimsa.Transition | ||
|
||
doctest Nilsimsa.Transition | ||
|
||
test "should xor to 0" do | ||
[v | rest] = Transition.generate(53) | ||
|
||
v = Enum.reduce(rest, v, &bxor(&2, &1)) | ||
|
||
assert v == 0 | ||
end | ||
end |