diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-08-02 16:52:44 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-08-02 16:52:44 +0700 |
commit | b033b509c53b79d564ba17c4a69de78bd917ef80 (patch) | |
tree | be14f91f7200da0b731f60fe0c53398efe83345b | |
parent | Refactor Equals/GetHashCode in X509 objects (diff) | |
download | BouncyCastle.NET-ed25519-b033b509c53b79d564ba17c4a69de78bd917ef80.tar.xz |
Updates to raw math classes from bc-java
-rw-r--r-- | crypto/src/math/raw/Nat.cs | 120 | ||||
-rw-r--r-- | crypto/src/math/raw/Nat128.cs | 14 | ||||
-rw-r--r-- | crypto/src/math/raw/Nat160.cs | 14 | ||||
-rw-r--r-- | crypto/src/math/raw/Nat192.cs | 14 | ||||
-rw-r--r-- | crypto/src/math/raw/Nat224.cs | 14 | ||||
-rw-r--r-- | crypto/src/math/raw/Nat256.cs | 14 |
6 files changed, 148 insertions, 42 deletions
diff --git a/crypto/src/math/raw/Nat.cs b/crypto/src/math/raw/Nat.cs index 5cf5fe218..9786d3ecf 100644 --- a/crypto/src/math/raw/Nat.cs +++ b/crypto/src/math/raw/Nat.cs @@ -161,6 +161,31 @@ namespace Org.BouncyCastle.Math.Raw return (uint)c; } + public static uint AddTo(int len, uint[] x, int xOff, uint[] z, int zOff, uint cIn) + { + ulong c = cIn; + for (int i = 0; i < len; ++i) + { + c += (ulong)x[xOff + i] + z[zOff + i]; + z[zOff + i] = (uint)c; + c >>= 32; + } + return (uint)c; + } + + public static uint AddToEachOther(int len, uint[] u, int uOff, uint[] v, int vOff) + { + ulong c = 0; + for (int i = 0; i < len; ++i) + { + c += (ulong)u[uOff + i] + v[vOff + i]; + u[uOff + i] = (uint)c; + v[vOff + i] = (uint)c; + c >>= 32; + } + return (uint)c; + } + public static uint AddWordAt(int len, uint x, uint[] z, int zPos) { Debug.Assert(zPos <= (len - 1)); @@ -297,6 +322,32 @@ namespace Org.BouncyCastle.Math.Raw return new ulong[len]; } + public static int CSub(int len, int mask, uint[] x, uint[] y, uint[] z) + { + long MASK = (uint)-(mask & 1); + long c = 0; + for (int i = 0; i < len; ++i) + { + c += (long)x[i] - (y[i] & MASK); + z[i] = (uint)c; + c >>= 32; + } + return (int)c; + } + + public static int CSub(int len, int mask, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff) + { + long MASK = (uint)-(mask & 1); + long c = 0; + for (int i = 0; i < len; ++i) + { + c += (long)x[xOff + i] - (y[yOff + i] & MASK); + z[zOff + i] = (uint)c; + c >>= 32; + } + return (int)c; + } + public static int Dec(int len, uint[] z) { for (int i = 0; i < len; ++i) @@ -554,10 +605,10 @@ namespace Org.BouncyCastle.Math.Raw ulong zc = 0; for (int i = 0; i < len; ++i) { - ulong c = MulWordAddTo(len, x[i], y, 0, zz, i) & M; - c += zc + (zz[i + len] & M); - zz[i + len] = (uint)c; - zc = c >> 32; + zc += MulWordAddTo(len, x[i], y, 0, zz, i) & M; + zc += zz[i + len] & M; + zz[i + len] = (uint)zc; + zc >>= 32; } return (uint)zc; } @@ -567,10 +618,10 @@ namespace Org.BouncyCastle.Math.Raw ulong zc = 0; for (int i = 0; i < len; ++i) { - ulong c = MulWordAddTo(len, x[xOff + i], y, yOff, zz, zzOff) & M; - c += zc + (zz[zzOff + len] & M); - zz[zzOff + len] = (uint)c; - zc = c >> 32; + zc += MulWordAddTo(len, x[xOff + i], y, yOff, zz, zzOff) & M; + zc += zz[zzOff + len] & M; + zz[zzOff + len] = (uint)zc; + zc >>= 32; ++zzOff; } return (uint)zc; @@ -902,11 +953,18 @@ namespace Org.BouncyCastle.Math.Raw } while (j > 0); + ulong d = 0UL; + int zzPos = 2; + for (int i = 1; i < len; ++i) { - c = SquareWordAdd(x, i, zz); - AddWordAt(extLen, c, zz, i << 1); + d += SquareWordAddTo(x, i, zz); + d += zz[zzPos]; + zz[zzPos++] = (uint)d; d >>= 32; + d += zz[zzPos]; + zz[zzPos++] = (uint)d; d >>= 32; } + Debug.Assert(0UL == d); ShiftUpBit(extLen, zz, x[0] << 31); } @@ -926,15 +984,23 @@ namespace Org.BouncyCastle.Math.Raw } while (j > 0); + ulong d = 0UL; + int zzPos = zzOff + 2; + for (int i = 1; i < len; ++i) { - c = SquareWordAdd(x, xOff, i, zz, zzOff); - AddWordAt(extLen, c, zz, zzOff, i << 1); + d += SquareWordAddTo(x, xOff, i, zz, zzOff); + d += zz[zzPos]; + zz[zzPos++] = (uint)d; d >>= 32; + d += zz[zzPos]; + zz[zzPos++] = (uint)d; d >>= 32; } + Debug.Assert(0UL == d); ShiftUpBit(extLen, zz, zzOff, x[xOff] << 31); } + [Obsolete("Use 'SquareWordAddTo' instead")] public static uint SquareWordAdd(uint[] x, int xPos, uint[] z) { ulong c = 0, xVal = (ulong)x[xPos]; @@ -949,6 +1015,7 @@ namespace Org.BouncyCastle.Math.Raw return (uint)c; } + [Obsolete("Use 'SquareWordAddTo' instead")] public static uint SquareWordAdd(uint[] x, int xOff, int xPos, uint[] z, int zOff) { ulong c = 0, xVal = (ulong)x[xOff + xPos]; @@ -964,6 +1031,35 @@ namespace Org.BouncyCastle.Math.Raw return (uint)c; } + public static uint SquareWordAddTo(uint[] x, int xPos, uint[] z) + { + ulong c = 0, xVal = (ulong)x[xPos]; + int i = 0; + do + { + c += xVal * x[i] + z[xPos + i]; + z[xPos + i] = (uint)c; + c >>= 32; + } + while (++i < xPos); + return (uint)c; + } + + public static uint SquareWordAddTo(uint[] x, int xOff, int xPos, uint[] z, int zOff) + { + ulong c = 0, xVal = (ulong)x[xOff + xPos]; + int i = 0; + do + { + c += xVal * (x[xOff + i] & M) + (z[xPos + zOff] & M); + z[xPos + zOff] = (uint)c; + c >>= 32; + ++zOff; + } + while (++i < xPos); + return (uint)c; + } + public static int Sub(int len, uint[] x, uint[] y, uint[] z) { long c = 0; diff --git a/crypto/src/math/raw/Nat128.cs b/crypto/src/math/raw/Nat128.cs index 27ed5abe4..7617a9ee9 100644 --- a/crypto/src/math/raw/Nat128.cs +++ b/crypto/src/math/raw/Nat128.cs @@ -422,9 +422,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_3 + zz[i + 3]; zz[i + 3] = (uint)c; c >>= 32; - c += zc + zz[i + 4]; - zz[i + 4] = (uint)c; - zc = c >> 32; + + zc += c + zz[i + 4]; + zz[i + 4] = (uint)zc; + zc >>= 32; } return (uint)zc; } @@ -452,9 +453,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_3 + zz[zzOff + 3]; zz[zzOff + 3] = (uint)c; c >>= 32; - c += zc + zz[zzOff + 4]; - zz[zzOff + 4] = (uint)c; - zc = c >> 32; + + zc += c + zz[zzOff + 4]; + zz[zzOff + 4] = (uint)zc; + zc >>= 32; ++zzOff; } return (uint)zc; diff --git a/crypto/src/math/raw/Nat160.cs b/crypto/src/math/raw/Nat160.cs index 57212cae0..f5514d7b4 100644 --- a/crypto/src/math/raw/Nat160.cs +++ b/crypto/src/math/raw/Nat160.cs @@ -384,9 +384,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_4 + zz[i + 4]; zz[i + 4] = (uint)c; c >>= 32; - c += zc + zz[i + 5]; - zz[i + 5] = (uint)c; - zc = c >> 32; + + zc += c + zz[i + 5]; + zz[i + 5] = (uint)zc; + zc >>= 32; } return (uint)zc; } @@ -418,9 +419,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_4 + zz[zzOff + 4]; zz[zzOff + 4] = (uint)c; c >>= 32; - c += zc + zz[zzOff + 5]; - zz[zzOff + 5] = (uint)c; - zc = c >> 32; + + zc += c + zz[zzOff + 5]; + zz[zzOff + 5] = (uint)zc; + zc >>= 32; ++zzOff; } return (uint)zc; diff --git a/crypto/src/math/raw/Nat192.cs b/crypto/src/math/raw/Nat192.cs index 06c75aa54..1311dee04 100644 --- a/crypto/src/math/raw/Nat192.cs +++ b/crypto/src/math/raw/Nat192.cs @@ -498,9 +498,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_5 + zz[i + 5]; zz[i + 5] = (uint)c; c >>= 32; - c += zc + zz[i + 6]; - zz[i + 6] = (uint)c; - zc = c >> 32; + + zc += c + zz[i + 6]; + zz[i + 6] = (uint)zc; + zc >>= 32; } return (uint)zc; } @@ -536,9 +537,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_5 + zz[zzOff + 5]; zz[zzOff + 5] = (uint)c; c >>= 32; - c += zc + zz[zzOff + 6]; - zz[zzOff + 6] = (uint)c; - zc = c >> 32; + + zc += c + zz[zzOff + 6]; + zz[zzOff + 6] = (uint)zc; + zc >>= 32; ++zzOff; } return (uint)zc; diff --git a/crypto/src/math/raw/Nat224.cs b/crypto/src/math/raw/Nat224.cs index ff1eb6306..565abcb9c 100644 --- a/crypto/src/math/raw/Nat224.cs +++ b/crypto/src/math/raw/Nat224.cs @@ -509,9 +509,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_6 + zz[i + 6]; zz[i + 6] = (uint)c; c >>= 32; - c += zc + zz[i + 7]; - zz[i + 7] = (uint)c; - zc = c >> 32; + + zc += c + zz[i + 7]; + zz[i + 7] = (uint)zc; + zc >>= 32; } return (uint)zc; } @@ -551,9 +552,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_6 + zz[zzOff + 6]; zz[zzOff + 6] = (uint)c; c >>= 32; - c += zc + zz[zzOff + 7]; - zz[zzOff + 7] = (uint)c; - zc = c >> 32; + + zc += c + zz[zzOff + 7]; + zz[zzOff + 7] = (uint)zc; + zc >>= 32; ++zzOff; } return (uint)zc; diff --git a/crypto/src/math/raw/Nat256.cs b/crypto/src/math/raw/Nat256.cs index 2be03d642..5c473c405 100644 --- a/crypto/src/math/raw/Nat256.cs +++ b/crypto/src/math/raw/Nat256.cs @@ -632,9 +632,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_7 + zz[i + 7]; zz[i + 7] = (uint)c; c >>= 32; - c += zc + zz[i + 8]; - zz[i + 8] = (uint)c; - zc = c >> 32; + + zc += c + zz[i + 8]; + zz[i + 8] = (uint)zc; + zc >>= 32; } return (uint)zc; } @@ -678,9 +679,10 @@ namespace Org.BouncyCastle.Math.Raw c += x_i * y_7 + zz[zzOff + 7]; zz[zzOff + 7] = (uint)c; c >>= 32; - c += zc + zz[zzOff + 8]; - zz[zzOff + 8] = (uint)c; - zc = c >> 32; + + zc += c + zz[zzOff + 8]; + zz[zzOff + 8] = (uint)zc; + zc >>= 32; ++zzOff; } return (uint)zc; |