uint8_t dsa_verify_bigint(const dsa_signature_t* s, const bigint_t* m, const dsa_ctx_t* ctx) { if(s->r.length_B==0 || s->s.length_B==0) { return DSA_SIGNATURE_FAIL; } if(bigint_cmp_u(&(s->r), &(ctx->domain.q))>=0 || bigint_cmp_u(&(s->s), &(ctx->domain.q))>=0) { return DSA_SIGNATURE_FAIL; } bigint_t w, u1, u2, v1, v2; uint8_t w_b[ctx->domain.q.length_B], u1_b[ctx->domain.q.length_B*2], u2_b[ctx->domain.q.length_B*2]; uint8_t v1_b[ctx->domain.p.length_B*2], v2_b[ctx->domain.p.length_B]; w.wordv = w_b; u1.wordv = u1_b; u2.wordv = u2_b; v1.wordv = v1_b; v2.wordv = v2_b; bigint_inverse(&w, &(s->s), &(ctx->domain.q)); bigint_mul_u(&u1, &w, m); bigint_reduce(&u1, &(ctx->domain.q)); bigint_mul_u(&u2, &w, &(s->r)); bigint_reduce(&u2, &(ctx->domain.q)); bigint_expmod_u(&v1, &(ctx->domain.g), &u1, &(ctx->domain.p)); bigint_expmod_u(&v2, &(ctx->pub), &u2, &(ctx->domain.p)); bigint_mul_u(&v1, &v1, &v2); bigint_reduce(&v1, &(ctx->domain.p)); bigint_reduce(&v1, &(ctx->domain.q)); if(bigint_cmp_u(&v1, &(s->r))==0) { return DSA_SIGNATURE_OK; } return DSA_SIGNATURE_FAIL; }
void testrun_performance_multiply_bigint(void){ printf_P(PSTR("\n=== performance measurement (invert) ===\n")); unsigned i,j; uint64_t time_a = 0, time_b = 0; uint32_t tmp; bigint_t a, b, v; bigint_word_t v_w[192 * 2 / BIGINT_WORD_SIZE]; bigint_word_t a_w[192 / BIGINT_WORD_SIZE]; bigint_word_t b_w[192 / BIGINT_WORD_SIZE]; a.wordv = a_w; b.wordv = b_w; v.wordv = v_w; for(j = 0; j < 32; ++j){ for(i = 0; i < 192 / BIGINT_WORD_SIZE; ++i){ ((uint8_t*)a_w)[i] = random(); } a.length_W = 192 / BIGINT_WORD_SIZE; a.info = 0; bigint_adjust(&a); for(i = 0; i < 192 / BIGINT_WORD_SIZE; ++i){ ((uint8_t*)b_w)[i] = random(); } b.length_W = 192 / BIGINT_WORD_SIZE; b.info = 0; bigint_adjust(&b); for(i = 0; i < 16; ++i){ startTimer(1); START_TIMER; bigint_mul_u(&v,&a, &b); STOP_TIMER; tmp = stopTimer(); time_a += tmp; time_b += tmp; START_TIMER; bigint_reduce_p192(&v); STOP_TIMER; tmp = stopTimer(); time_b += tmp; } } time_a >>= 8; ++time_a; time_a >>= 1; time_b >>= 8; ++time_b; time_b >>= 1; printf_P(PSTR(" multiply costs %7"PRIu32" cycles\n"), (uint32_t)time_a); printf_P(PSTR(" multiply + reduce costs %7"PRIu32" cycles\n"), (uint32_t)time_b); }