TheAlgorithms / C-Sharp

All algorithms implemented in C#.
GNU General Public License v3.0
7.12k stars 1.52k forks source link

Feature/iso7816d4 padding #409

Closed Kalkwst closed 1 year ago

Kalkwst commented 1 year ago

This pull request introduces a new class that implements the ISO 7816-4 padding algorithm. The class provides three main methods: AddPadding, RemovePadding, and GetPaddingSize.

codecov[bot] commented 1 year ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (e5cd1f5) 95.90% compared to head (9c6b527) 95.92%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #409 +/- ## ========================================== + Coverage 95.90% 95.92% +0.01% ========================================== Files 222 223 +1 Lines 9258 9302 +44 ========================================== + Hits 8879 8923 +44 Misses 379 379 ``` | [Files](https://app.codecov.io/gh/TheAlgorithms/C-Sharp/pull/409?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Coverage Δ | | |---|---|---| | [Algorithms/Crypto/Paddings/Iso7816D4Padding.cs](https://app.codecov.io/gh/TheAlgorithms/C-Sharp/pull/409?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-QWxnb3JpdGhtcy9DcnlwdG8vUGFkZGluZ3MvSXNvNzgxNkQ0UGFkZGluZy5jcw==) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Kalkwst commented 1 year ago

While Codacy might flag the following operation as unnecessary,

var isZeroMask = ((currentByte ^ 0x00) - 1) >> 31;

it's actually a bit manipulation trick often used in low-level or performance-critical code. Here's what it does:

So, this operation essentially checks if currentByte is zero. If it is, isZeroMask will be -1. Otherwise, it will be 0.

While this might seem more complex than simply using an equality check like currentByte == 0, these kinds of bit manipulation tricks can sometimes be faster than branching operations like if-else statements. They allow for more predictable execution paths and better CPU pipeline utilization.

Given that this algorithm demonstrates cryptographic padding, avoiding branching is crucial to prevent timing attacks in cryptographic contexts. Despite this implementation being less straightforward, it accurately reflects how we would implement this padding in a real-world cryptographic context. Therefore, we should disregard Codacy's issue in this instance.

siriak commented 1 year ago

I'm fine with the mask, but currentByte ^ 0x00 is redundant as you have mentioned, and it's not clear to me what indicate that a bitwise operation is taking place means. Subtraction of a 1 is not a bitwise operation, and shift is clearly a bitwise operation by itself, there's no need to specifically mark it as such. I'd say that removing xor simplifies the expression and makes it less confusing.