<commit_before>
# single-file pure-python ed25519 digital signatures, rearranged to minimize
# the namespace pollution so this can be embedded in another file. Adapted
# from https://bitbucket.org/dholth/ed25519ll


# Ed25519 digital signatures
# Based on http://ed25519.cr.yp.to/python/ed25519.py
# See also http://ed25519.cr.yp.to/software.html
# Adapted by <NAME>
# Sped up considerably using coordinate transforms found on:
# http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
# Specifically add-2008-hwcd-4 and dbl-2008-hwcd

export = {}

def Ed25519():
    # don't add many names to the file we're copied into

    try: # pragma nocover
        unicode
        PY3 = False
        def asbytes(b):
            """Convert array of integers to byte string"""
            return ''.join(chr(x) for x in b)
        def joinbytes(b):
            """Convert array of bytes to byte string"""
            return ''.join(b)
        def bit(h, i):
            """Return i'th bit of bytestring h"""
            return (ord(h[i//8]) >> (i%8)) & 1

    except NameError: # pragma nocover
        PY3 = True
        asbytes = bytes
        joinbytes = bytes
        def bit(h, i):
            return (h[i//8] >> (i%8)) & 1

    import hashlib

    b = 256
    q = 2**255 - 19
    l = 2**252 + 27742317777372353535851937790883648493

    def H(m):
        return hashlib.sha512(m).digest()

    # Can probably get some extra speedup here by replacing this with
    # an extended-euclidean, but performance seems OK without that
    def inv(x):
        return pow(x, q-2, q)

    d = -121665 * inv(121666)
    I = pow(2,(q-1)//4,q)

    def xrecover(y):
        xx = (y*y-1) * inv(d*y*y+1)
        x = pow(xx,(q+3)//8,q)
        if (x*x - xx) % q != 0: x = (x*I) % q
        if x % 2 != 0: x = q-x
        return x
    export["xrecover"] = xrecover

    By = 4 * inv(5)
    Bx = xrecover(By)
    export["Bx"] = Bx
    B = [Bx % q,By % q]
    export["B"] = B

    #def edwards(P,Q):
    #    x1 = P[0]
    #    y1 = P[1]
    #    x2 = Q[0]
    #    y2 = Q[1]
    #    x3 = (x1*y2+x2*y1) * inv(1+d*x1*x2*y1*y2)
    #    y3 = (y1*y2+x1*x2) * inv(1-d*x1*x2*y1*y2)
    #    return (x3 % q,y3 % q)

    #def scalarmult(P,e):
    #    if e == 0: return [0,1]
    #    Q = scalarmult(P,e/2)
    #    Q = edwards(Q,Q)
    #    if e & 1: Q = edwards(Q,P)
    #    return Q

    # Faster (!) version based on:
    # http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html

    def xpt_add(pt1, pt2):
        (X1, Y1, Z1, T1) = pt1
        (X2, Y2, Z2, T2) = pt2
        A = ((Y1-X1)*(Y2+X2)) % q
        B = ((Y1+X1)*(Y2-X2)) % q
        C = (Z1*2*T2) % q
        D = (T1*2*Z2) % q
        E = (D+C) % q
        F = (B-A) % q
        G = (B+A) % q
        H = (D-C) % q
        X3 = (E*F) % q
        Y3 = (G*H) % q
        Z3 = (F*G) % q
        T3 = (E*H) % q
        return (X3, Y3, Z3, T3)
    export["xpt_add"] = xpt_add

    def xpt_double (pt):
        (X1, Y1, Z1, _) = pt
        A = (X1*X1)
        B = (Y1*Y1)
        C = (2*Z1*Z1)
        D = (-A) % q
        J = (X1+Y1) % q
        E = (J*J-A-B) % q
        G = (D+B) % q
        F = (G-C) % q
        H = (D-B) % q
        X3 = (E*F) % q
        Y3 = (G*H) % q
        Z3 = (F*G) % q
        T3 = (E*H) % q
        return (X3, Y3, Z3, T3)

    def pt_xform (pt):
        (x, y) = pt
        return (x, y, 1, (x*y)%q)
    export["pt_xform"] = pt_xform

    def pt_unxform (pt):
        (x, y, z, _) = pt
        return ((x*inv(z))%q, (y*inv(z))%q)
    export["pt_unxform"] = pt_unxform

    def xpt_mult (pt, n):
        if n==0: return pt_xform((0,1))
        _ = xpt_double(xpt_mult(pt, n>>1))
        return xpt_add(_, pt) if n&1 else _
    export["xpt_mult"] = xpt_mult

    def scalarmult(pt, e):
        return pt_unxform(xpt_mult(pt_xform(pt), e))
    export["scalarmult"] = scalarmult

    def encodeint(y):
        bits = [(y >> i) & 1 for i in range(b)]
        e = [(sum([bits[i * 8 + j] << j for j in range(8)]))
                                        for i in range(b//8)]
        return asbytes(e)

    def encodepoint(P):
        x = P[0]
        y = P[1]
        bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
        e = [(sum([bits[i * 8 + j] << j for j in range(8)]))
                                        for i in range(b//8)]
        return asbytes(e)
    export["encodepoint"] = encodepoint

    def publickey(sk):
        h = H(sk)
        a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
        A = scalarmult(B,a)
        return encodepoint(A)
    export["publickey"] = publickey

    def Hint(m):
        h = H(m)
        return sum(2**i * bit(h,i) for i in range(2*b))
    export["Hint"] = Hint
<commit_msg>replace Hint() with a int(hexlify()). Saves 20% on sign(), 10% on verify().<commit_after>
# single-file pure-python ed25519 digital signatures, rearranged to minimize
# the namespace pollution so this can be embedded in another file. Adapted
# from https://bitbucket.org/dholth/ed25519ll


# Ed25519 digital signatures
# Based on http://ed25519.cr.yp.to/python/ed25519.py
# See also http://ed25519.cr.yp.to/software.html
# Adapted by <NAME>
# Sped up considerably using coordinate transforms found on:
# http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
# Specifically add-2008-hwcd-4 and dbl-2008-hwcd

export = {}

def Ed25519():
    # don't add many names to the file we're copied into

    try: # pragma nocover
        unicode
        PY3 = False
        def asbytes(b):
            """Convert array of integers to byte string"""
            return ''.join(chr(x) for x in b)
        def joinbytes(b):
            """Convert array of bytes to byte string"""
            return ''.join(b)
        def bit(h, i):
            """Return i'th bit of bytestring h"""
            return (ord(h[i//8]) >> (i%8)) & 1

    except NameError: # pragma nocover
        PY3 = True
        asbytes = bytes
        joinbytes = bytes
        def bit(h, i):
            return (h[i//8] >> (i%8)) & 1

    import hashlib, binascii

    b = 256
    q = 2**255 - 19
    l = 2**252 + 27742317777372353535851937790883648493

    def H(m):
        return hashlib.sha512(m).digest()

    # Can probably get some extra speedup here by replacing this with
    # an extended-euclidean, but performance seems OK without that
    def inv(x):
        return pow(x, q-2, q)

    d = -121665 * inv(121666)
    I = pow(2,(q-1)//4,q)

    def xrecover(y):
        xx = (y*y-1) * inv(d*y*y+1)
        x = pow(xx,(q+3)//8,q)
        if (x*x - xx) % q != 0: x = (x*I) % q
        if x % 2 != 0: x = q-x
        return x
    export["xrecover"] = xrecover

    By = 4 * inv(5)
    Bx = xrecover(By)
    export["Bx"] = Bx
    B = [Bx % q,By % q]
    export["B"] = B

    #def edwards(P,Q):
    #    x1 = P[0]
    #    y1 = P[1]
    #    x2 = Q[0]
    #    y2 = Q[1]
    #    x3 = (x1*y2+x2*y1) * inv(1+d*x1*x2*y1*y2)
    #    y3 = (y1*y2+x1*x2) * inv(1-d*x1*x2*y1*y2)
    #    return (x3 % q,y3 % q)

    #def scalarmult(P,e):
    #    if e == 0: return [0,1]
    #    Q = scalarmult(P,e/2)
    #    Q = edwards(Q,Q)
    #    if e & 1: Q = edwards(Q,P)
    #    return Q

    # Faster (!) version based on:
    # http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html

    def xpt_add(pt1, pt2):
        (X1, Y1, Z1, T1) = pt1
        (X2, Y2, Z2, T2) = pt2
        A = ((Y1-X1)*(Y2+X2)) % q
        B = ((Y1+X1)*(Y2-X2)) % q
        C = (Z1*2*T2) % q
        D = (T1*2*Z2) % q
        E = (D+C) % q
        F = (B-A) % q
        G = (B+A) % q
        H = (D-C) % q
        X3 = (E*F) % q
        Y3 = (G*H) % q
        Z3 = (F*G) % q
        T3 = (E*H) % q
        return (X3, Y3, Z3, T3)
    export["xpt_add"] = xpt_add

    def xpt_double (pt):
        (X1, Y1, Z1, _) = pt
        A = (X1*X1)
        B = (Y1*Y1)
        C = (2*Z1*Z1)
        D = (-A) % q
        J = (X1+Y1) % q
        E = (J*J-A-B) % q
        G = (D+B) % q
        F = (G-C) % q
        H = (D-B) % q
        X3 = (E*F) % q
        Y3 = (G*H) % q
        Z3 = (F*G) % q
        T3 = (E*H) % q
        return (X3, Y3, Z3, T3)

    def pt_xform (pt):
        (x, y) = pt
        return (x, y, 1, (x*y)%q)
    export["pt_xform"] = pt_xform

    def pt_unxform (pt):
        (x, y, z, _) = pt
        return ((x*inv(z))%q, (y*inv(z))%q)
    export["pt_unxform"] = pt_unxform

    def xpt_mult (pt, n):
        if n==0: return pt_xform((0,1))
        _ = xpt_double(xpt_mult(pt, n>>1))
        return xpt_add(_, pt) if n&1 else _
    export["xpt_mult"] = xpt_mult

    def scalarmult(pt, e):
        return pt_unxform(xpt_mult(pt_xform(pt), e))
    export["scalarmult"] = scalarmult

    def encodeint(y):
        bits = [(y >> i) & 1 for i in range(b)]
        e = [(sum([bits[i * 8 + j] << j for j in range(8)]))
                                        for i in range(b//8)]
        return asbytes(e)

    def encodepoint(P):
        x = P[0]
        y = P[1]
        bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
        e = [(sum([bits[i * 8 + j] << j for j in range(8)]))
                                        for i in range(b//8)]
        return asbytes(e)
    export["encodepoint"] = encodepoint

    def publickey(sk):
        h = H(sk)
        a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
        A = scalarmult(B,a)
        return encodepoint(A)
    export["publickey"] = publickey

    def Hint(m):
        h = H(m)
        return int(binascii.hexlify(h[::-1]), 16)
    export["Hint"] = Hint

{'fraction_non_alphanumeric': 0.16785864535128314, 'fraction_numerical': 0.05290281867900715, 'mean_word_length': 1.7215226101888952, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4787545', 'n_tokens_mistral': 4171, 'n_tokens_neox': 3910, 'n_words': 994}