from __future__ import print_function
import unittest
from binascii import hexlify, unhexlify
from pure25519.ed25519_oop import SigningKey, VerifyingKey

class KnownAnswerTests(unittest.TestCase):
    def test_all(self):
        # kat-ed25519.txt comes from "sign.input" on ed25519.cr.yp.to . The
        # pure-python ed25519.py in the same distribution uses a very
        # different key format than the one used by NaCl.
        lines = list(open("pure25519/kat-ed25519.txt"))
        for i,line in enumerate(lines):
            if not i%50: print("%d/%d" % (i, len(lines)))
            x = line.split(":")
            A,B,C,D = [unhexlify(i) for i in x[:4]]
            # A[:32] is the 32 byte seed (the entropy input to H())
            # A[32:] == B == the public point (pubkey)
            # C is the message
            # D is 64 bytes of signature (R+S) prepended to the message

            seed = A[:32]
            vk_s = B
            # the NaCl signature is R+S, which happens to be the same as ours
            msg = C
            sig = D[:64]
            # note that R depends only upon the second half of H(seed). S
            # depends upon both the first half (the exponent) and the second
            # half

            #if len(msg) % 16 == 1:
            #    print "msg len = %d" % len(msg), time.time()

            sk = SigningKey(seed)
            vk = sk.get_verifying_key()
            self.failUnlessEqual(vk.to_bytes(), vk_s)
            vk2 = VerifyingKey(vk_s)
            self.failUnlessEqual(vk2, vk) # objects should compare equal
            self.failUnlessEqual(vk2.to_bytes(), vk_s)
            newsig = sk.sign(msg)
            sig_R,sig_S = sig[:32],sig[32:]
            newsig_R,newsig_S = newsig[:32],newsig[32:]
            self.failUnlessEqual(hexlify(newsig), hexlify(sig)) # deterministic sigs
            self.failUnlessEqual(vk.verify(sig, msg), None) # no exception


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

@@ -1052,16 +1052,22 @@
 = D%5B:64%5D
+ # R+S
 %0A       
@@ -1659,108 +1659,14 @@
 msg)
-%0A            sig_R,sig_S = sig%5B:32%5D,sig%5B32:%5D%0A            newsig_R,newsig_S = newsig%5B:32%5D,newsig%5B32:%5D
+ # R+S
 %0A   

hush pyflakes