<commit_before>#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "ntru.h"
#include "rand.h"
#include "poly.h"
#include "idxgen.h"
#include "mgf.h"

const int8_t NTRU_COEFF1_TABLE[] = {0, 0, 0, 1, 1, 1, -1, -1};
const int8_t NTRU_COEFF2_TABLE[] = {0, 1, -1, 0, 1, -1, 0, 1};
const uint8_t NTRU_BIT1_TABLE[] = {1, 1, 1, 0, 0, 0, 1, 0, 1};
const uint8_t NTRU_BIT2_TABLE[] = {1, 1, 1, 1, 0, 0, 0, 1, 0};
const uint8_t NTRU_BIT3_TABLE[] = {1, 0, 1, 0, 0, 1, 1, 1, 0};

uint8_t ntru_gen_key_pair_internal(NtruEncParams *params, NtruEncKeyPair *kp, uint8_t (*rng)(uint8_t[], uint16_t, NtruRandContext*), NtruRandContext *rand_ctx) {
    uint16_t N = params->N;
    uint16_t q = params->q;
    uint16_t df1 = params->df1;
#ifndef NTRU_AVOID_HAMMING_WT_PATENT
    uint16_t df2 = params->df2;
    uint16_t df3 = params->df3;
#endif   /* NTRU_AVOID_HAMMING_WT_PATENT */

    NtruIntPoly fq;

    /* choose a random f that is invertible mod q */
    NtruIntPoly f;
#ifndef NTRU_AVOID_HAMMING_WT_PATENT
    if (params->prod_flag) {
        NtruProdPoly t;
        for (;;) {
            /* choose random t, calculate f=3t+1 */
            if (!ntru_rand_prod(N, df1, df2, df3, df3, &t, rng, rand_ctx))
                return NTRU_ERR_PRNG;
            ntru_prod_to_int(&t, &f);
            ntru_mult_fac(&f, 3);
            f.coeffs[0] += 1;

            if (ntru_invert(&f, q, &fq))
                break;
        }

        kp->priv.q = q;
        kp->priv.prod_flag = 1;
        NtruProdPoly prod = {N, t.f1, t.f2, t.f3};
        kp->priv.t.prod = prod;
    }
    else
#endif   /* NTRU_AVOID_HAMMING_WT_PATENT */
    {
        NtruTernPoly t;
        for (;;) {
            /* choose random t, calculate f=3t+1 */
            if (!ntru_rand_tern(N, df1, df1, &t, rng, rand_ctx))
                return NTRU_ERR_PRNG;
            ntru_tern_to_int(&t, &f);
            ntru_mult_fac(&f, 3);
            f.coeffs[0] += 1;

            if (ntru_invert(&f, q, &fq))
                break;
        }

        kp->priv.q = q;
        kp->priv.prod_flag = 0;
        kp->priv.t.tern = t;
    }

    /* choose a random g that is invertible mod q */
    NtruTernPoly g;
    uint16_t dg = N / 3;
    for (;;) {
        if (!ntru_rand_tern(N, dg, dg-1, &g, rng, rand_ctx))
            return NTRU_ERR_PRNG;
        NtruIntPoly g_int;
        ntru_tern_to_int(&g, &g_int);
        if (ntru_is_invertible_pow2(&g_int))
            break;
    }

    NtruIntPoly h;
    if (!ntru_mult_tern(&fq, &g, &h))
        return NTRU_ERR_PRNG;
    ntru_mult_fac(&h, 3);
    ntru_mod(&h, q);

    ntru_clear_tern(&g);
    ntru_clear_int(&fq);

    NtruEncPubKey pub = {q, h};
    kp->pub = pub;

    return NTRU_SUCCESS;
}

uint8_t ntru_gen_key_pair(NtruEncParams *params, NtruEncKeyPair *kp, uint8_t (*rng)(uint8_t[], uint16_t, NtruRandContext*)) {
    return ntru_gen_key_pair_internal(params, kp, rng, NULL);
}

