Skip to content

Commit

Permalink
Expose the vdf_sha2_nif to help with debugging a Rust implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPiechota committed Oct 11, 2023
1 parent d2c5fd1 commit 4fd88d2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
50 changes: 30 additions & 20 deletions apps/arweave/c_src/vdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,36 @@ void _vdf_sha2(unsigned char* saltBuffer, unsigned char* seed, unsigned char* ou
unsigned char* locIn = checkpointIdx == 0 ? seed : (outCheckpoint + VDF_SHA_HASH_SIZE*(checkpointIdx-1));
unsigned char* locOut = checkpointIdx == checkpointCount ? out : (outCheckpoint + VDF_SHA_HASH_SIZE*checkpointIdx);

{
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, saltBuffer, SALT_SIZE);
SHA256_Update(&sha256, locIn, VDF_SHA_HASH_SIZE); // -1 memcpy
SHA256_Final(tempOut, &sha256);
}
for(int i = 2; i < hashingIterations; i++) {
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, saltBuffer, SALT_SIZE);
SHA256_Update(&sha256, tempOut, VDF_SHA_HASH_SIZE);
SHA256_Final(tempOut, &sha256);
}
{
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, saltBuffer, SALT_SIZE);
SHA256_Update(&sha256, tempOut, VDF_SHA_HASH_SIZE);
SHA256_Final(locOut, &sha256);
if (hashingIterations > 1) {
{
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, saltBuffer, SALT_SIZE);
SHA256_Update(&sha256, locIn, VDF_SHA_HASH_SIZE); // -1 memcpy
SHA256_Final(tempOut, &sha256);
}
for(int i = 2; i < hashingIterations; i++) {
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, saltBuffer, SALT_SIZE);
SHA256_Update(&sha256, tempOut, VDF_SHA_HASH_SIZE);
SHA256_Final(tempOut, &sha256);
}
{
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, saltBuffer, SALT_SIZE);
SHA256_Update(&sha256, tempOut, VDF_SHA_HASH_SIZE);
SHA256_Final(locOut, &sha256);
}
} else {
{
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, saltBuffer, SALT_SIZE);
SHA256_Update(&sha256, locIn, VDF_SHA_HASH_SIZE); // -1 memcpy
SHA256_Final(locOut, &sha256);
}
}
long_add(saltBuffer, 1);
}
Expand Down
6 changes: 5 additions & 1 deletion apps/arweave/src/ar.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-behaviour(application).

-export([main/0, main/1, create_wallet/0, create_wallet/1,
benchmark_packing/1, benchmark_packing/0, benchmark_vdf/0, start/0,
benchmark_packing/1, benchmark_packing/0, benchmark_vdf/0, manual_vdf/0, start/0,
start/1, start/2, stop/1, stop_dependencies/0, tests/0, tests/1, tests/2, test_ipfs/0,
docs/0, start_for_tests/0, shutdown/1, console/1, console/2]).

Expand Down Expand Up @@ -721,6 +721,10 @@ benchmark_vdf() ->
ar_bench_vdf:run_benchmark(),
erlang:halt().

manual_vdf() ->
ar_manual_vdf:manual_vdf(),
erlang:halt().

shutdown([NodeName]) ->
rpc:cast(NodeName, init, stop, []).

Expand Down
21 changes: 21 additions & 0 deletions apps/arweave/src/ar_manual_vdf.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(ar_manual_vdf).

-export([manual_vdf/0]).

-include_lib("arweave/include/ar_vdf.hrl").

manual_vdf() ->
PrevOutput = ar_util:decode(<<"W-5B1F7rYIayc_1d3nq3bsLanXNympgA3tkIXOT0EF8">>),
StepNumber = 21499625,
Salt = ar_vdf:step_number_to_salt_number(StepNumber-1),
Salt2 = << Salt:256 >>,
CheckpointCount = 0,
SkipCheckpointCount = 0,
Iterations = 1,
{ok, Output, _CheckpointsBuffer} =
ar_mine_randomx:vdf_sha2_nif(
Salt2, PrevOutput, CheckpointCount, SkipCheckpointCount, Iterations),
io:format("~n~n"),
io:format("Salt: ~p~n", [ar_util:encode(Salt2)]),
io:format("Output: ~p~n", [ar_util:encode(Output)]).

17 changes: 17 additions & 0 deletions bin/manual-vdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR="$(dirname "$0")"
cd "$SCRIPT_DIR/.."

echo Building dependencies...
./rebar3 compile

ERL="erl"
# ERL="$ERL_TOP/bin/cerl -debug"

$ERL +MBas aobf +MBlmbcs 512 +A100 +SDio100 +Bi -pa `./rebar3 path` \
-config config/sys.config -args_file config/vm.args.dev \
-run ar manual_vdf ${@:1}

0 comments on commit 4fd88d2

Please sign in to comment.