print()
print '= ELLIPTIC CURVE ARITHMETIC OPERATIONS TESTS FOR BLS383'
print()

-- test values found in milagro's vector files
ecp1 = ECP.new(
   BIG.new(hex '01A8C7F0F24078E5F6073ED2B85FEEA9869B8B07008D44FD60316F9CD04E0C87A914D8BE9EFE54C5967E2F38E9E209F7'),
   BIG.new(hex '41D498085DC2BF0F6AAD8DF8B35A83A98644155365783CD9409AAF58FC69C1F797D04585A44574104DEB8F5C32594443'))
ecp2 = ECP.new(
   BIG.new(hex '42130BFAA05DE8C771237F0BC719D88385EF118D7920A70F89CBE4FD0517C2C96CB4237B968169BF05F74D18CA3DE38F'),
   BIG.new(hex '37657C984C38F160507F3E0887D070CF898D5EA3819020F2E67185A99F7B1EEE78D3B6D8AB0160E62D337D4AF752DC91'))

print("ECP1 =", ecp1)
print("ECP1 octet: ", ecp1:octet():base64());
print("ECP2 =", ecp2)
print("ECP2 octet: ", ecp2:octet():base64());

print "test addition's commutativity"
assert(ecp1 + ecp2 == ecp2 + ecp1)
print "OK"

print "test addition's associativity"
ecpsum = ECP.new(
   BIG.new(hex '122F9392D4D14D075134114DB62F4AC1E17573BB148156FAFC4260D8F79486E7A30F7567E03B4CD376486EF1055C5AD3'),
   BIG.new(hex '22894D1BBF4FD60278A88B1C00723F620C3C10FD7EA4146195F182FA7288342C7345535110603F693DEB5585D7A75759'))
ecpaux1 = ecp1 + ecp2 + ecpsum
ecpaux2 = ecpsum + ecp2 + ecp1
assert(ecpaux1 == ecpaux2)
print "OK"

print "test negative"
ecpneg = ECP.new(
   BIG.new(hex '01A8C7F0F24078E5F6073ED2B85FEEA9869B8B07008D44FD60316F9CD04E0C87A914D8BE9EFE54C5967E2F38E9E209F7'),
   BIG.new(hex '1390BE8D06E8AFA635C01FCB6C900EDB1A6930D98DED68386B9708A86CF762FCE2BBF9A3E61F111B8C4B8E0853516C68'))
assert(ecp1:negative() == ecpneg)
print "OK"

print "test subtraction"
ecpsub = ECP.new(
   BIG.new(hex '47E2C612FCD97456D1D434FB368FE4349FB53C71F0399B141CD28F795284F035F337879B17AEEB7D421BBDA6BF70A680'),
   BIG.new(hex '34DF4AD6D15F5197CF1B4DBFC295BE523A51626A372CD7127C207F3CA72BD21103B2D10206F055D7C6E2524F25A297D1'))
assert(ecp1 - ecp2 == ecpsub)
print "OK"

print "test double"
ecpdbl = ECP.new(
   BIG.new(hex '1CAB42D473217683DBB7C12480F1A12125E773D42949A8138B049B0304AA6EF3367F798B0029CCEFF9C6AF0AC9ED45E7'),
   BIG.new(hex '4B84C5A35E06B6291540188DE34EAC00084FF5C4DC2A9414264E3DC466E713A46011DEF8CA258D55E39C39AB4B1F7ABE'))
assert(ecp1:double() == ecpdbl)
print "OK"

print "test multiplication"
ecpmul = ECP.new(
   BIG.new(hex '0AD63D1A76AD74C74F3EBD655C092FD11F50FD6721EEE6CCB17D72FF0D382F2E4B040870F3AA3D937C0CAEA46135B071'),
   BIG.new(hex '46ECBEAF458606C9EF0C059EA3A1558C07B5683886BB506CC5C878D89F987727402D4FAB1690EFBDB2A09EF376B70A97'))
bigscalar = BIG.new(hex '2EFEC3970686F700EEF74206C0823C195C05F77A8894987DCBC00C71D44EC062E8C471F50ECBB7AEBBFFCD185D0A82F9')
assert(ecp1 * bigscalar == ecpmul)
print "OK"

print "more misc tests on operations"
print "test double and addition"
assert( ecp1:double() == ecp1 + ecp1)
print "test double and multiplication"
assert( ecp1:double() == ecp1 * BIG.new(2))
print "test triplication" 
assert( ecp1 * BIG.new(3) == ecp1 + ecp1 + ecp1 )
print "test addition fail"
assert( ecp1 + ecp1 + ecp1 ~= ecp1 + ecp1)
print "test negative and infinite"
assert( (ecp1:negative() + ecp1):isinf() )
print "OK"

print "test infinity"
O = ECP.infinity()
assert( ecp1 + O == ecp1)
assert( ecp1:negative() + ecp1 == O)
print "OK"
