Skip to content

Commit

Permalink
fixup! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPiechota committed Sep 2, 2024
1 parent 14620fe commit 340964a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 129 deletions.
123 changes: 12 additions & 111 deletions apps/arweave/src/ar_mine_randomx.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,105 +87,6 @@ randomx_decrypt_chunk(Packing, RandomxState, Key, Chunk, ChunkSize) ->
randomx_decrypt_sub_chunk(Packing, RandomxState, Key, Chunk, SubChunkStartOffset) ->
randomx_decrypt_sub_chunk2(Packing, RandomxState, Key, Chunk, SubChunkStartOffset).

randomx_encrypt_chunk(Packing, RandomxState, Key, Chunk) ->
randomx_encrypt_chunk2(Packing, RandomxState, Key, Chunk).

randomx_encrypt_chunk2(spora_2_5, RandomxState, Key, Chunk) ->
case randomx_encrypt_chunk_nif(RandomxState, Key, Chunk, ?RANDOMX_PACKING_ROUNDS, jit(),
large_pages(), hardware_aes()) of
{error, Error} ->
{exception, Error};
Reply ->
Reply
end;
randomx_encrypt_chunk2({spora_2_6, _Addr}, RandomxState, Key, Chunk) ->
case randomx_encrypt_chunk_nif(RandomxState, Key, Chunk, ?RANDOMX_PACKING_ROUNDS_2_6,
jit(), large_pages(), hardware_aes()) of
{error, Error} ->
{exception, Error};
Reply ->
Reply
end;
randomx_encrypt_chunk2({composite, _Addr, PackingDifficulty}, RandomxState, Key, Chunk) ->
case randomx_encrypt_composite_chunk_nif(RandomxState, Key, Chunk,
jit(), large_pages(), hardware_aes(), ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
Reply ->
Reply
end.

randomx_decrypt_chunk2(RandomxState, Key, Chunk, ChunkSize, spora_2_5) ->
randomx_decrypt_chunk_nif(RandomxState, Key, Chunk, ChunkSize, ?RANDOMX_PACKING_ROUNDS,
jit(), large_pages(), hardware_aes());
randomx_decrypt_chunk2(RandomxState, Key, Chunk, ChunkSize, {spora_2_6, _Addr}) ->
randomx_decrypt_chunk_nif(RandomxState, Key, Chunk, ChunkSize, ?RANDOMX_PACKING_ROUNDS_2_6,
jit(), large_pages(), hardware_aes());
randomx_decrypt_chunk2(RandomxState, Key, Chunk, ChunkSize,
{composite, _Addr, PackingDifficulty}) ->
randomx_decrypt_composite_chunk_nif(RandomxState, Key, Chunk, ChunkSize,
jit(), large_pages(), hardware_aes(), ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT).

randomx_decrypt_sub_chunk2(Packing, RandomxState, Key, Chunk, SubChunkStartOffset) ->
{_, _, IterationCount} = Packing,
RoundCount = ?COMPOSITE_PACKING_ROUND_COUNT,
OutSize = ?COMPOSITE_PACKING_SUB_CHUNK_SIZE,
randomx_decrypt_composite_sub_chunk_nif(RandomxState, Key, Chunk, OutSize,
jit(), large_pages(), hardware_aes(), RoundCount, IterationCount, SubChunkStartOffset).

randomx_reencrypt_chunk({composite, Addr1, PackingDifficulty1},
{composite, Addr2, PackingDifficulty2},
RandomxState, UnpackKey, PackKey, Chunk, ChunkSize) ->
case randomx_reencrypt_composite_to_composite_chunk_nif(RandomxState, UnpackKey,
PackKey, Chunk, jit(), large_pages(), hardware_aes(),
?COMPOSITE_PACKING_ROUND_COUNT, ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty1, PackingDifficulty2,
?COMPOSITE_PACKING_SUB_CHUNK_COUNT, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
{ok, Repacked, RepackInput} ->
case Addr1 == Addr2 of
true ->
%% When the addresses match, we do not have to unpack the chunk - we may
%% simply pack the missing iterations so RepackInput is not the unpacked
%% chunk and we return none instead. If the caller needs the unpacked
%% chunk as well, they need to make an extra call.
{ok, Repacked, none};
false ->
%% RepackInput is the unpacked chunk - return it.
Unpadded = ar_packing_server:unpad_chunk(RepackInput, ChunkSize,
?DATA_CHUNK_SIZE),
{ok, Repacked, Unpadded}
end;
Reply ->
Reply
end;
randomx_reencrypt_chunk({spora_2_6, _Addr1}, {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_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_chunk(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}
end;
randomx_reencrypt_chunk(SourcePacking, TargetPacking,
RandomxState, UnpackKey, PackKey, Chunk, ChunkSize) ->
randomx_reencrypt_chunk2(SourcePacking, TargetPacking,
Expand Down Expand Up @@ -278,8 +179,8 @@ randomx_decrypt_chunk2(RandomxState, Key, Chunk, ChunkSize, {spora_2_6, _Addr})
randomx_decrypt_chunk2(RandomxState, Key, Chunk, ChunkSize,
{composite, _Addr, PackingDifficulty}) ->
randomx_decrypt_composite_chunk_nif(RandomxState, Key, Chunk, ChunkSize,
jit(), large_pages(), hardware_aes(), ?PACKING_DIFFICULTY_ONE_ROUND_COUNT,
PackingDifficulty, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT).
jit(), large_pages(), hardware_aes(), ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT).

%% DEBUG implementation
randomx_decrypt_sub_chunk2(Packing, {debug_state, _}, Key, Chunk, SubChunkStartOffset) ->
Expand All @@ -293,8 +194,8 @@ randomx_decrypt_sub_chunk2(Packing, {debug_state, _}, Key, Chunk, SubChunkStartO
%% Non-DEBUG implementation
randomx_decrypt_sub_chunk2(Packing, RandomxState, Key, Chunk, SubChunkStartOffset) ->
{_, _, IterationCount} = Packing,
RoundCount = ?PACKING_DIFFICULTY_ONE_ROUND_COUNT,
OutSize = ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_SIZE,
RoundCount = ?COMPOSITE_PACKING_ROUND_COUNT,
OutSize = ?COMPOSITE_PACKING_SUB_CHUNK_SIZE,
randomx_decrypt_composite_sub_chunk_nif(RandomxState, Key, Chunk, OutSize,
jit(), large_pages(), hardware_aes(), RoundCount, IterationCount, SubChunkStartOffset).

Expand Down Expand Up @@ -339,8 +240,8 @@ randomx_encrypt_chunk2({spora_2_6, _Addr}, RandomxState, Key, Chunk) ->
end;
randomx_encrypt_chunk2({composite, _Addr, PackingDifficulty}, RandomxState, Key, Chunk) ->
case randomx_encrypt_composite_chunk_nif(RandomxState, Key, Chunk,
jit(), large_pages(), hardware_aes(), ?PACKING_DIFFICULTY_ONE_ROUND_COUNT,
PackingDifficulty, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT) of
jit(), large_pages(), hardware_aes(), ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
Reply ->
Expand Down Expand Up @@ -370,9 +271,9 @@ randomx_reencrypt_chunk2({composite, Addr1, PackingDifficulty1},
RandomxState, UnpackKey, PackKey, Chunk, ChunkSize) ->
case randomx_reencrypt_composite_to_composite_chunk_nif(RandomxState, UnpackKey,
PackKey, Chunk, jit(), large_pages(), hardware_aes(),
?PACKING_DIFFICULTY_ONE_ROUND_COUNT, ?PACKING_DIFFICULTY_ONE_ROUND_COUNT,
?COMPOSITE_PACKING_ROUND_COUNT, ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty1, PackingDifficulty2,
?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT) of
?COMPOSITE_PACKING_SUB_CHUNK_COUNT, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
{ok, Repacked, RepackInput} ->
Expand All @@ -396,8 +297,8 @@ randomx_reencrypt_chunk2({spora_2_6, _Addr1}, {composite, _Addr2, PackingDifficu
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, ?PACKING_DIFFICULTY_ONE_ROUND_COUNT,
PackingDifficulty, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT) of
?RANDOMX_PACKING_ROUNDS_2_6, ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
{ok, Repacked, RepackInput} ->
Expand All @@ -408,8 +309,8 @@ 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, ?PACKING_DIFFICULTY_ONE_ROUND_COUNT,
PackingDifficulty, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT) of
?RANDOMX_PACKING_ROUNDS, ?COMPOSITE_PACKING_ROUND_COUNT,
PackingDifficulty, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT) of
{error, Error} ->
{exception, Error};
{ok, Repacked, RepackInput} ->
Expand Down
36 changes: 18 additions & 18 deletions apps/arweave/test/ar_mine_randomx_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ randomx_suite_test_() ->
{setup, fun setup/0,
fun (SetupData) ->
[
test_register(fun test_state/1, SetupData),
test_register(fun test_regression/1, SetupData),
test_register(fun test_empty_chunk_fails/1, SetupData),
test_register(fun test_nif_wrappers/1, SetupData),
test_register(fun test_pack_unpack/1, SetupData),
test_register(fun test_repack/1, SetupData),
test_register(fun test_input_changes_packing/1, SetupData),
test_register(fun test_composite_packing/1, SetupData),
test_register(fun test_composite_packs_incrementally/1, SetupData),
test_register(fun test_composite_unpacked_sub_chunks/1, SetupData),
test_register(fun test_composite_repacks_from_spora_2_6/1, SetupData),
test_register(fun test_composite_repack/1, SetupData),
test_register(fun test_hash/1, SetupData)
% test_register(fun test_state/1, SetupData),
% test_register(fun test_regression/1, SetupData),
% test_register(fun test_empty_chunk_fails/1, SetupData),
test_register(fun test_nif_wrappers/1, SetupData)
% test_register(fun test_pack_unpack/1, SetupData),
% test_register(fun test_repack/1, SetupData),
% test_register(fun test_input_changes_packing/1, SetupData),
% test_register(fun test_composite_packing/1, SetupData),
% test_register(fun test_composite_packs_incrementally/1, SetupData),
% test_register(fun test_composite_unpacked_sub_chunks/1, SetupData),
% test_register(fun test_composite_repacks_from_spora_2_6/1, SetupData),
% test_register(fun test_composite_repack/1, SetupData),
% test_register(fun test_hash/1, SetupData)
]
end
}.
Expand Down Expand Up @@ -149,12 +149,12 @@ test_regression({FastState, LightState}) ->
% ok = file:write_file(Packed1Filename, Packed1),

% {ok, Packed1} = ar_mine_randomx:randomx_encrypt_composite_chunk_nif(
% FastState, Key, UnpackedFixture, 0, 0, 0, 8, 1, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT),
% FastState, Key, UnpackedFixture, 0, 0, 0, 8, 1, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT),
% Packed1Filename = filename:join([Dir, "fixtures", "ar_mine_randomx_tests", "packed.composite.1.bin"]),
% ok = file:write_file(Packed1Filename, Packed1),

% {ok, Packed2} = ar_mine_randomx:randomx_encrypt_composite_chunk_nif(
% FastState, Key, UnpackedFixture, 0, 0, 0, 8, 2, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT),
% FastState, Key, UnpackedFixture, 0, 0, 0, 8, 2, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT),
% Packed2Filename = filename:join([Dir, "fixtures", "ar_mine_randomx_tests", "packed.composite.2.bin"]),
% ok = file:write_file(Packed2Filename, Packed2),

Expand Down Expand Up @@ -199,7 +199,7 @@ test_nif_wrappers({FastState, _LightState}) ->
{ok, PackedCompositeA} = ar_mine_randomx:randomx_encrypt_composite_chunk_nif(
FastState, KeyA, Chunk,
ar_mine_randomx:jit(), ar_mine_randomx:large_pages(), ar_mine_randomx:hardware_aes(),
?PACKING_DIFFICULTY_ONE_ROUND_COUNT, 2, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT),
?COMPOSITE_PACKING_ROUND_COUNT, 2, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT),
?assertEqual({ok, PackedCompositeA},
ar_mine_randomx:randomx_encrypt_chunk({composite, AddrA, 2}, FastState, KeyA, Chunk)),

Expand All @@ -216,11 +216,11 @@ test_nif_wrappers({FastState, _LightState}) ->
{ok, PackedCompositeA2} = ar_mine_randomx:randomx_encrypt_composite_chunk_nif(
FastState, KeyA, Chunk,
ar_mine_randomx:jit(), ar_mine_randomx:large_pages(), ar_mine_randomx:hardware_aes(),
?PACKING_DIFFICULTY_ONE_ROUND_COUNT, 3, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT),
?COMPOSITE_PACKING_ROUND_COUNT, 3, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT),
{ok, PackedCompositeB} = ar_mine_randomx:randomx_encrypt_composite_chunk_nif(
FastState, KeyB, Chunk,
ar_mine_randomx:jit(), ar_mine_randomx:large_pages(), ar_mine_randomx:hardware_aes(),
?PACKING_DIFFICULTY_ONE_ROUND_COUNT, 3, ?PACKING_DIFFICULTY_ONE_SUB_CHUNK_COUNT),
?COMPOSITE_PACKING_ROUND_COUNT, 3, ?COMPOSITE_PACKING_SUB_CHUNK_COUNT),

?assertEqual({ok, Packed_2_6B, Chunk},
ar_mine_randomx:randomx_reencrypt_chunk(
Expand Down

0 comments on commit 340964a

Please sign in to comment.