Skip to content

Commit

Permalink
Internal Code Change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 591150703
  • Loading branch information
XLS Team authored and copybara-github committed Dec 15, 2023
1 parent 0a58003 commit 7278ad6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 40 deletions.
26 changes: 12 additions & 14 deletions xls/modules/aes/aes_ctr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ static void IvToBuffer(const InitVector& iv,

static void PrintTraceMessages(const InterpreterEvents& events) {
if (absl::GetFlag(FLAGS_print_traces) && !events.trace_msgs.empty()) {
std::cout << "Trace messages:" << std::endl;
std::cout << "Trace messages:" << '\n';
for (const auto& tm : events.trace_msgs) {
std::cout << " - " << tm << std::endl;
std::cout << " - " << tm << '\n';
}
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ static absl::StatusOr<bool> RunSample(JitData* jit_data,
if (reference_ciphertext.size() != xls_ciphertext.size()) {
std::cout << "Error: XLS and reference ciphertexts differ in num blocks: "
<< "XLS: " << xls_ciphertext.size()
<< ", ref: " << reference_ciphertext.size() << std::endl;
<< ", ref: " << reference_ciphertext.size() << '\n';
return false;
}

Expand All @@ -222,7 +222,7 @@ static absl::StatusOr<bool> RunSample(JitData* jit_data,
for (int32_t byte_idx = 0; byte_idx < kBlockBytes; byte_idx++) {
if (reference_ciphertext[block_idx][byte_idx] !=
xls_ciphertext[block_idx][byte_idx]) {
std::cout << "Error comparing block " << block_idx << ":" << std::endl;
std::cout << "Error comparing block " << block_idx << ":" << '\n';
PrintFailure(reference_ciphertext[block_idx], xls_ciphertext[block_idx],
byte_idx, /*ciphertext=*/true);
return false;
Expand All @@ -246,7 +246,7 @@ static absl::StatusOr<bool> RunSample(JitData* jit_data,
if (sample_data.input_blocks.size() != xls_plaintext.size()) {
std::cout << "Error: XLS decrypted plaintext and input plaintext differ "
<< "in num blocks XLS: " << xls_ciphertext.size()
<< ", ref: " << sample_data.input_blocks.size() << std::endl;
<< ", ref: " << sample_data.input_blocks.size() << '\n';
return false;
}

Expand All @@ -255,7 +255,7 @@ static absl::StatusOr<bool> RunSample(JitData* jit_data,
for (int32_t byte_idx = 0; byte_idx < kBlockBytes; byte_idx++) {
if (sample_data.input_blocks[block_idx][byte_idx] !=
xls_plaintext[block_idx][byte_idx]) {
std::cout << "Error comparing block " << block_idx << ":" << std::endl;
std::cout << "Error comparing block " << block_idx << ":" << '\n';
PrintFailure(sample_data.input_blocks[block_idx],
xls_plaintext[block_idx], byte_idx,
/*ciphertext=*/false);
Expand Down Expand Up @@ -319,21 +319,19 @@ static absl::Status RunTest(int32_t num_samples, int32_t key_bits) {
XLS_ASSIGN_OR_RETURN(bool proceed, RunSample(&encrypt_jit_data, sample_data,
&xls_encrypt_dur));
if (!proceed) {
std::cout << "Key : " << FormatKey(sample_data.key) << std::endl;
std::cout << "IV : " << FormatInitVector(sample_data.iv)
<< std::endl;
std::cout << "Plaintext: " << std::endl
<< FormatBlocks(sample_data.input_blocks, /*indent=*/4)
<< std::endl;
std::cout << "Key : " << FormatKey(sample_data.key) << '\n';
std::cout << "IV : " << FormatInitVector(sample_data.iv) << '\n';
std::cout << "Plaintext: " << '\n'
<< FormatBlocks(sample_data.input_blocks, /*indent=*/4) << '\n';
return absl::InternalError(
absl::StrCat("Testing failed at sample ", i, "."));
}
}

std::cout << "AES-CTR: Successfully ran " << num_samples << " samples."
<< std::endl;
<< '\n';
std::cout << "AES-CTR: Avg. XLS encryption time: "
<< xls_encrypt_dur / num_samples << std::endl;
<< xls_encrypt_dur / num_samples << '\n';

return absl::OkStatus();
}
Expand Down
22 changes: 10 additions & 12 deletions xls/modules/aes/aes_gcm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static bool CompareBlock(const Block& expected, const Block& actual,
std::string_view failure_msg, bool is_ciphertext) {
for (int byte_idx = 0; byte_idx < kBlockBytes; byte_idx++) {
if (expected[byte_idx] != actual[byte_idx]) {
std::cout << failure_msg << std::endl;
std::cout << failure_msg << '\n';
PrintFailure(expected, actual, byte_idx, is_ciphertext);
return false;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ static absl::StatusOr<bool> RunSample(JitData* jit_data,
xls_encrypted.output_data.size()) {
std::cout << "Reference & XLS ciphertext differed in sizes: "
<< "reference: " << reference_encrypted.output_data.size()
<< ", XLS: " << xls_encrypted.output_data.size() << std::endl;
<< ", XLS: " << xls_encrypted.output_data.size() << '\n';
return false;
}

Expand Down Expand Up @@ -275,8 +275,7 @@ static absl::StatusOr<bool> RunSample(JitData* jit_data,
if (xls_decrypted.output_data.size() != xls_encrypted.output_data.size()) {
std::cout << "Input plaintext and XLS deciphered text differed in sizes: "
<< "input: " << sample_data.input_data.size()
<< ", decrypted: " << xls_decrypted.output_data.size()
<< std::endl;
<< ", decrypted: " << xls_decrypted.output_data.size() << '\n';
return false;
}

Expand Down Expand Up @@ -365,21 +364,20 @@ static absl::Status RunTest(int num_samples, int key_bits) {

XLS_ASSIGN_OR_RETURN(bool proceed, RunSample(&jit_data, sample_data));
if (!proceed) {
std::cout << "Key : " << FormatKey(sample_data.key) << std::endl;
std::cout << "Key : " << FormatKey(sample_data.key) << '\n';
std::cout << "IV : " << FormatInitVector(sample_data.init_vector)
<< std::endl;
std::cout << "Plaintext: " << std::endl
<< FormatBlocks(sample_data.input_data, /*indent=*/4)
<< std::endl;
std::cout << "AAD: " << std::endl
<< FormatBlocks(sample_data.aad, /*indent=*/4) << std::endl;
<< '\n';
std::cout << "Plaintext: " << '\n'
<< FormatBlocks(sample_data.input_data, /*indent=*/4) << '\n';
std::cout << "AAD: " << '\n'
<< FormatBlocks(sample_data.aad, /*indent=*/4) << '\n';
return absl::InternalError(
absl::StrCat("Testing failed at sample ", sample_idx, "."));
}
}

std::cout << "AES-GCM: Successfully ran " << num_samples << " " << key_bits
<< "-bit samples." << std::endl;
<< "-bit samples." << '\n';

return absl::OkStatus();
}
Expand Down
17 changes: 8 additions & 9 deletions xls/modules/aes/aes_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ static absl::StatusOr<bool> RunSample(const Block& input, const Key& key,
*xls_encrypt_dur += absl::Now() - start_time;

XLS_VLOG(2) << "Reference ciphertext: " << FormatBlock(reference_ciphertext)
<< std::endl;
XLS_VLOG(2) << "XLS ciphertext: " << FormatBlock(xls_ciphertext) << std::endl;
<< '\n';
XLS_VLOG(2) << "XLS ciphertext: " << FormatBlock(xls_ciphertext) << '\n';

// Verify the ciphertexts match, to ensure we're actually doing the encryption
// properly.
Expand All @@ -105,8 +105,7 @@ static absl::StatusOr<bool> RunSample(const Block& input, const Key& key,
XLS_ASSIGN_OR_RETURN(Block xls_decrypted, XlsDecrypt(key, key_bytes, input));
*xls_decrypt_dur += absl::Now() - start_time;

XLS_VLOG(2) << "Decrypted plaintext: " << FormatBlock(xls_decrypted)
<< std::endl;
XLS_VLOG(2) << "Decrypted plaintext: " << FormatBlock(xls_decrypted) << '\n';

// We can just compare the XLS result to the input to verify we're decrypting
// right.
Expand Down Expand Up @@ -140,19 +139,19 @@ static absl::Status RunTest(int32_t key_bits, int32_t num_samples) {
bool proceed,
RunSample(input, key, key_bytes, &xls_encrypt_dur, &xls_decrypt_dur));
if (!proceed) {
std::cout << "Plaintext: " << FormatBlock(input) << std::endl;
std::cout << "Key: " << FormatKey(key) << std::endl;
std::cout << "Plaintext: " << FormatBlock(input) << '\n';
std::cout << "Key: " << FormatKey(key) << '\n';
return absl::InternalError(
absl::StrCat(key_bits, "-bit validation failed."));
}
}

std::cout << "Successfully ran " << num_samples << " " << key_bits
<< "-bit samples." << std::endl;
<< "-bit samples." << '\n';
std::cout << "Avg. XLS encryption time: " << xls_encrypt_dur / num_samples
<< std::endl;
<< '\n';
std::cout << "Avg. XLS decryption time: " << xls_decrypt_dur / num_samples
<< std::endl;
<< '\n';

return absl::OkStatus();
}
Expand Down
7 changes: 3 additions & 4 deletions xls/modules/aes/aes_test_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ void PrintFailure(const Block& expected_block, const Block& actual_block,
std::cout << "Mismatch in " << type_str << " at byte " << index << ": "
<< std::hex << "expected: 0x"
<< static_cast<uint32_t>(expected_block[index]) << "; actual: 0x"
<< static_cast<uint32_t>(actual_block[index]) << std::endl;
std::cout << " - Expected block: " << FormatBlock(expected_block)
<< std::endl;
std::cout << " - Actual block : " << FormatBlock(actual_block) << std::endl;
<< static_cast<uint32_t>(actual_block[index]) << '\n';
std::cout << " - Expected block: " << FormatBlock(expected_block) << '\n';
std::cout << " - Actual block : " << FormatBlock(actual_block) << '\n';
}

Value InitVectorToValue(const InitVector& iv) {
Expand Down
2 changes: 1 addition & 1 deletion xls/modules/aes/sample_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ static absl::Status RealMain(EncryptionMode mode, int key_bits,

XLS_ASSIGN_OR_RETURN(Result result, RunGcm(sample));

std::cout << DslxFormatOutput(sample, result) << std::endl;
std::cout << DslxFormatOutput(sample, result) << '\n';

return absl::OkStatus();
}
Expand Down

0 comments on commit 7278ad6

Please sign in to comment.