import unittest
from ecc_impl import ECC
from collections import namedtuple

class ECCTestCase(unittest.TestCase):

	def setUp(self):
		self.Expected = namedtuple('Expected', ['x', 'y'])

	def test_p_192(self):
		""" Testing curve P-192 """

		test_data = {
			1: self.Expected(
				x = 0x188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012,
				y = 0x07192B95FFC8DA78631011ED6B24CDD573F977A11E794811),
			2: self.Expected(
				x = 0xDAFEBF5828783F2AD35534631588A3F629A70FB16982A888,
				y = 0xDD6BDA0D993DA0FA46B27BBC141B868F59331AFA5C7E93AB),
			3: self.Expected(
				x = 0x76E32A2557599E6EDCD283201FB2B9AADFD0D359CBB263DA,
				y = 0x782C37E372BA4520AA62E0FED121D49EF3B543660CFD05FD),
			4: self.Expected(
				x = 0x35433907297CC378B0015703374729D7A4FE46647084E4BA,
				y = 0xA2649984F2135C301EA3ACB0776CD4F125389B311DB3BE32),
			5: self.Expected(
				x = 0x10BB8E9840049B183E078D9C300E1605590118EBDD7FF590,
				y = 0x31361008476F917BADC9F836E62762BE312B72543CCEAEA1),
			6277101735386680763835789423176059013767194773182842284072:
				self.Expected(
					x = 0x818A4D308B1CABB74E9E8F2BA8D27C9E1D9D375AB980388F, 
					y = 0xFE2E55A1DF72783283D6D08344BA831FCF15ABDE893718C6)
		}

		curve = ECC(
	    	-3, # a
			0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1, # b
			6277101735386680763835789423207666416083908700390324961279, # p
			(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012, # Gx
			0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811)) # Gy

		for k, expected in test_data.items():
			solution_x, solution_y = curve.base_point_mult(k)

			self.assertTrue(solution_x == expected.x)
			self.assertTrue(solution_y == expected.y)

	def test_p_224(self):
		""" Testing curve P-224 """

		test_data = {
			1: self.Expected(
				x = 0xB70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21,
				y = 0xBD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34),
			2: self.Expected(
				x = 0x706A46DC76DCB76798E60E6D89474788D16DC18032D268FD1A704FA6,
				y = 0x1C2B76A7BC25E7702A704FA986892849FCA629487ACF3709D2E4E8BB),
			3: self.Expected(
				x = 0xDF1B1D66A551D0D31EFF822558B9D2CC75C2180279FE0D08FD896D04,
				y = 0xA3F7F03CADD0BE444C0AA56830130DDF77D317344E1AF3591981A925),
			4: self.Expected(
				x = 0xAE99FEEBB5D26945B54892092A8AEE02912930FA41CD114E40447301,
				y = 0x0482580A0EC5BC47E88BC8C378632CD196CB3FA058A7114EB03054C9),
			5: self.Expected(
				x = 0x31C49AE75BCE7807CDFF22055D94EE9021FEDBB5AB51C57526F011AA,
				y = 0x27E8BFF1745635EC5BA0C9F1C2EDE15414C6507D29FFE37E790A079B)
		}

		curve = ECC(
	    	-3, # a
			0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4, # b
			26959946667150639794667015087019630673557916260026308143510066298881, # p
			(0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21, # Gx
			0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34)) # Gy

		for k, expected in test_data.items():
			solution_x, solution_y = curve.base_point_mult(k)

			self.assertTrue(solution_x == expected.x)
			self.assertTrue(solution_y == expected.y)

	def test_p_256(self):
		""" Testing curve P-256 """

		test_data = {
			1: self.Expected(
				x = 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296,
				y = 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5),
			2: self.Expected(
				x = 0x7CF27B188D034F7E8A52380304B51AC3C08969E277F21B35A60B48FC47669978,
				y = 0x07775510DB8ED040293D9AC69F7430DBBA7DADE63CE982299E04B79D227873D1),
			3: self.Expected(
				x = 0x5ECBE4D1A6330A44C8F7EF951D4BF165E6C6B721EFADA985FB41661BC6E7FD6C,
				y = 0x8734640C4998FF7E374B06CE1A64A2ECD82AB036384FB83D9A79B127A27D5032),
			4: self.Expected(
				x = 0xE2534A3532D08FBBA02DDE659EE62BD0031FE2DB785596EF509302446B030852,
				y = 0xE0F1575A4C633CC719DFEE5FDA862D764EFC96C3F30EE0055C42C23F184ED8C6),
			5: self.Expected(
				x = 0x51590B7A515140D2D784C85608668FDFEF8C82FD1F5BE52421554A0DC3D033ED,
				y = 0xE0C17DA8904A727D8AE1BF36BF8A79260D012F00D4D80888D1D0BB44FDA16DA4)
		}

		curve = ECC(
	    	-3, # a
			0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b, # b
			115792089210356248762697446949407573530086143415290314195533631308867097853951, # p
			(0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296, # Gx
			0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5)) # Gy

		for k, expected in test_data.items():
			solution_x, solution_y = curve.base_point_mult(k)

			self.assertTrue(solution_x == expected.x)
			self.assertTrue(solution_y == expected.y)

if __name__ == "__main__":
	unittest.main()
