/* * TEE_BigIntExtendedGcd */ void TEE_BigIntComputeExtendedGcd(TEE_BigInt *gcd, TEE_BigInt *u, TEE_BigInt *v, const TEE_BigInt *op1, const TEE_BigInt *op2) { mpanum mpa_gcd_res = (mpa_num_base *)gcd; mpanum mpa_u = (mpa_num_base *)u; mpanum mpa_v = (mpa_num_base *)v; mpanum mpa_op1 = (mpa_num_base *)op1; mpanum mpa_op2 = (mpa_num_base *)op2; mpa_extended_gcd(mpa_gcd_res, mpa_u, mpa_v, mpa_op1, mpa_op2, mempool); }
/*------------------------------------------------------------ * * mpa_inv_mod * */ int mpa_inv_mod(mpanum dest, const mpanum op, const mpanum n, mpa_scratch_mem pool) { mpanum gcd; mpanum tmp_dest; int mem_marker; int res; if (mpa_cmp_short(op, 1) == 0) { mpa_set_S32(dest, 1); return 0; } mem_marker = (dest == op); if (mem_marker) mpa_alloc_static_temp_var(&tmp_dest, pool); else tmp_dest = dest; mpa_alloc_static_temp_var(&gcd, pool); /* The function mpa_extended_gcd behaves badly if tmp_dest = op */ mpa_extended_gcd(gcd, tmp_dest, NULL, op, n, pool); res = mpa_cmp_short(gcd, 1); if (mem_marker) { mpa_copy(dest, tmp_dest); mpa_free_static_temp_var(&tmp_dest, pool); } mpa_free_static_temp_var(&gcd, pool); if (res == 0) { while (mpa_cmp_short(dest, 0) < 0) mpa_add(dest, dest, n, pool); return 0; } else { return -1; } }