

#include "internal/ffc.h"


int ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params,
                                  int N, int s, BIGNUM *priv)
{
    int ret = 0, qbits = BN_num_bits(params->q);
    BIGNUM *m, *two_powN = NULL;

    
    if (s == 0)
        goto err;
    if (N == 0)
        N = params->keylength ? params->keylength : 2 * s;

    
    if (N < 2 * s || N > qbits)
        return 0;

    two_powN = BN_new();
    
    if (two_powN == NULL || !BN_lshift(two_powN, BN_value_one(), N))
        goto err;

    
    m = (BN_cmp(two_powN, params->q) > 0) ? params->q : two_powN;

    do {
        
        if (!BN_priv_rand_range_ex(priv, two_powN, 0, ctx)
            || !BN_add_word(priv, 1))
            goto err;
        
        if (BN_cmp(priv, m) < 0)
            break;
    } while (1);

    ret = 1;
err:
    BN_free(two_powN);
    return ret;
}
