示例#1
0
 private static void ArgumentNullGuard(MyBigInteger operand)
 {
     if (operand is null)
     {
         throw new ArgumentNullException(nameof(operand));
     }
 }
示例#2
0
        public MyGenerator(MyPoint pointQ)
        {
            var curve = MyCurve.GetP256NistCurve();

            _pointQ = curve.CreatePointAt(NistConstants.P256.Qx, NistConstants.P256.Qy);
            _pointP = _pointQ * MyConstants.P256.Secret;
            _state  = (pointQ * MyConstants.P256.Secret).X;
        }
示例#3
0
        public MyGenerator(int initialState = 1)
        {
            _state = 1;
            var curve = MyCurve.GetP256NistCurve();

            _pointQ = curve.CreatePointAt(NistConstants.P256.Qx, NistConstants.P256.Qy) * initialState;
            _pointP = _pointQ * MyConstants.P256.Secret;
        }
        public bool IsPointOnCurve(MyBigInteger x, MyBigInteger y)
        {
            var result = x.ModPow(3, Prime) + A * x + _b - y.ModPow(2, Prime);

            if (result % Prime == new MyBigInteger(0f))
            {
                return(true);
            }
            throw new Exception();
        }
示例#5
0
 private static void ArgumentNullGuard(MyBigInteger leftOperand, MyBigInteger rightOperand)
 {
     if (leftOperand is null)
     {
         throw new ArgumentNullException(nameof(leftOperand));
     }
     if (rightOperand is null)
     {
         throw new ArgumentNullException(nameof(rightOperand));
     }
 }
        public List <MyPoint> GetPointsAt(MyBigInteger x)
        {
            var ySquared = (x * x * x % Prime + (A * x) + _b) % Prime;
            var y        = ySquared.ModSqrt(Prime);

            var result = new List <MyPoint>();

            y.Where(e => IsPointOnCurve(x, e))
            .ForEach(e => result.Add(CreatePointAt(x, e)));

            return(result);
        }
示例#7
0
 public static MyBigInteger GratestCommonDivisor(MyBigInteger leftOperand, MyBigInteger rightOperand)
 {
     while (true)
     {
         if (rightOperand == 0)
         {
             return(leftOperand);
         }
         var temp = leftOperand;
         leftOperand  = rightOperand;
         rightOperand = temp % rightOperand;
     }
 }
示例#8
0
        public MyBigInteger ModInverse(MyBigInteger modulo)
        {
            var value = _value;

            if (_value.Sign == -1)
            {
                value = BigInteger.Add(_value, modulo);
            }

            return(value == BigInteger.Zero ?
                   new MyBigInteger(BigInteger.Zero) :
                   new MyBigInteger(BigInteger.ModPow(value, modulo - 2, modulo)));
        }
示例#9
0
        public MyBigInteger GetNextRandomValue(bool stepPointP = true)
        {
            MyPoint newP = _pointP;
            MyPoint newQ;

            if (stepPointP)
            {
                newP   = _pointP * _state;
                _state = newP.X;
            }



            newQ = _pointQ * _state;

            var qx = newQ.X;

            var phiQx = qx.AsByteArray.Value;

            phiQx = phiQx.Take(phiQx.Length - MyConstants.P256.CutBytes).ToArray();

            return(new MyBigInteger(phiQx));
        }
示例#10
0
 public MyPoint CreatePointAt(MyBigInteger x, MyBigInteger y)
 {
     IsPointOnCurve(x, y);
     return(new MyPoint(x, y, this));
 }
示例#11
0
 public MyCurve(MyBigInteger a, MyBigInteger b, MyBigInteger prime)
 {
     A     = a;
     _b    = b;
     Prime = prime;
 }
示例#12
0
        public List <MyBigInteger> ModSqrt(MyBigInteger modulus)
        {
            if (modulus is null || modulus == 0 || modulus._value.Sign == -1)
            {
                return(null);
            }

            var result = new List <MyBigInteger>();

            //If the indicators value is "modulus - 1" then there is no modular square root
            var indicator = ModPow((modulus - 1) / 2, modulus);

            if (indicator == modulus - 1)
            {
                return(result);
            }

            //Special case for faster solution
            if (modulus % 4 == 3)
            {
                var exponant    = (modulus + 1) / 4;
                var firstResult = ModPow(exponant, modulus);

                if (firstResult * firstResult % modulus == this)
                {
                    result.Add(firstResult);
                    result.Add(firstResult.Negate());
                }

                return(result);
            }

            var          q = modulus - 1;
            MyBigInteger s = 0;

            while (q.IsEven)
            {
                q = q / 2;
                s++;
            }

            MyBigInteger z = 1;

            while (z.ModPow((modulus - 1) / 2, modulus) != modulus - 1)
            {
                z++;
            }

            var m = s;
            var c = z.ModPow(q, modulus);
            var t = ModPow(q, modulus);
            var r = ModPow((q + 1) / 2, modulus);

            while (true)
            {
                if (t == 0)
                {
                    return(result);
                }
                if (t == 1)
                {
                    if (r * r % modulus != this)
                    {
                        return(result);
                    }

                    result.Add(r);
                    result.Add(r.Negate());

                    return(result);
                }

                MyBigInteger i = 1;
                while (t.ModPow(MyConstants.Two ^ i, modulus) != 1 && i < m - 1)
                {
                    i++;
                }

                var b = c.ModPow(MyConstants.Two.ModPow(m - i - 1, modulus), modulus);
                m = i;
                c = b.ModPow(MyConstants.Two, modulus);
                t = t * c % modulus;
                r = r * b % modulus;
            }
        }
示例#13
0
 public MyBigInteger ModPow(MyBigInteger exponant, MyBigInteger modulus)
 {
     return(exponant == -1 ?
            ModInverse(modulus) :
            new MyBigInteger(BigInteger.ModPow(_value, exponant, modulus)));
 }
示例#14
0
 public void Reseed(MyBigInteger seed)
 {
     _state = seed;
 }