uint8_t ntru_gen_key_pair_det(NtruEncParams *params, NtruEncKeyPair *kp, uint8_t (*rng)(uint8_t[], uint16_t, NtruRandContext*), uint8_t *seed, uint16_t seed_len) {
    void *rand_state = NULL;
    NtruRandContext rand_ctx = {seed, seed_len, &rand_state};
    uint8_t result = ntru_gen_key_pair_internal(params, kp, rng, &rand_ctx);<commit_msg>Don't stack-allocate return values (or parts thereof)<commit_after>#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "ntru.h"
#include "rand.h"
#include "poly.h"
#include "idxgen.h"
#include "mgf.h"

const int8_t NTRU_COEFF1_TABLE[] = {0, 0, 0, 1, 1, 1, -1, -1};
const int8_t NTRU_COEFF2_TABLE[] = {0, 1, -1, 0, 1, -1, 0, 1};
const uint8_t NTRU_BIT1_TABLE[] = {1, 1, 1, 0, 0, 0, 1, 0, 1};
const uint8_t NTRU_BIT2_TABLE[] = {1, 1, 1, 1, 0, 0, 0, 1, 0};
const uint8_t NTRU_BIT3_TABLE[] = {1, 0, 1, 0, 0, 1, 1, 1, 0};

uint8_t ntru_gen_key_pair_internal(NtruEncParams *params, NtruEncKeyPair *kp, uint8_t (*rng)(uint8_t[], uint16_t, NtruRandContext*), NtruRandContext *rand_ctx) {
    uint16_t N = params->N;
    uint16_t q = params->q;
    uint16_t df1 = params->df1;
#ifndef NTRU_AVOID_HAMMING_WT_PATENT
    uint16_t df2 = params->df2;
    uint16_t df3 = params->df3;
#endif   /* NTRU_AVOID_HAMMING_WT_PATENT */

    NtruIntPoly fq;

    /* choose a random f that is invertible mod q */
    NtruIntPoly f;
#ifndef NTRU_AVOID_HAMMING_WT_PATENT
    if (params->prod_flag) {
        NtruProdPoly *t = &kp->priv.t.prod;
        for (;;) {
            /* choose random t, calculate f=3t+1 */
            if (!ntru_rand_prod(N, df1, df2, df3, df3, t, rng, rand_ctx))
                return NTRU_ERR_PRNG;
            ntru_prod_to_int(t, &f);
            ntru_mult_fac(&f, 3);
            f.coeffs[0] += 1;

            if (ntru_invert(&f, q, &fq))
                break;
        }

        kp->priv.q = q;
        kp->priv.prod_flag = 1;
        kp->priv.t.prod.N = N;
    }
    else
#endif   /* NTRU_AVOID_HAMMING_WT_PATENT */
    {
        NtruTernPoly *t = &kp->priv.t.tern;
        for (;;) {
            /* choose random t, calculate f=3t+1 */
            if (!ntru_rand_tern(N, df1, df1, t, rng, rand_ctx))
                return NTRU_ERR_PRNG;
            ntru_tern_to_int(t, &f);
            ntru_mult_fac(&f, 3);
            f.coeffs[0] += 1;

            if (ntru_invert(&f, q, &fq))
                break;
        }

        kp->priv.q = q;
        kp->priv.prod_flag = 0;
    }

    /* choose a random g that is invertible mod q */
    NtruTernPoly g;
    uint16_t dg = N / 3;
    for (;;) {
        if (!ntru_rand_tern(N, dg, dg-1, &g, rng, rand_ctx))
            return NTRU_ERR_PRNG;
        NtruIntPoly g_int;
        ntru_tern_to_int(&g, &g_int);
        if (ntru_is_invertible_pow2(&g_int))
            break;
    }

    NtruIntPoly *h = &kp->pub.h;
    if (!ntru_mult_tern(&fq, &g, h))
        return NTRU_ERR_PRNG;
    ntru_mult_fac(h, 3);
    ntru_mod(h, q);

    ntru_clear_tern(&g);
    ntru_clear_int(&fq);

    kp->pub.q = q;

    return NTRU_SUCCESS;
}

uint8_t ntru_gen_key_pair(NtruEncParams *params, NtruEncKeyPair *kp, uint8_t (*rng)(uint8_t[], uint16_t, NtruRandContext*)) {
    return ntru_gen_key_pair_internal(params, kp, rng, NULL);
}

uint8_t ntru_gen_key_pair_det(NtruEncParams *params, NtruEncKeyPair *kp, uint8_t (*rng)(uint8_t[], uint16_t, NtruRandContext*), uint8_t *seed, uint16_t seed_len) {
    void *rand_state = NULL;
    NtruRandContext rand_ctx = {seed, seed_len, &rand_state};
    uint8_t result = ntru_gen_key_pair_internal(params, kp, rng, &rand_ctx);
{'fraction_non_alphanumeric': 0.14316702819956617, 'fraction_numerical': 0.035171986365044934, 'mean_word_length': 2.536986301369863, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7093112', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2782, 'n_words': 586}