void fp_read_bin(fp_t a, const unsigned char *str, int len) { bn_t t; bn_null(t); TRY { bn_new(t); bn_read_bin(t, (unsigned char *) str, len); if (bn_is_zero(t)) { fp_zero(a); } else { if (t->used == 1) { fp_prime_conv_dig(a, t->dp[0]); } else { fp_prime_conv(a, t); } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { bn_free(t); } }
int fp_cmp_dig(const fp_t a, dig_t b) { #if FP_RDC == MONTY fp_t t; int r = CMP_EQ; fp_null(t); TRY { fp_new(t); fp_prime_conv_dig(t, b); r = fp_cmp(a, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t); } return r; #else for (int i = 1; i < FP_DIGS; i++) { if (a[i] > 0) { return CMP_GT; } } return fp_cmp1_low(a[0], b); #endif }
void fp_read_bin(fp_t a, const uint8_t *bin, int len) { bn_t t; bn_null(t); if (len != RLC_FP_BYTES) { THROW(ERR_NO_BUFFER); } TRY { bn_new(t); bn_read_bin(t, bin, len); if (bn_is_zero(t)) { fp_zero(a); } else { if (t->used == 1) { fp_prime_conv_dig(a, t->dp[0]); } else { fp_prime_conv(a, t); } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { bn_free(t); } }
void fp_read_str(fp_t a, const char *str, int len, int radix) { bn_t t; bn_null(t); TRY { bn_new(t); bn_read_str(t, str, len, radix); if (bn_is_zero(t)) { fp_zero(a); } else { if (t->used == 1) { fp_prime_conv_dig(a, t->dp[0]); } else { fp_prime_conv(a, t); } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { bn_free(t); } }
void fp_mul_dig(fp_t c, const fp_t a, dig_t b) { dv_t t; dv_null(t); TRY { dv_new(t); fp_prime_conv_dig(t, b); fp_mul(c, a, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
int fp_cmp_dig(const fp_t a, dig_t b) { fp_t t; int r = RLC_EQ; fp_null(t); TRY { fp_new(t); fp_prime_conv_dig(t, b); r = fp_cmp(a, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t); } return r; }
/** * Detects an optimization based on the curve coefficients. * * @param[out] opt - the resulting optimization. * @param[in] a - the curve coefficient. */ static void detect_opt(int *opt, fp_t a) { fp_t t; fp_null(t); TRY { fp_new(t); fp_prime_conv_dig(t, 3); fp_neg(t, t); if (fp_cmp(a, t) == CMP_EQ) { *opt = OPT_MINUS3; } else { if (fp_is_zero(a)) { *opt = OPT_ZERO; } else { fp_set_dig(t, 1); if (fp_cmp_dig(a, 1) == CMP_EQ) { *opt = OPT_ONE; } else { if (fp_cmp_dig(a, 2) == CMP_EQ) { *opt = OPT_TWO; } else { if (fp_bits(a) <= FP_DIGIT) { *opt = OPT_DIGIT; } else { *opt = RELIC_OPT_NONE; } } } } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t); } }
void fp_set_dig(fp_t c, dig_t a) { fp_prime_conv_dig(c, a); }