Skip to content

Commit

Permalink
remove the reencrypt_legacy_composit nif
Browse files Browse the repository at this point in the history
in preparation for increasing the randomx dataset for composite packing
  • Loading branch information
JamesPiechota committed Sep 2, 2024
1 parent f3df411 commit ff6dd4c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 177 deletions.
90 changes: 3 additions & 87 deletions apps/arweave/c_src/ar_mine_randomx.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ static ErlNifFunc nif_funcs[] = {
ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"randomx_decrypt_composite_sub_chunk_nif", 10, randomx_decrypt_composite_sub_chunk_nif,
ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"randomx_reencrypt_legacy_to_composite_chunk_nif", 11,
randomx_reencrypt_legacy_to_composite_chunk_nif, ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"randomx_reencrypt_composite_to_composite_chunk_nif", 13,
randomx_reencrypt_composite_to_composite_chunk_nif, ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"randomx_reencrypt_composite_chunk_nif", 13,
randomx_reencrypt_composite_chunk_nif, ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"vdf_sha2_nif", 5, vdf_sha2_nif, ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"vdf_parallel_sha_verify_with_reset_nif", 10, vdf_parallel_sha_verify_with_reset_nif,
ERL_NIF_DIRTY_JOB_CPU_BOUND}
Expand Down Expand Up @@ -971,89 +969,7 @@ static ERL_NIF_TERM randomx_decrypt_composite_sub_chunk_nif(
return ok_tuple(envPtr, decryptedSubChunkTerm);
}

static ERL_NIF_TERM randomx_reencrypt_legacy_to_composite_chunk_nif(
ErlNifEnv* envPtr,
int argc,
const ERL_NIF_TERM argv[]
) {
int decryptRandomxRoundCount, encryptRandomxRoundCount;
int jitEnabled, largePagesEnabled, hardwareAESEnabled;
int subChunkCount, iterations;
struct state* statePtr;
ErlNifBinary decryptKey;
ErlNifBinary encryptKey;
ErlNifBinary inputChunk;

if (argc != 11) {
return enif_make_badarg(envPtr);
}
if (!enif_get_resource(envPtr, argv[0], stateType, (void**) &statePtr)) {
return error(envPtr, "failed to read state");
}
if (!enif_inspect_binary(envPtr, argv[1], &decryptKey)) {
return enif_make_badarg(envPtr);
}
if (!enif_inspect_binary(envPtr, argv[2], &encryptKey)) {
return enif_make_badarg(envPtr);
}
if (!enif_inspect_binary(envPtr, argv[3], &inputChunk) ||
inputChunk.size != MAX_CHUNK_SIZE) {
return enif_make_badarg(envPtr);
}
if (!enif_get_int(envPtr, argv[4], &jitEnabled)) {
return enif_make_badarg(envPtr);
}
if (!enif_get_int(envPtr, argv[5], &largePagesEnabled)) {
return enif_make_badarg(envPtr);
}
if (!enif_get_int(envPtr, argv[6], &hardwareAESEnabled)) {
return enif_make_badarg(envPtr);
}
if (!enif_get_int(envPtr, argv[7], &decryptRandomxRoundCount)) {
return enif_make_badarg(envPtr);
}
if (!enif_get_int(envPtr, argv[8], &encryptRandomxRoundCount)) {
return enif_make_badarg(envPtr);
}
if (!enif_get_int(envPtr, argv[9], &iterations) ||
iterations < 1) {
return enif_make_badarg(envPtr);
}
if (!enif_get_int(envPtr, argv[10], &subChunkCount) ||
subChunkCount < 1 ||
MAX_CHUNK_SIZE % subChunkCount != 0 ||
(MAX_CHUNK_SIZE / subChunkCount) % 64 != 0 ||
subChunkCount > (MAX_CHUNK_SIZE / 64)) {
return enif_make_badarg(envPtr);
}

int isRandomxReleased;
randomx_vm *vmPtr = create_vm(statePtr, (statePtr->mode == HASHING_MODE_FAST),
jitEnabled, largePagesEnabled, hardwareAESEnabled, &isRandomxReleased);
if (vmPtr == NULL) {
if (isRandomxReleased != 0) {
return error(envPtr, "state has been released");
}
return error(envPtr, "randomx_create_vm failed");
}

unsigned char decryptedChunk[MAX_CHUNK_SIZE];
ERL_NIF_TERM decryptedChunkTerm = decrypt_chunk(envPtr, vmPtr,
decryptKey.data, decryptKey.size, inputChunk.data, inputChunk.size,
decryptedChunk, inputChunk.size, decryptRandomxRoundCount);
ErlNifBinary decryptedChunkBin;
if (!enif_inspect_binary(envPtr, decryptedChunkTerm, &decryptedChunkBin)) {
destroy_vm(statePtr, vmPtr);
return enif_make_badarg(envPtr);
}
ERL_NIF_TERM reencryptedChunkTerm = encrypt_composite_chunk(envPtr, vmPtr, &encryptKey,
&decryptedChunkBin, subChunkCount, iterations, encryptRandomxRoundCount,
jitEnabled, largePagesEnabled, hardwareAESEnabled);
destroy_vm(statePtr, vmPtr);
return ok_tuple2(envPtr, reencryptedChunkTerm, decryptedChunkTerm);
}

