/// <summary> /// M. /// Computes x to the power of y at mod z. /// </summary> /// <returns></returns> public static BigNum Power(BigNum x, BigNum y, BigNum z) { BigNum result = new BigNum(); if (y.CompNumWithoutPrint(GlobalVariables.zero)) { result = new BigNum(GlobalVariables.one); } else { if (y.IsEven()) { // x^y = [x^(y/2)]^2 mod z. BigNum yDividedBy2 = new BigNum(); DivNum(y, GlobalVariables.two, yDividedBy2, result); // y = y/2. result = Power(x, yDividedBy2, z); result.MultNum(new BigNum(result)); DivNum(result, z, new BigNum(), result); } else { BigNum yMinus1DividedBy2 = new BigNum(y); yMinus1DividedBy2.SubNum(GlobalVariables.one); // y = y-1. DivNum(y, GlobalVariables.two, yMinus1DividedBy2, result); // y = y-1/2. result = Power(x, yMinus1DividedBy2, z); result.MultNum(new BigNum(result)); DivNum(result, z, new BigNum(), result); result.MultNum(x); DivNum(result, z, new BigNum(), result); } } return(result); }
/// <summary> /// Gets a number m. /// Finds p >= 0 and k (k is odd), where m = 2^p * k. /// Return p and k by ref. /// /// p = 0; /// if (!IsEven(m)) /// { /// k = m; /// } /// else // m is even. /// { /// while(IsEven(m)) /// { /// m /= 2; /// p++; /// } /// k = m; /// } /// </summary> /// <param name=""></param> /// <param name=""></param> public static void FindExpressionOfGivenNumberByPowerOfTwoTimesOddNumber(BigNum m, BigNum p, BigNum k) { if (!m.IsEven()) { k.Set(m); } else { while (m.IsEven()) { DivNum(m, GlobalVariables.two, m, new BigNum()); p.AddNum(GlobalVariables.one); } k.Set(m); } }