/* Copyright (c) (2019,2020) Apple Inc. All rights reserved.
 *
 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
 * is contained in the License.txt file distributed with corecrypto) and only to
 * people who accept that license. IMPORTANT:  Any license rights granted to you by
 * Apple Inc. (if any) are limited to internal use within your organization only on
 * devices and computers you own or control, for the sole purpose of verifying the
 * security characteristics and correct functioning of the Apple Software.  You may
 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
 */

#include "ccperf.h"
#include "cczp.h"
#include "ccprime_internal.h"

// A 2048-bit prime.
const uint8_t PRIME[] = {
    0xfb, 0x00, 0x18, 0xa5, 0xa5, 0x37, 0x24, 0x68,
    0x07, 0x31, 0xdc, 0xc9, 0x4a, 0xf5, 0x64, 0x21,
    0xbb, 0x87, 0xce, 0x2c, 0xa5, 0xe0, 0x54, 0x50,
    0x31, 0xfd, 0x40, 0x3c, 0xca, 0x26, 0x39, 0xbd,
    0x6a, 0x45, 0x0f, 0x28, 0xf6, 0x95, 0x2d, 0x8e,
    0x11, 0x1c, 0xbe, 0x95, 0xf0, 0x36, 0x98, 0xc2,
    0x39, 0xb6, 0x61, 0xe4, 0x45, 0xd6, 0x51, 0x1a,
    0x16, 0x61, 0x64, 0x01, 0x17, 0x66, 0x57, 0xcd,
    0xfb, 0x0a, 0x17, 0x51, 0xc8, 0x5d, 0xaa, 0xf0,
    0xa4, 0x4d, 0x24, 0x24, 0xfb, 0x4c, 0xa4, 0x5c,
    0x76, 0x5b, 0xd3, 0x63, 0xf7, 0xe9, 0x07, 0xef,
    0xf0, 0x1a, 0xd5, 0x58, 0xd1, 0x7c, 0x17, 0x30,
    0xd8, 0x09, 0x9b, 0xcb, 0x39, 0x7e, 0x6e, 0xff,
    0x39, 0xc4, 0xc3, 0x4d, 0x6c, 0x7f, 0x48, 0x90,
    0x50, 0xaa, 0x04, 0x79, 0x40, 0x75, 0x6e, 0x30,
    0xa9, 0xe7, 0xdf, 0xd6, 0x35, 0x6b, 0xae, 0x00,
    0xac, 0x8b, 0x39, 0x2d, 0xda, 0xef, 0xa6, 0xb9,
    0x7c, 0x1a, 0xa4, 0x83, 0x9e, 0x6d, 0x9b, 0x2d,
    0x54, 0xdb, 0x62, 0x92, 0x96, 0x41, 0xf7, 0x34,
    0x9c, 0x63, 0x57, 0x5b, 0x80, 0x53, 0x39, 0xd1,
    0x98, 0xf5, 0xd1, 0x44, 0x4a, 0xf7, 0xb4, 0xff,
    0xd8, 0x5e, 0x05, 0xdf, 0xdf, 0xc0, 0x69, 0x70,
    0xdb, 0xde, 0xc0, 0x1b, 0x54, 0x4d, 0xa9, 0xbe,
    0x55, 0xd1, 0x03, 0x5b, 0x0a, 0x0a, 0xf5, 0xaa,
    0x48, 0xfe, 0x36, 0x2d, 0x1f, 0x1b, 0x80, 0x8f,
    0xba, 0xbe, 0x5d, 0xe6, 0x41, 0x33, 0x14, 0xed,
    0xd3, 0xde, 0x88, 0x6d, 0x67, 0xa1, 0x23, 0x7b,
    0xe8, 0xee, 0xe3, 0xbe, 0x25, 0xc3, 0xab, 0x54,
    0x50, 0x59, 0xd6, 0xce, 0x29, 0xb5, 0x1c, 0x21,
    0x7c, 0xd6, 0x6e, 0xe8, 0xea, 0x55, 0x9e, 0xc9,
    0xeb, 0xdb, 0x5a, 0x6f, 0x27, 0x08, 0x0e, 0xfc,
    0x89, 0x7c, 0xd4, 0x6d, 0x65, 0xac, 0x58, 0xf9
};

static double perf_ccprime_miller_rabin(size_t loops, cc_size mr_depth)
{
    cc_size n = ccn_nof_size(sizeof(PRIME));
    cc_unit p[n];
    ccn_read_uint(n, p, sizeof(PRIME), PRIME);

    perf_start();

    do {
        if (ccprime_rabin_miller(n, p, mr_depth, rng) != 1) {
            abort();
        }
    } while (--loops != 0);

    return perf_seconds();
}

#define _TEST(_x)                      \
    {                                  \
        .name = #_x, .func = perf_##_x \
    }
static struct ccprime_perf_test {
    const char *name;
    double (*func)(size_t loops, cc_size nbits);
} ccprime_perf_tests[] = {
    _TEST(ccprime_miller_rabin),
};

static double perf_ccprime(size_t loops, size_t *psize, const void *arg)
{
    const struct ccprime_perf_test *test = arg;
    return test->func(loops, *psize);
}

static struct ccperf_family family;

struct ccperf_family *ccperf_family_ccprime(int argc, char *argv[])
{
    F_GET_ALL(family, ccprime);
    static const size_t group_nbits[] = { 1, 2, 16, 32 };
    F_SIZES_FROM_ARRAY(family, group_nbits);
    family.size_kind = ccperf_size_bits;
    return &family;
}