static ERL_NIF_TERM randomx_reencrypt_composite_to_composite_chunk_nif(
static ERL_NIF_TERM randomx_reencrypt_composite_chunk_nif(
ErlNifEnv* envPtr,
int argc,
const ERL_NIF_TERM argv[]
Expand Down
3 changes: 1 addition & 2 deletions apps/arweave/c_src/ar_mine_randomx.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ static ERL_NIF_TERM randomx_reencrypt_chunk_nif(ErlNifEnv*, int, const ERL_NIF_T
static ERL_NIF_TERM randomx_encrypt_composite_chunk_nif(ErlNifEnv*, int, const ERL_NIF_TERM []);
static ERL_NIF_TERM randomx_decrypt_composite_chunk_nif(ErlNifEnv*, int, const ERL_NIF_TERM []);
static ERL_NIF_TERM randomx_decrypt_composite_sub_chunk_nif(ErlNifEnv*, int, const ERL_NIF_TERM []);
static ERL_NIF_TERM randomx_reencrypt_legacy_to_composite_chunk_nif(ErlNifEnv*, int, const ERL_NIF_TERM []);
static ERL_NIF_TERM randomx_reencrypt_composite_to_composite_chunk_nif(ErlNifEnv*, int, const ERL_NIF_TERM []);
static ERL_NIF_TERM randomx_reencrypt_composite_chunk_nif(ErlNifEnv*, int, const ERL_NIF_TERM []);

static ERL_NIF_TERM randomx_hash_long_with_entropy_nif(ErlNifEnv* envPtr, int argc, const ERL_NIF_TERM argv[], hashing_mode hashingMode);

Expand Down
10 changes: 5 additions & 5 deletions apps/arweave/src/ar_bench_packing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
baseline_repack => {true, fun baseline_repack_chunks/4},
nif_repack => {true, fun nif_repack_chunks/4},
nif_repack_legacy_to_composite => {true, fun nif_repack_legacy_to_composite_chunks/4},
nif_repack_composite_to_composite => {true, fun nif_repack_composite_to_composite_chunks/4},
nif_repack_composite => {true, fun nif_repack_composite_chunks/4},
baseline_pack_composite => {false, fun baseline_pack_composite_chunks/4}
}).

Expand Down Expand Up @@ -533,9 +533,9 @@ nif_repack_legacy_to_composite_chunks(WorkerID, Config, Offset, Size) ->
end,
nif_repack_legacy_to_composite_chunks(WorkerID, Config, Offset+ChunkSize, RemainingSize).

nif_repack_composite_to_composite_chunks(_WorkerID, _Config, _Offset, Size) when Size =< 0 ->
nif_repack_composite_chunks(_WorkerID, _Config, _Offset, Size) when Size =< 0 ->
ok;
nif_repack_composite_to_composite_chunks(WorkerID, Config, Offset, Size) ->
nif_repack_composite_chunks(WorkerID, Config, Offset, Size) ->
#test_config{
randomx_state = RandomXState,
jit = JIT,
Expand All @@ -555,7 +555,7 @@ nif_repack_composite_to_composite_chunks(WorkerID, Config, Offset, Size) ->
ReadResult = file:pread(PackedFileHandle, Offset, ChunkSize),
RemainingSize = case ReadResult of
{ok, PackedChunk} ->
{ok, RepackedChunk, _} = ar_mine_randomx:randomx_reencrypt_composite_to_composite_chunk_nif(
{ok, RepackedChunk, _} = ar_mine_randomx:randomx_reencrypt_composite_chunk_nif(
RandomXState, UnpackKey, PackKey, PackedChunk, JIT, LargePages, HardwareAES,
Rounds, Rounds, PackingDifficulty, PackingDifficulty,
?COMPOSITE_PACKING_SUB_CHUNK_COUNT, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT),
Expand All @@ -568,7 +568,7 @@ nif_repack_composite_to_composite_chunks(WorkerID, Config, Offset, Size) ->
io:format("Error reading file: ~p~n", [Reason]),
0
end,
nif_repack_composite_to_composite_chunks(WorkerID, Config, Offset+ChunkSize, RemainingSize).
nif_repack_composite_chunks(WorkerID, Config, Offset+ChunkSize, RemainingSize).

%% --------------------------------------------------------------------------------------------
%% Helpers
Expand Down
42 changes: 11 additions & 31 deletions apps/arweave/src/ar_mine_randomx.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
randomx_encrypt_composite_chunk_nif/9,
randomx_decrypt_composite_chunk_nif/10,
randomx_decrypt_composite_sub_chunk_nif/10,
randomx_reencrypt_legacy_to_composite_chunk_nif/11,
randomx_reencrypt_composite_to_composite_chunk_nif/13
randomx_reencrypt_composite_chunk_nif/13
]).

