diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-18 14:04:40 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-18 14:04:40 +0700 |
commit | 2a27035daa92bb2fc1c89914481db58e9336d35e (patch) | |
tree | 84737dbc459344c32878722e1787fe097e766f62 /crypto/src | |
parent | LongArray cleanup and refactoring (diff) | |
download | BouncyCastle.NET-ed25519-2a27035daa92bb2fc1c89914481db58e9336d35e.tar.xz |
Factor out Unshuffle methods
Diffstat (limited to '')
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT113Field.cs | 4 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT131Field.cs | 10 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT163Field.cs | 10 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT193Field.cs | 11 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT233Field.cs | 10 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT239Field.cs | 10 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT283Field.cs | 15 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT409Field.cs | 20 | ||||
-rw-r--r-- | crypto/src/math/ec/custom/sec/SecT571Field.cs | 18 | ||||
-rw-r--r-- | crypto/src/math/raw/Interleave.cs | 15 |
10 files changed, 39 insertions, 84 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecT113Field.cs b/crypto/src/math/ec/custom/sec/SecT113Field.cs index 56738a219..c41d9f7d7 100644 --- a/crypto/src/math/ec/custom/sec/SecT113Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT113Field.cs @@ -123,9 +123,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec public static void Sqrt(ulong[] x, ulong[] z) { - ulong u0 = Interleave.Unshuffle(x[0]), u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c0 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); + ulong c0 = Interleave.Unshuffle(x[0], x[1], out ulong e0); z[0] = e0 ^ (c0 << 57) ^ (c0 << 5); z[1] = (c0 >> 7) ^ (c0 >> 59); diff --git a/crypto/src/math/ec/custom/sec/SecT131Field.cs b/crypto/src/math/ec/custom/sec/SecT131Field.cs index adf4f0448..4ff5999a4 100644 --- a/crypto/src/math/ec/custom/sec/SecT131Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT131Field.cs @@ -135,14 +135,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec { ulong[] odd = Nat192.Create64(); - ulong u0, u1; - u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - odd[0] = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[2]); - ulong e1 = (u0 & 0x00000000FFFFFFFFUL); - odd[1] = (u0 >> 32); + odd[0] = Interleave.Unshuffle(x[0], x[1], out ulong e0); + odd[1] = Interleave.Unshuffle(x[2] , out ulong e1); Multiply(odd, ROOT_Z, z); diff --git a/crypto/src/math/ec/custom/sec/SecT163Field.cs b/crypto/src/math/ec/custom/sec/SecT163Field.cs index 79079ac0b..44105039d 100644 --- a/crypto/src/math/ec/custom/sec/SecT163Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT163Field.cs @@ -148,14 +148,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec { ulong[] odd = Nat192.Create64(); - ulong u0, u1; - u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - odd[0] = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[2]); - ulong e1 = (u0 & 0x00000000FFFFFFFFUL); - odd[1] = (u0 >> 32); + odd[0] = Interleave.Unshuffle(x[0], x[1], out ulong e0); + odd[1] = Interleave.Unshuffle(x[2] , out ulong e1); Multiply(odd, ROOT_Z, z); diff --git a/crypto/src/math/ec/custom/sec/SecT193Field.cs b/crypto/src/math/ec/custom/sec/SecT193Field.cs index 1a4739b69..59da8b000 100644 --- a/crypto/src/math/ec/custom/sec/SecT193Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT193Field.cs @@ -155,14 +155,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec public static void Sqrt(ulong[] x, ulong[] z) { - ulong u0, u1; - u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c0 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[2]); - ulong e1 = (u0 & 0x00000000FFFFFFFFUL) ^ (x[3] << 32); - ulong c1 = (u0 >> 32); + ulong c0 = Interleave.Unshuffle(x[0], x[1], out ulong e0); + ulong c1 = Interleave.Unshuffle(x[2] , out ulong e1); + e1 ^= x[3] << 32; z[0] = e0 ^ (c0 << 8); z[1] = e1 ^ (c1 << 8) ^ (c0 >> 56) ^ (c0 << 33); diff --git a/crypto/src/math/ec/custom/sec/SecT233Field.cs b/crypto/src/math/ec/custom/sec/SecT233Field.cs index 1ebac2eac..c16a3d612 100644 --- a/crypto/src/math/ec/custom/sec/SecT233Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT233Field.cs @@ -151,14 +151,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec public static void Sqrt(ulong[] x, ulong[] z) { - ulong u0, u1; - u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c0 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[2]); u1 = Interleave.Unshuffle(x[3]); - ulong e1 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c1 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); + ulong c0 = Interleave.Unshuffle(x[0], x[1], out ulong e0); + ulong c1 = Interleave.Unshuffle(x[2], x[3], out ulong e1); ulong c2; c2 = (c1 >> 27); diff --git a/crypto/src/math/ec/custom/sec/SecT239Field.cs b/crypto/src/math/ec/custom/sec/SecT239Field.cs index ce2e3ba84..de87b18a2 100644 --- a/crypto/src/math/ec/custom/sec/SecT239Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT239Field.cs @@ -159,14 +159,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec public static void Sqrt(ulong[] x, ulong[] z) { - ulong u0, u1; - u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c0 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[2]); u1 = Interleave.Unshuffle(x[3]); - ulong e1 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c1 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); + ulong c0 = Interleave.Unshuffle(x[0], x[1], out ulong e0); + ulong c1 = Interleave.Unshuffle(x[2], x[3], out ulong e1); ulong c2, c3; c3 = (c1 >> 49); diff --git a/crypto/src/math/ec/custom/sec/SecT283Field.cs b/crypto/src/math/ec/custom/sec/SecT283Field.cs index 61a1c9afd..ee5ad89c5 100644 --- a/crypto/src/math/ec/custom/sec/SecT283Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT283Field.cs @@ -158,18 +158,9 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec { ulong[] odd = Nat320.Create64(); - ulong u0, u1; - u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - odd[0] = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[2]); u1 = Interleave.Unshuffle(x[3]); - ulong e1 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - odd[1] = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[4]); - ulong e2 = (u0 & 0x00000000FFFFFFFFUL); - odd[2] = (u0 >> 32); + odd[0] = Interleave.Unshuffle(x[0], x[1], out ulong e0); + odd[1] = Interleave.Unshuffle(x[2], x[3], out ulong e1); + odd[2] = Interleave.Unshuffle(x[4] , out ulong e2); Multiply(odd, ROOT_Z, z); diff --git a/crypto/src/math/ec/custom/sec/SecT409Field.cs b/crypto/src/math/ec/custom/sec/SecT409Field.cs index c35d3cef0..0fb7377f6 100644 --- a/crypto/src/math/ec/custom/sec/SecT409Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT409Field.cs @@ -191,22 +191,10 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec public static void Sqrt(ulong[] x, ulong[] z) { - ulong u0, u1; - u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]); - ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c0 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[2]); u1 = Interleave.Unshuffle(x[3]); - ulong e1 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c1 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[4]); u1 = Interleave.Unshuffle(x[5]); - ulong e2 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - ulong c2 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - - u0 = Interleave.Unshuffle(x[6]); - ulong e3 = (u0 & 0x00000000FFFFFFFFUL); - ulong c3 = (u0 >> 32); + ulong c0 = Interleave.Unshuffle(x[0], x[1], out ulong e0); + ulong c1 = Interleave.Unshuffle(x[2], x[3], out ulong e1); + ulong c2 = Interleave.Unshuffle(x[4], x[5], out ulong e2); + ulong c3 = Interleave.Unshuffle(x[6] , out ulong e3); z[0] = e0 ^ (c0 << 44); z[1] = e1 ^ (c1 << 44) ^ (c0 >> 20); diff --git a/crypto/src/math/ec/custom/sec/SecT571Field.cs b/crypto/src/math/ec/custom/sec/SecT571Field.cs index 1b8bb763e..4d3f715fa 100644 --- a/crypto/src/math/ec/custom/sec/SecT571Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT571Field.cs @@ -231,19 +231,11 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec { ulong[] evn = Nat576.Create64(), odd = Nat576.Create64(); - int pos = 0; - for (int i = 0; i < 4; ++i) - { - ulong u0 = Interleave.Unshuffle(x[pos++]); - ulong u1 = Interleave.Unshuffle(x[pos++]); - evn[i] = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32); - odd[i] = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); - } - { - ulong u0 = Interleave.Unshuffle(x[pos]); - evn[4] = (u0 & 0x00000000FFFFFFFFUL); - odd[4] = (u0 >> 32); - } + odd[0] = Interleave.Unshuffle(x[0], x[1], out evn[0]); + odd[1] = Interleave.Unshuffle(x[2], x[3], out evn[1]); + odd[2] = Interleave.Unshuffle(x[4], x[5], out evn[2]); + odd[3] = Interleave.Unshuffle(x[6], x[7], out evn[3]); + odd[4] = Interleave.Unshuffle(x[8] , out evn[4]); Multiply(odd, ROOT_Z, z); Add(z, evn, z); diff --git a/crypto/src/math/raw/Interleave.cs b/crypto/src/math/raw/Interleave.cs index 4d866c245..409ab83e8 100644 --- a/crypto/src/math/raw/Interleave.cs +++ b/crypto/src/math/raw/Interleave.cs @@ -135,6 +135,21 @@ namespace Org.BouncyCastle.Math.Raw return x; } + internal static ulong Unshuffle(ulong x, out ulong even) + { + ulong u0 = Unshuffle(x); + even = u0 & 0x00000000FFFFFFFFUL; + return u0 >> 32; + } + + internal static ulong Unshuffle(ulong x0, ulong x1, out ulong even) + { + ulong u0 = Unshuffle(x0); + ulong u1 = Unshuffle(x1); + even = (u1 << 32) | (u0 & 0x00000000FFFFFFFFUL); + return (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL); + } + internal static uint Unshuffle2(uint x) { // "unshuffle" (twice) even bits to low half and odd bits to high half |