def test_RandomPolynomialHex(): for x in range(5): fx = PyBigNumbers.GenerateRandHex(256) degree = randint(10, 15) modulo = PyBigNumbers.GenerateRandPrimeHex(100) hex_value = 0 # create a random polynomial listCoefficients = PyPolynomial.randomPolynomial( degree, modulo, hex_value) assert len(listCoefficients) == (degree + 1), "Test failed" # calling evaluate polynomial function polynomialFX = polynomialEvaluation(listCoefficients, fx, modulo, hex_value) # convert list of coefficients from string to decimal lst = [] for i in range(len(listCoefficients)): lst.append(int(listCoefficients[i], 16)) fx = int(fx, 16) modulo = int(modulo, 16) actualValue = polynomial_evaluate(lst, fx, modulo) assert polynomialFX.lstrip("0") == hex(actualValue).upper().lstrip( "0X"), "Test failed"
def test_InitFromListHex(): hex_value = 0 for x in range(10, 15): fx = PyBigNumbers.GenerateRandHex(256) modulo = PyBigNumbers.GenerateRandPrimeHex(100) listCoefficients = [] for i in range(x): # Generate random coefficients for the polynomial listCoefficients.append(PyBigNumbers.GenerateRandHex(256)) # create a Polynomial from a list of coefficients allCoeffeicient = PyPolynomial.initFromList(listCoefficients, hex_value) assert len(allCoeffeicient) == x, "Test failed" # Calling evaluate polynomial function polynomialFX = polynomialEvaluation(listCoefficients, fx, modulo, hex_value) # convert list of coefficients from string to decimal lst = [] for i in range(len(allCoeffeicient)): lst.append(int(allCoeffeicient[i], 16)) fx = int(fx, 16) modulo = int(modulo, 16) actualValue = polynomial_evaluate(lst, fx, modulo) assert polynomialFX.lstrip("0") == hex(actualValue).upper().lstrip( "0X"), "Test failed"
def test_RandomPolynomialDec(): for x in range(5): fx = PyBigNumbers.GenerateRandDec(256) degree = randint(10, 15) modulo = PyBigNumbers.GenerateRandPrimeDec(100) dec = 1 # create a random polynomial listCoefficients = PyPolynomial.randomPolynomial(degree, modulo, dec) assert len(listCoefficients) == (degree + 1), "Test failed" # calling evaluate polynomial function polynomialFX = polynomialEvaluation(listCoefficients, fx, modulo, dec) # convert list of coefficients from string to decimal lst = [] for i in range(len(listCoefficients)): lst.append(int(listCoefficients[i])) fx = int(fx) modulo = int(modulo) actualValue = polynomial_evaluate(lst, fx, modulo) assert polynomialFX == str(actualValue), "Test failed"
def test_GenerateRandPrimeHex(): #Generating prime decimal numbers with input parameter for x in range(10, 100, 10): # Generate Random Prime Number of arbitary precision in hex primeHex_Value = PyBigNumbers.GenerateRandPrimeHex(x) # Verifying the actual value as prime hex number or not assert PyBigNumbers.isPrimeHex(primeHex_Value), "Test failed"
def __init__(self, value=None, mod=None, isDec=False): self.mod = mod self.isDec = isDec if value is None: if (self.isDec): self.value = PyBigNumbers.GenerateRandDec(256) else: self.value = PyBigNumbers.GenerateRandHex(256) else: self.value = value
def test_GenerateRandPrimeDecWithSeed(): seed = "moKDVMpSuLxh3tS2baDvmM4XmfTugpctBX" #Generating prime decimal numbers with input parameter for x in range(10, 100, 10): # Generate Random Prime Number of arbitary precision in dec with seed (specified as a string) primeDec_Value = PyBigNumbers.GenerateRandPrimeDecWithSeed(seed, x) # Verifying the actual value as prime number or not assert PyBigNumbers.isPrimeDec(primeDec_Value), "Test failed"
def test_GenerateRandPrimeHexWithSeed(): seed = "mhxdmxVS4qJWMyB7jsH9qfpS4qm1KHfY42" #Generating prime decimal numbers with input parameter for x in range(10, 100, 10): # Generate Random Prime Number of arbitary precision in hex with seed (specified as a string) primeHex_Value = PyBigNumbers.GenerateRandPrimeHexWithSeed(seed, x) # Verifying the actual value as prime hex number or not assert PyBigNumbers.isPrimeHex(primeHex_Value), "Test failed"
def test_InitFromListModuloHex(): for x in range(10, 15): modHex = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141" fx = PyBigNumbers.GenerateRandHex(256) degree = randint(10, 15) hex_value = 0 # create a random polynomial listCoefficients = PyPolynomial.randomPolynomial( degree, modHex, hex_value) assert len(listCoefficients) == (degree + 1), "Test failed" # create a Polynomial from a list of coefficient with modulo coefficientsWithModulo = PyPolynomial.initFromListModulo( listCoefficients, modHex, hex_value) assert len(coefficientsWithModulo) == degree + 1, "Test failed" # calling evaluate polynomial function polynomialFX = polynomialEvaluation(coefficientsWithModulo, fx, modHex, hex_value) # convert list of coefficients from string to decimal lst = [] for i in range(len(coefficientsWithModulo)): lst.append(int(coefficientsWithModulo[i], 16)) fx = int(fx, 16) modulo = int(modHex, 16) actualValue = polynomial_evaluate(lst, fx, modulo) assert polynomialFX.lstrip("0") == hex(actualValue).upper().lstrip( "0X"), "Test failed"
def __eq__(self, obj): if (self.isDec == obj.isDec): if (PyBigNumbers.isEqual(self.value, obj.value, self.isDec)): if (self.mod is None and obj.mod is None): return True if ((self.mod is None and obj.mod is not None) or (self.mod is not None and obj.mod is None)): return False if (PyBigNumbers.isEqual(self.mod, obj.mod, self.isDec)): return True else: return False else: return False else: return False
def test_InitFromListDec(): for x in range(10, 15): fx = str(randint(10, 100000)) modulo = str(PyBigNumbers.GenerateRandPrimeDec(100)) listCoefficients = [] dec = 1 for i in range(x): # Generate random coefficients for the polynomial listCoefficients.append(str(randint(100000, 1000000))) # create a Polynomial from a list of coefficients allCoeffeicient = PyPolynomial.initFromList(listCoefficients, dec) assert len(allCoeffeicient) == x, "Test failed" # Calling evaluate polynomial function polynomialFX = polynomialEvaluation(listCoefficients, fx, modulo, dec) # convert list of coefficients from string to decimal lst = [] for i in range(len(allCoeffeicient)): lst.append(int(allCoeffeicient[i])) fx = int(fx) modulo = int(modulo) actualValue = polynomial_evaluate(lst, fx, modulo) assert polynomialFX == str(actualValue), "Test failed"
def inverse(self): if (self.isDec): if (self.mod is None): numerator = BigNumber('1', self.mod, self.isDec) inverseVal = numerator / self else: inverseVal = PyBigNumbers.Inv_mod_Dec(self.value, self.mod) retVal = BigNum(inverseVal, self.mod, self.isDec) return retVal else: if (self.mod is None): numerator = BigNumber('1', self.mod) inverseVal = numberator % self else: inverseVal = PyBigNumbers.Inv_mod_Hex(self.value, self.mod) retVal = BigNum(inverseVal, self.mod, self.isDec) return retVal
def __truediv__(self, obj): if (self.isDec): if (self.mod is None): divVal = PyBigNumbers.divideFromDec(self.value, obj.value) else: divVal = PyBigNumbers.Div_mod_Dec(self.value, obj.value, self.mod) retVal = BigNum(divVal, self.mod, self.isDec) else: if (self.mod is None): divVal = PyBigNumbers.divideFromHex(self.value, obj.value) else: divVal = PyBigNumbers.Div_mod_Hex(self.value, obj.value, self.mod) retVal = BigNum(divVal, self.mod, self.isDec) return retVal
def __sub__(self, obj): if (self.isDec): if (self.mod is None): diffVal = PyBigNumbers.subFromDec(self.value, obj.value) else: diffVal = PyBigNumbers.Sub_mod_Dec(self.value, obj.value, self.mod) retVal = diffVal(sumVal, self.mod, self.isDec) return retVal else: if (self.mod is None): diffVal = PyBigNumbers.subFromHex(self.value, obj.value) else: diffVal = PyBigNumbers.Sub_mod_Hex(self.value, obj.value, self.mod) retVal = BigNum(diffVal, self.mod, self.isDec) return retVal
def test_MultiplyScalarMNOnCurve(): # Generating Random EC Points for x in range(100): # Generate a Random EC Point with default NID ==> NID_secp256k1 ecPoint_value = PyECPoint.GenerateRandomEC(0, hex, True ) # Check if the point is on the curve with the supplied NID default NID ==> NID_secp256k1 assert PyECPoint.CheckOnCurve(ecPoint_value, 0, hex), "Test failed" #Generate a Random Big number M and N using BigNumberAPIs bigNumbM = PyBigNumbers.GenerateRandDec(257) bigNumbN = PyBigNumbers.GenerateRandDec(128) # EC Point Scalar multiply with supplied curve ID actual_value = PyECPoint.MultiplyScalarMN(ecPoint_value, bigNumbM, bigNumbN, nid_Id, hex, True) # Verifying the the length of actual value as 66 assert len(actual_value) == 66, "Test failed"
def __add__(self, obj): if (self.isDec): if (self.mod is None): sumVal = PyBigNumbers.addFromDec(self.value, obj.value) else: #check for self.mod == obj.mod sumVal = PyBigNumbers.Add_mod_Dec(self.value, obj.value, self.mod) retVal = BigNum(sumVal, self.mod, self.isDec) return retVal else: if (self.mod is None): sumVal = PyBigNumbers.addFromHex(self.value, obj.value) else: #check for self.mod == obj.mod sumVal = PyBigNumbers.Add_mod_Hex(self.value, obj.value, self.mod) retVal = BigNum(sumVal, self.mod, self.isDec) return retVal
def test_SubFromDecWithBigNumApi(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_SubDec", "r") as subDec_txt: for x in subDec_txt: decNumber = x.split() # Subtract too big numbers of arbitrary precision in dec actual_Value = PyBigNumbers.subFromDec(decNumber[0], decNumber[1]) #Verifying the actual value with expected value assert actual_Value == decNumber[2], "Test failed"
def test_SubFromHexWithBigNumApi(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_SubHex", "r") as subHex_txt: for x in subHex_txt: hexNumber = x.split() # Subtract too big numbers of arbitrary precision in hex actual_Value = PyBigNumbers.subFromHex(hexNumber[0], hexNumber[1]) #Verifying the actual value with expected value assert actual_Value.upper() == hexNumber[2].upper(), "Test failed"
def __mul__(self, obj): if (self.isDec): if (self.mod is None): prodVal = PyBigNumbers.multiplyFromDec(self.value, obj.value) else: #print ('%s * %s mod %s' % (self.value, obj.value, self.mod)) prodVal = PyBigNumbers.Mul_mod_Dec(self.value, obj.value, self.mod) retVal = BigNum(prodVal, self.mod, self.isDec) return retVal else: if (self.mod is None): prodVal = PyBigNumbers.multiplyFromHex(self.value, obj.value) else: #print ('%s * %s mod %s' % (self.value, obj.value, self.mod)) prodVal = PyBigNumbers.Mul_mod_Hex(self.value, obj.value, self.mod) retVal = BigNum(prodVal, self.mod, self.isDec) return retVal
def test_IsPrimeFastDec(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_PrimeDec", "r") as primeFastDec_txt: for x in primeFastDec_txt: decNumber = x.split(",") # Check if dec big number is a prime (Fasttest) actual_Value = PyBigNumbers.isPrimeFasttestDec(decNumber[0]) # Verifying the actual value with expected value assert actual_Value == int(decNumber[1]), "Test failed"
def test_MulModDec(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_MulModDec", "r") as mulModDec_txt: for x in mulModDec_txt: decNumber = x.split(",") # Multiply modulo of big numbers of arbitrary precision in dec actual_Value = PyBigNumbers.Mul_mod_Dec(decNumber[0], decNumber[1], decNumber[2]) # Verifying the actual value with expected value assert actual_Value == decNumber[3].rstrip("\n"), "Test failed"
def test_ModuloHex(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_ModuloHex", "r") as moduloHex_txt: for x in moduloHex_txt: decNumber = x.split(",") # Modulo of big numbers of arbitrary precision in hex actual_Value = PyBigNumbers.Mod_Hex(decNumber[0], decNumber[1]) expected_Value = decNumber[2].rstrip("\n") # Verifying the actual value with expected value assert actual_Value == expected_Value.upper(), "Test failed"
def test_DivideDec(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_DivideDec", "r") as divideDec_txt: for x in divideDec_txt: decNumber = x.split(",") # Divide two big numbers of arbitrary precision in dec actual_Value = PyBigNumbers.divideFromDec(decNumber[0], decNumber[1]) # Verifying the actual value with expected value assert actual_Value == decNumber[2].rstrip("\n"), "Test failed"
def test_LGECInterpolatorFull(): modulo = PyBigNumbers.GenerateRandPrimeHex(1000) xValue = PyBigNumbers.GenerateRandHex(1000) listTupleObj = [] dec = False # Generating Random EC for x in range(10, 50): # Generate a Random EC Point with default NID ==> NID_secp256k1 hexValue = Nakasendo.ECPoint() # Check if the point is on the curve with the supplied NID default NID ==> NID_secp256k1 assert hexValue.IsPointOnCurve(), "Test failed" x_Axis, y_axis = hexValue.GetAffineCoOrdinates() # EC Point GetAffineCoOrdinates_GFp with default NID => NID_secp256k1 listTupleObj.append((str(x), x_Axis, y_axis)) lgInterpolatorX = PyPolynomial.LGECInterpolatorFull( listTupleObj, modulo, xValue, dec) assert type(lgInterpolatorX) == str, "Test failed"
def test_genRandDecWithSeed(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_SeedDec", "r") as seedDec_txt: for x in seedDec_txt: decNumber = x.split() # Generate Random Number of arbitrary precision in Dec with seed (specified as a string) actual_Value = PyBigNumbers.GenerateRandDecWithSeed( decNumber[0], int(decNumber[1])) #Verifying the actual value as a string with no negative sign assert type( actual_Value) is str and actual_Value != "-1", "Test failed"
def test_GenRandHex(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_GenBigNum", "r") as genHex_txt: for x in genHex_txt.readlines(): decNumber = int(x) # Generate Random Number of arbitrary precision in dec actual_Value = PyBigNumbers.GenerateRandHex(decNumber) #Verifying the actual value as a string and not negative value assert type( actual_Value) is str and actual_Value != "-1", "Test failed"
def test_InvModDec(test_data_dir): #Reading test data from the file with open(test_data_dir / "testData_InvModDec", "r") as invModDec_txt: for x in invModDec_txt: decNumber = x.split(",") #Inverse modulo of big numbers of arbitrary precision in dec actual_value = PyBigNumbers.Inv_mod_Dec(decNumber[0], decNumber[1]) #verifying the actual value with the expected value assert actual_value == decNumber[2].rstrip("\n"), "Test failed"
def test_LGInterpolatorFullHex(): listTupleObj = [(2, "A"), (3, "F")] modulo = "0" hex_value = 0 for x in range(100, 110): randomX = PyBigNumbers.GenerateRandHex(100000) xValue = str(randomX) # LGInterpolator, full evaluation of the polynomial at xValue lgInterpolatorX = PyPolynomial.LGInterpolatorFull( listTupleObj, modulo, xValue, hex_value) assert lgInterpolatorX.lstrip("0") == hex( int(randomX, 16) * 5).lstrip("0x").upper(), "Test failed"
def test_LGInterpolatorSingleHex(): listTupleObj = [(1, "13"), (2, "4"), (3, "2"), (4, "5"), (5, "11"), (6, "1")] modulo = PyBigNumbers.GenerateRandPrimeHex(100) hex_value = 0 xValue = str(randint(10, 100000)) for x in range(1, 6): xPoint = str(x) # LGInterpolator, evaluate the ith basis polynomial at xValue lgInterpolatorX = PyPolynomial.LGInterpolatorSingle( listTupleObj, modulo, xValue, xPoint, hex_value) assert type(lgInterpolatorX) == str, "Test failed"
def test_RightShiftDec(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_LeftRightShiftDec", "r") as rightDec_txt: for x in rightDec_txt: decNumber = x.split(",") # rightshift bitwise operation that moves bits of right big number to the right by left big number value in dec actual_Value = PyBigNumbers.rightShiftFromDec( decNumber[2].rstrip("\n"), decNumber[1]) # Verifying the actual value with expected value assert actual_Value == decNumber[0], "Test failed"
def test_MulModHex(test_data_dir): # Reading test data from the file with open(test_data_dir / "testData_MulModHex", "r") as mulModHex_txt: for x in mulModHex_txt: hexNumber = x.split(",") # Multiply modulo of big numbers of arbitrary precision in hex actual_Value = PyBigNumbers.Mul_mod_Hex(hexNumber[0], hexNumber[1], hexNumber[2]) expected_Value = hexNumber[3].rstrip("\n") # Verifying the actual value with expected value assert actual_Value.lstrip( "0") == expected_Value.upper(), "Test failed"