From 9fcaf7d0bef68795cb3519437c26162961de8aaf Mon Sep 17 00:00:00 2001 From: Yang Kun <91833768+ikspress@users.noreply.github.com> Date: Thu, 13 Jun 2024 08:42:33 +0800 Subject: [PATCH 1/2] Simplify `compar_int()` and `compar_special_int()` --- src/mfoc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mfoc.c b/src/mfoc.c index 9887553..8418b0f 100644 --- a/src/mfoc.c +++ b/src/mfoc.c @@ -1198,15 +1198,15 @@ uint32_t median(denonce d) int compar_int(const void *a, const void *b) { - if (*(uint64_t *)b == *(uint64_t *)a) return 0; - if (*(uint64_t *)b < * (uint64_t *)a) return 1; - return -1; + uint64_t m = *(uint64_t *)a, n = *(uint64_t *)b; + return (m > n) - (m < n); } // Compare countKeys structure int compar_special_int(const void *a, const void *b) { - return (((countKeys *)b)->count - ((countKeys *)a)->count); + int m = ((countKeys *)a)->count, n = ((countKeys *)b)->count; + return (m < n) - (m > n); } countKeys *uniqsort(uint64_t *possibleKeys, uint32_t size) From 76f477e87bf2a140245fa4e8dfc7961cc92e25fe Mon Sep 17 00:00:00 2001 From: Yang Kun <91833768+ikspress@users.noreply.github.com> Date: Thu, 13 Jun 2024 08:55:14 +0800 Subject: [PATCH 2/2] Simpilify `valid_nonce()` --- src/mfoc.c | 6 +++--- src/mfoc.h | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/mfoc.c b/src/mfoc.c index 8418b0f..b02285a 100644 --- a/src/mfoc.c +++ b/src/mfoc.c @@ -1241,9 +1241,9 @@ countKeys *uniqsort(uint64_t *possibleKeys, uint32_t size) // Return 1 if the nonce is invalid else return 0 int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) { - return ((odd_parity((Nt >> 24) & 0xFF) == ((parity[0]) ^ odd_parity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1, 16))) & \ - (odd_parity((Nt >> 16) & 0xFF) == ((parity[1]) ^ odd_parity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1, 8))) & \ - (odd_parity((Nt >> 8) & 0xFF) == ((parity[2]) ^ odd_parity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1, 0)))) ? 1 : 0; + return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1, 16))) && + (oddparity((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1, 8))) && + (oddparity((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1, 0)))) ? 1 : 0; } void num_to_bytes(uint64_t n, uint32_t len, uint8_t *dest) diff --git a/src/mfoc.h b/src/mfoc.h index 9ea7547..8b0c5c7 100644 --- a/src/mfoc.h +++ b/src/mfoc.h @@ -35,8 +35,6 @@ // Number of sets with 32b keys #define DEFAULT_SETS_NR 5 -#define odd_parity(i) (( (i) ^ (i)>>1 ^ (i)>>2 ^ (i)>>3 ^ (i)>>4 ^ (i)>>5 ^ (i)>>6 ^ (i)>>7 ^ 1) & 0x01) - typedef struct { uint8_t KeyA[6]; uint8_t KeyB[6];