def test_GetGroupOrderFromHex(test_data_dir): # 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" # EC Point Generator with the supplied curve ID generator_Point = PyECPoint.GetGenerator(ecPoint_value, nid_Id, hex, True) # Reading a Random generated EC Point with default NID ==> NID_secp256k1 from file with open(test_data_dir/"testData_GetGroupDegree", "r") as getGrpDegree_txt: #Test data are generated from https://svn.python.org/projects/external/openssl-0.9.8a/crypto/ec/ec_curve.c for x in getGrpDegree_txt: # Reading the line of the file as string and splitting into list nidID_Degree_Value = x.split(",") #EC Point Group Degree with supplied curve grpDegreeHex = PyECPoint.GetGroupDegree(generator_Point, int(nidID_Degree_Value[0]), hex) # Verifying the actual value with the expected value. assert grpDegreeHex == int(nidID_Degree_Value[1]), "Test failed" #EC Point Group Order with supplied curve actual_value = PyECPoint.GetGroupOrder(generator_Point, int(nidID_Degree_Value[0]), hex) assert actual_value == nidID_Degree_Value[2].rstrip("\n"), "Test failed"
def test_CheckInfinityFromHexCurve(): # 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" # Check if the given point is at infinity on the given curve ID actual_value = PyECPoint.GenerateEC(ecPoint_value, 0, hex, True ) assert PyECPoint.CheckInfinity(actual_value, nid_Id, hex) is False, "Test failed"
def test_GenerateECHex(): # Generating Random EC Points ranging for x in range(100): # Generate a Random EC Point with default NID ==> NID_secp256k1 value_ECHex = PyECPoint.GenerateRandomEC(0, hex, True ) #Generate a EC Point from hex string with default NID ==> NID_secp256k1 actual_value = PyECPoint.GenerateEC(value_ECHex, 0, hex, True ) # Verifying the the length of actual value as 66 assert len(actual_value) == 66 and actual_value == value_ECHex, "Test failed"
def test_GenerateECHexFromHexOnCurve(): # Generating Random EC Points for x in range(100): # Generate a Random EC Point with default NID ==> NID_secp256k1 value_ECHex = PyECPoint.GenerateRandomEC(0, hex, True ) #Generate a EC Point from hex string with supplied NID actual_value = PyECPoint.GenerateEC(value_ECHex, nid_Id, hex, True) # Verifying the the length of actual value as 66 assert len(actual_value) == 66, "Test failed"
def test_CompareECPoint(): # 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 ) ecPoint_value2 = 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" assert PyECPoint.CheckOnCurve(ecPoint_value2, 0, hex ), "Test failed" #Compare two given ECPoints assert PyECPoint.Compare(ecPoint_value, ecPoint_value2, 0, hex) is False, "Test failed"
def test_GetAffineCoOrdinatesOnCurve(): # Generating Random EC Points for x in range(100): # Generate a Random EC Point with default NID ==> NID_secp256k1 hexValue = PyECPoint.GenerateRandomEC( 0, hex, True ) # Check if the point is on the curve with the supplied NID default NID ==> NID_secp256k1 assert PyECPoint.CheckOnCurve(hexValue, 0, hex ), "Test failed" # EC Point GetAffineCoOrdinates_GFp with supplied curve x_axis, y_axis = PyECPoint.GetAffineCoOrdinates(hexValue, nid_Id, hex ) assert len(x_axis) == 62 or len(x_axis) == 64, "Test failed"
def test_CheckOnCurve(): # Generating Random EC Points for x in range(100): # Generate a Random EC Point with default NID ==> NID_secp256k1 value_ECHex = PyECPoint.GenerateRandomEC(0, hex, True ) # Generate a EC Point from hex string with supplied NID hexOnCurve_value = PyECPoint.GenerateEC(value_ECHex, nid_Id, hex, True) #Check if the point is on the curve with the supplied NID default NID ==> NID_secp256k1 actual_value = PyECPoint.CheckOnCurve(hexOnCurve_value, 0, hex ) # Verifying the actual value as True or False assert actual_value, "Test failed"
def test_InvertFromHex(): # 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" #Invert the ECPoint in hex with the default NID ==> NID_secp256k1 actual_value = PyECPoint.Invert(ecPoint_value, 0, hex, True) # Verifying the the length of actual value as 66 assert len(actual_value) == 66, "Test failed"
def test_AddECFromHexWithCurveID(test_data_dir): #Reading a Random generated EC Point with default NID ==> NID_secp256k1 from file with open(test_data_dir/"testData_AddECFromHex", "r") as addEChex_txt: for x in addEChex_txt: hexNumber = x.split(",") # Check if the point is on the curve with the supplied NID default NID ==> NID_secp256k1 assert PyECPoint.CheckOnCurve(hexNumber[0], 0, hex ), "Test failed" # Add two ECPoints in hex with the supplied curve IDs actual_value = PyECPoint.Add(hexNumber[0], hexNumber[1], nid_Id, hex, True) # Verifying the actual value with expected value assert actual_value == hexNumber[2].rstrip("\n") and len(actual_value) == 66, "Test failed"
def test_GetGenerator(): # 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" #EC Point Generator with the supplied curve ID actual_value = PyECPoint.GetGenerator(ecPoint_value, nid_Id, hex, True) # Verifying the the length of actual value as 66 assert len(actual_value) == 66, "Test failed"
def MultiplyByGenerator(m, isDec=False, nid=ECPoint.defaultNID, compressed=False): pt = ECPoint(nid=nid, isDec=isDec) pt.value = PyECPoint.MultiplyByGenerator(m.value, nid, isDec, compressed) return pt
def multipltScalarEx(self, objm, objn): mulVal = PyECPoint.MultiplyScalarMN(self.value, objm.value, objn.value, self.nid, self.isDec, self.isCompressed) ecpRetVal = ECPoint(self.nid) ecpRetVal.SetValue(mulVal) return ecpRetVal
def test_MultiplyScalarMOnCurve(): # Generating Random EC Points for x in range(100): # Generate a Random EC Point with default NID ==> NID_secp256k1 x = PyECPoint.GenerateRandomEC(0, hex, True ) y = PyECPoint.GenerateRandomEC(0, hex, True ) # Check if the point is on the curve with the supplied NID default NID ==> NID_secp256k1 assert PyECPoint.CheckOnCurve(x, 0, hex) and PyECPoint.CheckOnCurve(y, 0, hex ) , "Test failed" # EC Point Scalar multiply on curve with supplied ID actual_value = PyECPoint.MultiplyScalarM(x, y, nid_Id, hex, True ) # Verifying the actual value with expected value assert len(actual_value) == 66, "Test failed"
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 test_GenerateRandomECHex(): # Generating Random EC Points for x in range(100): # Generate a Random EC Point with default NID ==> NID_secp256k1 actual_value = PyECPoint.GenerateRandomEC( 0, hex, True ) # Verifying the the length of actual value as 66 assert len(actual_value) == 66, "Test failed"
def test_GetCurveList(): #Get all the list of curves listOfCurve = PyECPoint.GetCurveList() #converting string to dictionary actual_value = ast.literal_eval(listOfCurve) #Verifying one of the keys of the dictionary with it's value assert actual_value[nid_Id] == "secp256k1", "Test failed"
def test_GenerateRandomECHexOnCurve(): # Generating Random EC Points for x in range(100): #Generate a Random EC Point with supplied NID actual_value = PyECPoint.GenerateRandomEC(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.nid != obj.nid): print("Points not on the same curve %i and %i" % (self.nid, obj.nid)) return None sumVal = PyECPoint.Add(self.value, obj.value, self.nid, self.isDec, self.isCompressed) ecpRetVal = ECPoint(self.nid, self.isDec) ecpRetVal.SetValue(sumVal) return ecpRetVal
def __init__(self, nid=0, isDec=False, isCompressed=True): self.isDec = isDec self.isCompressed = isCompressed if (nid == 0): self.nid = self.defaultNID else: self.nid = nid self.isDec = isDec self.value = PyECPoint.GenerateRandomEC(self.nid, self.isDec, self.isCompressed) # self-check for valid value if isDec: if not ECPoint.isValidDec(self.value): raise Exception( 'ECPoint value is not a valid decimal string. value: {}'. format(self.value)) else: if not ECPoint.isValidHex(self.value): raise Exception( 'ECPoint value is not a valid hexadecimal string. value: {}' .format(self.value))
def GetDegree(self): deg = PyECPoint.GetGroupDegree(self.value, self.nid, self.isDec) return deg
def GetOrder(self): order = PyECPoint.GetGroupOrder(self.value, self.nid, self.isDec) return order
def GetGeneratorPoint(self): genPoint = ECPoint(self.nid, self.isDec) genPoint.value = PyECPoint.GetGenerator(self.value, self.nid, self.isDec, self.isCompressed) return genPoint
def IsPointOnCurve(self): if (PyECPoint.CheckOnCurve(self.value, self.nid, self.isDec)): return True return False
def GetAffineCoOrdinates(self): points = PyECPoint.GetAffineCoOrdinates(self.value, self.nid, self.isDec) return points
def multipleScalar(self, objm): mulVal = PyECPoint.MultiplyScalarM(self.value, objm.value, self.nid, self.isDec, self.isCompressed) ecpRetVal = ECPoint(self.nid, self.isDec) ecpRetVal.SetValue(mulVal) return ecpRetVal
def __eq__(self, obj): if (self.nid == obj.nid): return (PyECPoint.Compare(self.value, obj.value, self.nid, self.isDec)) else: return False