-include_lib("arweave/include/ar.hrl").
Expand Down Expand Up @@ -269,7 +268,7 @@ randomx_reencrypt_chunk2(SourcePacking, TargetPacking,
randomx_reencrypt_chunk2({composite, Addr1, PackingDifficulty1},
{composite, Addr2, PackingDifficulty2},
RandomxState, UnpackKey, PackKey, Chunk, ChunkSize) ->
case randomx_reencrypt_composite_to_composite_chunk_nif(RandomxState, UnpackKey,
case randomx_reencrypt_composite_chunk_nif(RandomxState, UnpackKey,
PackKey, Chunk, jit(), large_pages(), hardware_aes(),
?COMPOSITE_PACKING_ROUND_COUNT, ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty1, PackingDifficulty2,
Expand All @@ -293,29 +292,15 @@ randomx_reencrypt_chunk2({composite, Addr1, PackingDifficulty1},
Reply ->
Reply
end;
randomx_reencrypt_chunk2({spora_2_6, _Addr1}, {composite, _Addr2, PackingDifficulty},
randomx_reencrypt_chunk2(SourcePacking, {composite, _Addr, _PackingDifficulty} = TargetPacking,
RandomxState, UnpackKey, PackKey, Chunk, ChunkSize) ->
case randomx_reencrypt_legacy_to_composite_chunk_nif(RandomxState, UnpackKey,
PackKey, Chunk, jit(), large_pages(), hardware_aes(),
?RANDOMX_PACKING_ROUNDS_2_6, ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
{ok, Repacked, RepackInput} ->
Unpadded = ar_packing_server:unpad_chunk(RepackInput, ChunkSize, ?DATA_CHUNK_SIZE),
{ok, Repacked, Unpadded}
end;
randomx_reencrypt_chunk2(spora_2_5, {composite, _Addr2, PackingDifficulty},
RandomxState, UnpackKey, PackKey, Chunk, ChunkSize) ->
case randomx_reencrypt_legacy_to_composite_chunk_nif(RandomxState, UnpackKey,
PackKey, Chunk, jit(), large_pages(), hardware_aes(),
?RANDOMX_PACKING_ROUNDS, ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
{ok, Repacked, RepackInput} ->
Unpadded = ar_packing_server:unpad_chunk(RepackInput, ChunkSize, ?DATA_CHUNK_SIZE),
{ok, Repacked, Unpadded}
case randomx_decrypt_chunk(SourcePacking, RandomxState, UnpackKey, Chunk, ChunkSize) of
{ok, UnpackedChunk} ->
{ok, RepackedChunk} = randomx_encrypt_chunk(TargetPacking, RandomxState, PackKey,
ar_packing_server:pad_chunk(UnpackedChunk)),
{ok, RepackedChunk, UnpackedChunk};
Error ->
Error
end;
randomx_reencrypt_chunk2(SourcePacking, TargetPacking,
RandomxState, UnpackKey, PackKey, Chunk, ChunkSize) ->
Expand Down Expand Up @@ -371,12 +356,7 @@ randomx_decrypt_composite_sub_chunk_nif(_State, _Data, _Chunk, _OutSize,
_JIT, _LargePages, _HardwareAES, _RoundCount, _IterationCount, _Offset) ->
erlang:nif_error(nif_not_loaded).

randomx_reencrypt_legacy_to_composite_chunk_nif(_State, _DecryptKey, _EncryptKey,
_Chunk, _JIT, _LargePages, _HardwareAES,
_DecryptRoundCount, _EncryptRoundCount, _IterationCount, _SubChunkCount) ->
erlang:nif_error(nif_not_loaded).

randomx_reencrypt_composite_to_composite_chunk_nif(_State,
randomx_reencrypt_composite_chunk_nif(_State,
_DecryptKey, _EncryptKey, _Chunk,
_JIT, _LargePages, _HardwareAES,
_DecryptRoundCount, _EncryptRoundCount,
Expand Down
Loading

0 comments on commit ff6dd4c

Please sign in to comment.