int SHA512_Final(unsigned char *md, SHA512_CTX *c)
{
    unsigned char *p = (unsigned char *)c->u.p;
    size_t n = c->num;
    p[n] = 0x80;
    n++;
    if (n > (sizeof(c->u) - 16)) {
        memset(p + n, 0, sizeof(c->u) - n);
        n = 0;
        sha512_block_data_order(c, p, 1);
    }
    memset(p + n, 0, sizeof(c->u) - 16 - n);
#ifdef  B_ENDIAN
    c->u.d[SHA_LBLOCK - 2] = c->Nh;
    c->u.d[SHA_LBLOCK - 1] = c->Nl;
#else
    p[sizeof(c->u) - 1] = (unsigned char)(c->Nl);
    p[sizeof(c->u) - 2] = (unsigned char)(c->Nl >> 8);
    p[sizeof(c->u) - 3] = (unsigned char)(c->Nl >> 16);
    p[sizeof(c->u) - 4] = (unsigned char)(c->Nl >> 24);
    p[sizeof(c->u) - 5] = (unsigned char)(c->Nl >> 32);
    p[sizeof(c->u) - 6] = (unsigned char)(c->Nl >> 40);
    p[sizeof(c->u) - 7] = (unsigned char)(c->Nl >> 48);
    p[sizeof(c->u) - 8] = (unsigned char)(c->Nl >> 56);
    p[sizeof(c->u) - 9] = (unsigned char)(c->Nh);
    p[sizeof(c->u) - 10] = (unsigned char)(c->Nh >> 8);
    p[sizeof(c->u) - 11] = (unsigned char)(c->Nh >> 16);
    p[sizeof(c->u) - 12] = (unsigned char)(c->Nh >> 24);
    p[sizeof(c->u) - 13] = (unsigned char)(c->Nh >> 32);
    p[sizeof(c->u) - 14] = (unsigned char)(c->Nh >> 40);
    p[sizeof(c->u) - 15] = (unsigned char)(c->Nh >> 48);
    p[sizeof(c->u) - 16] = (unsigned char)(c->Nh >> 56);
#endif
    sha512_block_data_order(c, p, 1);
    if (md == 0)
        return 0;
    switch (c->md_len) {
    case SHA224_DIGEST_LENGTH:
        for (n = 0; n < SHA224_DIGEST_LENGTH / 8; n++) {
            SHA_LONG64 t = c->h[n];
            *(md++) = (unsigned char)(t >> 56);
            *(md++) = (unsigned char)(t >> 48);
            *(md++) = (unsigned char)(t >> 40);
            *(md++) = (unsigned char)(t >> 32);
            *(md++) = (unsigned char)(t >> 24);
            *(md++) = (unsigned char)(t >> 16);
            *(md++) = (unsigned char)(t >> 8);
            *(md++) = (unsigned char)(t);
        }
        {
            SHA_LONG64 t = c->h[SHA224_DIGEST_LENGTH / 8];
            *(md++) = (unsigned char)(t >> 56);
            *(md++) = (unsigned char)(t >> 48);
            *(md++) = (unsigned char)(t >> 40);
            *(md++) = (unsigned char)(t >> 32);
        }
        break;
    case SHA256_DIGEST_LENGTH:
        for (n = 0; n < SHA256_DIGEST_LENGTH / 8; n++) {
            SHA_LONG64 t = c->h[n];
            *(md++) = (unsigned char)(t >> 56);
            *(md++) = (unsigned char)(t >> 48);
            *(md++) = (unsigned char)(t >> 40);
            *(md++) = (unsigned char)(t >> 32);
            *(md++) = (unsigned char)(t >> 24);
            *(md++) = (unsigned char)(t >> 16);
            *(md++) = (unsigned char)(t >> 8);
            *(md++) = (unsigned char)(t);
        }
        break;
    case SHA384_DIGEST_LENGTH:
        for (n = 0; n < SHA384_DIGEST_LENGTH / 8; n++) {
            SHA_LONG64 t = c->h[n];
            *(md++) = (unsigned char)(t >> 56);
            *(md++) = (unsigned char)(t >> 48);
            *(md++) = (unsigned char)(t >> 40);
            *(md++) = (unsigned char)(t >> 32);
            *(md++) = (unsigned char)(t >> 24);
            *(md++) = (unsigned char)(t >> 16);
            *(md++) = (unsigned char)(t >> 8);
            *(md++) = (unsigned char)(t);
        }
        break;
    case SHA512_DIGEST_LENGTH:
        for (n = 0; n < SHA512_DIGEST_LENGTH / 8; n++) {
            SHA_LONG64 t = c->h[n];
            *(md++) = (unsigned char)(t >> 56);
            *(md++) = (unsigned char)(t >> 48);
            *(md++) = (unsigned char)(t >> 40);
            *(md++) = (unsigned char)(t >> 32);
            *(md++) = (unsigned char)(t >> 24);
            *(md++) = (unsigned char)(t >> 16);
            *(md++) = (unsigned char)(t >> 8);
            *(md++) = (unsigned char)(t);
        }
        break;
    default:
        return 0;
    }
    return 1;
}
['int ED25519_verify(const uint8_t *message, size_t message_len,\n                   const uint8_t signature[64], const uint8_t public_key[32])\n{\n    int i;\n    ge_p3 A;\n    const uint8_t *r, *s;\n    SHA512_CTX hash_ctx;\n    ge_p2 R;\n    uint8_t rcheck[32];\n    uint8_t h[SHA512_DIGEST_LENGTH];\n    const uint8_t l_low[16] = {\n        0xED, 0xD3, 0xF5, 0x5C, 0x1A, 0x63, 0x12, 0x58, 0xD6, 0x9C, 0xF7, 0xA2,\n        0xDE, 0xF9, 0xDE, 0x14\n    };\n    r = signature;\n    s = signature + 32;\n    if (s[31] > 0x10)\n        return 0;\n    if (s[31] == 0x10) {\n        if (memcmp(s + 16, allzeroes, sizeof(allzeroes)) != 0)\n            return 0;\n        for (i = 15; i >= 0; i--) {\n            if (s[i] < l_low[i])\n                break;\n            if (s[i] > l_low[i])\n                return 0;\n        }\n        if (i < 0)\n            return 0;\n    }\n    if (ge_frombytes_vartime(&A, public_key) != 0) {\n        return 0;\n    }\n    fe_neg(A.X, A.X);\n    fe_neg(A.T, A.T);\n    SHA512_Init(&hash_ctx);\n    SHA512_Update(&hash_ctx, r, 32);\n    SHA512_Update(&hash_ctx, public_key, 32);\n    SHA512_Update(&hash_ctx, message, message_len);\n    SHA512_Final(h, &hash_ctx);\n    x25519_sc_reduce(h);\n    ge_double_scalarmult_vartime(&R, h, &A, s);\n    ge_tobytes(rcheck, &R);\n    return CRYPTO_memcmp(rcheck, r, sizeof(rcheck)) == 0;\n}', 'int SHA512_Init(SHA512_CTX *c)\n{\n    c->h[0] = U64(0x6a09e667f3bcc908);\n    c->h[1] = U64(0xbb67ae8584caa73b);\n    c->h[2] = U64(0x3c6ef372fe94f82b);\n    c->h[3] = U64(0xa54ff53a5f1d36f1);\n    c->h[4] = U64(0x510e527fade682d1);\n    c->h[5] = U64(0x9b05688c2b3e6c1f);\n    c->h[6] = U64(0x1f83d9abfb41bd6b);\n    c->h[7] = U64(0x5be0cd19137e2179);\n    c->Nl = 0;\n    c->Nh = 0;\n    c->num = 0;\n    c->md_len = SHA512_DIGEST_LENGTH;\n    return 1;\n}', 'int SHA512_Update(SHA512_CTX *c, const void *_data, size_t len)\n{\n    SHA_LONG64 l;\n    unsigned char *p = c->u.p;\n    const unsigned char *data = (const unsigned char *)_data;\n    if (len == 0)\n        return 1;\n    l = (c->Nl + (((SHA_LONG64) len) << 3)) & U64(0xffffffffffffffff);\n    if (l < c->Nl)\n        c->Nh++;\n    if (sizeof(len) >= 8)\n        c->Nh += (((SHA_LONG64) len) >> 61);\n    c->Nl = l;\n    if (c->num != 0) {\n        size_t n = sizeof(c->u) - c->num;\n        if (len < n) {\n            memcpy(p + c->num, data, len), c->num += (unsigned int)len;\n            return 1;\n        } else {\n            memcpy(p + c->num, data, n), c->num = 0;\n            len -= n, data += n;\n            sha512_block_data_order(c, p, 1);\n        }\n    }\n    if (len >= sizeof(c->u)) {\n#ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA\n        if ((size_t)data % sizeof(c->u.d[0]) != 0)\n            while (len >= sizeof(c->u))\n                memcpy(p, data, sizeof(c->u)),\n                sha512_block_data_order(c, p, 1),\n                len -= sizeof(c->u), data += sizeof(c->u);\n        else\n#endif\n            sha512_block_data_order(c, data, len / sizeof(c->u)),\n            data += len, len %= sizeof(c->u), data -= len;\n    }\n    if (len != 0)\n        memcpy(p, data, len), c->num = (int)len;\n    return 1;\n}', 'int SHA512_Final(unsigned char *md, SHA512_CTX *c)\n{\n    unsigned char *p = (unsigned char *)c->u.p;\n    size_t n = c->num;\n    p[n] = 0x80;\n    n++;\n    if (n > (sizeof(c->u) - 16)) {\n        memset(p + n, 0, sizeof(c->u) - n);\n        n = 0;\n        sha512_block_data_order(c, p, 1);\n    }\n    memset(p + n, 0, sizeof(c->u) - 16 - n);\n#ifdef  B_ENDIAN\n    c->u.d[SHA_LBLOCK - 2] = c->Nh;\n    c->u.d[SHA_LBLOCK - 1] = c->Nl;\n#else\n    p[sizeof(c->u) - 1] = (unsigned char)(c->Nl);\n    p[sizeof(c->u) - 2] = (unsigned char)(c->Nl >> 8);\n    p[sizeof(c->u) - 3] = (unsigned char)(c->Nl >> 16);\n    p[sizeof(c->u) - 4] = (unsigned char)(c->Nl >> 24);\n    p[sizeof(c->u) - 5] = (unsigned char)(c->Nl >> 32);\n    p[sizeof(c->u) - 6] = (unsigned char)(c->Nl >> 40);\n    p[sizeof(c->u) - 7] = (unsigned char)(c->Nl >> 48);\n    p[sizeof(c->u) - 8] = (unsigned char)(c->Nl >> 56);\n    p[sizeof(c->u) - 9] = (unsigned char)(c->Nh);\n    p[sizeof(c->u) - 10] = (unsigned char)(c->Nh >> 8);\n    p[sizeof(c->u) - 11] = (unsigned char)(c->Nh >> 16);\n    p[sizeof(c->u) - 12] = (unsigned char)(c->Nh >> 24);\n    p[sizeof(c->u) - 13] = (unsigned char)(c->Nh >> 32);\n    p[sizeof(c->u) - 14] = (unsigned char)(c->Nh >> 40);\n    p[sizeof(c->u) - 15] = (unsigned char)(c->Nh >> 48);\n    p[sizeof(c->u) - 16] = (unsigned char)(c->Nh >> 56);\n#endif\n    sha512_block_data_order(c, p, 1);\n    if (md == 0)\n        return 0;\n    switch (c->md_len) {\n    case SHA224_DIGEST_LENGTH:\n        for (n = 0; n < SHA224_DIGEST_LENGTH / 8; n++) {\n            SHA_LONG64 t = c->h[n];\n            *(md++) = (unsigned char)(t >> 56);\n            *(md++) = (unsigned char)(t >> 48);\n            *(md++) = (unsigned char)(t >> 40);\n            *(md++) = (unsigned char)(t >> 32);\n            *(md++) = (unsigned char)(t >> 24);\n            *(md++) = (unsigned char)(t >> 16);\n            *(md++) = (unsigned char)(t >> 8);\n            *(md++) = (unsigned char)(t);\n        }\n        {\n            SHA_LONG64 t = c->h[SHA224_DIGEST_LENGTH / 8];\n            *(md++) = (unsigned char)(t >> 56);\n            *(md++) = (unsigned char)(t >> 48);\n            *(md++) = (unsigned char)(t >> 40);\n            *(md++) = (unsigned char)(t >> 32);\n        }\n        break;\n    case SHA256_DIGEST_LENGTH:\n        for (n = 0; n < SHA256_DIGEST_LENGTH / 8; n++) {\n            SHA_LONG64 t = c->h[n];\n            *(md++) = (unsigned char)(t >> 56);\n            *(md++) = (unsigned char)(t >> 48);\n            *(md++) = (unsigned char)(t >> 40);\n            *(md++) = (unsigned char)(t >> 32);\n            *(md++) = (unsigned char)(t >> 24);\n            *(md++) = (unsigned char)(t >> 16);\n            *(md++) = (unsigned char)(t >> 8);\n            *(md++) = (unsigned char)(t);\n        }\n        break;\n    case SHA384_DIGEST_LENGTH:\n        for (n = 0; n < SHA384_DIGEST_LENGTH / 8; n++) {\n            SHA_LONG64 t = c->h[n];\n            *(md++) = (unsigned char)(t >> 56);\n            *(md++) = (unsigned char)(t >> 48);\n            *(md++) = (unsigned char)(t >> 40);\n            *(md++) = (unsigned char)(t >> 32);\n            *(md++) = (unsigned char)(t >> 24);\n            *(md++) = (unsigned char)(t >> 16);\n            *(md++) = (unsigned char)(t >> 8);\n            *(md++) = (unsigned char)(t);\n        }\n        break;\n    case SHA512_DIGEST_LENGTH:\n        for (n = 0; n < SHA512_DIGEST_LENGTH / 8; n++) {\n            SHA_LONG64 t = c->h[n];\n            *(md++) = (unsigned char)(t >> 56);\n            *(md++) = (unsigned char)(t >> 48);\n            *(md++) = (unsigned char)(t >> 40);\n            *(md++) = (unsigned char)(t >> 32);\n            *(md++) = (unsigned char)(t >> 24);\n            *(md++) = (unsigned char)(t >> 16);\n            *(md++) = (unsigned char)(t >> 8);\n            *(md++) = (unsigned char)(t);\n        }\n        break;\n    default:\n        return 0;\n    }\n    return 1;\n}']