summary refs log tree commit diff
path: root/crypto/src/pqc
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-01 17:20:21 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-01 17:20:21 +0700
commit4de18964f35fc169cb94058dcfe2e1ccb59b2524 (patch)
tree289c3b209782ce0af94f5bf0362a68b3e3764ec2 /crypto/src/pqc
parentBIKE init perf. opts. (diff)
downloadBouncyCastle.NET-ed25519-4de18964f35fc169cb94058dcfe2e1ccb59b2524.tar.xz
Add Integers.PopCount
Diffstat (limited to 'crypto/src/pqc')
-rw-r--r--crypto/src/pqc/crypto/picnic/PicnicUtilities.cs35
1 files changed, 3 insertions, 32 deletions
diff --git a/crypto/src/pqc/crypto/picnic/PicnicUtilities.cs b/crypto/src/pqc/crypto/picnic/PicnicUtilities.cs
index a2f1ca080..d77d7ce29 100644
--- a/crypto/src/pqc/crypto/picnic/PicnicUtilities.cs
+++ b/crypto/src/pqc/crypto/picnic/PicnicUtilities.cs
@@ -34,48 +34,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Picnic
                 x ^= data[i];
             }
 
-            return (int)Parity16(x);
+            return Integers.PopCount(x) & 1;
         }
 
         internal static uint Parity16(uint x)
         {
-#if NETCOREAPP3_0_OR_GREATER
-            if (Popcnt.IsSupported)
-            {
-                return Popcnt.PopCount(x & 0xFFFF) & 1U;
-            }
-#endif
-
-            uint y = x ^ (x >> 1);
-
-            y ^= (y >> 2);
-            y ^= (y >> 4);
-            y ^= (y >> 8);
-            return y & 1;
+            return (uint)(Integers.PopCount(x & 0xFFFF) & 1);
         }
 
         internal static uint Parity32(uint x)
         {
-#if NETCOREAPP3_0_OR_GREATER
-            if (Popcnt.IsSupported)
-            {
-                return Popcnt.PopCount(x) & 1U;
-            }
-#endif
-
-            /* Compute parity of x using code from Section 5-2 of
-             * H.S. Warren, *Hacker's Delight*, Pearson Education, 2003.
-             * http://www.hackersdelight.org/hdcodetxt/parity.c.txt
-             */
-            uint y = (x ^ (x >> 1));
-            y ^= (y >> 2);
-            y ^= (y >> 4);
-            y ^= (y >> 8);
-            y ^= (y >> 16);
-            return (y & 1);
+            return (uint)(Integers.PopCount(x) & 1);
         }
 
-
         /* Set a specific bit in a byte array to a given value */
         internal static void SetBitInWordArray(uint[] array, int bitNumber, uint val)
         {