예제 #1
0
파일: myFloat.cs 프로젝트: xgalv/Cryptool2
 public static void to_Float(ref MyFloat res, ref BigInteger x)
 {
     res.x = x;
     //res.e = 1024;
     res.e = prec;
     res.normalize();
 }
예제 #2
0
파일: myFloat.cs 프로젝트: xgalv/Cryptool2
        public void sub(ref MyFloat res, ref MyFloat op1, ref MyFloat op2)
        {
            int d = op1.e - op2.e;

            if (d >= 0)
            {
                res.x = op1.x.Subtract((op2.x.ShiftRight(d)));
                res.e = op1.e;
            }
            else
            {
                res.x = (op1.x.ShiftRight(-d)).Subtract(op1.x);
                res.e = op2.e;
            }
            res.normalize();
        }
예제 #3
0
파일: myFloat.cs 프로젝트: xgalv/Cryptool2
        public static void add(ref MyFloat res, ref MyFloat op1, ref MyFloat op2)
        {
            int d = op1.e - op2.e;

            if (d >= 0)
            {
                res.x = op1.x.Add((op2.x.ShiftRight(d)));
                res.e = op1.e;
            }
            else
            {
                res.x = (op1.x.ShiftRight(-d)).Add(op2.x);
                res.e = op2.e;
            }
            res.normalize();
        }
예제 #4
0
파일: myFloat.cs 프로젝트: xgalv/Cryptool2
        public static BigInteger to_ZZ(ref MyFloat x)
        {
            BigInteger res = x.x;

            if (x.e <= prec)
            {
                //res <<= prec + x.e;
                res = res.ShiftRight(prec - x.e);
            }
            else
            {
                //res >>= prec - x.e;
                res = res.ShiftLeft(prec - x.e);
            }

            return(res);
        }
예제 #5
0
파일: myFloat.cs 프로젝트: xgalv/Cryptool2
 public static void div(ref MyFloat res, ref MyFloat op1, ref MyFloat op2)
 {
     res.x = (op1.x.ShiftLeft(prec)).Divide(op2.x);
     res.e = op1.e - op2.e;
     res.normalize();
 }
예제 #6
0
파일: myFloat.cs 프로젝트: xgalv/Cryptool2
 public static void mul(ref MyFloat res, ref MyFloat op1, ref MyFloat op2)
 {
     res.x = op1.x.Multiply(op2.x);
     res.e = op1.e + op2.e - prec;
     res.normalize();
 }
예제 #7
0
파일: myFloat.cs 프로젝트: xgalv/Cryptool2
 public MyFloat(MyFloat z)
 {
     x = z.x;
     e = z.e;
 }
예제 #8
0
        static public BigInteger cuberoot4(BigInteger BigIntRad, int prec)
        {
            //BigInteger x;                    // ZZ: Langzahl-Integer
            MyFloat a     = new MyFloat();
            MyFloat xi    = new MyFloat();
            MyFloat x3    = new MyFloat();
            MyFloat two   = new MyFloat(); // RR: Gleitkommazahlen beliebiger Präzision
            MyFloat three = new MyFloat(); // RR: Gleitkommazahlen beliebiger Präzision

            MyFloat.setPrec(prec);

            //x = BigIntRad;

            BigInteger BigInt2 = BigInteger.Two;

            MyFloat.to_Float(ref two, ref BigInt2);

            BigInteger BigInt3 = BigInteger.Three;

            MyFloat.to_Float(ref three, ref BigInt3);

            // 1. Startwert für die Approximation berechnen (mit double)
            //appr_cr_x = exp( 1.0/3.0 * log(x) );


            // 2. Startwert (xi) und Ausgangswert (a=x) in Gleitkommazahl mit hoher Präzision überführen
            //a  = to_RR(x);
            MyFloat.to_Float(ref a, ref BigIntRad);

            MyFloat    tmp  = new MyFloat();
            BigInteger tmp2 = BigInteger.ValueOf(BigIntRad.BitLength);

            MyFloat.to_Float(ref tmp, ref tmp2);
            //xi = to_RR(appr_cr_x);
            //xi = new MyFloat(appr_cr_x);
            //MyFloat.div(ref xi, ref a,ref tmp);
            BigInteger start = BigIntRad.ShiftRight(BigIntRad.BitLength * 2 / 3);

            MyFloat.to_Float(ref xi, ref start);


            // 3. Halley's konvergierende Folge (x[i+1] = xi*(xi^3 + 2*a)/(2*xi^3 + a) --> x^(1/3)) mit 200 Iterationen -- *nicht optimiert*
            //two = to_RR(2.0);
            //two = new MyFloat(2.0);

            for (int i = 0; i < 200; i++)
            {
                //x3 = xi*xi*xi;
                MyFloat.mul(ref x3, ref xi, ref xi);
                MyFloat.mul(ref x3, ref x3, ref xi);
                //xi = (xi*(x3 + two * a)) / ( two * x3 + a );

                //xi = xi*( (x3 + two * a) / ( two * x3 +a ) );
                MyFloat twoA = new MyFloat();
                MyFloat.mul(ref twoA, ref two, ref a);

                MyFloat left = new MyFloat();
                MyFloat.add(ref left, ref x3, ref twoA);


                MyFloat twoX3 = new MyFloat();
                MyFloat.mul(ref twoX3, ref two, ref x3);

                MyFloat right = new MyFloat();
                MyFloat.add(ref right, ref twoX3, ref a);

                MyFloat division = new MyFloat();
                MyFloat.div(ref division, ref left, ref right);

                MyFloat.mul(ref xi, ref xi, ref division);
            }

            return(MyFloat.to_ZZ(ref xi));
        }