add all
This commit is contained in:
8
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/djb.meta
vendored
Normal file
8
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/djb.meta
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62f318930c856fc418115396d3c8d386
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 918daba9489c9ac4a81cf8769c7c518d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09cf8f472b66db04ebbde3a88ce37d44
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm.meta
vendored
Normal file
8
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm.meta
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a169e85b7fbd1d34d94d1220c74f9a19
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm/SM2P256V1Curve.cs
vendored
Normal file
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm/SM2P256V1Curve.cs
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.GM
|
||||
{
|
||||
internal class SM2P256V1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SM2P256V1FieldElement.Q;
|
||||
|
||||
private const int SM2P256V1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SM2P256V1_FE_INTS = 8;
|
||||
private static readonly ECFieldElement[] SM2P256V1_AFFINE_ZS = new ECFieldElement[] { new SM2P256V1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SM2P256V1Point m_infinity;
|
||||
|
||||
public SM2P256V1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SM2P256V1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SM2P256V1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SM2P256V1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SM2P256V1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SM2P256V1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SM2P256V1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SM2P256V1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat256.Copy(((SM2P256V1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SM2P256V1_FE_INTS;
|
||||
Nat256.Copy(((SM2P256V1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SM2P256V1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SM2P256V1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat256.Create();
|
||||
SM2P256V1Field.Random(r, x);
|
||||
return new SM2P256V1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat256.Create();
|
||||
SM2P256V1Field.RandomMult(r, x);
|
||||
return new SM2P256V1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SM2P256V1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SM2P256V1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SM2P256V1LookupTable(SM2P256V1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SM2P256V1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SM2P256V1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SM2P256V1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = index * SM2P256V1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SM2P256V1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SM2P256V1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SM2P256V1FieldElement(x), new SM2P256V1FieldElement(y), SM2P256V1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f6a8d7e07e8d1c8469a394ff8a7e56b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
349
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm/SM2P256V1Field.cs
vendored
Normal file
349
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm/SM2P256V1Field.cs
vendored
Normal file
@@ -0,0 +1,349 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.GM
|
||||
{
|
||||
internal class SM2P256V1Field
|
||||
{
|
||||
// 2^256 - 2^224 - 2^96 + 2^64 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE };
|
||||
private static readonly uint[] PExt = new uint[]{ 00000001, 0x00000000, 0xFFFFFFFE, 0x00000001, 0x00000001,
|
||||
0xFFFFFFFE, 0x00000000, 0x00000002, 0xFFFFFFFE, 0xFFFFFFFD, 0x00000003, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0x00000000, 0xFFFFFFFE };
|
||||
private const uint P7 = 0xFFFFFFFE;
|
||||
private const uint PExt15 = 0xFFFFFFFE;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat256.Add(x, y, z);
|
||||
if (c != 0 || (z[7] >= P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(16, xx, yy, zz);
|
||||
if (c != 0 || (zz[15] >= PExt15 && Nat.Gte(16, zz, PExt)))
|
||||
{
|
||||
Nat.SubFrom(16, PExt, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(8, x, z);
|
||||
if (c != 0 || (z[7] >= P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(256, x);
|
||||
if (z[7] >= P7 && Nat256.Gte(z, P))
|
||||
{
|
||||
Nat256.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(8, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat256.Add(x, P, z);
|
||||
Nat.ShiftDownBit(8, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat256.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[15] >= PExt15 && Nat.Gte(16, zz, PExt)))
|
||||
{
|
||||
Nat.SubFrom(16, PExt, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat256.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat256.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[8 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 8);
|
||||
}
|
||||
while (0 == Nat.LessThan(8, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
long xx08 = xx[8], xx09 = xx[9], xx10 = xx[10], xx11 = xx[11];
|
||||
long xx12 = xx[12], xx13 = xx[13], xx14 = xx[14], xx15 = xx[15];
|
||||
|
||||
long t0 = xx08 + xx09;
|
||||
long t1 = xx10 + xx11;
|
||||
long t2 = xx12 + xx15;
|
||||
long t3 = xx13 + xx14;
|
||||
long t4 = t3 + (xx15 << 1);
|
||||
|
||||
long ts = t0 + t3;
|
||||
long tt = t1 + t2 + ts;
|
||||
|
||||
long cc = 0;
|
||||
cc += (long)xx[0] + tt + xx13 + xx14 + xx15;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[1] + tt - xx08 + xx14 + xx15;
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[2] - ts;
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[3] + tt - xx09 - xx10 + xx13;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[4] + tt - t1 - xx08 + xx14;
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[5] + t4 + xx10;
|
||||
z[5] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[6] + xx11 + xx14 + xx15;
|
||||
z[6] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[7] + tt + t4 + xx12;
|
||||
z[7] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
Debug.Assert(cc >= 0);
|
||||
|
||||
Reduce32((uint)cc, z);
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
long cc = 0;
|
||||
|
||||
if (x != 0)
|
||||
{
|
||||
long xx08 = x;
|
||||
|
||||
cc += (long)z[0] + xx08;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (long)z[1];
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
}
|
||||
cc += (long)z[2] - xx08;
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[3] + xx08;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (long)z[4];
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[5];
|
||||
z[5] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[6];
|
||||
z[6] = (uint)cc;
|
||||
cc >>= 32;
|
||||
}
|
||||
cc += (long)z[7] + xx08;
|
||||
z[7] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
Debug.Assert(cc == 0 || cc == 1);
|
||||
}
|
||||
|
||||
if (cc != 0 || (z[7] >= P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat256.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat256.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
SubPInvFrom(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(16, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.AddTo(16, PExt, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(8, x, 0, z);
|
||||
if (c != 0 || (z[7] >= P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddPInvTo(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] + 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[2] - 1;
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[3] + 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[4];
|
||||
z[4] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[5];
|
||||
z[5] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[6];
|
||||
z[6] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[7] + 1;
|
||||
z[7] = (uint)c;
|
||||
//c >>= 32;
|
||||
}
|
||||
|
||||
private static void SubPInvFrom(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] - 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[2] + 1;
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[3] - 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[4];
|
||||
z[4] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[5];
|
||||
z[5] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[6];
|
||||
z[6] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[7] - 1;
|
||||
z[7] = (uint)c;
|
||||
//c >>= 32;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 641e3afe0dc583148b27346717c6c185
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,216 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.GM
|
||||
{
|
||||
internal class SM2P256V1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SM2P256V1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SM2P256V1FieldElement", "x");
|
||||
|
||||
this.x = SM2P256V1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SM2P256V1FieldElement()
|
||||
{
|
||||
this.x = Nat256.Create();
|
||||
}
|
||||
|
||||
protected internal SM2P256V1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat256.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat256.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat256.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat256.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SM2P256V1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.Add(x, ((SM2P256V1FieldElement)b).x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.AddOne(x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.Subtract(x, ((SM2P256V1FieldElement)b).x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.Multiply(x, ((SM2P256V1FieldElement)b).x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.Inv(((SM2P256V1FieldElement)b).x, z);
|
||||
SM2P256V1Field.Multiply(z, x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.Negate(x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.Square(x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SM2P256V1Field.Inv(x, z);
|
||||
return new SM2P256V1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
/*
|
||||
* Raise this element to the exponent 2^254 - 2^222 - 2^94 + 2^62
|
||||
*
|
||||
* Breaking up the exponent's binary representation into "repunits", we get:
|
||||
* { 31 1s } { 1 0s } { 128 1s } { 31 0s } { 1 1s } { 62 0s }
|
||||
*
|
||||
* We use an addition chain for the beginning: [1], 2, 3, 6, 12, [24], 30, [31]
|
||||
*/
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
uint[] x2 = Nat256.Create();
|
||||
SM2P256V1Field.Square(x1, x2);
|
||||
SM2P256V1Field.Multiply(x2, x1, x2);
|
||||
uint[] x4 = Nat256.Create();
|
||||
SM2P256V1Field.SquareN(x2, 2, x4);
|
||||
SM2P256V1Field.Multiply(x4, x2, x4);
|
||||
uint[] x6 = Nat256.Create();
|
||||
SM2P256V1Field.SquareN(x4, 2, x6);
|
||||
SM2P256V1Field.Multiply(x6, x2, x6);
|
||||
uint[] x12 = x2;
|
||||
SM2P256V1Field.SquareN(x6, 6, x12);
|
||||
SM2P256V1Field.Multiply(x12, x6, x12);
|
||||
uint[] x24 = Nat256.Create();
|
||||
SM2P256V1Field.SquareN(x12, 12, x24);
|
||||
SM2P256V1Field.Multiply(x24, x12, x24);
|
||||
uint[] x30 = x12;
|
||||
SM2P256V1Field.SquareN(x24, 6, x30);
|
||||
SM2P256V1Field.Multiply(x30, x6, x30);
|
||||
uint[] x31 = x6;
|
||||
SM2P256V1Field.Square(x30, x31);
|
||||
SM2P256V1Field.Multiply(x31, x1, x31);
|
||||
|
||||
uint[] t1 = x24;
|
||||
SM2P256V1Field.SquareN(x31, 31, t1);
|
||||
|
||||
uint[] x62 = x30;
|
||||
SM2P256V1Field.Multiply(t1, x31, x62);
|
||||
|
||||
SM2P256V1Field.SquareN(t1, 32, t1);
|
||||
SM2P256V1Field.Multiply(t1, x62, t1);
|
||||
SM2P256V1Field.SquareN(t1, 62, t1);
|
||||
SM2P256V1Field.Multiply(t1, x62, t1);
|
||||
SM2P256V1Field.SquareN(t1, 4, t1);
|
||||
SM2P256V1Field.Multiply(t1, x4, t1);
|
||||
SM2P256V1Field.SquareN(t1, 32, t1);
|
||||
SM2P256V1Field.Multiply(t1, x1, t1);
|
||||
SM2P256V1Field.SquareN(t1, 62, t1);
|
||||
|
||||
uint[] t2 = x4;
|
||||
SM2P256V1Field.Square(t1, t2);
|
||||
|
||||
return Nat256.Eq(x1, t2) ? new SM2P256V1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SM2P256V1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SM2P256V1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SM2P256V1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat256.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78e1ffe95a089cd41bcf8fd1fbde9440
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm/SM2P256V1Point.cs
vendored
Normal file
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/gm/SM2P256V1Point.cs
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.GM
|
||||
{
|
||||
internal class SM2P256V1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SM2P256V1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SM2P256V1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SM2P256V1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SM2P256V1FieldElement X1 = (SM2P256V1FieldElement)this.RawXCoord, Y1 = (SM2P256V1FieldElement)this.RawYCoord;
|
||||
SM2P256V1FieldElement X2 = (SM2P256V1FieldElement)b.RawXCoord, Y2 = (SM2P256V1FieldElement)b.RawYCoord;
|
||||
|
||||
SM2P256V1FieldElement Z1 = (SM2P256V1FieldElement)this.RawZCoords[0];
|
||||
SM2P256V1FieldElement Z2 = (SM2P256V1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat256.CreateExt();
|
||||
uint[] t2 = Nat256.Create();
|
||||
uint[] t3 = Nat256.Create();
|
||||
uint[] t4 = Nat256.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SM2P256V1Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SM2P256V1Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SM2P256V1Field.Multiply(S2, Z1.x, S2);
|
||||
SM2P256V1Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SM2P256V1Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SM2P256V1Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SM2P256V1Field.Multiply(S1, Z2.x, S1);
|
||||
SM2P256V1Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat256.Create();
|
||||
SM2P256V1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SM2P256V1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat256.IsZero(H))
|
||||
{
|
||||
if (Nat256.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SM2P256V1Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat256.Create();
|
||||
SM2P256V1Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SM2P256V1Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SM2P256V1Field.Negate(G, G);
|
||||
Nat256.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat256.AddBothTo(V, V, G);
|
||||
SM2P256V1Field.Reduce32(c, G);
|
||||
|
||||
SM2P256V1FieldElement X3 = new SM2P256V1FieldElement(t4);
|
||||
SM2P256V1Field.Square(R, X3.x);
|
||||
SM2P256V1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SM2P256V1FieldElement Y3 = new SM2P256V1FieldElement(G);
|
||||
SM2P256V1Field.Subtract(V, X3.x, Y3.x);
|
||||
SM2P256V1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SM2P256V1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SM2P256V1FieldElement Z3 = new SM2P256V1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SM2P256V1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SM2P256V1Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
|
||||
|
||||
return new SM2P256V1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SM2P256V1FieldElement Y1 = (SM2P256V1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SM2P256V1FieldElement X1 = (SM2P256V1FieldElement)this.RawXCoord, Z1 = (SM2P256V1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] t1 = Nat256.Create();
|
||||
uint[] t2 = Nat256.Create();
|
||||
|
||||
uint[] Y1Squared = Nat256.Create();
|
||||
SM2P256V1Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat256.Create();
|
||||
SM2P256V1Field.Square(Y1Squared, T);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SM2P256V1Field.Square(Z1.x, Z1Squared);
|
||||
}
|
||||
|
||||
SM2P256V1Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SM2P256V1Field.Add(X1.x, Z1Squared, M);
|
||||
SM2P256V1Field.Multiply(M, t1, M);
|
||||
c = Nat256.AddBothTo(M, M, M);
|
||||
SM2P256V1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SM2P256V1Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(8, S, 2, 0);
|
||||
SM2P256V1Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(8, T, 3, 0, t1);
|
||||
SM2P256V1Field.Reduce32(c, t1);
|
||||
|
||||
SM2P256V1FieldElement X3 = new SM2P256V1FieldElement(T);
|
||||
SM2P256V1Field.Square(M, X3.x);
|
||||
SM2P256V1Field.Subtract(X3.x, S, X3.x);
|
||||
SM2P256V1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SM2P256V1FieldElement Y3 = new SM2P256V1FieldElement(S);
|
||||
SM2P256V1Field.Subtract(S, X3.x, Y3.x);
|
||||
SM2P256V1Field.Multiply(Y3.x, M, Y3.x);
|
||||
SM2P256V1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SM2P256V1FieldElement Z3 = new SM2P256V1FieldElement(M);
|
||||
SM2P256V1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SM2P256V1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SM2P256V1Point(curve, X3, Y3, new ECFieldElement[]{ Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SM2P256V1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b1d26c8aa2f6d74785ce58e201217f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec.meta
vendored
Normal file
8
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec.meta
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ec1640b499c9e044a1564cdadc1975c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP128R1Curve.cs
vendored
Normal file
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP128R1Curve.cs
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP128R1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP128R1FieldElement.Q;
|
||||
|
||||
private const int SECP128R1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP128R1_FE_INTS = 4;
|
||||
private static readonly ECFieldElement[] SECP128R1_AFFINE_ZS = new ECFieldElement[] { new SecP128R1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP128R1Point m_infinity;
|
||||
|
||||
public SecP128R1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP128R1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("E87579C11079F43DD824993C2CEE5ED3")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFE0000000075A30D1B9038A115"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
|
||||
this.m_coord = SECP128R1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP128R1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP128R1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP128R1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP128R1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP128R1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat128.Copy(((SecP128R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP128R1_FE_INTS;
|
||||
Nat128.Copy(((SecP128R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP128R1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP128R1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat128.Create();
|
||||
SecP128R1Field.Random(r, x);
|
||||
return new SecP128R1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat128.Create();
|
||||
SecP128R1Field.RandomMult(r, x);
|
||||
return new SecP128R1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP128R1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP128R1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP128R1LookupTable(SecP128R1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat128.Create(), y = Nat128.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP128R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP128R1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP128R1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat128.Create(), y = Nat128.Create();
|
||||
int pos = index * SECP128R1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP128R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP128R1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP128R1FieldElement(x), new SecP128R1FieldElement(y), SECP128R1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89445e594db6ebf48a2bc80814ac22a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
265
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP128R1Field.cs
vendored
Normal file
265
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP128R1Field.cs
vendored
Normal file
@@ -0,0 +1,265 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP128R1Field
|
||||
{
|
||||
// 2^128 - 2^97 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFD };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0x00000004, 0xFFFFFFFE,
|
||||
0xFFFFFFFF, 0x00000003, 0xFFFFFFFC };
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFB,
|
||||
0x00000001, 0x00000000, 0xFFFFFFFC, 0x00000003 };
|
||||
private const uint P3 = 0xFFFFFFFD;
|
||||
private const uint PExt7 = 0xFFFFFFFC;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat128.Add(x, y, z);
|
||||
if (c != 0 || (z[3] >= P3 && Nat128.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat256.Add(xx, yy, zz);
|
||||
if (c != 0 || (zz[7] >= PExt7 && Nat256.Gte(zz, PExt)))
|
||||
{
|
||||
Nat.AddTo(PExtInv.Length, PExtInv, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(4, x, z);
|
||||
if (c != 0 || (z[3] >= P3 && Nat128.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(128, x);
|
||||
if (z[3] >= P3 && Nat128.Gte(z, P))
|
||||
{
|
||||
Nat128.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(4, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat128.Add(x, P, z);
|
||||
Nat.ShiftDownBit(4, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat128.CreateExt();
|
||||
Nat128.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat128.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[7] >= PExt7 && Nat256.Gte(zz, PExt)))
|
||||
{
|
||||
Nat.AddTo(PExtInv.Length, PExtInv, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat128.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat128.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[4 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 4);
|
||||
}
|
||||
while (0 == Nat.LessThan(4, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
ulong x0 = xx[0], x1 = xx[1], x2 = xx[2], x3 = xx[3];
|
||||
ulong x4 = xx[4], x5 = xx[5], x6 = xx[6], x7 = xx[7];
|
||||
|
||||
x3 += x7; x6 += (x7 << 1);
|
||||
x2 += x6; x5 += (x6 << 1);
|
||||
x1 += x5; x4 += (x5 << 1);
|
||||
x0 += x4; x3 += (x4 << 1);
|
||||
|
||||
z[0] = (uint)x0; x1 += (x0 >> 32);
|
||||
z[1] = (uint)x1; x2 += (x1 >> 32);
|
||||
z[2] = (uint)x2; x3 += (x2 >> 32);
|
||||
z[3] = (uint)x3;
|
||||
|
||||
Reduce32((uint)(x3 >> 32), z);
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
while (x != 0)
|
||||
{
|
||||
ulong c, x4 = x;
|
||||
|
||||
c = (ulong)z[0] + x4;
|
||||
z[0] = (uint)c; c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (ulong)z[1];
|
||||
z[1] = (uint)c; c >>= 32;
|
||||
c += (ulong)z[2];
|
||||
z[2] = (uint)c; c >>= 32;
|
||||
}
|
||||
c += (ulong)z[3] + (x4 << 1);
|
||||
z[3] = (uint)c; c >>= 32;
|
||||
|
||||
Debug.Assert(c >= 0 && c <= 2);
|
||||
|
||||
x = (uint)c;
|
||||
}
|
||||
|
||||
if (z[3] >= P3 && Nat128.Gte(z, P))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat128.CreateExt();
|
||||
Nat128.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat128.CreateExt();
|
||||
Nat128.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat128.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat128.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
SubPInvFrom(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(10, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.SubFrom(PExtInv.Length, PExtInv, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(4, x, 0, z);
|
||||
if (c != 0 || (z[3] >= P3 && Nat128.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddPInvTo(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] + 1;
|
||||
z[0] = (uint)c; c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c; c >>= 32;
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c; c >>= 32;
|
||||
}
|
||||
c += (long)z[3] + 2;
|
||||
z[3] = (uint)c;
|
||||
}
|
||||
|
||||
private static void SubPInvFrom(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] - 1;
|
||||
z[0] = (uint)c; c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c; c >>= 32;
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c; c >>= 32;
|
||||
}
|
||||
c += (long)z[3] - 2;
|
||||
z[3] = (uint)c;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8d3cfee2e6a54024a900a084075280c8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,203 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP128R1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP128R1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP128R1FieldElement", "x");
|
||||
|
||||
this.x = SecP128R1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP128R1FieldElement()
|
||||
{
|
||||
this.x = Nat128.Create();
|
||||
}
|
||||
|
||||
protected internal SecP128R1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat128.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat128.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat128.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat128.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP128R1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.Add(x, ((SecP128R1FieldElement)b).x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.AddOne(x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.Subtract(x, ((SecP128R1FieldElement)b).x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.Multiply(x, ((SecP128R1FieldElement)b).x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
// return multiply(b.invert());
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.Inv(((SecP128R1FieldElement)b).x, z);
|
||||
SecP128R1Field.Multiply(z, x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.Negate(x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.Square(x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat128.Create();
|
||||
SecP128R1Field.Inv(x, z);
|
||||
return new SecP128R1FieldElement(z);
|
||||
}
|
||||
|
||||
// D.1.4 91
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
/*
|
||||
* Raise this element to the exponent 2^126 - 2^95
|
||||
*
|
||||
* Breaking up the exponent's binary representation into "repunits", we get:
|
||||
* { 31 1s } { 95 0s }
|
||||
*
|
||||
* Therefore we need an addition chain containing 31 (the length of the repunit) We use:
|
||||
* 1, 2, 4, 8, 10, 20, 30, [31]
|
||||
*/
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat128.IsZero(x1) || Nat128.IsOne(x1))
|
||||
return this;
|
||||
|
||||
uint[] x2 = Nat128.Create();
|
||||
SecP128R1Field.Square(x1, x2);
|
||||
SecP128R1Field.Multiply(x2, x1, x2);
|
||||
uint[] x4 = Nat128.Create();
|
||||
SecP128R1Field.SquareN(x2, 2, x4);
|
||||
SecP128R1Field.Multiply(x4, x2, x4);
|
||||
uint[] x8 = Nat128.Create();
|
||||
SecP128R1Field.SquareN(x4, 4, x8);
|
||||
SecP128R1Field.Multiply(x8, x4, x8);
|
||||
uint[] x10 = x4;
|
||||
SecP128R1Field.SquareN(x8, 2, x10);
|
||||
SecP128R1Field.Multiply(x10, x2, x10);
|
||||
uint[] x20 = x2;
|
||||
SecP128R1Field.SquareN(x10, 10, x20);
|
||||
SecP128R1Field.Multiply(x20, x10, x20);
|
||||
uint[] x30 = x8;
|
||||
SecP128R1Field.SquareN(x20, 10, x30);
|
||||
SecP128R1Field.Multiply(x30, x10, x30);
|
||||
uint[] x31 = x10;
|
||||
SecP128R1Field.Square(x30, x31);
|
||||
SecP128R1Field.Multiply(x31, x1, x31);
|
||||
|
||||
uint[] t1 = x31;
|
||||
SecP128R1Field.SquareN(t1, 95, t1);
|
||||
|
||||
uint[] t2 = x30;
|
||||
SecP128R1Field.Square(t1, t2);
|
||||
|
||||
return Nat128.Eq(x1, t2) ? new SecP128R1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP128R1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP128R1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP128R1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat128.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 69c28fc8275118743a6303f5213bd5ef
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP128R1Point.cs
vendored
Normal file
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP128R1Point.cs
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP128R1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP128R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP128R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP128R1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP128R1FieldElement X1 = (SecP128R1FieldElement)this.RawXCoord, Y1 = (SecP128R1FieldElement)this.RawYCoord;
|
||||
SecP128R1FieldElement X2 = (SecP128R1FieldElement)b.RawXCoord, Y2 = (SecP128R1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP128R1FieldElement Z1 = (SecP128R1FieldElement)this.RawZCoords[0];
|
||||
SecP128R1FieldElement Z2 = (SecP128R1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat128.CreateExt();
|
||||
uint[] t2 = Nat128.Create();
|
||||
uint[] t3 = Nat128.Create();
|
||||
uint[] t4 = Nat128.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP128R1Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP128R1Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP128R1Field.Multiply(S2, Z1.x, S2);
|
||||
SecP128R1Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP128R1Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP128R1Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP128R1Field.Multiply(S1, Z2.x, S1);
|
||||
SecP128R1Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat128.Create();
|
||||
SecP128R1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP128R1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat128.IsZero(H))
|
||||
{
|
||||
if (Nat128.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP128R1Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat128.Create();
|
||||
SecP128R1Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP128R1Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP128R1Field.Negate(G, G);
|
||||
Nat128.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat128.AddBothTo(V, V, G);
|
||||
SecP128R1Field.Reduce32(c, G);
|
||||
|
||||
SecP128R1FieldElement X3 = new SecP128R1FieldElement(t4);
|
||||
SecP128R1Field.Square(R, X3.x);
|
||||
SecP128R1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP128R1FieldElement Y3 = new SecP128R1FieldElement(G);
|
||||
SecP128R1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP128R1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP128R1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP128R1FieldElement Z3 = new SecP128R1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP128R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP128R1Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
|
||||
|
||||
return new SecP128R1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP128R1FieldElement Y1 = (SecP128R1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP128R1FieldElement X1 = (SecP128R1FieldElement)this.RawXCoord, Z1 = (SecP128R1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] t1 = Nat128.Create();
|
||||
uint[] t2 = Nat128.Create();
|
||||
|
||||
uint[] Y1Squared = Nat128.Create();
|
||||
SecP128R1Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat128.Create();
|
||||
SecP128R1Field.Square(Y1Squared, T);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SecP128R1Field.Square(Z1.x, Z1Squared);
|
||||
}
|
||||
|
||||
SecP128R1Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SecP128R1Field.Add(X1.x, Z1Squared, M);
|
||||
SecP128R1Field.Multiply(M, t1, M);
|
||||
c = Nat128.AddBothTo(M, M, M);
|
||||
SecP128R1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP128R1Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(4, S, 2, 0);
|
||||
SecP128R1Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(4, T, 3, 0, t1);
|
||||
SecP128R1Field.Reduce32(c, t1);
|
||||
|
||||
SecP128R1FieldElement X3 = new SecP128R1FieldElement(T);
|
||||
SecP128R1Field.Square(M, X3.x);
|
||||
SecP128R1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP128R1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP128R1FieldElement Y3 = new SecP128R1FieldElement(S);
|
||||
SecP128R1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP128R1Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP128R1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP128R1FieldElement Z3 = new SecP128R1FieldElement(M);
|
||||
SecP128R1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP128R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP128R1Point(curve, X3, Y3, new ECFieldElement[]{ Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between twicePlus and threeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP128R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ede02f0086365e54584f6ee4a1ae2434
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160K1Curve.cs
vendored
Normal file
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160K1Curve.cs
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160K1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP160R2FieldElement.Q;
|
||||
|
||||
private const int SECP160K1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP160K1_FE_INTS = 5;
|
||||
private static readonly ECFieldElement[] SECP160K1_AFFINE_ZS = new ECFieldElement[] { new SecP160R2FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP160K1Point m_infinity;
|
||||
|
||||
public SecP160K1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP160K1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(BigInteger.Zero);
|
||||
this.m_b = FromBigInteger(BigInteger.ValueOf(7));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("0100000000000000000001B8FA16DFAB9ACA16B6B3"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SECP160K1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP160K1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP160R2FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP160K1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP160K1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP160K1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat160.Copy(((SecP160R2FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP160K1_FE_INTS;
|
||||
Nat160.Copy(((SecP160R2FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP160K1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP160K1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat160.Create();
|
||||
SecP160R2Field.Random(r, x);
|
||||
return new SecP160R2FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat160.Create();
|
||||
SecP160R2Field.RandomMult(r, x);
|
||||
return new SecP160R2FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP160K1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP160K1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP160K1LookupTable(SecP160K1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP160K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP160K1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP160K1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = index * SECP160K1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP160K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP160K1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP160R2FieldElement(x), new SecP160R2FieldElement(y), SECP160K1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea8333585039e43408c80fccb422d442
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
238
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160K1Point.cs
vendored
Normal file
238
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160K1Point.cs
vendored
Normal file
@@ -0,0 +1,238 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160K1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP160K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP160K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP160K1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
// B.3 pg 62
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Y1 = (SecP160R2FieldElement)this.RawYCoord;
|
||||
SecP160R2FieldElement X2 = (SecP160R2FieldElement)b.RawXCoord, Y2 = (SecP160R2FieldElement)b.RawYCoord;
|
||||
|
||||
SecP160R2FieldElement Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
|
||||
SecP160R2FieldElement Z2 = (SecP160R2FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat160.CreateExt();
|
||||
uint[] t2 = Nat160.Create();
|
||||
uint[] t3 = Nat160.Create();
|
||||
uint[] t4 = Nat160.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP160R2Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP160R2Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP160R2Field.Multiply(S2, Z1.x, S2);
|
||||
SecP160R2Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP160R2Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP160R2Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP160R2Field.Multiply(S1, Z2.x, S1);
|
||||
SecP160R2Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat160.Create();
|
||||
SecP160R2Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP160R2Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat160.IsZero(H))
|
||||
{
|
||||
if (Nat160.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP160R2Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat160.Create();
|
||||
SecP160R2Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP160R2Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP160R2Field.Negate(G, G);
|
||||
Nat160.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat160.AddBothTo(V, V, G);
|
||||
SecP160R2Field.Reduce32(c, G);
|
||||
|
||||
SecP160R2FieldElement X3 = new SecP160R2FieldElement(t4);
|
||||
SecP160R2Field.Square(R, X3.x);
|
||||
SecP160R2Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP160R2FieldElement Y3 = new SecP160R2FieldElement(G);
|
||||
SecP160R2Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP160R2Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP160R2Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP160R2FieldElement Z3 = new SecP160R2FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP160R2Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
|
||||
|
||||
return new SecP160K1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
// B.3 pg 62
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP160R2FieldElement Y1 = (SecP160R2FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
|
||||
uint[] Y1Squared = Nat160.Create();
|
||||
SecP160R2Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat160.Create();
|
||||
SecP160R2Field.Square(Y1Squared, T);
|
||||
|
||||
uint[] M = Nat160.Create();
|
||||
SecP160R2Field.Square(X1.x, M);
|
||||
c = Nat160.AddBothTo(M, M, M);
|
||||
SecP160R2Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP160R2Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(5, S, 2, 0);
|
||||
SecP160R2Field.Reduce32(c, S);
|
||||
|
||||
uint[] t1 = Nat160.Create();
|
||||
c = Nat.ShiftUpBits(5, T, 3, 0, t1);
|
||||
SecP160R2Field.Reduce32(c, t1);
|
||||
|
||||
SecP160R2FieldElement X3 = new SecP160R2FieldElement(T);
|
||||
SecP160R2Field.Square(M, X3.x);
|
||||
SecP160R2Field.Subtract(X3.x, S, X3.x);
|
||||
SecP160R2Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP160R2FieldElement Y3 = new SecP160R2FieldElement(S);
|
||||
SecP160R2Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP160R2Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP160R2Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP160R2FieldElement Z3 = new SecP160R2FieldElement(M);
|
||||
SecP160R2Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1.IsOne)
|
||||
{
|
||||
SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP160K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and threeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP160K1Point(Curve, this.RawXCoord, this.RawYCoord.Negate(), this.RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 751987f4dd7ab91428967217a99fe3eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R1Curve.cs
vendored
Normal file
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R1Curve.cs
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP160R1FieldElement.Q;
|
||||
|
||||
private const int SECP160R1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP160R1_FE_INTS = 5;
|
||||
private static readonly ECFieldElement[] SECP160R1_AFFINE_ZS = new ECFieldElement[] { new SecP160R1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP160R1Point m_infinity;
|
||||
|
||||
public SecP160R1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP160R1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("0100000000000000000001F4C8F927AED3CA752257"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
|
||||
this.m_coord = SECP160R1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP160R1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP160R1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP160R1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP160R1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP160R1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat160.Copy(((SecP160R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP160R1_FE_INTS;
|
||||
Nat160.Copy(((SecP160R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP160R1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP160R1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat160.Create();
|
||||
SecP160R1Field.Random(r, x);
|
||||
return new SecP160R1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat160.Create();
|
||||
SecP160R1Field.RandomMult(r, x);
|
||||
return new SecP160R1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP160R1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP160R1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP160R1LookupTable(SecP160R1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat160.Create(), y = Nat160.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP160R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP160R1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP160R1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat160.Create(), y = Nat160.Create();
|
||||
int pos = index * SECP160R1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP160R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP160R1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP160R1FieldElement(x), new SecP160R1FieldElement(y), SECP160R1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2470d39d565196048b3caa3c8e57defc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
228
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R1Field.cs
vendored
Normal file
228
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R1Field.cs
vendored
Normal file
@@ -0,0 +1,228 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R1Field
|
||||
{
|
||||
// 2^160 - 2^31 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x40000001, 0x00000000, 0x00000000, 0x00000000,
|
||||
0xFFFFFFFE, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0xBFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0x00000001, 0x00000001 };
|
||||
private const uint P4 = 0xFFFFFFFF;
|
||||
private const uint PExt9 = 0xFFFFFFFF;
|
||||
private const uint PInv = 0x80000001;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat160.Add(x, y, z);
|
||||
if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.AddWordTo(5, PInv, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(10, xx, yy, zz);
|
||||
if (c != 0 || (zz[9] == PExt9 && Nat.Gte(10, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(10, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(5, x, z);
|
||||
if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.AddWordTo(5, PInv, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(160, x);
|
||||
if (z[4] == P4 && Nat160.Gte(z, P))
|
||||
{
|
||||
Nat160.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(5, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat160.Add(x, P, z);
|
||||
Nat.ShiftDownBit(5, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat160.CreateExt();
|
||||
Nat160.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat160.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[9] == PExt9 && Nat.Gte(10, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(10, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat160.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat160.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[5 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 5);
|
||||
}
|
||||
while (0 == Nat.LessThan(5, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
ulong x5 = xx[5], x6 = xx[6], x7 = xx[7], x8 = xx[8], x9 = xx[9];
|
||||
|
||||
ulong c = 0;
|
||||
c += (ulong)xx[0] + x5 + (x5 << 31);
|
||||
z[0] = (uint)c; c >>= 32;
|
||||
c += (ulong)xx[1] + x6 + (x6 << 31);
|
||||
z[1] = (uint)c; c >>= 32;
|
||||
c += (ulong)xx[2] + x7 + (x7 << 31);
|
||||
z[2] = (uint)c; c >>= 32;
|
||||
c += (ulong)xx[3] + x8 + (x8 << 31);
|
||||
z[3] = (uint)c; c >>= 32;
|
||||
c += (ulong)xx[4] + x9 + (x9 << 31);
|
||||
z[4] = (uint)c; c >>= 32;
|
||||
|
||||
Debug.Assert(c >> 32 == 0);
|
||||
|
||||
Reduce32((uint)c, z);
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
if ((x != 0 && Nat160.MulWordsAdd(PInv, x, z, 0) != 0)
|
||||
|| (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.AddWordTo(5, PInv, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat160.CreateExt();
|
||||
Nat160.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat160.CreateExt();
|
||||
Nat160.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat160.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat160.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.SubWordFrom(5, PInv, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(10, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(10, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(5, x, 0, z);
|
||||
if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.AddWordTo(5, PInv, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 841753f0e6888334aab3a0b0c9d7a34f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,208 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP160R1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP160R1FieldElement", "x");
|
||||
|
||||
this.x = SecP160R1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP160R1FieldElement()
|
||||
{
|
||||
this.x = Nat160.Create();
|
||||
}
|
||||
|
||||
protected internal SecP160R1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat160.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat160.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat160.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat160.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP160R1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.Add(x, ((SecP160R1FieldElement)b).x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.AddOne(x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.Subtract(x, ((SecP160R1FieldElement)b).x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.Multiply(x, ((SecP160R1FieldElement)b).x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
// return multiply(b.invert());
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.Inv(((SecP160R1FieldElement)b).x, z);
|
||||
SecP160R1Field.Multiply(z, x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.Negate(x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.Square(x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R1Field.Inv(x, z);
|
||||
return new SecP160R1FieldElement(z);
|
||||
}
|
||||
|
||||
// D.1.4 91
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
/*
|
||||
* Raise this element to the exponent 2^158 - 2^29
|
||||
*
|
||||
* Breaking up the exponent's binary representation into "repunits", we get:
|
||||
* { 129 1s } { 29 0s }
|
||||
*
|
||||
* Therefore we need an addition chain containing 129 (the length of the repunit) We use:
|
||||
* 1, 2, 4, 8, 16, 32, 64, 128, [129]
|
||||
*/
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat160.IsZero(x1) || Nat160.IsOne(x1))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
uint[] x2 = Nat160.Create();
|
||||
SecP160R1Field.Square(x1, x2);
|
||||
SecP160R1Field.Multiply(x2, x1, x2);
|
||||
uint[] x4 = Nat160.Create();
|
||||
SecP160R1Field.SquareN(x2, 2, x4);
|
||||
SecP160R1Field.Multiply(x4, x2, x4);
|
||||
uint[] x8 = x2;
|
||||
SecP160R1Field.SquareN(x4, 4, x8);
|
||||
SecP160R1Field.Multiply(x8, x4, x8);
|
||||
uint[] x16 = x4;
|
||||
SecP160R1Field.SquareN(x8, 8, x16);
|
||||
SecP160R1Field.Multiply(x16, x8, x16);
|
||||
uint[] x32 = x8;
|
||||
SecP160R1Field.SquareN(x16, 16, x32);
|
||||
SecP160R1Field.Multiply(x32, x16, x32);
|
||||
uint[] x64 = x16;
|
||||
SecP160R1Field.SquareN(x32, 32, x64);
|
||||
SecP160R1Field.Multiply(x64, x32, x64);
|
||||
uint[] x128 = x32;
|
||||
SecP160R1Field.SquareN(x64, 64, x128);
|
||||
SecP160R1Field.Multiply(x128, x64, x128);
|
||||
uint[] x129 = x64;
|
||||
SecP160R1Field.Square(x128, x129);
|
||||
SecP160R1Field.Multiply(x129, x1, x129);
|
||||
|
||||
uint[] t1 = x129;
|
||||
SecP160R1Field.SquareN(t1, 29, t1);
|
||||
|
||||
uint[] t2 = x128;
|
||||
SecP160R1Field.Square(t1, t2);
|
||||
|
||||
return Nat160.Eq(x1, t2) ? new SecP160R1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP160R1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP160R1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP160R1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat160.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: babb04ce52cde9d48a3badbb7728aca0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R1Point.cs
vendored
Normal file
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R1Point.cs
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP160R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP160R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP160R1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP160R1FieldElement X1 = (SecP160R1FieldElement)this.RawXCoord, Y1 = (SecP160R1FieldElement)this.RawYCoord;
|
||||
SecP160R1FieldElement X2 = (SecP160R1FieldElement)b.RawXCoord, Y2 = (SecP160R1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP160R1FieldElement Z1 = (SecP160R1FieldElement)this.RawZCoords[0];
|
||||
SecP160R1FieldElement Z2 = (SecP160R1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat160.CreateExt();
|
||||
uint[] t2 = Nat160.Create();
|
||||
uint[] t3 = Nat160.Create();
|
||||
uint[] t4 = Nat160.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP160R1Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP160R1Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP160R1Field.Multiply(S2, Z1.x, S2);
|
||||
SecP160R1Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP160R1Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP160R1Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP160R1Field.Multiply(S1, Z2.x, S1);
|
||||
SecP160R1Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat160.Create();
|
||||
SecP160R1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP160R1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat160.IsZero(H))
|
||||
{
|
||||
if (Nat160.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP160R1Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat160.Create();
|
||||
SecP160R1Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP160R1Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP160R1Field.Negate(G, G);
|
||||
Nat160.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat160.AddBothTo(V, V, G);
|
||||
SecP160R1Field.Reduce32(c, G);
|
||||
|
||||
SecP160R1FieldElement X3 = new SecP160R1FieldElement(t4);
|
||||
SecP160R1Field.Square(R, X3.x);
|
||||
SecP160R1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP160R1FieldElement Y3 = new SecP160R1FieldElement(G);
|
||||
SecP160R1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP160R1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP160R1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP160R1FieldElement Z3 = new SecP160R1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP160R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP160R1Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
|
||||
|
||||
return new SecP160R1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP160R1FieldElement Y1 = (SecP160R1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP160R1FieldElement X1 = (SecP160R1FieldElement)this.RawXCoord, Z1 = (SecP160R1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] t1 = Nat160.Create();
|
||||
uint[] t2 = Nat160.Create();
|
||||
|
||||
uint[] Y1Squared = Nat160.Create();
|
||||
SecP160R1Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat160.Create();
|
||||
SecP160R1Field.Square(Y1Squared, T);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SecP160R1Field.Square(Z1.x, Z1Squared);
|
||||
}
|
||||
|
||||
SecP160R1Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SecP160R1Field.Add(X1.x, Z1Squared, M);
|
||||
SecP160R1Field.Multiply(M, t1, M);
|
||||
c = Nat160.AddBothTo(M, M, M);
|
||||
SecP160R1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP160R1Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(5, S, 2, 0);
|
||||
SecP160R1Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(5, T, 3, 0, t1);
|
||||
SecP160R1Field.Reduce32(c, t1);
|
||||
|
||||
SecP160R1FieldElement X3 = new SecP160R1FieldElement(T);
|
||||
SecP160R1Field.Square(M, X3.x);
|
||||
SecP160R1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP160R1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP160R1FieldElement Y3 = new SecP160R1FieldElement(S);
|
||||
SecP160R1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP160R1Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP160R1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP160R1FieldElement Z3 = new SecP160R1FieldElement(M);
|
||||
SecP160R1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP160R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP160R1Point(curve, X3, Y3, new ECFieldElement[]{ Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP160R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5257bd5e96b8134cafbdb832c1465f0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R2Curve.cs
vendored
Normal file
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R2Curve.cs
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R2Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP160R2FieldElement.Q;
|
||||
|
||||
private const int SECP160R2_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP160R2_FE_INTS = 5;
|
||||
private static readonly ECFieldElement[] SECP160R2_AFFINE_ZS = new ECFieldElement[] { new SecP160R2FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP160R2Point m_infinity;
|
||||
|
||||
public SecP160R2Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP160R2Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC70")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("B4E134D3FB59EB8BAB57274904664D5AF50388BA")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("0100000000000000000000351EE786A818F3A1A16B"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
|
||||
this.m_coord = SECP160R2_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP160R2Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP160R2FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP160R2Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP160R2Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP160R2_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat160.Copy(((SecP160R2FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP160R2_FE_INTS;
|
||||
Nat160.Copy(((SecP160R2FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP160R2_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP160R2LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat160.Create();
|
||||
SecP160R2Field.Random(r, x);
|
||||
return new SecP160R2FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat160.Create();
|
||||
SecP160R2Field.RandomMult(r, x);
|
||||
return new SecP160R2FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP160R2LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP160R2Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP160R2LookupTable(SecP160R2Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat160.Create(), y = Nat160.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP160R2_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP160R2_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP160R2_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat160.Create(), y = Nat160.Create();
|
||||
int pos = index * SECP160R2_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP160R2_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP160R2_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP160R2FieldElement(x), new SecP160R2FieldElement(y), SECP160R2_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6713865f83b41649a4c63d52eae94f6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
220
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R2Field.cs
vendored
Normal file
220
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R2Field.cs
vendored
Normal file
@@ -0,0 +1,220 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R2Field
|
||||
{
|
||||
// 2^160 - 2^32 - 2^14 - 2^12 - 2^9 - 2^8 - 2^7 - 2^3 - 2^2 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFAC73, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x1B44BBA9, 0x0000A71A, 0x00000001, 0x00000000, 0x00000000,
|
||||
0xFFFF58E6, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xE4BB4457, 0xFFFF58E5, 0xFFFFFFFE, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0x0000A719, 0x00000002 };
|
||||
private const uint P4 = 0xFFFFFFFF;
|
||||
private const uint PExt9 = 0xFFFFFFFF;
|
||||
private const uint PInv33 = 0x538D;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat160.Add(x, y, z);
|
||||
if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(5, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(10, xx, yy, zz);
|
||||
if (c != 0 || (zz[9] == PExt9 && Nat.Gte(10, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(10, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(5, x, z);
|
||||
if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(5, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(160, x);
|
||||
if (z[4] == P4 && Nat160.Gte(z, P))
|
||||
{
|
||||
Nat160.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(5, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat160.Add(x, P, z);
|
||||
Nat.ShiftDownBit(5, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat160.CreateExt();
|
||||
Nat160.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat160.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[9] == PExt9 && Nat.Gte(10, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(10, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat160.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat160.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[5 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 5);
|
||||
}
|
||||
while (0 == Nat.LessThan(5, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
ulong cc = Nat160.Mul33Add(PInv33, xx, 5, xx, 0, z, 0);
|
||||
uint c = Nat160.Mul33DWordAdd(PInv33, cc, z, 0);
|
||||
|
||||
Debug.Assert(c == 0 || c == 1);
|
||||
|
||||
if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(5, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
if ((x != 0 && Nat160.Mul33WordAdd(PInv33, x, z, 0) != 0)
|
||||
|| (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(5, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat160.CreateExt();
|
||||
Nat160.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat160.CreateExt();
|
||||
Nat160.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat160.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat160.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.Sub33From(5, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(10, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(10, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(5, x, 0, z);
|
||||
if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(5, PInv33, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2390e73468d24064b83cb935831081b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,223 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R2FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP160R2FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP160R2FieldElement", "x");
|
||||
|
||||
this.x = SecP160R2Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP160R2FieldElement()
|
||||
{
|
||||
this.x = Nat160.Create();
|
||||
}
|
||||
|
||||
protected internal SecP160R2FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat160.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat160.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat160.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat160.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP160R2Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.Add(x, ((SecP160R2FieldElement)b).x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.AddOne(x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.Subtract(x, ((SecP160R2FieldElement)b).x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.Multiply(x, ((SecP160R2FieldElement)b).x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
// return Multiply(b.invert());
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.Inv(((SecP160R2FieldElement)b).x, z);
|
||||
SecP160R2Field.Multiply(z, x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.Negate(x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.Square(x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat160.Create();
|
||||
SecP160R2Field.Inv(x, z);
|
||||
return new SecP160R2FieldElement(z);
|
||||
}
|
||||
|
||||
// D.1.4 91
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
/*
|
||||
* Raise this element to the exponent 2^158 - 2^30 - 2^12 - 2^10 - 2^7 - 2^6 - 2^5 - 2^1 - 2^0
|
||||
*
|
||||
* Breaking up the exponent's binary representation into "repunits", we get: { 127 1s } { 1
|
||||
* 0s } { 17 1s } { 1 0s } { 1 1s } { 1 0s } { 2 1s } { 3 0s } { 3 1s } { 1 0s } { 1 1s }
|
||||
*
|
||||
* Therefore we need an Addition chain containing 1, 2, 3, 17, 127 (the lengths of the repunits)
|
||||
* We use: [1], [2], [3], 4, 7, 14, [17], 31, 62, 124, [127]
|
||||
*/
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat160.IsZero(x1) || Nat160.IsOne(x1))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
uint[] x2 = Nat160.Create();
|
||||
SecP160R2Field.Square(x1, x2);
|
||||
SecP160R2Field.Multiply(x2, x1, x2);
|
||||
uint[] x3 = Nat160.Create();
|
||||
SecP160R2Field.Square(x2, x3);
|
||||
SecP160R2Field.Multiply(x3, x1, x3);
|
||||
uint[] x4 = Nat160.Create();
|
||||
SecP160R2Field.Square(x3, x4);
|
||||
SecP160R2Field.Multiply(x4, x1, x4);
|
||||
uint[] x7 = Nat160.Create();
|
||||
SecP160R2Field.SquareN(x4, 3, x7);
|
||||
SecP160R2Field.Multiply(x7, x3, x7);
|
||||
uint[] x14 = x4;
|
||||
SecP160R2Field.SquareN(x7, 7, x14);
|
||||
SecP160R2Field.Multiply(x14, x7, x14);
|
||||
uint[] x17 = x7;
|
||||
SecP160R2Field.SquareN(x14, 3, x17);
|
||||
SecP160R2Field.Multiply(x17, x3, x17);
|
||||
uint[] x31 = Nat160.Create();
|
||||
SecP160R2Field.SquareN(x17, 14, x31);
|
||||
SecP160R2Field.Multiply(x31, x14, x31);
|
||||
uint[] x62 = x14;
|
||||
SecP160R2Field.SquareN(x31, 31, x62);
|
||||
SecP160R2Field.Multiply(x62, x31, x62);
|
||||
uint[] x124 = x31;
|
||||
SecP160R2Field.SquareN(x62, 62, x124);
|
||||
SecP160R2Field.Multiply(x124, x62, x124);
|
||||
uint[] x127 = x62;
|
||||
SecP160R2Field.SquareN(x124, 3, x127);
|
||||
SecP160R2Field.Multiply(x127, x3, x127);
|
||||
|
||||
uint[] t1 = x127;
|
||||
SecP160R2Field.SquareN(t1, 18, t1);
|
||||
SecP160R2Field.Multiply(t1, x17, t1);
|
||||
SecP160R2Field.SquareN(t1, 2, t1);
|
||||
SecP160R2Field.Multiply(t1, x1, t1);
|
||||
SecP160R2Field.SquareN(t1, 3, t1);
|
||||
SecP160R2Field.Multiply(t1, x2, t1);
|
||||
SecP160R2Field.SquareN(t1, 6, t1);
|
||||
SecP160R2Field.Multiply(t1, x3, t1);
|
||||
SecP160R2Field.SquareN(t1, 2, t1);
|
||||
SecP160R2Field.Multiply(t1, x1, t1);
|
||||
|
||||
uint[] t2 = x2;
|
||||
SecP160R2Field.Square(t1, t2);
|
||||
|
||||
return Nat160.Eq(x1, t2) ? new SecP160R2FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP160R2FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP160R2FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP160R2FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat160.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6dcbdbdfbeb7864479dc053e9004a4b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R2Point.cs
vendored
Normal file
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP160R2Point.cs
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP160R2Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP160R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP160R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP160R2Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Y1 = (SecP160R2FieldElement)this.RawYCoord;
|
||||
SecP160R2FieldElement X2 = (SecP160R2FieldElement)b.RawXCoord, Y2 = (SecP160R2FieldElement)b.RawYCoord;
|
||||
|
||||
SecP160R2FieldElement Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
|
||||
SecP160R2FieldElement Z2 = (SecP160R2FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat160.CreateExt();
|
||||
uint[] t2 = Nat160.Create();
|
||||
uint[] t3 = Nat160.Create();
|
||||
uint[] t4 = Nat160.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP160R2Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP160R2Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP160R2Field.Multiply(S2, Z1.x, S2);
|
||||
SecP160R2Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP160R2Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP160R2Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP160R2Field.Multiply(S1, Z2.x, S1);
|
||||
SecP160R2Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat160.Create();
|
||||
SecP160R2Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP160R2Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat160.IsZero(H))
|
||||
{
|
||||
if (Nat160.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP160R2Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat160.Create();
|
||||
SecP160R2Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP160R2Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP160R2Field.Negate(G, G);
|
||||
Nat160.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat160.AddBothTo(V, V, G);
|
||||
SecP160R2Field.Reduce32(c, G);
|
||||
|
||||
SecP160R2FieldElement X3 = new SecP160R2FieldElement(t4);
|
||||
SecP160R2Field.Square(R, X3.x);
|
||||
SecP160R2Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP160R2FieldElement Y3 = new SecP160R2FieldElement(G);
|
||||
SecP160R2Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP160R2Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP160R2Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP160R2FieldElement Z3 = new SecP160R2FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP160R2Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
|
||||
|
||||
return new SecP160R2Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP160R2FieldElement Y1 = (SecP160R2FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] t1 = Nat160.Create();
|
||||
uint[] t2 = Nat160.Create();
|
||||
|
||||
uint[] Y1Squared = Nat160.Create();
|
||||
SecP160R2Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat160.Create();
|
||||
SecP160R2Field.Square(Y1Squared, T);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SecP160R2Field.Square(Z1.x, Z1Squared);
|
||||
}
|
||||
|
||||
SecP160R2Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SecP160R2Field.Add(X1.x, Z1Squared, M);
|
||||
SecP160R2Field.Multiply(M, t1, M);
|
||||
c = Nat160.AddBothTo(M, M, M);
|
||||
SecP160R2Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP160R2Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(5, S, 2, 0);
|
||||
SecP160R2Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(5, T, 3, 0, t1);
|
||||
SecP160R2Field.Reduce32(c, t1);
|
||||
|
||||
SecP160R2FieldElement X3 = new SecP160R2FieldElement(T);
|
||||
SecP160R2Field.Square(M, X3.x);
|
||||
SecP160R2Field.Subtract(X3.x, S, X3.x);
|
||||
SecP160R2Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP160R2FieldElement Y3 = new SecP160R2FieldElement(S);
|
||||
SecP160R2Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP160R2Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP160R2Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP160R2FieldElement Z3 = new SecP160R2FieldElement(M);
|
||||
SecP160R2Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP160R2Point(curve, X3, Y3, new ECFieldElement[]{ Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP160R2Point(Curve, this.RawXCoord, this.RawYCoord.Negate(), this.RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4cbae21702094a47bdafac12ade6c28
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192K1Curve.cs
vendored
Normal file
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192K1Curve.cs
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192K1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP192K1FieldElement.Q;
|
||||
|
||||
private const int SECP192K1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP192K1_FE_INTS = 6;
|
||||
private static readonly ECFieldElement[] SECP192K1_AFFINE_ZS = new ECFieldElement[] { new SecP192K1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP192K1Point m_infinity;
|
||||
|
||||
public SecP192K1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP192K1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(BigInteger.Zero);
|
||||
this.m_b = FromBigInteger(BigInteger.ValueOf(3));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SECP192K1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP192K1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP192K1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP192K1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP192K1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP192K1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat192.Copy(((SecP192K1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP192K1_FE_INTS;
|
||||
Nat192.Copy(((SecP192K1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP192K1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP192K1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat192.Create();
|
||||
SecP192K1Field.Random(r, x);
|
||||
return new SecP192K1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat192.Create();
|
||||
SecP192K1Field.RandomMult(r, x);
|
||||
return new SecP192K1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP192K1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP192K1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP192K1LookupTable(SecP192K1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat192.Create(), y = Nat192.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP192K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP192K1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP192K1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat192.Create(), y = Nat192.Create();
|
||||
int pos = index * SECP192K1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP192K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP192K1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP192K1FieldElement(x), new SecP192K1FieldElement(y), SECP192K1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 956ec0f59602e03408afd341170ff390
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
221
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192K1Field.cs
vendored
Normal file
221
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192K1Field.cs
vendored
Normal file
@@ -0,0 +1,221 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192K1Field
|
||||
{
|
||||
// 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFEE37, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x013C4FD1, 0x00002392, 0x00000001, 0x00000000, 0x00000000,
|
||||
0x00000000, 0xFFFFDC6E, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFEC3B02F, 0xFFFFDC6D, 0xFFFFFFFE, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF, 0x00002391, 0x00000002 };
|
||||
private const uint P5 = 0xFFFFFFFF;
|
||||
private const uint PExt11 = 0xFFFFFFFF;
|
||||
private const uint PInv33 = 0x11C9;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat192.Add(x, y, z);
|
||||
if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(6, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(12, xx, yy, zz);
|
||||
if (c != 0 || (zz[11] == PExt11 && Nat.Gte(12, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(12, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(6, x, z);
|
||||
if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(6, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(192, x);
|
||||
if (z[5] == P5 && Nat192.Gte(z, P))
|
||||
{
|
||||
Nat192.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(6, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat192.Add(x, P, z);
|
||||
Nat.ShiftDownBit(6, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 6; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat192.CreateExt();
|
||||
Nat192.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat192.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[11] == PExt11 && Nat.Gte(12, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(12, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat192.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat192.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[6 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 6);
|
||||
}
|
||||
while (0 == Nat.LessThan(6, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
ulong cc = Nat192.Mul33Add(PInv33, xx, 6, xx, 0, z, 0);
|
||||
uint c = Nat192.Mul33DWordAdd(PInv33, cc, z, 0);
|
||||
|
||||
Debug.Assert(c == 0 || c == 1);
|
||||
|
||||
if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(6, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
if ((x != 0 && Nat192.Mul33WordAdd(PInv33, x, z, 0) != 0)
|
||||
|| (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(6, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat192.CreateExt();
|
||||
Nat192.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat192.CreateExt();
|
||||
Nat192.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat192.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat192.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.Sub33From(6, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(12, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(12, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(6, x, 0, z);
|
||||
if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(6, PInv33, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e387de7a272f3c74cbc975cc08467172
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,218 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192K1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP192K1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP192K1FieldElement", "x");
|
||||
|
||||
this.x = SecP192K1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP192K1FieldElement()
|
||||
{
|
||||
this.x = Nat192.Create();
|
||||
}
|
||||
|
||||
protected internal SecP192K1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat192.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat192.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat192.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat192.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP192K1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.Add(x, ((SecP192K1FieldElement)b).x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.AddOne(x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.Subtract(x, ((SecP192K1FieldElement)b).x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.Multiply(x, ((SecP192K1FieldElement)b).x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.Inv(((SecP192K1FieldElement)b).x, z);
|
||||
SecP192K1Field.Multiply(z, x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.Negate(x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.Square(x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192K1Field.Inv(x, z);
|
||||
return new SecP192K1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
/*
|
||||
* Raise this element to the exponent 2^190 - 2^30 - 2^10 - 2^6 - 2^5 - 2^4 - 2^1
|
||||
*
|
||||
* Breaking up the exponent's binary representation into "repunits", we get:
|
||||
* { 159 1s } { 1 0s } { 19 1s } { 1 0s } { 3 1s } { 3 0s } { 3 1s } { 1 0s }
|
||||
*
|
||||
* Therefore we need an addition chain containing 3, 19, 159 (the lengths of the repunits)
|
||||
* We use: 1, 2, [3], 6, 8, 16, [19], 35, 70, 140, [159]
|
||||
*/
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat192.IsZero(x1) || Nat192.IsOne(x1))
|
||||
return this;
|
||||
|
||||
uint[] x2 = Nat192.Create();
|
||||
SecP192K1Field.Square(x1, x2);
|
||||
SecP192K1Field.Multiply(x2, x1, x2);
|
||||
uint[] x3 = Nat192.Create();
|
||||
SecP192K1Field.Square(x2, x3);
|
||||
SecP192K1Field.Multiply(x3, x1, x3);
|
||||
uint[] x6 = Nat192.Create();
|
||||
SecP192K1Field.SquareN(x3, 3, x6);
|
||||
SecP192K1Field.Multiply(x6, x3, x6);
|
||||
uint[] x8 = x6;
|
||||
SecP192K1Field.SquareN(x6, 2, x8);
|
||||
SecP192K1Field.Multiply(x8, x2, x8);
|
||||
uint[] x16 = x2;
|
||||
SecP192K1Field.SquareN(x8, 8, x16);
|
||||
SecP192K1Field.Multiply(x16, x8, x16);
|
||||
uint[] x19 = x8;
|
||||
SecP192K1Field.SquareN(x16, 3, x19);
|
||||
SecP192K1Field.Multiply(x19, x3, x19);
|
||||
uint[] x35 = Nat192.Create();
|
||||
SecP192K1Field.SquareN(x19, 16, x35);
|
||||
SecP192K1Field.Multiply(x35, x16, x35);
|
||||
uint[] x70 = x16;
|
||||
SecP192K1Field.SquareN(x35, 35, x70);
|
||||
SecP192K1Field.Multiply(x70, x35, x70);
|
||||
uint[] x140 = x35;
|
||||
SecP192K1Field.SquareN(x70, 70, x140);
|
||||
SecP192K1Field.Multiply(x140, x70, x140);
|
||||
uint[] x159 = x70;
|
||||
SecP192K1Field.SquareN(x140, 19, x159);
|
||||
SecP192K1Field.Multiply(x159, x19, x159);
|
||||
|
||||
uint[] t1 = x159;
|
||||
SecP192K1Field.SquareN(t1, 20, t1);
|
||||
SecP192K1Field.Multiply(t1, x19, t1);
|
||||
SecP192K1Field.SquareN(t1, 4, t1);
|
||||
SecP192K1Field.Multiply(t1, x3, t1);
|
||||
SecP192K1Field.SquareN(t1, 6, t1);
|
||||
SecP192K1Field.Multiply(t1, x3, t1);
|
||||
SecP192K1Field.Square(t1, t1);
|
||||
|
||||
uint[] t2 = x3;
|
||||
SecP192K1Field.Square(t1, t2);
|
||||
|
||||
return Nat192.Eq(x1, t2) ? new SecP192K1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP192K1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP192K1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP192K1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat192.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b4ad382e2b622e4fa7c595b129ed9d0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
236
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192K1Point.cs
vendored
Normal file
236
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192K1Point.cs
vendored
Normal file
@@ -0,0 +1,236 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192K1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP192K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP192K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP192K1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP192K1FieldElement X1 = (SecP192K1FieldElement)this.RawXCoord, Y1 = (SecP192K1FieldElement)this.RawYCoord;
|
||||
SecP192K1FieldElement X2 = (SecP192K1FieldElement)b.RawXCoord, Y2 = (SecP192K1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP192K1FieldElement Z1 = (SecP192K1FieldElement)this.RawZCoords[0];
|
||||
SecP192K1FieldElement Z2 = (SecP192K1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat192.CreateExt();
|
||||
uint[] t2 = Nat192.Create();
|
||||
uint[] t3 = Nat192.Create();
|
||||
uint[] t4 = Nat192.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP192K1Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP192K1Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP192K1Field.Multiply(S2, Z1.x, S2);
|
||||
SecP192K1Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP192K1Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP192K1Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP192K1Field.Multiply(S1, Z2.x, S1);
|
||||
SecP192K1Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat192.Create();
|
||||
SecP192K1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP192K1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat192.IsZero(H))
|
||||
{
|
||||
if (Nat192.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP192K1Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat192.Create();
|
||||
SecP192K1Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP192K1Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP192K1Field.Negate(G, G);
|
||||
Nat192.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat192.AddBothTo(V, V, G);
|
||||
SecP192K1Field.Reduce32(c, G);
|
||||
|
||||
SecP192K1FieldElement X3 = new SecP192K1FieldElement(t4);
|
||||
SecP192K1Field.Square(R, X3.x);
|
||||
SecP192K1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP192K1FieldElement Y3 = new SecP192K1FieldElement(G);
|
||||
SecP192K1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP192K1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP192K1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP192K1FieldElement Z3 = new SecP192K1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP192K1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP192K1Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
|
||||
|
||||
return new SecP192K1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP192K1FieldElement Y1 = (SecP192K1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP192K1FieldElement X1 = (SecP192K1FieldElement)this.RawXCoord, Z1 = (SecP192K1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
|
||||
uint[] Y1Squared = Nat192.Create();
|
||||
SecP192K1Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat192.Create();
|
||||
SecP192K1Field.Square(Y1Squared, T);
|
||||
|
||||
uint[] M = Nat192.Create();
|
||||
SecP192K1Field.Square(X1.x, M);
|
||||
c = Nat192.AddBothTo(M, M, M);
|
||||
SecP192K1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP192K1Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(6, S, 2, 0);
|
||||
SecP192K1Field.Reduce32(c, S);
|
||||
|
||||
uint[] t1 = Nat192.Create();
|
||||
c = Nat.ShiftUpBits(6, T, 3, 0, t1);
|
||||
SecP192K1Field.Reduce32(c, t1);
|
||||
|
||||
SecP192K1FieldElement X3 = new SecP192K1FieldElement(T);
|
||||
SecP192K1Field.Square(M, X3.x);
|
||||
SecP192K1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP192K1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP192K1FieldElement Y3 = new SecP192K1FieldElement(S);
|
||||
SecP192K1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP192K1Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP192K1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP192K1FieldElement Z3 = new SecP192K1FieldElement(M);
|
||||
SecP192K1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1.IsOne)
|
||||
{
|
||||
SecP192K1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP192K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP192K1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3d1420e93e58f04a851be43a58445da
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192R1Curve.cs
vendored
Normal file
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192R1Curve.cs
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192R1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP192R1FieldElement.Q;
|
||||
|
||||
private const int SECP192R1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP192R1_FE_INTS = 6;
|
||||
private static readonly ECFieldElement[] SECP192R1_AFFINE_ZS = new ECFieldElement[] { new SecP192R1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP192R1Point m_infinity;
|
||||
|
||||
public SecP192R1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP192R1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
|
||||
this.m_coord = SECP192R1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP192R1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP192R1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP192R1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP192R1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP192R1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat192.Copy(((SecP192R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP192R1_FE_INTS;
|
||||
Nat192.Copy(((SecP192R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP192R1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP192R1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat192.Create();
|
||||
SecP192R1Field.Random(r, x);
|
||||
return new SecP192R1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat192.Create();
|
||||
SecP192R1Field.RandomMult(r, x);
|
||||
return new SecP192R1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP192R1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP192R1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP192R1LookupTable(SecP192R1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat192.Create(), y = Nat192.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP192R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP192R1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP192R1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat192.Create(), y = Nat192.Create();
|
||||
int pos = index * SECP192R1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP192R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP192R1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP192R1FieldElement(x), new SecP192R1FieldElement(y), SECP192R1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 804ccccce56de074eb8ebec2c3939340
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
326
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192R1Field.cs
vendored
Normal file
326
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192R1Field.cs
vendored
Normal file
@@ -0,0 +1,326 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192R1Field
|
||||
{
|
||||
// 2^192 - 2^64 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000002, 0x00000000, 0x00000001,
|
||||
0x00000000, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF,
|
||||
0xFFFFFFFE, 0xFFFFFFFF, 0x00000001, 0x00000000, 0x00000002 };
|
||||
private const uint P5 = 0xFFFFFFFF;
|
||||
private const uint PExt11 = 0xFFFFFFFF;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat192.Add(x, y, z);
|
||||
if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(12, xx, yy, zz);
|
||||
if (c != 0 || (zz[11] == PExt11 && Nat.Gte(12, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(12, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(6, x, z);
|
||||
if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(192, x);
|
||||
if (z[5] == P5 && Nat192.Gte(z, P))
|
||||
{
|
||||
Nat192.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(6, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat192.Add(x, P, z);
|
||||
Nat.ShiftDownBit(6, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 6; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat192.CreateExt();
|
||||
Nat192.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat192.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[11] == PExt11 && Nat.Gte(12, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(12, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat192.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat192.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[6 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 6);
|
||||
}
|
||||
while (0 == Nat.LessThan(6, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
ulong xx06 = xx[6], xx07 = xx[7], xx08 = xx[8];
|
||||
ulong xx09 = xx[9], xx10 = xx[10], xx11 = xx[11];
|
||||
|
||||
ulong t0 = xx06 + xx10;
|
||||
ulong t1 = xx07 + xx11;
|
||||
|
||||
ulong cc = 0;
|
||||
cc += (ulong)xx[0] + t0;
|
||||
uint z0 = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (ulong)xx[1] + t1;
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
t0 += xx08;
|
||||
t1 += xx09;
|
||||
|
||||
cc += (ulong)xx[2] + t0;
|
||||
ulong z2 = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (ulong)xx[3] + t1;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
t0 -= xx06;
|
||||
t1 -= xx07;
|
||||
|
||||
cc += (ulong)xx[4] + t0;
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (ulong)xx[5] + t1;
|
||||
z[5] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
z2 += cc;
|
||||
|
||||
cc += z0;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += z[1];
|
||||
z[1] = (uint)cc;
|
||||
z2 += cc >> 32;
|
||||
}
|
||||
z[2] = (uint)z2;
|
||||
cc = z2 >> 32;
|
||||
|
||||
Debug.Assert(cc == 0 || cc == 1);
|
||||
|
||||
if ((cc != 0 && Nat.IncAt(6, z, 3) != 0)
|
||||
|| (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
ulong cc = 0;
|
||||
|
||||
if (x != 0)
|
||||
{
|
||||
cc += (ulong)z[0] + x;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (ulong)z[1];
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
}
|
||||
cc += (ulong)z[2] + x;
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
Debug.Assert(cc == 0 || cc == 1);
|
||||
}
|
||||
|
||||
if ((cc != 0 && Nat.IncAt(6, z, 3) != 0)
|
||||
|| (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat192.CreateExt();
|
||||
Nat192.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat192.CreateExt();
|
||||
Nat192.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat192.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat192.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
SubPInvFrom(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(12, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(12, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(6, x, 0, z);
|
||||
if (c != 0 || (z[5] == P5 && Nat192.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddPInvTo(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] + 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[2] + 1;
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.IncAt(6, z, 3);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SubPInvFrom(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] - 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[2] - 1;
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.DecAt(6, z, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 135629c36ebb8664584912ab9c807117
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,193 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192R1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP192R1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP192R1FieldElement", "x");
|
||||
|
||||
this.x = SecP192R1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP192R1FieldElement()
|
||||
{
|
||||
this.x = Nat192.Create();
|
||||
}
|
||||
|
||||
protected internal SecP192R1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat192.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat192.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat192.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat192.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP192R1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.Add(x, ((SecP192R1FieldElement)b).x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.AddOne(x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.Subtract(x, ((SecP192R1FieldElement)b).x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.Multiply(x, ((SecP192R1FieldElement)b).x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.Inv(((SecP192R1FieldElement)b).x, z);
|
||||
SecP192R1Field.Multiply(z, x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.Negate(x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.Square(x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat192.Create();
|
||||
SecP192R1Field.Inv(x, z);
|
||||
return new SecP192R1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
// Raise this element to the exponent 2^190 - 2^62
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat192.IsZero(x1) || Nat192.IsOne(x1))
|
||||
return this;
|
||||
|
||||
uint[] t1 = Nat192.Create();
|
||||
uint[] t2 = Nat192.Create();
|
||||
|
||||
SecP192R1Field.Square(x1, t1);
|
||||
SecP192R1Field.Multiply(t1, x1, t1);
|
||||
|
||||
SecP192R1Field.SquareN(t1, 2, t2);
|
||||
SecP192R1Field.Multiply(t2, t1, t2);
|
||||
|
||||
SecP192R1Field.SquareN(t2, 4, t1);
|
||||
SecP192R1Field.Multiply(t1, t2, t1);
|
||||
|
||||
SecP192R1Field.SquareN(t1, 8, t2);
|
||||
SecP192R1Field.Multiply(t2, t1, t2);
|
||||
|
||||
SecP192R1Field.SquareN(t2, 16, t1);
|
||||
SecP192R1Field.Multiply(t1, t2, t1);
|
||||
|
||||
SecP192R1Field.SquareN(t1, 32, t2);
|
||||
SecP192R1Field.Multiply(t2, t1, t2);
|
||||
|
||||
SecP192R1Field.SquareN(t2, 64, t1);
|
||||
SecP192R1Field.Multiply(t1, t2, t1);
|
||||
|
||||
SecP192R1Field.SquareN(t1, 62, t1);
|
||||
SecP192R1Field.Square(t1, t2);
|
||||
|
||||
return Nat192.Eq(x1, t2) ? new SecP192R1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP192R1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP192R1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP192R1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat192.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f25dd4ee3aacb3143b980262b93981eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192R1Point.cs
vendored
Normal file
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP192R1Point.cs
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP192R1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP192R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP192R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP192R1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP192R1FieldElement X1 = (SecP192R1FieldElement)this.RawXCoord, Y1 = (SecP192R1FieldElement)this.RawYCoord;
|
||||
SecP192R1FieldElement X2 = (SecP192R1FieldElement)b.RawXCoord, Y2 = (SecP192R1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP192R1FieldElement Z1 = (SecP192R1FieldElement)this.RawZCoords[0];
|
||||
SecP192R1FieldElement Z2 = (SecP192R1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat192.CreateExt();
|
||||
uint[] t2 = Nat192.Create();
|
||||
uint[] t3 = Nat192.Create();
|
||||
uint[] t4 = Nat192.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP192R1Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP192R1Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP192R1Field.Multiply(S2, Z1.x, S2);
|
||||
SecP192R1Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP192R1Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP192R1Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP192R1Field.Multiply(S1, Z2.x, S1);
|
||||
SecP192R1Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat192.Create();
|
||||
SecP192R1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP192R1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat192.IsZero(H))
|
||||
{
|
||||
if (Nat192.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP192R1Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat192.Create();
|
||||
SecP192R1Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP192R1Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP192R1Field.Negate(G, G);
|
||||
Nat192.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat192.AddBothTo(V, V, G);
|
||||
SecP192R1Field.Reduce32(c, G);
|
||||
|
||||
SecP192R1FieldElement X3 = new SecP192R1FieldElement(t4);
|
||||
SecP192R1Field.Square(R, X3.x);
|
||||
SecP192R1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP192R1FieldElement Y3 = new SecP192R1FieldElement(G);
|
||||
SecP192R1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP192R1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP192R1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP192R1FieldElement Z3 = new SecP192R1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP192R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP192R1Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
|
||||
|
||||
return new SecP192R1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP192R1FieldElement Y1 = (SecP192R1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP192R1FieldElement X1 = (SecP192R1FieldElement)this.RawXCoord, Z1 = (SecP192R1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] t1 = Nat192.Create();
|
||||
uint[] t2 = Nat192.Create();
|
||||
|
||||
uint[] Y1Squared = Nat192.Create();
|
||||
SecP192R1Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat192.Create();
|
||||
SecP192R1Field.Square(Y1Squared, T);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SecP192R1Field.Square(Z1.x, Z1Squared);
|
||||
}
|
||||
|
||||
SecP192R1Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SecP192R1Field.Add(X1.x, Z1Squared, M);
|
||||
SecP192R1Field.Multiply(M, t1, M);
|
||||
c = Nat192.AddBothTo(M, M, M);
|
||||
SecP192R1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP192R1Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(6, S, 2, 0);
|
||||
SecP192R1Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(6, T, 3, 0, t1);
|
||||
SecP192R1Field.Reduce32(c, t1);
|
||||
|
||||
SecP192R1FieldElement X3 = new SecP192R1FieldElement(T);
|
||||
SecP192R1Field.Square(M, X3.x);
|
||||
SecP192R1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP192R1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP192R1FieldElement Y3 = new SecP192R1FieldElement(S);
|
||||
SecP192R1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP192R1Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP192R1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP192R1FieldElement Z3 = new SecP192R1FieldElement(M);
|
||||
SecP192R1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP192R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP192R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP192R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a71d16df6540b974fb6fb1f2749ca234
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224K1Curve.cs
vendored
Normal file
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224K1Curve.cs
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224K1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP224K1FieldElement.Q;
|
||||
|
||||
private const int SECP224K1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP224K1_FE_INTS = 7;
|
||||
private static readonly ECFieldElement[] SECP224K1_AFFINE_ZS = new ECFieldElement[] { new SecP224K1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP224K1Point m_infinity;
|
||||
|
||||
public SecP224K1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP224K1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(BigInteger.Zero);
|
||||
this.m_b = FromBigInteger(BigInteger.ValueOf(5));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SECP224K1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP224K1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP224K1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP224K1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP224K1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP224K1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat224.Copy(((SecP224K1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP224K1_FE_INTS;
|
||||
Nat224.Copy(((SecP224K1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP224K1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP224K1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat224.Create();
|
||||
SecP224K1Field.Random(r, x);
|
||||
return new SecP224K1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat224.Create();
|
||||
SecP224K1Field.RandomMult(r, x);
|
||||
return new SecP224K1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP224K1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP224K1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP224K1LookupTable(SecP224K1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat224.Create(), y = Nat224.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP224K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP224K1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP224K1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat224.Create(), y = Nat224.Create();
|
||||
int pos = index * SECP224K1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP224K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP224K1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP224K1FieldElement(x), new SecP224K1FieldElement(y), SECP224K1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a7cf766b4ace8a4fb7fd49941cc6656
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
222
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224K1Field.cs
vendored
Normal file
222
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224K1Field.cs
vendored
Normal file
@@ -0,0 +1,222 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224K1Field
|
||||
{
|
||||
// 2^224 - 2^32 - 2^12 - 2^11 - 2^9 - 2^7 - 2^4 - 2 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFE56D, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x02C23069, 0x00003526, 0x00000001, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0xFFFFCADA, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
|
||||
};
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFD3DCF97, 0xFFFFCAD9, 0xFFFFFFFE, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00003525, 0x00000002 };
|
||||
private const uint P6 = 0xFFFFFFFF;
|
||||
private const uint PExt13 = 0xFFFFFFFF;
|
||||
private const uint PInv33 = 0x1A93;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat224.Add(x, y, z);
|
||||
if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(7, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(14, xx, yy, zz);
|
||||
if (c != 0 || (zz[13] == PExt13 && Nat.Gte(14, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(14, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(7, x, z);
|
||||
if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(7, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(224, x);
|
||||
if (z[6] == P6 && Nat224.Gte(z, P))
|
||||
{
|
||||
Nat224.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(7, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat224.Add(x, P, z);
|
||||
Nat.ShiftDownBit(7, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 7; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat224.CreateExt();
|
||||
Nat224.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat224.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[13] == PExt13 && Nat.Gte(14, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(14, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat224.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat224.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[7 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 7);
|
||||
}
|
||||
while (0 == Nat.LessThan(7, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
ulong cc = Nat224.Mul33Add(PInv33, xx, 7, xx, 0, z, 0);
|
||||
uint c = Nat224.Mul33DWordAdd(PInv33, cc, z, 0);
|
||||
|
||||
Debug.Assert(c == 0 || c == 1);
|
||||
|
||||
if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(7, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
if ((x != 0 && Nat224.Mul33WordAdd(PInv33, x, z, 0) != 0)
|
||||
|| (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(7, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat224.CreateExt();
|
||||
Nat224.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat224.CreateExt();
|
||||
Nat224.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat224.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat224.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.Sub33From(7, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(14, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(14, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(7, x, 0, z);
|
||||
if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(7, PInv33, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 815a691732590d84388ed5639f9ed39e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,247 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224K1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D"));
|
||||
|
||||
// Calculated as BigInteger.Two.ModPow(Q.ShiftRight(2), Q)
|
||||
private static readonly uint[] PRECOMP_POW2 = new uint[]{ 0x33bfd202, 0xdcfad133, 0x2287624a, 0xc3811ba8,
|
||||
0xa85558fc, 0x1eaef5d7, 0x8edf154c };
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP224K1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP224K1FieldElement", "x");
|
||||
|
||||
this.x = SecP224K1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP224K1FieldElement()
|
||||
{
|
||||
this.x = Nat224.Create();
|
||||
}
|
||||
|
||||
protected internal SecP224K1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat224.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat224.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat224.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat224.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP224K1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.Add(x, ((SecP224K1FieldElement)b).x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.AddOne(x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.Subtract(x, ((SecP224K1FieldElement)b).x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.Multiply(x, ((SecP224K1FieldElement)b).x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.Inv(((SecP224K1FieldElement)b).x, z);
|
||||
SecP224K1Field.Multiply(z, x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.Negate(x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.Square(x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224K1Field.Inv(x, z);
|
||||
return new SecP224K1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
/*
|
||||
* Q == 8m + 5, so we use Pocklington's method for this case.
|
||||
*
|
||||
* First, raise this element to the exponent 2^221 - 2^29 - 2^9 - 2^8 - 2^6 - 2^4 - 2^1 (i.e. m + 1)
|
||||
*
|
||||
* Breaking up the exponent's binary representation into "repunits", we get:
|
||||
* { 191 1s } { 1 0s } { 19 1s } { 2 0s } { 1 1s } { 1 0s } { 1 1s } { 1 0s } { 3 1s } { 1 0s }
|
||||
*
|
||||
* Therefore we need an addition chain containing 1, 3, 19, 191 (the lengths of the repunits)
|
||||
* We use: [1], 2, [3], 4, 8, 11, [19], 23, 42, 84, 107, [191]
|
||||
*/
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat224.IsZero(x1) || Nat224.IsOne(x1))
|
||||
return this;
|
||||
|
||||
uint[] x2 = Nat224.Create();
|
||||
SecP224K1Field.Square(x1, x2);
|
||||
SecP224K1Field.Multiply(x2, x1, x2);
|
||||
uint[] x3 = x2;
|
||||
SecP224K1Field.Square(x2, x3);
|
||||
SecP224K1Field.Multiply(x3, x1, x3);
|
||||
uint[] x4 = Nat224.Create();
|
||||
SecP224K1Field.Square(x3, x4);
|
||||
SecP224K1Field.Multiply(x4, x1, x4);
|
||||
uint[] x8 = Nat224.Create();
|
||||
SecP224K1Field.SquareN(x4, 4, x8);
|
||||
SecP224K1Field.Multiply(x8, x4, x8);
|
||||
uint[] x11 = Nat224.Create();
|
||||
SecP224K1Field.SquareN(x8, 3, x11);
|
||||
SecP224K1Field.Multiply(x11, x3, x11);
|
||||
uint[] x19 = x11;
|
||||
SecP224K1Field.SquareN(x11, 8, x19);
|
||||
SecP224K1Field.Multiply(x19, x8, x19);
|
||||
uint[] x23 = x8;
|
||||
SecP224K1Field.SquareN(x19, 4, x23);
|
||||
SecP224K1Field.Multiply(x23, x4, x23);
|
||||
uint[] x42 = x4;
|
||||
SecP224K1Field.SquareN(x23, 19, x42);
|
||||
SecP224K1Field.Multiply(x42, x19, x42);
|
||||
uint[] x84 = Nat224.Create();
|
||||
SecP224K1Field.SquareN(x42, 42, x84);
|
||||
SecP224K1Field.Multiply(x84, x42, x84);
|
||||
uint[] x107 = x42;
|
||||
SecP224K1Field.SquareN(x84, 23, x107);
|
||||
SecP224K1Field.Multiply(x107, x23, x107);
|
||||
uint[] x191 = x23;
|
||||
SecP224K1Field.SquareN(x107, 84, x191);
|
||||
SecP224K1Field.Multiply(x191, x84, x191);
|
||||
|
||||
uint[] t1 = x191;
|
||||
SecP224K1Field.SquareN(t1, 20, t1);
|
||||
SecP224K1Field.Multiply(t1, x19, t1);
|
||||
SecP224K1Field.SquareN(t1, 3, t1);
|
||||
SecP224K1Field.Multiply(t1, x1, t1);
|
||||
SecP224K1Field.SquareN(t1, 2, t1);
|
||||
SecP224K1Field.Multiply(t1, x1, t1);
|
||||
SecP224K1Field.SquareN(t1, 4, t1);
|
||||
SecP224K1Field.Multiply(t1, x3, t1);
|
||||
SecP224K1Field.Square(t1, t1);
|
||||
|
||||
uint[] t2 = x84;
|
||||
SecP224K1Field.Square(t1, t2);
|
||||
|
||||
if (Nat224.Eq(x1, t2))
|
||||
{
|
||||
return new SecP224K1FieldElement(t1);
|
||||
}
|
||||
|
||||
/*
|
||||
* If the first guess is incorrect, we multiply by a precomputed power of 2 to get the second guess,
|
||||
* which is ((4x)^(m + 1))/2 mod Q
|
||||
*/
|
||||
SecP224K1Field.Multiply(t1, PRECOMP_POW2, t1);
|
||||
|
||||
SecP224K1Field.Square(t1, t2);
|
||||
|
||||
if (Nat224.Eq(x1, t2))
|
||||
{
|
||||
return new SecP224K1FieldElement(t1);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP224K1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP224K1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP224K1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat224.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 7);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eb18e886f88cd2d40ad8a95b171b1a7f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
236
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224K1Point.cs
vendored
Normal file
236
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224K1Point.cs
vendored
Normal file
@@ -0,0 +1,236 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224K1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP224K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP224K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP224K1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP224K1FieldElement X1 = (SecP224K1FieldElement)this.RawXCoord, Y1 = (SecP224K1FieldElement)this.RawYCoord;
|
||||
SecP224K1FieldElement X2 = (SecP224K1FieldElement)b.RawXCoord, Y2 = (SecP224K1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP224K1FieldElement Z1 = (SecP224K1FieldElement)this.RawZCoords[0];
|
||||
SecP224K1FieldElement Z2 = (SecP224K1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat224.CreateExt();
|
||||
uint[] t2 = Nat224.Create();
|
||||
uint[] t3 = Nat224.Create();
|
||||
uint[] t4 = Nat224.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP224K1Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP224K1Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP224K1Field.Multiply(S2, Z1.x, S2);
|
||||
SecP224K1Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP224K1Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP224K1Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP224K1Field.Multiply(S1, Z2.x, S1);
|
||||
SecP224K1Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat224.Create();
|
||||
SecP224K1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP224K1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat224.IsZero(H))
|
||||
{
|
||||
if (Nat224.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP224K1Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat224.Create();
|
||||
SecP224K1Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP224K1Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP224K1Field.Negate(G, G);
|
||||
Nat224.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat224.AddBothTo(V, V, G);
|
||||
SecP224K1Field.Reduce32(c, G);
|
||||
|
||||
SecP224K1FieldElement X3 = new SecP224K1FieldElement(t4);
|
||||
SecP224K1Field.Square(R, X3.x);
|
||||
SecP224K1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP224K1FieldElement Y3 = new SecP224K1FieldElement(G);
|
||||
SecP224K1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP224K1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP224K1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP224K1FieldElement Z3 = new SecP224K1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP224K1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP224K1Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
|
||||
|
||||
return new SecP224K1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP224K1FieldElement Y1 = (SecP224K1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP224K1FieldElement X1 = (SecP224K1FieldElement)this.RawXCoord, Z1 = (SecP224K1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
|
||||
uint[] Y1Squared = Nat224.Create();
|
||||
SecP224K1Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat224.Create();
|
||||
SecP224K1Field.Square(Y1Squared, T);
|
||||
|
||||
uint[] M = Nat224.Create();
|
||||
SecP224K1Field.Square(X1.x, M);
|
||||
c = Nat224.AddBothTo(M, M, M);
|
||||
SecP224K1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP224K1Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(7, S, 2, 0);
|
||||
SecP224K1Field.Reduce32(c, S);
|
||||
|
||||
uint[] t1 = Nat224.Create();
|
||||
c = Nat.ShiftUpBits(7, T, 3, 0, t1);
|
||||
SecP224K1Field.Reduce32(c, t1);
|
||||
|
||||
SecP224K1FieldElement X3 = new SecP224K1FieldElement(T);
|
||||
SecP224K1Field.Square(M, X3.x);
|
||||
SecP224K1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP224K1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP224K1FieldElement Y3 = new SecP224K1FieldElement(S);
|
||||
SecP224K1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP224K1Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP224K1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP224K1FieldElement Z3 = new SecP224K1FieldElement(M);
|
||||
SecP224K1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1.IsOne)
|
||||
{
|
||||
SecP224K1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP224K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP224K1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da88518e11481b447952b1724b7b3aa8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224R1Curve.cs
vendored
Normal file
175
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224R1Curve.cs
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224R1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP224R1FieldElement.Q;
|
||||
|
||||
private const int SECP224R1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP224R1_FE_INTS = 7;
|
||||
private static readonly ECFieldElement[] SECP224R1_AFFINE_ZS = new ECFieldElement[] { new SecP224R1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP224R1Point m_infinity;
|
||||
|
||||
public SecP224R1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP224R1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
|
||||
this.m_coord = SECP224R1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP224R1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP224R1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP224R1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP224R1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP224R1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat224.Copy(((SecP224R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP224R1_FE_INTS;
|
||||
Nat224.Copy(((SecP224R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP224R1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP224R1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat224.Create();
|
||||
SecP224R1Field.Random(r, x);
|
||||
return new SecP224R1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat224.Create();
|
||||
SecP224R1Field.RandomMult(r, x);
|
||||
return new SecP224R1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP224R1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP224R1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP224R1LookupTable(SecP224R1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat224.Create(), y = Nat224.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP224R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP224R1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP224R1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat224.Create(), y = Nat224.Create();
|
||||
int pos = index * SECP224R1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP224R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP224R1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP224R1FieldElement(x), new SecP224R1FieldElement(y), SECP224R1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 994b1ae3b0bcbef4f835df86ca21931a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
341
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224R1Field.cs
vendored
Normal file
341
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224R1Field.cs
vendored
Normal file
@@ -0,0 +1,341 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224R1Field
|
||||
{
|
||||
// 2^224 - 2^96 + 1
|
||||
internal static readonly uint[] P = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0x00000000, 0x00000002, 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
|
||||
};
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001,
|
||||
0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001 };
|
||||
private const uint P6 = 0xFFFFFFFF;
|
||||
private const uint PExt13 = 0xFFFFFFFF;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat224.Add(x, y, z);
|
||||
if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(14, xx, yy, zz);
|
||||
if (c != 0 || (zz[13] == PExt13 && Nat.Gte(14, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(14, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(7, x, z);
|
||||
if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(224, x);
|
||||
if (z[6] == P6 && Nat224.Gte(z, P))
|
||||
{
|
||||
Nat224.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(7, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat224.Add(x, P, z);
|
||||
Nat.ShiftDownBit(7, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 7; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat224.CreateExt();
|
||||
Nat224.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat224.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[13] == PExt13 && Nat.Gte(14, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(14, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat224.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat224.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[7 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 7);
|
||||
}
|
||||
while (0 == Nat.LessThan(7, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
long xx10 = xx[10], xx11 = xx[11], xx12 = xx[12], xx13 = xx[13];
|
||||
|
||||
const long n = 1;
|
||||
|
||||
long t0 = (long)xx[7] + xx11 - n;
|
||||
long t1 = (long)xx[8] + xx12;
|
||||
long t2 = (long)xx[9] + xx13;
|
||||
|
||||
long cc = 0;
|
||||
cc += (long)xx[0] - t0;
|
||||
long z0 = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[1] - t1;
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[2] - t2;
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[3] + t0 - xx10;
|
||||
long z3 = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[4] + t1 - xx11;
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[5] + t2 - xx12;
|
||||
z[5] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[6] + xx10 - xx13;
|
||||
z[6] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += n;
|
||||
|
||||
Debug.Assert(cc >= 0);
|
||||
|
||||
z3 += cc;
|
||||
|
||||
z0 -= cc;
|
||||
z[0] = (uint)z0;
|
||||
cc = z0 >> 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (long)z[1];
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[2];
|
||||
z[2] = (uint)cc;
|
||||
z3 += cc >> 32;
|
||||
}
|
||||
z[3] = (uint)z3;
|
||||
cc = z3 >> 32;
|
||||
|
||||
Debug.Assert(cc == 0 || cc == 1);
|
||||
|
||||
if ((cc != 0 && Nat.IncAt(7, z, 4) != 0)
|
||||
|| (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
long cc = 0;
|
||||
|
||||
if (x != 0)
|
||||
{
|
||||
long xx07 = x;
|
||||
|
||||
cc += (long)z[0] - xx07;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (long)z[1];
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[2];
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
}
|
||||
cc += (long)z[3] + xx07;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
Debug.Assert(cc == 0 || cc == 1);
|
||||
}
|
||||
|
||||
if ((cc != 0 && Nat.IncAt(7, z, 4) != 0)
|
||||
|| (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat224.CreateExt();
|
||||
Nat224.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat224.CreateExt();
|
||||
Nat224.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat224.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat224.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
SubPInvFrom(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(14, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(14, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(7, x, 0, z);
|
||||
if (c != 0 || (z[6] == P6 && Nat224.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddPInvTo(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] - 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[3] + 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.IncAt(7, z, 4);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SubPInvFrom(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] + 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[3] - 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.DecAt(7, z, 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d6a512d6eb991e4387453c92c828d27
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,275 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224R1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP224R1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP224R1FieldElement", "x");
|
||||
|
||||
this.x = SecP224R1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP224R1FieldElement()
|
||||
{
|
||||
this.x = Nat224.Create();
|
||||
}
|
||||
|
||||
protected internal SecP224R1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat224.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat224.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat224.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat224.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP224R1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.Add(x, ((SecP224R1FieldElement)b).x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.AddOne(x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.Subtract(x, ((SecP224R1FieldElement)b).x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.Multiply(x, ((SecP224R1FieldElement)b).x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.Inv(((SecP224R1FieldElement)b).x, z);
|
||||
SecP224R1Field.Multiply(z, x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.Negate(x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.Square(x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat224.Create();
|
||||
SecP224R1Field.Inv(x, z);
|
||||
return new SecP224R1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
uint[] c = this.x;
|
||||
if (Nat224.IsZero(c) || Nat224.IsOne(c))
|
||||
return this;
|
||||
|
||||
uint[] nc = Nat224.Create();
|
||||
SecP224R1Field.Negate(c, nc);
|
||||
|
||||
uint[] r = Mod.Random(SecureRandom.ArbitraryRandom, SecP224R1Field.P);
|
||||
uint[] t = Nat224.Create();
|
||||
|
||||
if (!IsSquare(c))
|
||||
return null;
|
||||
|
||||
while (!TrySqrt(nc, r, t))
|
||||
{
|
||||
SecP224R1Field.AddOne(r, r);
|
||||
}
|
||||
|
||||
SecP224R1Field.Square(t, r);
|
||||
|
||||
return Nat224.Eq(c, r) ? new SecP224R1FieldElement(t) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP224R1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP224R1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP224R1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat224.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 7);
|
||||
}
|
||||
|
||||
private static bool IsSquare(uint[] x)
|
||||
{
|
||||
uint[] t1 = Nat224.Create();
|
||||
uint[] t2 = Nat224.Create();
|
||||
Nat224.Copy(x, t1);
|
||||
|
||||
for (int i = 0; i < 7; ++i)
|
||||
{
|
||||
Nat224.Copy(t1, t2);
|
||||
SecP224R1Field.SquareN(t1, 1 << i, t1);
|
||||
SecP224R1Field.Multiply(t1, t2, t1);
|
||||
}
|
||||
|
||||
SecP224R1Field.SquareN(t1, 95, t1);
|
||||
return Nat224.IsOne(t1);
|
||||
}
|
||||
|
||||
private static void RM(uint[] nc, uint[] d0, uint[] e0, uint[] d1, uint[] e1, uint[] f1, uint[] t)
|
||||
{
|
||||
SecP224R1Field.Multiply(e1, e0, t);
|
||||
SecP224R1Field.Multiply(t, nc, t);
|
||||
SecP224R1Field.Multiply(d1, d0, f1);
|
||||
SecP224R1Field.Add(f1, t, f1);
|
||||
SecP224R1Field.Multiply(d1, e0, t);
|
||||
Nat224.Copy(f1, d1);
|
||||
SecP224R1Field.Multiply(e1, d0, e1);
|
||||
SecP224R1Field.Add(e1, t, e1);
|
||||
SecP224R1Field.Square(e1, f1);
|
||||
SecP224R1Field.Multiply(f1, nc, f1);
|
||||
}
|
||||
|
||||
private static void RP(uint[] nc, uint[] d1, uint[] e1, uint[] f1, uint[] t)
|
||||
{
|
||||
Nat224.Copy(nc, f1);
|
||||
|
||||
uint[] d0 = Nat224.Create();
|
||||
uint[] e0 = Nat224.Create();
|
||||
|
||||
for (int i = 0; i < 7; ++i)
|
||||
{
|
||||
Nat224.Copy(d1, d0);
|
||||
Nat224.Copy(e1, e0);
|
||||
|
||||
int j = 1 << i;
|
||||
while (--j >= 0)
|
||||
{
|
||||
RS(d1, e1, f1, t);
|
||||
}
|
||||
|
||||
RM(nc, d0, e0, d1, e1, f1, t);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RS(uint[] d, uint[] e, uint[] f, uint[] t)
|
||||
{
|
||||
SecP224R1Field.Multiply(e, d, e);
|
||||
SecP224R1Field.Twice(e, e);
|
||||
SecP224R1Field.Square(d, t);
|
||||
SecP224R1Field.Add(f, t, d);
|
||||
SecP224R1Field.Multiply(f, t, f);
|
||||
uint c = Nat.ShiftUpBits(7, f, 2, 0);
|
||||
SecP224R1Field.Reduce32(c, f);
|
||||
}
|
||||
|
||||
private static bool TrySqrt(uint[] nc, uint[] r, uint[] t)
|
||||
{
|
||||
uint[] d1 = Nat224.Create();
|
||||
Nat224.Copy(r, d1);
|
||||
uint[] e1 = Nat224.Create();
|
||||
e1[0] = 1;
|
||||
uint[] f1 = Nat224.Create();
|
||||
RP(nc, d1, e1, f1, t);
|
||||
|
||||
uint[] d0 = Nat224.Create();
|
||||
uint[] e0 = Nat224.Create();
|
||||
|
||||
for (int k = 1; k < 96; ++k)
|
||||
{
|
||||
Nat224.Copy(d1, d0);
|
||||
Nat224.Copy(e1, e0);
|
||||
|
||||
RS(d1, e1, f1, t);
|
||||
|
||||
if (Nat224.IsZero(d1))
|
||||
{
|
||||
SecP224R1Field.Inv(e0, t);
|
||||
SecP224R1Field.Multiply(t, d0, t);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec09615374fd6e64bbecd69995924dbd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224R1Point.cs
vendored
Normal file
249
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP224R1Point.cs
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP224R1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP224R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP224R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP224R1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP224R1FieldElement X1 = (SecP224R1FieldElement)this.RawXCoord, Y1 = (SecP224R1FieldElement)this.RawYCoord;
|
||||
SecP224R1FieldElement X2 = (SecP224R1FieldElement)b.RawXCoord, Y2 = (SecP224R1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP224R1FieldElement Z1 = (SecP224R1FieldElement)this.RawZCoords[0];
|
||||
SecP224R1FieldElement Z2 = (SecP224R1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt1 = Nat224.CreateExt();
|
||||
uint[] t2 = Nat224.Create();
|
||||
uint[] t3 = Nat224.Create();
|
||||
uint[] t4 = Nat224.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP224R1Field.Square(Z1.x, S2);
|
||||
|
||||
U2 = t2;
|
||||
SecP224R1Field.Multiply(S2, X2.x, U2);
|
||||
|
||||
SecP224R1Field.Multiply(S2, Z1.x, S2);
|
||||
SecP224R1Field.Multiply(S2, Y2.x, S2);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP224R1Field.Square(Z2.x, S1);
|
||||
|
||||
U1 = tt1;
|
||||
SecP224R1Field.Multiply(S1, X1.x, U1);
|
||||
|
||||
SecP224R1Field.Multiply(S1, Z2.x, S1);
|
||||
SecP224R1Field.Multiply(S1, Y1.x, S1);
|
||||
}
|
||||
|
||||
uint[] H = Nat224.Create();
|
||||
SecP224R1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP224R1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat224.IsZero(H))
|
||||
{
|
||||
if (Nat224.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP224R1Field.Square(H, HSquared);
|
||||
|
||||
uint[] G = Nat224.Create();
|
||||
SecP224R1Field.Multiply(HSquared, H, G);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP224R1Field.Multiply(HSquared, U1, V);
|
||||
|
||||
SecP224R1Field.Negate(G, G);
|
||||
Nat224.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat224.AddBothTo(V, V, G);
|
||||
SecP224R1Field.Reduce32(c, G);
|
||||
|
||||
SecP224R1FieldElement X3 = new SecP224R1FieldElement(t4);
|
||||
SecP224R1Field.Square(R, X3.x);
|
||||
SecP224R1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP224R1FieldElement Y3 = new SecP224R1FieldElement(G);
|
||||
SecP224R1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP224R1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP224R1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP224R1FieldElement Z3 = new SecP224R1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP224R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP224R1Field.Multiply(Z3.x, Z2.x, Z3.x);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
|
||||
|
||||
return new SecP224R1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP224R1FieldElement Y1 = (SecP224R1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP224R1FieldElement X1 = (SecP224R1FieldElement)this.RawXCoord, Z1 = (SecP224R1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] t1 = Nat224.Create();
|
||||
uint[] t2 = Nat224.Create();
|
||||
|
||||
uint[] Y1Squared = Nat224.Create();
|
||||
SecP224R1Field.Square(Y1.x, Y1Squared);
|
||||
|
||||
uint[] T = Nat224.Create();
|
||||
SecP224R1Field.Square(Y1Squared, T);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SecP224R1Field.Square(Z1.x, Z1Squared);
|
||||
}
|
||||
|
||||
SecP224R1Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SecP224R1Field.Add(X1.x, Z1Squared, M);
|
||||
SecP224R1Field.Multiply(M, t1, M);
|
||||
c = Nat224.AddBothTo(M, M, M);
|
||||
SecP224R1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP224R1Field.Multiply(Y1Squared, X1.x, S);
|
||||
c = Nat.ShiftUpBits(7, S, 2, 0);
|
||||
SecP224R1Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(7, T, 3, 0, t1);
|
||||
SecP224R1Field.Reduce32(c, t1);
|
||||
|
||||
SecP224R1FieldElement X3 = new SecP224R1FieldElement(T);
|
||||
SecP224R1Field.Square(M, X3.x);
|
||||
SecP224R1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP224R1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP224R1FieldElement Y3 = new SecP224R1FieldElement(S);
|
||||
SecP224R1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP224R1Field.Multiply(Y3.x, M, Y3.x);
|
||||
SecP224R1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP224R1FieldElement Z3 = new SecP224R1FieldElement(M);
|
||||
SecP224R1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP224R1Field.Multiply(Z3.x, Z1.x, Z3.x);
|
||||
}
|
||||
|
||||
return new SecP224R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP224R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2c3ae093fd3dce48839537407abb29e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256K1Curve.cs
vendored
Normal file
172
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256K1Curve.cs
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256K1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP256K1FieldElement.Q;
|
||||
|
||||
private const int SECP256K1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP256K1_FE_INTS = 8;
|
||||
private static readonly ECFieldElement[] SECP256K1_AFFINE_ZS = new ECFieldElement[] { new SecP256K1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP256K1Point m_infinity;
|
||||
|
||||
public SecP256K1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP256K1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(BigInteger.Zero);
|
||||
this.m_b = FromBigInteger(BigInteger.ValueOf(7));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SECP256K1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP256K1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP256K1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP256K1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP256K1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP256K1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat256.Copy(((SecP256K1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP256K1_FE_INTS;
|
||||
Nat256.Copy(((SecP256K1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP256K1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP256K1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat256.Create();
|
||||
SecP256K1Field.Random(r, x);
|
||||
return new SecP256K1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat256.Create();
|
||||
SecP256K1Field.RandomMult(r, x);
|
||||
return new SecP256K1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP256K1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP256K1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP256K1LookupTable(SecP256K1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP256K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP256K1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP256K1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = index * SECP256K1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP256K1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP256K1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP256K1FieldElement(x), new SecP256K1FieldElement(y), SECP256K1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc638d3c9d333c64bad41728fe8935d6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
248
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256K1Field.cs
vendored
Normal file
248
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256K1Field.cs
vendored
Normal file
@@ -0,0 +1,248 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256K1Field
|
||||
{
|
||||
// 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x000E90A1, 0x000007A2, 0x00000001, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0xFFFFF85E, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFFF16F5F, 0xFFFFF85D, 0xFFFFFFFE, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x000007A1, 0x00000002 };
|
||||
private const uint P7 = 0xFFFFFFFF;
|
||||
private const uint PExt15 = 0xFFFFFFFF;
|
||||
private const uint PInv33 = 0x3D1;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat256.Add(x, y, z);
|
||||
if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(8, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(16, xx, yy, zz);
|
||||
if (c != 0 || (zz[15] == PExt15 && Nat.Gte(16, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(16, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(8, x, z);
|
||||
if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(8, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(256, x);
|
||||
if (z[7] == P7 && Nat256.Gte(z, P))
|
||||
{
|
||||
Nat256.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(8, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat256.Add(x, P, z);
|
||||
Nat.ShiftDownBit(8, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
|
||||
{
|
||||
Nat256.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat256.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[15] == PExt15 && Nat.Gte(16, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(16, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat256.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat256.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[8 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 8);
|
||||
}
|
||||
while (0 == Nat.LessThan(8, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
ulong cc = Nat256.Mul33Add(PInv33, xx, 8, xx, 0, z, 0);
|
||||
uint c = Nat256.Mul33DWordAdd(PInv33, cc, z, 0);
|
||||
|
||||
Debug.Assert(c == 0 || c == 1);
|
||||
|
||||
if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(8, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
if ((x != 0 && Nat256.Mul33WordAdd(PInv33, x, z, 0) != 0)
|
||||
|| (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(8, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z, uint[] tt)
|
||||
{
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat256.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat256.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat256.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.Sub33From(8, PInv33, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(16, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(16, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(8, x, 0, z);
|
||||
if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
Nat.Add33To(8, PInv33, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 608dda2db7c79524698c3182e0289378
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,220 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256K1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP256K1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP256K1FieldElement", "x");
|
||||
|
||||
this.x = SecP256K1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP256K1FieldElement()
|
||||
{
|
||||
this.x = Nat256.Create();
|
||||
}
|
||||
|
||||
protected internal SecP256K1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat256.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat256.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat256.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat256.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP256K1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.Add(x, ((SecP256K1FieldElement)b).x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.AddOne(x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.Subtract(x, ((SecP256K1FieldElement)b).x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.Multiply(x, ((SecP256K1FieldElement)b).x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.Inv(((SecP256K1FieldElement)b).x, z);
|
||||
SecP256K1Field.Multiply(z, x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.Negate(x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.Square(x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256K1Field.Inv(x, z);
|
||||
return new SecP256K1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
/*
|
||||
* Raise this element to the exponent 2^254 - 2^30 - 2^7 - 2^6 - 2^5 - 2^4 - 2^2
|
||||
*
|
||||
* Breaking up the exponent's binary representation into "repunits", we get:
|
||||
* { 223 1s } { 1 0s } { 22 1s } { 4 0s } { 2 1s } { 2 0s}
|
||||
*
|
||||
* Therefore we need an addition chain containing 2, 22, 223 (the lengths of the repunits)
|
||||
* We use: 1, [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
|
||||
*/
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
|
||||
return this;
|
||||
|
||||
uint[] tt0 = Nat256.CreateExt();
|
||||
|
||||
uint[] x2 = Nat256.Create();
|
||||
SecP256K1Field.Square(x1, x2, tt0);
|
||||
SecP256K1Field.Multiply(x2, x1, x2, tt0);
|
||||
uint[] x3 = Nat256.Create();
|
||||
SecP256K1Field.Square(x2, x3, tt0);
|
||||
SecP256K1Field.Multiply(x3, x1, x3, tt0);
|
||||
uint[] x6 = Nat256.Create();
|
||||
SecP256K1Field.SquareN(x3, 3, x6, tt0);
|
||||
SecP256K1Field.Multiply(x6, x3, x6, tt0);
|
||||
uint[] x9 = x6;
|
||||
SecP256K1Field.SquareN(x6, 3, x9, tt0);
|
||||
SecP256K1Field.Multiply(x9, x3, x9, tt0);
|
||||
uint[] x11 = x9;
|
||||
SecP256K1Field.SquareN(x9, 2, x11, tt0);
|
||||
SecP256K1Field.Multiply(x11, x2, x11, tt0);
|
||||
uint[] x22 = Nat256.Create();
|
||||
SecP256K1Field.SquareN(x11, 11, x22, tt0);
|
||||
SecP256K1Field.Multiply(x22, x11, x22, tt0);
|
||||
uint[] x44 = x11;
|
||||
SecP256K1Field.SquareN(x22, 22, x44, tt0);
|
||||
SecP256K1Field.Multiply(x44, x22, x44, tt0);
|
||||
uint[] x88 = Nat256.Create();
|
||||
SecP256K1Field.SquareN(x44, 44, x88, tt0);
|
||||
SecP256K1Field.Multiply(x88, x44, x88, tt0);
|
||||
uint[] x176 = Nat256.Create();
|
||||
SecP256K1Field.SquareN(x88, 88, x176, tt0);
|
||||
SecP256K1Field.Multiply(x176, x88, x176, tt0);
|
||||
uint[] x220 = x88;
|
||||
SecP256K1Field.SquareN(x176, 44, x220, tt0);
|
||||
SecP256K1Field.Multiply(x220, x44, x220, tt0);
|
||||
uint[] x223 = x44;
|
||||
SecP256K1Field.SquareN(x220, 3, x223, tt0);
|
||||
SecP256K1Field.Multiply(x223, x3, x223, tt0);
|
||||
|
||||
uint[] t1 = x223;
|
||||
SecP256K1Field.SquareN(t1, 23, t1, tt0);
|
||||
SecP256K1Field.Multiply(t1, x22, t1, tt0);
|
||||
SecP256K1Field.SquareN(t1, 6, t1, tt0);
|
||||
SecP256K1Field.Multiply(t1, x2, t1, tt0);
|
||||
SecP256K1Field.SquareN(t1, 2, t1, tt0);
|
||||
|
||||
uint[] t2 = x2;
|
||||
SecP256K1Field.Square(t1, t2, tt0);
|
||||
|
||||
return Nat256.Eq(x1, t2) ? new SecP256K1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP256K1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP256K1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP256K1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat256.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d21546ca42446f4e95c7125ef071242
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
238
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256K1Point.cs
vendored
Normal file
238
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256K1Point.cs
vendored
Normal file
@@ -0,0 +1,238 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256K1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP256K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP256K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP256K1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Y1 = (SecP256K1FieldElement)this.RawYCoord;
|
||||
SecP256K1FieldElement X2 = (SecP256K1FieldElement)b.RawXCoord, Y2 = (SecP256K1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP256K1FieldElement Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
|
||||
SecP256K1FieldElement Z2 = (SecP256K1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt0 = Nat256.CreateExt();
|
||||
uint[] tt1 = Nat256.CreateExt();
|
||||
uint[] t2 = Nat256.Create();
|
||||
uint[] t3 = Nat256.Create();
|
||||
uint[] t4 = Nat256.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP256K1Field.Square(Z1.x, S2, tt0);
|
||||
|
||||
U2 = t2;
|
||||
SecP256K1Field.Multiply(S2, X2.x, U2, tt0);
|
||||
|
||||
SecP256K1Field.Multiply(S2, Z1.x, S2, tt0);
|
||||
SecP256K1Field.Multiply(S2, Y2.x, S2, tt0);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP256K1Field.Square(Z2.x, S1, tt0);
|
||||
|
||||
U1 = tt1;
|
||||
SecP256K1Field.Multiply(S1, X1.x, U1, tt0);
|
||||
|
||||
SecP256K1Field.Multiply(S1, Z2.x, S1, tt0);
|
||||
SecP256K1Field.Multiply(S1, Y1.x, S1, tt0);
|
||||
}
|
||||
|
||||
uint[] H = Nat256.Create();
|
||||
SecP256K1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP256K1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat256.IsZero(H))
|
||||
{
|
||||
if (Nat256.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP256K1Field.Square(H, HSquared, tt0);
|
||||
|
||||
uint[] G = Nat256.Create();
|
||||
SecP256K1Field.Multiply(HSquared, H, G, tt0);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP256K1Field.Multiply(HSquared, U1, V, tt0);
|
||||
|
||||
SecP256K1Field.Negate(G, G);
|
||||
Nat256.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat256.AddBothTo(V, V, G);
|
||||
SecP256K1Field.Reduce32(c, G);
|
||||
|
||||
SecP256K1FieldElement X3 = new SecP256K1FieldElement(t4);
|
||||
SecP256K1Field.Square(R, X3.x, tt0);
|
||||
SecP256K1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP256K1FieldElement Y3 = new SecP256K1FieldElement(G);
|
||||
SecP256K1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP256K1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP256K1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP256K1FieldElement Z3 = new SecP256K1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP256K1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
|
||||
|
||||
return new SecP256K1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP256K1FieldElement Y1 = (SecP256K1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt0 = Nat256.CreateExt();
|
||||
|
||||
uint[] Y1Squared = Nat256.Create();
|
||||
SecP256K1Field.Square(Y1.x, Y1Squared, tt0);
|
||||
|
||||
uint[] T = Nat256.Create();
|
||||
SecP256K1Field.Square(Y1Squared, T, tt0);
|
||||
|
||||
uint[] M = Nat256.Create();
|
||||
SecP256K1Field.Square(X1.x, M, tt0);
|
||||
c = Nat256.AddBothTo(M, M, M);
|
||||
SecP256K1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP256K1Field.Multiply(Y1Squared, X1.x, S, tt0);
|
||||
c = Nat.ShiftUpBits(8, S, 2, 0);
|
||||
SecP256K1Field.Reduce32(c, S);
|
||||
|
||||
uint[] t1 = Nat256.Create();
|
||||
c = Nat.ShiftUpBits(8, T, 3, 0, t1);
|
||||
SecP256K1Field.Reduce32(c, t1);
|
||||
|
||||
SecP256K1FieldElement X3 = new SecP256K1FieldElement(T);
|
||||
SecP256K1Field.Square(M, X3.x, tt0);
|
||||
SecP256K1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP256K1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP256K1FieldElement Y3 = new SecP256K1FieldElement(S);
|
||||
SecP256K1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP256K1Field.Multiply(Y3.x, M, Y3.x, tt0);
|
||||
SecP256K1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP256K1FieldElement Z3 = new SecP256K1FieldElement(M);
|
||||
SecP256K1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1.IsOne)
|
||||
{
|
||||
SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
|
||||
}
|
||||
|
||||
return new SecP256K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP256K1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bd0044c4ee15670419af9195db5bb384
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256R1Curve.cs
vendored
Normal file
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256R1Curve.cs
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256R1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP256R1FieldElement.Q;
|
||||
|
||||
private const int SECP256R1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP256R1_FE_INTS = 8;
|
||||
private static readonly ECFieldElement[] SECP256R1_AFFINE_ZS = new ECFieldElement[] { new SecP256R1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP256R1Point m_infinity;
|
||||
|
||||
public SecP256R1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP256R1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SECP256R1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP256R1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP256R1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP256R1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP256R1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP256R1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat256.Copy(((SecP256R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP256R1_FE_INTS;
|
||||
Nat256.Copy(((SecP256R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP256R1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP256R1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat256.Create();
|
||||
SecP256R1Field.Random(r, x);
|
||||
return new SecP256R1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat256.Create();
|
||||
SecP256R1Field.RandomMult(r, x);
|
||||
return new SecP256R1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP256R1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP256R1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP256R1LookupTable(SecP256R1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP256R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP256R1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP256R1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat256.Create(), y = Nat256.Create();
|
||||
int pos = index * SECP256R1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP256R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP256R1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP256R1FieldElement(x), new SecP256R1FieldElement(y), SECP256R1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9a8aec9baf18876489e744ea976640b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
380
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256R1Field.cs
vendored
Normal file
380
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256R1Field.cs
vendored
Normal file
@@ -0,0 +1,380 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256R1Field
|
||||
{
|
||||
// 2^256 - 2^224 + 2^192 + 2^96 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000001, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
|
||||
0xFFFFFFFF, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0x00000001, 0xFFFFFFFE,
|
||||
0x00000002, 0xFFFFFFFE };
|
||||
private const uint P7 = 0xFFFFFFFF;
|
||||
private const uint PExt15 = 0xFFFFFFFE;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat256.Add(x, y, z);
|
||||
if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(16, xx, yy, zz);
|
||||
if (c != 0 || (zz[15] >= PExt15 && Nat.Gte(16, zz, PExt)))
|
||||
{
|
||||
Nat.SubFrom(16, PExt, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(8, x, z);
|
||||
if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(256, x);
|
||||
if (z[7] == P7 && Nat256.Gte(z, P))
|
||||
{
|
||||
Nat256.SubFrom(P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(8, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat256.Add(x, P, z);
|
||||
Nat.ShiftDownBit(8, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
|
||||
{
|
||||
Nat256.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
|
||||
{
|
||||
uint c = Nat256.MulAddTo(x, y, zz);
|
||||
if (c != 0 || (zz[15] >= PExt15 && Nat.Gte(16, zz, PExt)))
|
||||
{
|
||||
Nat.SubFrom(16, PExt, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat256.Sub(P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat256.Sub(P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[8 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 8);
|
||||
}
|
||||
while (0 == Nat.LessThan(8, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
long xx08 = xx[8], xx09 = xx[9], xx10 = xx[10], xx11 = xx[11];
|
||||
long xx12 = xx[12], xx13 = xx[13], xx14 = xx[14], xx15 = xx[15];
|
||||
|
||||
const long n = 6;
|
||||
|
||||
xx08 -= n;
|
||||
|
||||
long t0 = xx08 + xx09;
|
||||
long t1 = xx09 + xx10;
|
||||
long t2 = xx10 + xx11 - xx15;
|
||||
long t3 = xx11 + xx12;
|
||||
long t4 = xx12 + xx13;
|
||||
long t5 = xx13 + xx14;
|
||||
long t6 = xx14 + xx15;
|
||||
long t7 = t5 - t0;
|
||||
|
||||
long cc = 0;
|
||||
cc += (long)xx[0] - t3 - t7;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[1] + t1 - t4 - t6;
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[2] + t2 - t5;
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[3] + (t3 << 1) + t7 - t6;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[4] + (t4 << 1) + xx14 - t1;
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[5] + (t5 << 1) - t2;
|
||||
z[5] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[6] + (t6 << 1) + t7;
|
||||
z[6] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[7] + (xx15 << 1) + xx08 - t2 - t4;
|
||||
z[7] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += n;
|
||||
|
||||
Debug.Assert(cc >= 0);
|
||||
|
||||
Reduce32((uint)cc, z);
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
long cc = 0;
|
||||
|
||||
if (x != 0)
|
||||
{
|
||||
long xx08 = x;
|
||||
|
||||
cc += (long)z[0] + xx08;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (long)z[1];
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[2];
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
}
|
||||
cc += (long)z[3] - xx08;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (long)z[4];
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[5];
|
||||
z[5] = (uint)cc;
|
||||
cc >>= 32;
|
||||
}
|
||||
cc += (long)z[6] - xx08;
|
||||
z[6] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[7] + xx08;
|
||||
z[7] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
Debug.Assert(cc == 0 || cc == 1);
|
||||
}
|
||||
|
||||
if (cc != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z, uint[] tt)
|
||||
{
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat256.CreateExt();
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat256.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
Nat256.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat256.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat256.Sub(x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
SubPInvFrom(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(16, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.AddTo(16, PExt, zz);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(8, x, 0, z);
|
||||
if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddPInvTo(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] + 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[3] - 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[4];
|
||||
z[4] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[5];
|
||||
z[5] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[6] - 1;
|
||||
z[6] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[7] + 1;
|
||||
z[7] = (uint)c;
|
||||
//c >>= 32;
|
||||
}
|
||||
|
||||
private static void SubPInvFrom(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] - 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[1];
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[3] + 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[4];
|
||||
z[4] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[5];
|
||||
z[5] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[6] + 1;
|
||||
z[6] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[7] - 1;
|
||||
z[7] = (uint)c;
|
||||
//c >>= 32;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3640e5c2b2edffa45ae0610c1b75a04c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,194 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256R1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP256R1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP256R1FieldElement", "x");
|
||||
|
||||
this.x = SecP256R1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP256R1FieldElement()
|
||||
{
|
||||
this.x = Nat256.Create();
|
||||
}
|
||||
|
||||
protected internal SecP256R1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat256.IsZero(x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat256.IsOne(x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat256.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat256.ToBigInteger(x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP256R1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.Add(x, ((SecP256R1FieldElement)b).x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.AddOne(x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.Subtract(x, ((SecP256R1FieldElement)b).x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.Multiply(x, ((SecP256R1FieldElement)b).x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.Inv(((SecP256R1FieldElement)b).x, z);
|
||||
SecP256R1Field.Multiply(z, x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.Negate(x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.Square(x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat256.Create();
|
||||
SecP256R1Field.Inv(x, z);
|
||||
return new SecP256R1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
// Raise this element to the exponent 2^254 - 2^222 + 2^190 + 2^94
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
|
||||
return this;
|
||||
|
||||
uint[] tt0 = Nat256.CreateExt();
|
||||
uint[] t1 = Nat256.Create();
|
||||
uint[] t2 = Nat256.Create();
|
||||
|
||||
SecP256R1Field.Square(x1, t1, tt0);
|
||||
SecP256R1Field.Multiply(t1, x1, t1, tt0);
|
||||
|
||||
SecP256R1Field.SquareN(t1, 2, t2, tt0);
|
||||
SecP256R1Field.Multiply(t2, t1, t2, tt0);
|
||||
|
||||
SecP256R1Field.SquareN(t2, 4, t1, tt0);
|
||||
SecP256R1Field.Multiply(t1, t2, t1, tt0);
|
||||
|
||||
SecP256R1Field.SquareN(t1, 8, t2, tt0);
|
||||
SecP256R1Field.Multiply(t2, t1, t2, tt0);
|
||||
|
||||
SecP256R1Field.SquareN(t2, 16, t1, tt0);
|
||||
SecP256R1Field.Multiply(t1, t2, t1, tt0);
|
||||
|
||||
SecP256R1Field.SquareN(t1, 32, t1, tt0);
|
||||
SecP256R1Field.Multiply(t1, x1, t1, tt0);
|
||||
|
||||
SecP256R1Field.SquareN(t1, 96, t1, tt0);
|
||||
SecP256R1Field.Multiply(t1, x1, t1, tt0);
|
||||
|
||||
SecP256R1Field.SquareN(t1, 94, t1, tt0);
|
||||
SecP256R1Field.Multiply(t1, t1, t2, tt0);
|
||||
|
||||
return Nat256.Eq(x1, t2) ? new SecP256R1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP256R1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP256R1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP256R1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat256.Eq(x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8577a529c38b84438ba01a17b683c02
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
251
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256R1Point.cs
vendored
Normal file
251
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP256R1Point.cs
vendored
Normal file
@@ -0,0 +1,251 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP256R1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP256R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP256R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP256R1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP256R1FieldElement X1 = (SecP256R1FieldElement)this.RawXCoord, Y1 = (SecP256R1FieldElement)this.RawYCoord;
|
||||
SecP256R1FieldElement X2 = (SecP256R1FieldElement)b.RawXCoord, Y2 = (SecP256R1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP256R1FieldElement Z1 = (SecP256R1FieldElement)this.RawZCoords[0];
|
||||
SecP256R1FieldElement Z2 = (SecP256R1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt0 = Nat256.CreateExt();
|
||||
uint[] tt1 = Nat256.CreateExt();
|
||||
uint[] t2 = Nat256.Create();
|
||||
uint[] t3 = Nat256.Create();
|
||||
uint[] t4 = Nat256.Create();
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP256R1Field.Square(Z1.x, S2, tt0);
|
||||
|
||||
U2 = t2;
|
||||
SecP256R1Field.Multiply(S2, X2.x, U2, tt0);
|
||||
|
||||
SecP256R1Field.Multiply(S2, Z1.x, S2, tt0);
|
||||
SecP256R1Field.Multiply(S2, Y2.x, S2, tt0);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP256R1Field.Square(Z2.x, S1, tt0);
|
||||
|
||||
U1 = tt1;
|
||||
SecP256R1Field.Multiply(S1, X1.x, U1, tt0);
|
||||
|
||||
SecP256R1Field.Multiply(S1, Z2.x, S1, tt0);
|
||||
SecP256R1Field.Multiply(S1, Y1.x, S1, tt0);
|
||||
}
|
||||
|
||||
uint[] H = Nat256.Create();
|
||||
SecP256R1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = t2;
|
||||
SecP256R1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat256.IsZero(H))
|
||||
{
|
||||
if (Nat256.IsZero(R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP256R1Field.Square(H, HSquared, tt0);
|
||||
|
||||
uint[] G = Nat256.Create();
|
||||
SecP256R1Field.Multiply(HSquared, H, G, tt0);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP256R1Field.Multiply(HSquared, U1, V, tt0);
|
||||
|
||||
SecP256R1Field.Negate(G, G);
|
||||
Nat256.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat256.AddBothTo(V, V, G);
|
||||
SecP256R1Field.Reduce32(c, G);
|
||||
|
||||
SecP256R1FieldElement X3 = new SecP256R1FieldElement(t4);
|
||||
SecP256R1Field.Square(R, X3.x, tt0);
|
||||
SecP256R1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP256R1FieldElement Y3 = new SecP256R1FieldElement(G);
|
||||
SecP256R1Field.Subtract(V, X3.x, Y3.x);
|
||||
SecP256R1Field.MultiplyAddToExt(Y3.x, R, tt1);
|
||||
SecP256R1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP256R1FieldElement Z3 = new SecP256R1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP256R1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
|
||||
|
||||
return new SecP256R1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP256R1FieldElement Y1 = (SecP256R1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP256R1FieldElement X1 = (SecP256R1FieldElement)this.RawXCoord, Z1 = (SecP256R1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt0 = Nat256.CreateExt();
|
||||
uint[] t1 = Nat256.Create();
|
||||
uint[] t2 = Nat256.Create();
|
||||
|
||||
uint[] Y1Squared = Nat256.Create();
|
||||
SecP256R1Field.Square(Y1.x, Y1Squared, tt0);
|
||||
|
||||
uint[] T = Nat256.Create();
|
||||
SecP256R1Field.Square(Y1Squared, T, tt0);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SecP256R1Field.Square(Z1.x, Z1Squared, tt0);
|
||||
}
|
||||
|
||||
SecP256R1Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SecP256R1Field.Add(X1.x, Z1Squared, M);
|
||||
SecP256R1Field.Multiply(M, t1, M, tt0);
|
||||
c = Nat256.AddBothTo(M, M, M);
|
||||
SecP256R1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP256R1Field.Multiply(Y1Squared, X1.x, S, tt0);
|
||||
c = Nat.ShiftUpBits(8, S, 2, 0);
|
||||
SecP256R1Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(8, T, 3, 0, t1);
|
||||
SecP256R1Field.Reduce32(c, t1);
|
||||
|
||||
SecP256R1FieldElement X3 = new SecP256R1FieldElement(T);
|
||||
SecP256R1Field.Square(M, X3.x, tt0);
|
||||
SecP256R1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP256R1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP256R1FieldElement Y3 = new SecP256R1FieldElement(S);
|
||||
SecP256R1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP256R1Field.Multiply(Y3.x, M, Y3.x, tt0);
|
||||
SecP256R1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP256R1FieldElement Z3 = new SecP256R1FieldElement(M);
|
||||
SecP256R1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
|
||||
}
|
||||
|
||||
return new SecP256R1Point(curve, X3, Y3, new ECFieldElement[]{ Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP256R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78fe183c245187b44a7f2614e627f658
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP384R1Curve.cs
vendored
Normal file
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP384R1Curve.cs
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP384R1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP384R1FieldElement.Q;
|
||||
|
||||
private const int SECP384R1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP384R1_FE_INTS = 12;
|
||||
private static readonly ECFieldElement[] SECP384R1_AFFINE_ZS = new ECFieldElement[] { new SecP384R1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP384R1Point m_infinity;
|
||||
|
||||
public SecP384R1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP384R1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SECP384R1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP384R1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP384R1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP384R1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP384R1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP384R1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat.Copy(SECP384R1_FE_INTS, ((SecP384R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP384R1_FE_INTS;
|
||||
Nat.Copy(SECP384R1_FE_INTS, ((SecP384R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP384R1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP384R1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat.Create(12);
|
||||
SecP384R1Field.Random(r, x);
|
||||
return new SecP384R1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat.Create(12);
|
||||
SecP384R1Field.RandomMult(r, x);
|
||||
return new SecP384R1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP384R1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP384R1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP384R1LookupTable(SecP384R1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat.Create(SECP384R1_FE_INTS), y = Nat.Create(SECP384R1_FE_INTS);
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP384R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP384R1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP384R1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat.Create(SECP384R1_FE_INTS), y = Nat.Create(SECP384R1_FE_INTS);
|
||||
int pos = index * SECP384R1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP384R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP384R1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP384R1FieldElement(x), new SecP384R1FieldElement(y), SECP384R1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb0aa384df631da46ba41af8f288f320
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
364
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP384R1Field.cs
vendored
Normal file
364
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP384R1Field.cs
vendored
Normal file
@@ -0,0 +1,364 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP384R1Field
|
||||
{
|
||||
// 2^384 - 2^128 - 2^96 + 2^32 - 1
|
||||
internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFE,
|
||||
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
private static readonly uint[] PExt = new uint[]{ 0x00000001, 0xFFFFFFFE, 0x00000000, 0x00000002, 0x00000000,
|
||||
0xFFFFFFFE, 0x00000000, 0x00000002, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFE, 0x00000001,
|
||||
0x00000000, 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
|
||||
0xFFFFFFFF };
|
||||
private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0xFFFFFFFD,
|
||||
0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001,
|
||||
0xFFFFFFFE, 0xFFFFFFFF, 0x00000001, 0x00000002 };
|
||||
private const uint P11 = 0xFFFFFFFF;
|
||||
private const uint PExt23 = 0xFFFFFFFF;
|
||||
|
||||
public static void Add(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint c = Nat.Add(12, x, y, z);
|
||||
if (c != 0 || (z[11] == P11 && Nat.Gte(12, z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
uint c = Nat.Add(24, xx, yy, zz);
|
||||
if (c != 0 || (zz[23] == PExt23 && Nat.Gte(24, zz, PExt)))
|
||||
{
|
||||
if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.IncAt(24, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddOne(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.Inc(12, x, z);
|
||||
if (c != 0 || (z[11] == P11 && Nat.Gte(12, z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static uint[] FromBigInteger(BigInteger x)
|
||||
{
|
||||
uint[] z = Nat.FromBigInteger(384, x);
|
||||
if (z[11] == P11 && Nat.Gte(12, z, P))
|
||||
{
|
||||
Nat.SubFrom(12, P, z);
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
public static void Half(uint[] x, uint[] z)
|
||||
{
|
||||
if ((x[0] & 1) == 0)
|
||||
{
|
||||
Nat.ShiftDownBit(12, x, 0, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint c = Nat.Add(12, x, P, z);
|
||||
Nat.ShiftDownBit(12, z, c);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Inv(uint[] x, uint[] z)
|
||||
{
|
||||
Mod.CheckedModOddInverse(P, x, z);
|
||||
}
|
||||
|
||||
public static int IsZero(uint[] x)
|
||||
{
|
||||
uint d = 0;
|
||||
for (int i = 0; i < 12; ++i)
|
||||
{
|
||||
d |= x[i];
|
||||
}
|
||||
d = (d >> 1) | (d & 1);
|
||||
return ((int)d - 1) >> 31;
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat.Create(24);
|
||||
Nat384.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
|
||||
{
|
||||
Nat384.Mul(x, y, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void Negate(uint[] x, uint[] z)
|
||||
{
|
||||
if (0 != IsZero(x))
|
||||
{
|
||||
Nat.Sub(12, P, P, z);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nat.Sub(12, P, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Random(SecureRandom r, uint[] z)
|
||||
{
|
||||
byte[] bb = new byte[12 * 4];
|
||||
do
|
||||
{
|
||||
r.NextBytes(bb);
|
||||
Pack.LE_To_UInt32(bb, 0, z, 0, 12);
|
||||
}
|
||||
while (0 == Nat.LessThan(12, z, P));
|
||||
}
|
||||
|
||||
public static void RandomMult(SecureRandom r, uint[] z)
|
||||
{
|
||||
do
|
||||
{
|
||||
Random(r, z);
|
||||
}
|
||||
while (0 != IsZero(z));
|
||||
}
|
||||
|
||||
public static void Reduce(uint[] xx, uint[] z)
|
||||
{
|
||||
long xx16 = xx[16], xx17 = xx[17], xx18 = xx[18], xx19 = xx[19];
|
||||
long xx20 = xx[20], xx21 = xx[21], xx22 = xx[22], xx23 = xx[23];
|
||||
|
||||
const long n = 1;
|
||||
|
||||
long t0 = (long)xx[12] + xx20 - n;
|
||||
long t1 = (long)xx[13] + xx22;
|
||||
long t2 = (long)xx[14] + xx22 + xx23;
|
||||
long t3 = (long)xx[15] + xx23;
|
||||
long t4 = xx17 + xx21;
|
||||
long t5 = xx21 - xx23;
|
||||
long t6 = xx22 - xx23;
|
||||
long t7 = t0 + t5;
|
||||
|
||||
long cc = 0;
|
||||
cc += (long)xx[0] + t7;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[1] + xx23 - t0 + t1;
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[2] - xx21 - t1 + t2;
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[3] - t2 + t3 + t7;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[4] + xx16 + xx21 + t1 - t3 + t7;
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[5] - xx16 + t1 + t2 + t4;
|
||||
z[5] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[6] + xx18 - xx17 + t2 + t3;
|
||||
z[6] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[7] + xx16 + xx19 - xx18 + t3;
|
||||
z[7] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[8] + xx16 + xx17 + xx20 - xx19;
|
||||
z[8] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[9] + xx18 - xx20 + t4;
|
||||
z[9] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[10] + xx18 + xx19 - t5 + t6;
|
||||
z[10] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)xx[11] + xx19 + xx20 - t6;
|
||||
z[11] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += n;
|
||||
|
||||
Debug.Assert(cc >= 0);
|
||||
|
||||
Reduce32((uint)cc, z);
|
||||
}
|
||||
|
||||
public static void Reduce32(uint x, uint[] z)
|
||||
{
|
||||
long cc = 0;
|
||||
|
||||
if (x != 0)
|
||||
{
|
||||
long xx12 = x;
|
||||
|
||||
cc += (long)z[0] + xx12;
|
||||
z[0] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[1] - xx12;
|
||||
z[1] = (uint)cc;
|
||||
cc >>= 32;
|
||||
if (cc != 0)
|
||||
{
|
||||
cc += (long)z[2];
|
||||
z[2] = (uint)cc;
|
||||
cc >>= 32;
|
||||
}
|
||||
cc += (long)z[3] + xx12;
|
||||
z[3] = (uint)cc;
|
||||
cc >>= 32;
|
||||
cc += (long)z[4] + xx12;
|
||||
z[4] = (uint)cc;
|
||||
cc >>= 32;
|
||||
|
||||
Debug.Assert(cc == 0 || cc == 1);
|
||||
}
|
||||
|
||||
if ((cc != 0 && Nat.IncAt(12, z, 5) != 0)
|
||||
|| (z[11] == P11 && Nat.Gte(12, z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z)
|
||||
{
|
||||
uint[] tt = Nat.Create(24);
|
||||
Nat384.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void Square(uint[] x, uint[] z, uint[] tt)
|
||||
{
|
||||
Nat384.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
uint[] tt = Nat.Create(24);
|
||||
Nat384.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat384.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
|
||||
{
|
||||
Debug.Assert(n > 0);
|
||||
|
||||
Nat384.Square(x, tt);
|
||||
Reduce(tt, z);
|
||||
|
||||
while (--n > 0)
|
||||
{
|
||||
Nat384.Square(z, tt);
|
||||
Reduce(tt, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Subtract(uint[] x, uint[] y, uint[] z)
|
||||
{
|
||||
int c = Nat.Sub(12, x, y, z);
|
||||
if (c != 0)
|
||||
{
|
||||
SubPInvFrom(z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
|
||||
{
|
||||
int c = Nat.Sub(24, xx, yy, zz);
|
||||
if (c != 0)
|
||||
{
|
||||
if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
|
||||
{
|
||||
Nat.DecAt(24, zz, PExtInv.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Twice(uint[] x, uint[] z)
|
||||
{
|
||||
uint c = Nat.ShiftUpBit(12, x, 0, z);
|
||||
if (c != 0 || (z[11] == P11 && Nat.Gte(12, z, P)))
|
||||
{
|
||||
AddPInvTo(z);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddPInvTo(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] + 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[1] - 1;
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[3] + 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[4] + 1;
|
||||
z[4] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.IncAt(12, z, 5);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SubPInvFrom(uint[] z)
|
||||
{
|
||||
long c = (long)z[0] - 1;
|
||||
z[0] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[1] + 1;
|
||||
z[1] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
c += (long)z[2];
|
||||
z[2] = (uint)c;
|
||||
c >>= 32;
|
||||
}
|
||||
c += (long)z[3] - 1;
|
||||
z[3] = (uint)c;
|
||||
c >>= 32;
|
||||
c += (long)z[4] - 1;
|
||||
z[4] = (uint)c;
|
||||
c >>= 32;
|
||||
if (c != 0)
|
||||
{
|
||||
Nat.DecAt(12, z, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe192fdf195378545a78c6ed2bf23b78
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,216 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP384R1FieldElement
|
||||
: AbstractFpFieldElement
|
||||
{
|
||||
public static readonly BigInteger Q = new BigInteger(1,
|
||||
Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"));
|
||||
|
||||
protected internal readonly uint[] x;
|
||||
|
||||
public SecP384R1FieldElement(BigInteger x)
|
||||
{
|
||||
if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
|
||||
throw new ArgumentException("value invalid for SecP384R1FieldElement", "x");
|
||||
|
||||
this.x = SecP384R1Field.FromBigInteger(x);
|
||||
}
|
||||
|
||||
public SecP384R1FieldElement()
|
||||
{
|
||||
this.x = Nat.Create(12);
|
||||
}
|
||||
|
||||
protected internal SecP384R1FieldElement(uint[] x)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool IsZero
|
||||
{
|
||||
get { return Nat.IsZero(12, x); }
|
||||
}
|
||||
|
||||
public override bool IsOne
|
||||
{
|
||||
get { return Nat.IsOne(12, x); }
|
||||
}
|
||||
|
||||
public override bool TestBitZero()
|
||||
{
|
||||
return Nat.GetBit(x, 0) == 1;
|
||||
}
|
||||
|
||||
public override BigInteger ToBigInteger()
|
||||
{
|
||||
return Nat.ToBigInteger(12, x);
|
||||
}
|
||||
|
||||
public override string FieldName
|
||||
{
|
||||
get { return "SecP384R1Field"; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return Q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement Add(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.Add(x, ((SecP384R1FieldElement)b).x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement AddOne()
|
||||
{
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.AddOne(x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Subtract(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.Subtract(x, ((SecP384R1FieldElement)b).x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Multiply(ECFieldElement b)
|
||||
{
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.Multiply(x, ((SecP384R1FieldElement)b).x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Divide(ECFieldElement b)
|
||||
{
|
||||
//return Multiply(b.Invert());
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.Inv(((SecP384R1FieldElement)b).x, z);
|
||||
SecP384R1Field.Multiply(z, x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Negate()
|
||||
{
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.Negate(x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Square()
|
||||
{
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.Square(x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
public override ECFieldElement Invert()
|
||||
{
|
||||
uint[] z = Nat.Create(12);
|
||||
SecP384R1Field.Inv(x, z);
|
||||
return new SecP384R1FieldElement(z);
|
||||
}
|
||||
|
||||
/**
|
||||
* return a sqrt root - the routine verifies that the calculation returns the right value - if
|
||||
* none exists it returns null.
|
||||
*/
|
||||
public override ECFieldElement Sqrt()
|
||||
{
|
||||
// Raise this element to the exponent 2^382 - 2^126 - 2^94 + 2^30
|
||||
|
||||
uint[] x1 = this.x;
|
||||
if (Nat.IsZero(12, x1) || Nat.IsOne(12, x1))
|
||||
return this;
|
||||
|
||||
uint[] tt0 = Nat.Create(24);
|
||||
uint[] t1 = Nat.Create(12);
|
||||
uint[] t2 = Nat.Create(12);
|
||||
uint[] t3 = Nat.Create(12);
|
||||
uint[] t4 = Nat.Create(12);
|
||||
|
||||
SecP384R1Field.Square(x1, t1, tt0);
|
||||
SecP384R1Field.Multiply(t1, x1, t1, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(t1, 2, t2, tt0);
|
||||
SecP384R1Field.Multiply(t2, t1, t2, tt0);
|
||||
|
||||
SecP384R1Field.Square(t2, t2, tt0);
|
||||
SecP384R1Field.Multiply(t2, x1, t2, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(t2, 5, t3, tt0);
|
||||
SecP384R1Field.Multiply(t3, t2, t3, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(t3, 5, t4, tt0);
|
||||
SecP384R1Field.Multiply(t4, t2, t4, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(t4, 15, t2, tt0);
|
||||
SecP384R1Field.Multiply(t2, t4, t2, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(t2, 2, t3, tt0);
|
||||
SecP384R1Field.Multiply(t1, t3, t1, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(t3, 28, t3, tt0);
|
||||
SecP384R1Field.Multiply(t2, t3, t2, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(t2, 60, t3, tt0);
|
||||
SecP384R1Field.Multiply(t3, t2, t3, tt0);
|
||||
|
||||
uint[] r = t2;
|
||||
|
||||
SecP384R1Field.SquareN(t3, 120, r, tt0);
|
||||
SecP384R1Field.Multiply(r, t3, r, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(r, 15, r, tt0);
|
||||
SecP384R1Field.Multiply(r, t4, r, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(r, 33, r, tt0);
|
||||
SecP384R1Field.Multiply(r, t1, r, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(r, 64, r, tt0);
|
||||
SecP384R1Field.Multiply(r, x1, r, tt0);
|
||||
|
||||
SecP384R1Field.SquareN(r, 30, t1, tt0);
|
||||
SecP384R1Field.Square(t1, t2, tt0);
|
||||
|
||||
return Nat.Eq(12, x1, t2) ? new SecP384R1FieldElement(t1) : null;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return Equals(obj as SecP384R1FieldElement);
|
||||
}
|
||||
|
||||
public override bool Equals(ECFieldElement other)
|
||||
{
|
||||
return Equals(other as SecP384R1FieldElement);
|
||||
}
|
||||
|
||||
public virtual bool Equals(SecP384R1FieldElement other)
|
||||
{
|
||||
if (this == other)
|
||||
return true;
|
||||
if (null == other)
|
||||
return false;
|
||||
return Nat.Eq(12, x, other.x);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 12);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f6d8e1f2882f3a459dfd2da3a9dd21b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
252
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP384R1Point.cs
vendored
Normal file
252
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP384R1Point.cs
vendored
Normal file
@@ -0,0 +1,252 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP384R1Point
|
||||
: AbstractFpPoint
|
||||
{
|
||||
internal SecP384R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
|
||||
: base(curve, x, y)
|
||||
{
|
||||
}
|
||||
|
||||
internal SecP384R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
: base(curve, x, y, zs)
|
||||
{
|
||||
}
|
||||
|
||||
protected override ECPoint Detach()
|
||||
{
|
||||
return new SecP384R1Point(null, AffineXCoord, AffineYCoord);
|
||||
}
|
||||
|
||||
public override ECPoint Add(ECPoint b)
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return this;
|
||||
if (this == b)
|
||||
return Twice();
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP384R1FieldElement X1 = (SecP384R1FieldElement)this.RawXCoord, Y1 = (SecP384R1FieldElement)this.RawYCoord;
|
||||
SecP384R1FieldElement X2 = (SecP384R1FieldElement)b.RawXCoord, Y2 = (SecP384R1FieldElement)b.RawYCoord;
|
||||
|
||||
SecP384R1FieldElement Z1 = (SecP384R1FieldElement)this.RawZCoords[0];
|
||||
SecP384R1FieldElement Z2 = (SecP384R1FieldElement)b.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt0 = Nat.Create(24);
|
||||
uint[] tt1 = Nat.Create(24);
|
||||
uint[] tt2 = Nat.Create(24);
|
||||
uint[] t3 = Nat.Create(12);
|
||||
uint[] t4 = Nat.Create(12);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
uint[] U2, S2;
|
||||
if (Z1IsOne)
|
||||
{
|
||||
U2 = X2.x;
|
||||
S2 = Y2.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S2 = t3;
|
||||
SecP384R1Field.Square(Z1.x, S2, tt0);
|
||||
|
||||
U2 = tt2;
|
||||
SecP384R1Field.Multiply(S2, X2.x, U2, tt0);
|
||||
|
||||
SecP384R1Field.Multiply(S2, Z1.x, S2, tt0);
|
||||
SecP384R1Field.Multiply(S2, Y2.x, S2, tt0);
|
||||
}
|
||||
|
||||
bool Z2IsOne = Z2.IsOne;
|
||||
uint[] U1, S1;
|
||||
if (Z2IsOne)
|
||||
{
|
||||
U1 = X1.x;
|
||||
S1 = Y1.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
S1 = t4;
|
||||
SecP384R1Field.Square(Z2.x, S1, tt0);
|
||||
|
||||
U1 = tt1;
|
||||
SecP384R1Field.Multiply(S1, X1.x, U1, tt0);
|
||||
|
||||
SecP384R1Field.Multiply(S1, Z2.x, S1, tt0);
|
||||
SecP384R1Field.Multiply(S1, Y1.x, S1, tt0);
|
||||
}
|
||||
|
||||
uint[] H = Nat.Create(12);
|
||||
SecP384R1Field.Subtract(U1, U2, H);
|
||||
|
||||
uint[] R = Nat.Create(12);
|
||||
SecP384R1Field.Subtract(S1, S2, R);
|
||||
|
||||
// Check if b == this or b == -this
|
||||
if (Nat.IsZero(12, H))
|
||||
{
|
||||
if (Nat.IsZero(12, R))
|
||||
{
|
||||
// this == b, i.e. this must be doubled
|
||||
return this.Twice();
|
||||
}
|
||||
|
||||
// this == -b, i.e. the result is the point at infinity
|
||||
return curve.Infinity;
|
||||
}
|
||||
|
||||
uint[] HSquared = t3;
|
||||
SecP384R1Field.Square(H, HSquared, tt0);
|
||||
|
||||
uint[] G = Nat.Create(12);
|
||||
SecP384R1Field.Multiply(HSquared, H, G, tt0);
|
||||
|
||||
uint[] V = t3;
|
||||
SecP384R1Field.Multiply(HSquared, U1, V, tt0);
|
||||
|
||||
SecP384R1Field.Negate(G, G);
|
||||
Nat384.Mul(S1, G, tt1);
|
||||
|
||||
c = Nat.AddBothTo(12, V, V, G);
|
||||
SecP384R1Field.Reduce32(c, G);
|
||||
|
||||
SecP384R1FieldElement X3 = new SecP384R1FieldElement(t4);
|
||||
SecP384R1Field.Square(R, X3.x, tt0);
|
||||
SecP384R1Field.Subtract(X3.x, G, X3.x);
|
||||
|
||||
SecP384R1FieldElement Y3 = new SecP384R1FieldElement(G);
|
||||
SecP384R1Field.Subtract(V, X3.x, Y3.x);
|
||||
Nat384.Mul(Y3.x, R, tt2);
|
||||
SecP384R1Field.AddExt(tt1, tt2, tt1);
|
||||
SecP384R1Field.Reduce(tt1, Y3.x);
|
||||
|
||||
SecP384R1FieldElement Z3 = new SecP384R1FieldElement(H);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
|
||||
}
|
||||
if (!Z2IsOne)
|
||||
{
|
||||
SecP384R1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
|
||||
}
|
||||
|
||||
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
|
||||
|
||||
return new SecP384R1Point(curve, X3, Y3, zs);
|
||||
}
|
||||
|
||||
public override ECPoint Twice()
|
||||
{
|
||||
if (this.IsInfinity)
|
||||
return this;
|
||||
|
||||
ECCurve curve = this.Curve;
|
||||
|
||||
SecP384R1FieldElement Y1 = (SecP384R1FieldElement)this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return curve.Infinity;
|
||||
|
||||
SecP384R1FieldElement X1 = (SecP384R1FieldElement)this.RawXCoord, Z1 = (SecP384R1FieldElement)this.RawZCoords[0];
|
||||
|
||||
uint c;
|
||||
uint[] tt0 = Nat.Create(24);
|
||||
uint[] t1 = Nat.Create(12);
|
||||
uint[] t2 = Nat.Create(12);
|
||||
|
||||
uint[] Y1Squared = Nat.Create(12);
|
||||
SecP384R1Field.Square(Y1.x, Y1Squared, tt0);
|
||||
|
||||
uint[] T = Nat.Create(12);
|
||||
SecP384R1Field.Square(Y1Squared, T, tt0);
|
||||
|
||||
bool Z1IsOne = Z1.IsOne;
|
||||
|
||||
uint[] Z1Squared = Z1.x;
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
Z1Squared = t2;
|
||||
SecP384R1Field.Square(Z1.x, Z1Squared, tt0);
|
||||
}
|
||||
|
||||
SecP384R1Field.Subtract(X1.x, Z1Squared, t1);
|
||||
|
||||
uint[] M = t2;
|
||||
SecP384R1Field.Add(X1.x, Z1Squared, M);
|
||||
SecP384R1Field.Multiply(M, t1, M, tt0);
|
||||
c = Nat.AddBothTo(12, M, M, M);
|
||||
SecP384R1Field.Reduce32(c, M);
|
||||
|
||||
uint[] S = Y1Squared;
|
||||
SecP384R1Field.Multiply(Y1Squared, X1.x, S, tt0);
|
||||
c = Nat.ShiftUpBits(12, S, 2, 0);
|
||||
SecP384R1Field.Reduce32(c, S);
|
||||
|
||||
c = Nat.ShiftUpBits(12, T, 3, 0, t1);
|
||||
SecP384R1Field.Reduce32(c, t1);
|
||||
|
||||
SecP384R1FieldElement X3 = new SecP384R1FieldElement(T);
|
||||
SecP384R1Field.Square(M, X3.x, tt0);
|
||||
SecP384R1Field.Subtract(X3.x, S, X3.x);
|
||||
SecP384R1Field.Subtract(X3.x, S, X3.x);
|
||||
|
||||
SecP384R1FieldElement Y3 = new SecP384R1FieldElement(S);
|
||||
SecP384R1Field.Subtract(S, X3.x, Y3.x);
|
||||
SecP384R1Field.Multiply(Y3.x, M, Y3.x, tt0);
|
||||
SecP384R1Field.Subtract(Y3.x, t1, Y3.x);
|
||||
|
||||
SecP384R1FieldElement Z3 = new SecP384R1FieldElement(M);
|
||||
SecP384R1Field.Twice(Y1.x, Z3.x);
|
||||
if (!Z1IsOne)
|
||||
{
|
||||
SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
|
||||
}
|
||||
|
||||
return new SecP384R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
|
||||
}
|
||||
|
||||
public override ECPoint TwicePlus(ECPoint b)
|
||||
{
|
||||
if (this == b)
|
||||
return ThreeTimes();
|
||||
if (this.IsInfinity)
|
||||
return b;
|
||||
if (b.IsInfinity)
|
||||
return Twice();
|
||||
|
||||
ECFieldElement Y1 = this.RawYCoord;
|
||||
if (Y1.IsZero)
|
||||
return b;
|
||||
|
||||
return Twice().Add(b);
|
||||
}
|
||||
|
||||
public override ECPoint ThreeTimes()
|
||||
{
|
||||
if (this.IsInfinity || this.RawYCoord.IsZero)
|
||||
return this;
|
||||
|
||||
// NOTE: Be careful about recursions between TwicePlus and ThreeTimes
|
||||
return Twice().Add(this);
|
||||
}
|
||||
|
||||
public override ECPoint Negate()
|
||||
{
|
||||
if (IsInfinity)
|
||||
return this;
|
||||
|
||||
return new SecP384R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32d0493feac8f2d4188dcdcf83da34ca
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP521R1Curve.cs
vendored
Normal file
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/math/ec/custom/sec/SecP521R1Curve.cs
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
|
||||
{
|
||||
internal class SecP521R1Curve
|
||||
: AbstractFpCurve
|
||||
{
|
||||
public static readonly BigInteger q = SecP521R1FieldElement.Q;
|
||||
|
||||
private const int SECP521R1_DEFAULT_COORDS = COORD_JACOBIAN;
|
||||
private const int SECP521R1_FE_INTS = 17;
|
||||
private static readonly ECFieldElement[] SECP521R1_AFFINE_ZS = new ECFieldElement[] { new SecP521R1FieldElement(BigInteger.One) };
|
||||
|
||||
protected readonly SecP521R1Point m_infinity;
|
||||
|
||||
public SecP521R1Curve()
|
||||
: base(q)
|
||||
{
|
||||
this.m_infinity = new SecP521R1Point(this, null, null);
|
||||
|
||||
this.m_a = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC")));
|
||||
this.m_b = FromBigInteger(new BigInteger(1,
|
||||
Hex.DecodeStrict("0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00")));
|
||||
this.m_order = new BigInteger(1, Hex.DecodeStrict("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409"));
|
||||
this.m_cofactor = BigInteger.One;
|
||||
this.m_coord = SECP521R1_DEFAULT_COORDS;
|
||||
}
|
||||
|
||||
protected override ECCurve CloneCurve()
|
||||
{
|
||||
return new SecP521R1Curve();
|
||||
}
|
||||
|
||||
public override bool SupportsCoordinateSystem(int coord)
|
||||
{
|
||||
switch (coord)
|
||||
{
|
||||
case COORD_JACOBIAN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual BigInteger Q
|
||||
{
|
||||
get { return q; }
|
||||
}
|
||||
|
||||
public override ECPoint Infinity
|
||||
{
|
||||
get { return m_infinity; }
|
||||
}
|
||||
|
||||
public override int FieldSize
|
||||
{
|
||||
get { return q.BitLength; }
|
||||
}
|
||||
|
||||
public override ECFieldElement FromBigInteger(BigInteger x)
|
||||
{
|
||||
return new SecP521R1FieldElement(x);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y)
|
||||
{
|
||||
return new SecP521R1Point(this, x, y);
|
||||
}
|
||||
|
||||
protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs)
|
||||
{
|
||||
return new SecP521R1Point(this, x, y, zs);
|
||||
}
|
||||
|
||||
public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
|
||||
{
|
||||
uint[] table = new uint[len * SECP521R1_FE_INTS * 2];
|
||||
{
|
||||
int pos = 0;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
ECPoint p = points[off + i];
|
||||
Nat.Copy(SECP521R1_FE_INTS, ((SecP521R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP521R1_FE_INTS;
|
||||
Nat.Copy(SECP521R1_FE_INTS, ((SecP521R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP521R1_FE_INTS;
|
||||
}
|
||||
}
|
||||
|
||||
return new SecP521R1LookupTable(this, table, len);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElement(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat.Create(17);
|
||||
SecP521R1Field.Random(r, x);
|
||||
return new SecP521R1FieldElement(x);
|
||||
}
|
||||
|
||||
public override ECFieldElement RandomFieldElementMult(SecureRandom r)
|
||||
{
|
||||
uint[] x = Nat.Create(17);
|
||||
SecP521R1Field.RandomMult(r, x);
|
||||
return new SecP521R1FieldElement(x);
|
||||
}
|
||||
|
||||
private class SecP521R1LookupTable
|
||||
: AbstractECLookupTable
|
||||
{
|
||||
private readonly SecP521R1Curve m_outer;
|
||||
private readonly uint[] m_table;
|
||||
private readonly int m_size;
|
||||
|
||||
internal SecP521R1LookupTable(SecP521R1Curve outer, uint[] table, int size)
|
||||
{
|
||||
this.m_outer = outer;
|
||||
this.m_table = table;
|
||||
this.m_size = size;
|
||||
}
|
||||
|
||||
public override int Size
|
||||
{
|
||||
get { return m_size; }
|
||||
}
|
||||
|
||||
public override ECPoint Lookup(int index)
|
||||
{
|
||||
uint[] x = Nat.Create(SECP521R1_FE_INTS), y = Nat.Create(SECP521R1_FE_INTS);
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < m_size; ++i)
|
||||
{
|
||||
uint MASK = (uint)(((i ^ index) - 1) >> 31);
|
||||
|
||||
for (int j = 0; j < SECP521R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] ^= m_table[pos + j] & MASK;
|
||||
y[j] ^= m_table[pos + SECP521R1_FE_INTS + j] & MASK;
|
||||
}
|
||||
|
||||
pos += (SECP521R1_FE_INTS * 2);
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
public override ECPoint LookupVar(int index)
|
||||
{
|
||||
uint[] x = Nat.Create(SECP521R1_FE_INTS), y = Nat.Create(SECP521R1_FE_INTS);
|
||||
int pos = index * SECP521R1_FE_INTS * 2;
|
||||
|
||||
for (int j = 0; j < SECP521R1_FE_INTS; ++j)
|
||||
{
|
||||
x[j] = m_table[pos + j];
|
||||
y[j] = m_table[pos + SECP521R1_FE_INTS + j];
|
||||
}
|
||||
|
||||
return CreatePoint(x, y);
|
||||
}
|
||||
|
||||
private ECPoint CreatePoint(uint[] x, uint[] y)
|
||||
{
|
||||
return m_outer.CreateRawPoint(new SecP521R1FieldElement(x), new SecP521R1FieldElement(y), SECP521R1_AFFINE_ZS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user