static void FreeMember(adbus_Member* m) { if (!m) return; if (m->release[0]) m->release[0](m->ruser[0]); if (m->release[1]) m->release[1](m->ruser[1]); for (dh_Iter ii = dh_begin(&m->annotations); ii != dh_end(&m->annotations); ++ii) { if (dh_exist(&m->annotations, ii)) { free((char*) dh_key(&m->annotations, ii)); free(dh_val(&m->annotations, ii)); } } dh_free(StringPair, &m->annotations); for (size_t i = 0; i < dv_size(&m->arguments); i++) { free(dv_a(&m->arguments, i)); } dv_free(String, &m->arguments); for (size_t j = 0; j < dv_size(&m->returns); j++) { free(dv_a(&m->returns, j)); } dv_free(String, &m->returns); free(m->propertyType); free((char*) m->name.str); free(m); }
void fp_rdc_basic(fp_t c, dv_t a) { dv_t t0, t1, t2, t3; dv_null(t0); dv_null(t1); dv_null(t2); dv_null(t3); TRY { dv_new(t0); dv_new(t1); dv_new(t2); dv_new(t3); dv_copy(t2, a, 2 * FP_DIGS); dv_copy(t3, fp_prime_get(), FP_DIGS); bn_divn_low(t0, t1, t2, 2 * FP_DIGS, t3, FP_DIGS); fp_copy(c, t1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t0); dv_free(t1); dv_free(t2); dv_free(t3); } }
void fp2_mul_basic(fp2_t c, fp2_t a, fp2_t b) { dv_t t0, t1, t2, t3, t4; dv_null(t0); dv_null(t1); dv_null(t2); dv_null(t3); dv_null(t4); TRY { dv_new(t0); dv_new(t1); dv_new(t2); dv_new(t3); dv_new(t4); /* Karatsuba algorithm. */ /* t2 = a_0 + a_1, t1 = b0 + b1. */ fp_add(t2, a[0], a[1]); fp_add(t1, b[0], b[1]); /* t3 = (a_0 + a_1) * (b0 + b1). */ fp_muln_low(t3, t2, t1); /* t0 = a_0 * b0, t4 = a_1 * b1. */ fp_muln_low(t0, a[0], b[0]); fp_muln_low(t4, a[1], b[1]); /* t2 = (a_0 * b0) + (a_1 * b1). */ fp_addc_low(t2, t0, t4); /* t1 = (a_0 * b0) + u^2 * (a_1 * b1). */ fp_subc_low(t1, t0, t4); /* t1 = u^2 * (a_1 * b1). */ for (int i = -1; i > fp_prime_get_qnr(); i--) { fp_subc_low(t1, t1, t4); } /* c_0 = t1 mod p. */ fp_rdc(c[0], t1); /* t4 = t3 - t2. */ fp_subc_low(t4, t3, t2); /* c_1 = t4 mod p. */ fp_rdc(c[1], t4); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t0); dv_free(t1); dv_free(t2); dv_free(t3); dv_free(t4); } }
void fb_mul_lcomb(fb_t c, const fb_t a, const fb_t b) { dv_t t; dig_t carry; dv_null(t); TRY { dv_new(t); dv_zero(t, 2 * FB_DIGS); for (int i = FB_DIGIT - 1; i >= 0; i--) { for (int j = 0; j < FB_DIGS; j++) { if (a[j] & ((dig_t)1 << i)) { /* This cannot use fb_addn_low() because there is no * guarantee that operands will be aligned. */ fb_addd_low(t + j, t + j, b, FB_DIGS); } } if (i != 0) { carry = fb_lsh1_low(t, t); fb_lsh1_low(t + FB_DIGS, t + FB_DIGS); t[FB_DIGS] |= carry; } } fb_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
/** * Multiplies two binary field elements using right-to-left comb multiplication. * * @param c - the result. * @param a - the first binary field element. * @param b - the second binary field element. * @param size - the number of digits to multiply. */ static void fb_mul_rcomb_imp(dig_t *c, const dig_t *a, const dig_t *b, int size) { dv_t _b; dv_null(_b); TRY { dv_new(_b); dv_zero(c, 2 * size); for (int i = 0; i < size; i++) _b[i] = b[i]; _b[size] = 0; for (int i = 0; i < FB_DIGIT; i++) { for (int j = 0; j < size; j++) { if (a[j] & ((dig_t)1 << i)) { fb_addd_low(c + j, c + j, _b, size + 1); } } if (i != FB_DIGIT - 1) { bn_lsh1_low(_b, _b, size + 1); } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(_b); } }
/* -------------------------------------------------------------------------- */ void adbusI_serv_freeservice(struct Service* s) { if (s) { dv_free(ServiceOwner, &s->queue); free(s); } }
/** Removes a remote from the server * \relates adbus_Server */ void adbus_remote_disconnect(adbus_Remote* r) { if (r == NULL) return; adbus_Server* s = r->server; dl_remove(Remote, r, &r->hl); // Free the matches struct Match* m = r->matches.next; while (m) { struct Match* next = m->hl.next; adbusI_serv_freematch(m); m = next; } dl_clear(Match, &r->matches); while (dv_size(&r->services) > 0) { adbusI_serv_releasename(s, r, dv_a(&r->services, 0)->name); } dv_free(Service, &r->services); adbus_buf_free(r->msg); adbus_buf_free(r->dispatch); ds_free(&r->unique); free(r); }
void fp_mul_karat(fp_t c, const fp_t a, const fp_t b) { dv_t t; dv_null(t); TRY { /* We need a temporary variable so that c can be a or b. */ dv_new(t); dv_zero(t, 2 * FP_DIGS); if (FP_DIGS > 1) { fp_mul_karat_imp(t, a, b, FP_DIGS, FP_KARAT); } else { fp_muln_low(t, a, b); } fp_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void fp_prime_back(bn_t c, const fp_t a) { dv_t t; int i; dv_null(t); TRY { dv_new(t); bn_grow(c, FP_DIGS); for (i = 0; i < FP_DIGS; i++) { c->dp[i] = a[i]; } #if FP_RDC == MONTY dv_zero(t, 2 * FP_DIGS + 1); dv_copy(t, a, FP_DIGS); fp_rdc(c->dp, t); #endif c->used = FP_DIGS; c->sign = BN_POS; bn_trim(c); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void fp_prime_conv_dig(fp_t c, dig_t a) { dv_t t; ctx_t *ctx = core_get(); bn_null(t); TRY { dv_new(t); #if FP_RDC == MONTY if (a != 1) { dv_zero(t, 2 * FP_DIGS + 1); t[FP_DIGS] = fp_mul1_low(t, ctx->conv.dp, a); fp_rdc(c, t); } else { dv_copy(c, ctx->one.dp, FP_DIGS); } #else (void)ctx; fp_zero(c); c[0] = a; #endif } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void fp_mul_basic(fp_t c, const fp_t a, const fp_t b) { int i; dv_t t; dig_t carry; dv_null(t); TRY { /* We need a temporary variable so that c can be a or b. */ dv_new(t); dv_zero(t, 2 * FP_DIGS); for (i = 0; i < FP_DIGS; i++) { carry = fp_mula_low(t + i, b, *(a + i)); *(t + i + FP_DIGS) = carry; } fp_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
/** * Multiplies two binary field elements using shift-and-add multiplication. * * @param c - the result. * @param a - the first binary field element. * @param b - the second binary field element. * @param size - the number of digits to multiply. */ static void fb_mul_basic_imp(dig_t *c, const dig_t *a, const dig_t *b, int size) { int i; dv_t s; dv_null(s); TRY { /* We need a temporary variable so that c can be a or b. */ dv_new(s); dv_zero(s, 2 * FB_DIGS); dv_copy(s, b, size); dv_zero(c, 2 * size); if (a[0] & 1) { dv_copy(c, b, size); } for (i = 1; i <= (FB_DIGIT * size) - 1; i++) { fb_lsh1_low(s, s); fb_rdc(s, s); if (fb_get_bit(a, i)) { fb_add(c, c, s); } } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(s); } }
void fp_mul_comba(fp_t c, const fp_t a, const fp_t b) { dv_t t; dv_null(t); TRY { /* We need a temporary variable so that c can be a or b. */ dv_new(t); fp_muln_low(t, a, b); fp_rdc(c, t); dv_free(t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void dv_draw_status(cairo_t *cr) { cairo_save(cr); cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); cairo_select_font_face(cr, "Courier", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size(cr, 14); char *s[10]; int n = 0; int length = 50; // Depth s[n] = (char *) dv_malloc( length * sizeof(char) ); sprintf(s[n], "D=%d/%d", S->sel, G->lvmax); n++; // Nodes drawn s[n] = (char *) dv_malloc( length * sizeof(char) ); sprintf(s[n], "ND=%ld, ", S->nd); n++; // ratio if (S->a->on) { s[n] = (char *) dv_malloc( length * sizeof(char) ); sprintf(s[n], "ratio=%0.2f, ", S->a->ratio); n++; } /*s[n] = (char *) dv_malloc( length * sizeof(char) ); sprintf(s[n], ", "); n++;*/ int slength = 0; int i; for (i=0; i<n; i++) { slength += strlen(s[i]); } const double char_width = 8; double x = S->vpw - DV_STATUS_PADDING - slength * char_width; double y = S->vph - DV_STATUS_PADDING; cairo_new_path(cr); cairo_move_to(cr, x, y); for (i=n-1; i>=0; i--) { cairo_show_text(cr, s[i]); } cairo_restore(cr); for (i=0; i<n; i++) dv_free(s[i], length * sizeof(char)); }
void fb_mul_rcomb(fb_t c, const fb_t a, const fb_t b) { dv_t t, _b; dig_t carry; dv_null(t); dv_null(_b); TRY { dv_new(t); dv_new(_b); dv_zero(t, 2 * FB_DIGS); dv_zero(_b, FB_DIGS + 1); fb_copy(_b, b); for (int i = 0; i < FB_DIGIT; i++) { for (int j = 0; j < FB_DIGS; j++) { if (a[j] & ((dig_t)1 << i)) { fb_addd_low(t + j, t + j, _b, FB_DIGS + 1); } } if (i != FB_DIGIT - 1) { carry = fb_lsh1_low(_b, _b); _b[FB_DIGS] = (_b[FB_DIGS] << 1) | carry; } } fb_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); dv_free(_b); } }
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); } }
void fb_sqr_table(fb_t c, const fb_t a) { dv_t t; dv_null(t); TRY { /* We need a temporary variable so that c can be a or b. */ dv_new(t); fb_sqrl_low(t, a); fb_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void fb_mul_lodah(fb_t c, const fb_t a, const fb_t b) { dv_t t; dv_null(t); TRY { dv_new(t); fb_muln_low(t, a, b); fb_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void fb_mul_dig(fb_t c, fb_t a, dig_t b) { dv_t t; dv_null(t); TRY { /* We need a temporary variable so that c can be a or b. */ dv_new(t); fb_mul1_low(t, a, b); fb_rdc1_low(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void fb_mul_karat(fb_t c, const fb_t a, const fb_t b) { dv_t t; dv_null(t); TRY { /* We need a temporary variable so that c can be a or b. */ dv_new(t); dv_zero(t, 2 * FB_DIGS); fb_mul_karat_imp(t, a, b, FB_DIGS, FB_KARAT); fb_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
void fp_sqr_karat(fp_t c, const fp_t a) { dv_t t; dv_null(t); TRY { dv_new(t); dv_zero(t, 2 * RLC_FP_DIGS); if (RLC_FP_DIGS > 1) { fp_sqr_karat_imp(t, a, RLC_FP_DIGS, FP_KARAT); } else { fp_sqrn_low(t, a); } fp_rdc(c, t); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t); } }
/** * Assigns the prime field modulus. * * @param[in] p - the new prime field modulus. */ static void fp_prime_set(const bn_t p) { dv_t s, q; bn_t t; ctx_t *ctx = core_get(); if (p->used != FP_DIGS) { THROW(ERR_NO_VALID); } dv_null(s); bn_null(t); dv_null(q); TRY { dv_new(s); bn_new(t); dv_new(q); bn_copy(&(ctx->prime), p); bn_mod_dig(&(ctx->mod8), &(ctx->prime), 8); switch (ctx->mod8) { case 3: case 7: ctx->qnr = -1; /* The current code for extensions of Fp^3 relies on qnr being * also a cubic non-residue. */ ctx->cnr = 0; break; case 1: case 5: ctx->qnr = ctx->cnr = -2; break; default: ctx->qnr = ctx->cnr = 0; THROW(ERR_NO_VALID); break; } #ifdef FP_QNRES if (ctx->mod8 != 3) { THROW(ERR_NO_VALID); } #endif #if FP_RDC == MONTY || !defined(STRIP) bn_mod_pre_monty(t, &(ctx->prime)); ctx->u = t->dp[0]; dv_zero(s, 2 * FP_DIGS); s[2 * FP_DIGS] = 1; dv_zero(q, 2 * FP_DIGS + 1); dv_copy(q, ctx->prime.dp, FP_DIGS); bn_divn_low(t->dp, ctx->conv.dp, s, 2 * FP_DIGS + 1, q, FP_DIGS); ctx->conv.used = FP_DIGS; bn_trim(&(ctx->conv)); bn_set_dig(&(ctx->one), 1); bn_lsh(&(ctx->one), &(ctx->one), ctx->prime.used * BN_DIGIT); bn_mod(&(ctx->one), &(ctx->one), &(ctx->prime)); #endif fp_prime_calc(); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { bn_free(t); dv_free(s); dv_free(q); } }
int main(void) { d_vector(int) v = DV_INIT; int *p; int int3[3] = {1,2,3}; int idx; check_int(v.size, 0); check(v.data == NULL); dv_reserve(&v, 1); check(v.data != NULL); check_int(v.size, 0); check(dv_reserved(v) >= 1); dv_free(v); dv_init(&v); check_int(v.size, 0); check(v.data == NULL); dv_resize(&v, 2); check_int(v.size, 2); check(v.data != NULL); v.data[0] = 50000; v.data[1] = 50000; dv_resize(&v, 1); check_int(v.size, 1); check_int(v.data[0], 50000); check_int(v.data[1], 0); dv_clear(&v); check_int(v.size, 0); check(v.data != NULL); check_int(v.data[0], 0); dv_resize(&v, 3); v.data[0] = 1; v.data[1] = 2; v.data[2] = 3; p = dv_insert_buffer(&v, 1, 2); check(p == &v.data[1]); check_int(v.size, 5); check_int(v.data[0], 1); check_int(v.data[1], 2); check_int(v.data[2], 3); check_int(v.data[3], 2); check_int(v.data[4], 3); dv_resize(&v, 2); p = dv_insert_zeroed(&v, 1, 2); check(p == &v.data[1]); check_int(v.size, 4); check_int(v.data[0], 1); check_int(v.data[1], 0); check_int(v.data[2], 0); check_int(v.data[3], 2); dv_resize(&v, 5); v.data[4] = 4; dv_resize(&v, 1); p = dv_append_buffer(&v, 4); check(p == &v.data[1]); check_int(v.size, 5); check_int(v.data[4], 4); dv_resize(&v, 5); v.data[4] = 4; dv_resize(&v, 1); p = dv_append_zeroed(&v, 4); check(p == &v.data[1]); check_int(v.size, 5); check_int(v.data[4], 0); dv_resize(&v, 1); v.data[0] = 64; dv_append2(&v, int3, 3); check_int(v.size, 4); check_int(v.data[0], 64); check_int(v.data[1], 1); check_int(v.data[2], 2); check_int(v.data[3], 3); dv_resize(&v, 1); dv_append1(&v, 56); check_int(v.size, 2); check_int(v.data[1], 56); check_int(v.data[2], 0); dv_resize(&v, 1); generations = 0; v.data[0] = 64; dv_append(&v, generate()); check_int(generations, 1); check_int(v.size, 4); check_int(v.data[0], 64); check_int(v.data[1], 1); check_int(v.data[2], 2); check_int(v.data[3], 3); dv_resize(&v, 1); v.data[0] = 64; dv_set2(&v, int3, 3); check_int(v.size, 3); check_int(v.data[0], 1); check_int(v.data[1], 2); check_int(v.data[2], 3); dv_resize(&v, 1); generations = 0; v.data[0] = 64; dv_set(&v, generate()); check_int(generations, 1); check_int(v.size, 3); check_int(v.data[0], 1); check_int(v.data[1], 2); check_int(v.data[2], 3); dv_resize(&v, 2); generations = 0; v.data[0] = 64; v.data[1] = 36; dv_insert(&v, 1, generate()); check_int(generations, 1); check_int(v.size, 5); check_int(v.data[0], 64); check_int(v.data[1], 1); check_int(v.data[2], 2); check_int(v.data[3], 3); check_int(v.data[4], 36); dv_resize(&v, 2); generations = 0; v.data[0] = 64; v.data[1] = 36; dv_insert2(&v, 1, int3, 3); check_int(v.size, 5); check_int(v.data[0], 64); check_int(v.data[1], 1); check_int(v.data[2], 2); check_int(v.data[3], 3); check_int(v.data[4], 36); dv_erase(&v, 1, 3); check_int(v.size, 2); check_int(v.data[0], 64); check_int(v.data[1], 36); dv_resize(&v, 3); v.data[0] = 45; v.data[1] = 35; v.data[2] = 45; dv_remove(&v, 45); check_int(v.size, 1); check_int(v.data[0], 35); dv_resize(&v, 3); v.data[0] = 45; v.data[1] = 35; v.data[2] = 45; dv_find(v, 35, &idx); check_int(idx, 1); dv_find(v, 55, &idx); check_int(idx, -1); dv_resize(&v, 3); v.data[0] = 1; v.data[1] = 2; v.data[2] = 3; check(dv_begins_with(v, generate())); check(dv_equals(v, generate())); check(dv_cmp(v, generate()) == 0); check(dv_ends_with(v, generate())); dv_resize(&v, 4); v.data[3] = 5; check(dv_begins_with(v, generate())); check(dv_cmp(v, generate()) > 0); check(!dv_ends_with(v, generate())); v.data[2] = 1; check(!dv_begins_with(v, generate())); check(dv_cmp(v, generate()) < 0); dv_resize(&v, 4); v.data[0] = 5; v.data[1] = 1; v.data[2] = 2; v.data[3] = 3; check(dv_ends_with(v, generate())); v.data[2] = 5; check(!dv_ends_with(v, generate())); return 0; }
struct Match* adbusI_serv_newmatch(const char* mstr, size_t len) { d_Vector(Argument) args; ZERO(&args); struct Match* m = (struct Match*) calloc(1, sizeof(struct Match) + len + 1); m->size = len; memcpy(m->data, mstr, len); const char* line = m->data; const char* end = m->data + len; while (line < end) { // Look for a key/value pair "<key>='<value>'," // Comma is optional for the last key/value pair const char* keyb = line; const char* keye = (const char*) memchr(keyb, '=', end - keyb); // keye needs to be followed by ' if (!keye || keye + 1 >= end || keye[1] != '\'') goto error; const char* valb = keye+2; const char* vale = (const char*) memchr(valb, '\'', end - valb); // vale is either the last character or followed by a , if (!vale || (vale + 1 != end && vale[1] != ',' )) goto error; line = vale + 2; #define MATCH(BEG, END, STR) \ ( END - BEG == sizeof(STR) - 1 \ && memcmp(BEG, STR, END - BEG) == 0) int argnum; if (MATCH(keyb, keye, "type")) { if (MATCH(valb, vale, "signal")) { m->type = ADBUS_MSG_SIGNAL; } else if (MATCH(valb, vale, "method_call")) { m->type = ADBUS_MSG_METHOD; } else if (MATCH(valb, vale, "method_return")) { m->type = ADBUS_MSG_RETURN; } else if (MATCH(valb, vale, "error")) { m->type = ADBUS_MSG_ERROR; } else { goto error; } } else if (MATCH(keyb, keye, "sender")) { m->sender = valb; m->senderSize = vale - valb; } else if (MATCH(keyb, keye, "interface")) { m->interface = valb; m->interfaceSize = vale - valb; } else if (MATCH(keyb, keye, "member")) { m->member = valb; m->memberSize = vale - valb; } else if (MATCH(keyb, keye, "path")) { m->path = valb; m->pathSize = vale - valb; } else if (MATCH(keyb, keye, "destination")) { m->destination = valb; m->destinationSize = vale - valb; } else if (IsArgKey(keyb, keye, &argnum)) { int toadd = argnum + 1 - (int) dv_size(&args); if (toadd > 0) { adbus_Argument* a = dv_push(Argument, &args, toadd); adbus_arg_init(a, toadd); } adbus_Argument* a = &dv_a(&args, argnum); a->value = valb; a->size = (int) (vale - valb); } } m->argumentsSize = dv_size(&args); m->arguments = dv_release(Argument, &args); return m; error: dv_free(Argument, &args); free(m); return NULL; }
void fp3_sqr_basic(fp3_t c, fp3_t a) { dv_t t0, t1, t2, t3, t4, t5; dv_null(t0); dv_null(t1); dv_null(t2); dv_null(t3); dv_null(t4); dv_null(t5); TRY { dv_new(t0); dv_new(t1); dv_new(t2); dv_new(t3); dv_new(t4); dv_new(t5); /* t0 = a_0^2. */ fp_sqrn_low(t0, a[0]); /* t1 = 2 * a_1 * a_2. */ fp_dbl(t2, a[1]); fp_muln_low(t1, t2, a[2]); /* t2 = a_2^2. */ fp_sqrn_low(t2, a[2]); /* t3 = (a_0 + a_2 + a_1)^2, t4 = (a_0 + a_2 - a_1)^2. */ fp_add(t3, a[0], a[2]); fp_add(t4, t3, a[1]); fp_sub(t5, t3, a[1]); fp_sqrn_low(t3, t4); fp_sqrn_low(t4, t5); /* t4 = (t4 + t3)/2. */ fp_addd_low(t4, t4, t3); fp_hlvd_low(t4, t4); /* t3 = t3 - t4 - t1. */ fp_addc_low(t5, t1, t4); fp_subc_low(t3, t3, t5); /* c_2 = t4 - t0 - t2. */ fp_addc_low(t5, t0, t2); fp_subc_low(t4, t4, t5); fp_rdc(c[2], t4); /* c_0 = t0 + t1 * B. */ fp_subc_low(t0, t0, t1); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_subc_low(t0, t0, t1); } fp_rdc(c[0], t0); /* c_1 = t3 + t2 * B. */ fp_subc_low(t3, t3, t2); for (int i = -1; i > fp_prime_get_cnr(); i--) { fp_subc_low(t3, t3, t2); } fp_rdc(c[1], t3); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t0); dv_free(t1); dv_free(t2); dv_free(t3); dv_free(t4); dv_free(t5); } }
/** * Multiplies two binary field elements using recursive Karatsuba * multiplication. * * @param[out] c - the result. * @param[in] a - the first binary field element. * @param[in] b - the second binary field element. * @param[in] size - the number of digits to multiply. * @param[in] level - the number of Karatsuba steps to apply. */ static void fb_mul_karat_imp(dv_t c, const fb_t a, const fb_t b, int size, int level) { int i, h, h1; dv_t a1, b1, ab; dig_t *a0b0, *a1b1; dv_null(a1); dv_null(b1); dv_null(ab); /* Compute half the digits of a or b. */ h = size >> 1; h1 = size - h; TRY { /* Allocate the temp variables. */ dv_new(a1); dv_new(b1); dv_new(ab); a0b0 = ab; a1b1 = ab + 2 * h; /* a0b0 = a0 * b0 and a1b1 = a1 * b1 */ if (level <= 1) { #if FB_MUL == BASIC fb_mul_basic_imp(a0b0, a, b, h); fb_mul_basic_imp(a1b1, a + h, b + h, h1); #elif FB_MUL == LCOMB fb_mul_lcomb_imp(a0b0, a, b, h); fb_mul_lcomb_imp(a1b1, a + h, b + h, h1); #elif FB_MUL == RCOMB fb_mul_rcomb_imp(a0b0, a, b, h); fb_mul_rcomb_imp(a1b1, a + h, b + h, h1); #elif FB_MUL == INTEG || FB_MUL == LODAH fb_muld_low(a0b0, a, b, h); fb_muld_low(a1b1, a + h, b + h, h1); #endif } else { fb_mul_karat_imp(a0b0, a, b, h, level - 1); fb_mul_karat_imp(a1b1, a + h, b + h, h1, level - 1); } for (i = 0; i < 2 * size; i++) { c[i] = ab[i]; } /* c = c - (a0*b0 << h digits) */ fb_addd_low(c + h, c + h, a0b0, 2 * h); /* c = c - (a1*b1 << h digits) */ fb_addd_low(c + h, c + h, a1b1, 2 * h1); /* a1 = (a1 + a0) */ fb_addd_low(a1, a, a + h, h); /* b1 = (b1 + b0) */ fb_addd_low(b1, b, b + h, h); if (h1 > h) { a1[h1 - 1] = a[h + h1 - 1]; b1[h1 - 1] = b[h + h1 - 1]; } if (level <= 1) { /* a1b1 = (a1 + a0)*(b1 + b0) */ #if FB_MUL == BASIC fb_mul_basic_imp(a1b1, a1, b1, h1); #elif FB_MUL == LCOMB fb_mul_lcomb_imp(a1b1, a1, b1, h1); #elif FB_MUL == RCOMB fb_mul_rcomb_imp(a1b1, a1, b1, h1); #elif FB_MUL == INTEG || FB_MUL == LODAH fb_muld_low(a1b1, a1, b1, h1); #endif } else { fb_mul_karat_imp(a1b1, a1, b1, h1, level - 1); } /* c = c + [(a1 + a0)*(b1 + b0) << digits] */ fb_addd_low(c + h, c + h, a1b1, 2 * h1); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(a1); dv_free(b1); dv_free(ab); } }
/** * Multiplies two prime field elements using recursive Karatsuba * multiplication. * * @param[out] c - the result. * @param[in] a - the first prime field element. * @param[in] b - the second prime field element. * @param[in] size - the number of digits to multiply. * @param[in] level - the number of Karatsuba steps to apply. */ static void fp_mul_karat_imp(dv_t c, const fp_t a, const fp_t b, int size, int level) { int i, h, h1; dv_t a1, b1, a0b0, a1b1, t; dig_t carry; /* Compute half the digits of a or b. */ h = size >> 1; h1 = size - h; dv_null(a1); dv_null(b1); dv_null(a0b0); dv_null(a1b1); TRY { /* Allocate the temp variables. */ dv_new(a1); dv_new(b1); dv_new(a0b0); dv_new(a1b1); dv_new(t); dv_zero(a1, h1 + 1); dv_zero(b1, h1 + 1); dv_zero(a0b0, 2 * h); dv_zero(a1b1, 2 * h1); dv_zero(t, 2 * h1 + 1); /* a0b0 = a0 * b0 and a1b1 = a1 * b1 */ if (level <= 1) { #if FP_MUL == BASIC for (i = 0; i < h; i++) { carry = bn_mula_low(a0b0 + i, a, *(b + i), h); *(a0b0 + i + h) = carry; } for (i = 0; i < h1; i++) { carry = bn_mula_low(a1b1 + i, a + h, *(b + h + i), h1); *(a1b1 + i + h1) = carry; } #elif FP_MUL == COMBA || FP_MUL == INTEG bn_muln_low(a0b0, a, b, h); bn_muln_low(a1b1, a + h, b + h, h1); #endif } else { fp_mul_karat_imp(a0b0, a, b, h, level - 1); fp_mul_karat_imp(a1b1, a + h, b + h, h1, level - 1); } for (i = 0; i < 2 * h; i++) { c[i] = a0b0[i]; } for (i = 0; i < 2 * h1 + 1; i++) { c[2 * h + i] = a1b1[i]; } /* a1 = (a1 + a0) */ carry = bn_addn_low(a1, a, a + h, h); bn_add1_low(a1 + h, a1 + h, carry, 2); if (h1 > h) { bn_add1_low(a1 + h, a1 + h, *(a + 2 * h), 2); } /* b1 = (b1 + b0) */ carry = bn_addn_low(b1, b, b + h, h); bn_add1_low(b1 + h, b1 + h, carry, 2); if (h1 > h) { bn_add1_low(b1 + h, b1 + h, *(b + 2 * h), 2); } if (level <= 1) { /* t = (a1 + a0)*(b1 + b0) */ #if FP_MUL == BASIC for (i = 0; i < h1 + 1; i++) { carry = bn_mula_low(t + i, a1, *(b1 + i), h1 + 1); *(t + i + h1 + 1) = carry; } #elif FP_MUL == COMBA || FP_MUL == INTEG bn_muln_low(t, a1, b1, h1 + 1); #endif } else { fp_mul_karat_imp(t, a1, b1, h1 + 1, level - 1); } /* t = t - (a0*b0 << h digits) */ carry = bn_subn_low(t, t, a0b0, 2 * h); bn_sub1_low(t + 2 * h, t + 2 * h, carry, 2 * (h1 + 1) - 2 * h); /* t = t - (a1*b1 << h digits) */ carry = bn_subn_low(t, t, a1b1, 2 * h1); bn_sub1_low(t + 2 * h1, t + 2 * h1, carry, 2 * (h1 + 1) - 2 * h1); /* c = c + [(a1 + a0)*(b1 + b0) << digits] */ c += h; carry = bn_addn_low(c, c, t, 2 * (h1 + 1)); c += 2 * (h1 + 1); bn_add1_low(c, c, carry, 2 * size - h - 2 * (h1 + 1)); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(a1); dv_free(b1); dv_free(a0b0); dv_free(a1b1); dv_free(t); } }
static void draw_dvdag_infotag(cairo_t *cr, dv_dag_node_t *node) { double line_height = 12; double padding = 4; int n = 6; /* number of lines */ double xx = node->vl->c + DV_RADIUS + padding; double yy = node->c - DV_RADIUS - 2*padding - line_height * (n - 1); // Cover rectangle double width = 450.0; double height = n * line_height + 3*padding; draw_rounded_rectangle(cr, node->vl->c + DV_RADIUS, node->c - DV_RADIUS - height, width, height); // Lines cairo_set_source_rgb(cr, 1.0, 1.0, 0.1); cairo_select_font_face(cr, "Courier", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size(cr, 12); // Line 1 /* TODO: adaptable string length */ char *s = (char *) dv_malloc( DV_STRING_LENGTH * sizeof(char) ); sprintf(s, "[%ld] %s lv=%d f=%d%d%d%d dc=%0.1lf c=%0.1lf", node - G->T, NODE_KIND_NAMES[node->pi->info.kind], node->lv, dv_is_union(node), dv_is_shrinked(node), dv_is_expanding(node), dv_is_shrinking(node), node->dc, node->c); cairo_move_to(cr, xx, yy); cairo_show_text(cr, s); yy += line_height; // Line 2 sprintf(s, "%llu-%llu (%llu) est=%llu", node->pi->info.start.t, node->pi->info.end.t, node->pi->info.start.t - node->pi->info.end.t, node->pi->info.est); cairo_move_to(cr, xx, yy); cairo_show_text(cr, s); yy += line_height; // Line 3 sprintf(s, "T=%llu/%llu,nodes=%ld/%ld/%ld,edges=%ld/%ld/%ld/%ld", node->pi->info.t_1, node->pi->info.t_inf, node->pi->info.logical_node_counts[dr_dag_node_kind_create_task], node->pi->info.logical_node_counts[dr_dag_node_kind_wait_tasks], node->pi->info.logical_node_counts[dr_dag_node_kind_end_task], node->pi->info.logical_edge_counts[dr_dag_edge_kind_end], node->pi->info.logical_edge_counts[dr_dag_edge_kind_create], node->pi->info.logical_edge_counts[dr_dag_edge_kind_create_cont], node->pi->info.logical_edge_counts[dr_dag_edge_kind_wait_cont]); cairo_move_to(cr, xx, yy); cairo_show_text(cr, s); yy += line_height; // Line 4 sprintf(s, "by worker %d on cpu %d", node->pi->info.worker, node->pi->info.cpu); cairo_move_to(cr, xx, yy); cairo_show_text(cr, s); yy += line_height; // Line 5 dv_free(s, DV_STRING_LENGTH * sizeof(char)); const char *ss = P->S->C + P->S->I[node->pi->info.start.pos.file_idx]; s = (char *) dv_malloc( strlen(ss) + 10 ); sprintf(s, "%s:%ld", ss, node->pi->info.start.pos.line); cairo_move_to(cr, xx, yy); cairo_show_text(cr, s); yy += line_height; // Line 6 dv_free(s, strlen(ss) + 10); ss = P->S->C + P->S->I[node->pi->info.end.pos.file_idx]; s = (char *) dv_malloc( strlen(ss) + 10 ); sprintf(s, "%s:%ld", ss, node->pi->info.end.pos.line); cairo_move_to(cr, xx, yy); cairo_show_text(cr, s); yy += line_height; dv_free(s, strlen(ss) + 10); }
void pp_dbl_k2_projc_lazyr(fp2_t l, ep_t r, ep_t p, ep_t q) { fp_t t0, t1, t2, t3, t4, t5; dv_t u0, u1; fp_null(t0); fp_null(t1); fp_null(t2); fp_null(t3); fp_null(t4); fp_null(t5); dv_null(u0); dv_null(u1); TRY { fp_new(t0); fp_new(t1); fp_new(t2); fp_new(t3); fp_new(t4); fp_new(t5); dv_new(u0); dv_new(u1); /* For these curves, we always can choose a = -3. */ /* dbl-2001-b formulas: 3M + 5S + 8add + 1*4 + 2*8 + 1*3 */ /* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b */ /* t0 = delta = z1^2. */ fp_sqr(t0, p->z); /* t1 = gamma = y1^2. */ fp_sqr(t1, p->y); /* t2 = beta = x1 * y1^2. */ fp_mul(t2, p->x, t1); /* t3 = alpha = 3 * (x1 - z1^2) * (x1 + z1^2). */ fp_sub(t3, p->x, t0); fp_add(t4, p->x, t0); fp_mul(t4, t3, t4); fp_dbl(t3, t4); fp_add(t3, t3, t4); /* t2 = 4 * beta. */ fp_dbl(t2, t2); fp_dbl(t2, t2); /* z3 = (y1 + z1)^2 - gamma - delta. */ fp_add(r->z, p->y, p->z); fp_sqr(r->z, r->z); fp_sub(r->z, r->z, t1); fp_sub(r->z, r->z, t0); /* l0 = 2 * gamma - alpha * (delta * xq + x1). */ fp_dbl(t1, t1); fp_mul(t5, t0, q->x); fp_add(t5, t5, p->x); fp_mul(t5, t5, t3); fp_sub(l[0], t1, t5); /* x3 = alpha^2 - 8 * beta. */ fp_dbl(t5, t2); fp_sqr(r->x, t3); fp_sub(r->x, r->x, t5); /* y3 = alpha * (4 * beta - x3) - 8 * gamma^2. */ fp_sqrn_low(u0, t1); fp_addc_low(u0, u0, u0); fp_subm_low(r->y, t2, r->x); fp_muln_low(u1, r->y, t3); fp_subc_low(u1, u1, u0); fp_rdcn_low(r->y, u1); /* l1 = - z3 * delta * yq. */ fp_mul(l[1], r->z, t0); fp_mul(l[1], l[1], q->y); r->norm = 0; } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { fp_free(t0); fp_free(t1); fp_free(t2); fp_free(t3); fp_free(t4); fp_free(t5); dv_free(u0); dv_free(u1); } }
/** * Computes the square of a multiple precision integer using recursive Karatsuba * squaring. * * @param[out] c - the result. * @param[in] a - the prime field element to square. * @param[in] size - the number of digits to square. * @param[in] level - the number of Karatsuba steps to apply. */ static void fp_sqr_karat_imp(dv_t c, const fp_t a, int size, int level) { int i, h, h1; dv_t t0, t1, a0a0, a1a1; dig_t carry; /* Compute half the digits of a or b. */ h = size >> 1; h1 = size - h; dv_null(t0); dv_null(t1); dv_null(a0a0); dv_null(a1a1); TRY { /* Allocate the temp variables. */ dv_new(t0); dv_new(t1); dv_new(a0a0); dv_new(a1a1); dv_zero(t0, 2 * h1); dv_zero(t1, 2 * (h1 + 1)); dv_zero(a0a0, 2 * h); dv_zero(a1a1, 2 * h1); if (level <= 1) { /* a0a0 = a0 * a0 and a1a1 = a1 * a1 */ #if FP_SQR == BASIC for (i = 0; i < h; i++) { bn_sqra_low(a0a0 + (2 * i), a + i, h - i); } for (i = 0; i < h1; i++) { bn_sqra_low(a1a1 + (2 * i), a + h + i, h1 - i); } #elif FP_SQR == COMBA || FP_SQR == INTEG bn_sqrn_low(a0a0, a, h); bn_sqrn_low(a1a1, a + h, h1); #elif FP_SQR == MULTP bn_muln_low(a0a0, a, a, h); bn_muln_low(a1a1, a + h, a + h, h1); #endif } else { fp_sqr_karat_imp(a0a0, a, h, level - 1); fp_sqr_karat_imp(a1a1, a + h, h1, level - 1); } /* t2 = a1 * a1 << 2*h digits + a0 * a0. */ for (i = 0; i < 2 * h; i++) { c[i] = a0a0[i]; } for (i = 0; i < 2 * h1; i++) { c[2 * h + i] = a1a1[i]; } /* t = (a1 + a0) */ carry = bn_addn_low(t0, a, a + h, h); carry = bn_add1_low(t0 + h, t0 + h, carry, 2); if (h1 > h) { carry = bn_add1_low(t0 + h, t0 + h, *(a + 2 * h), 2); } if (level <= 1) { /* a1a1 = (a1 + a0)*(a1 + a0) */ #if FP_SQR == BASIC for (i = 0; i < h1 + 1; i++) { bn_sqra_low(t1 + (2 * i), t0 + i, h1 + 1 - i); } #elif FP_SQR == COMBA || FP_SQR == INTEG bn_sqrn_low(t1, t0, h1 + 1); #elif FP_SQR == MULTP bn_muln_low(t1, t0, t0, h1 + 1); #endif } else { fp_sqr_karat_imp(t1, t0, h1 + 1, level - 1); } /* t = t - (a0*a0 << h digits) */ carry = bn_subn_low(t1, t1, a0a0, 2 * h); bn_sub1_low(t1 + 2 * h, t1 + 2 * h, carry, 2 * (h1 + 1) - 2 * h); /* t = t - (a1*a1 << h digits) */ carry = bn_subn_low(t1, t1, a1a1, 2 * h1); bn_sub1_low(t1 + 2 * h, t1 + 2 * h, carry, 2 * (h1 + 1) - 2 * h); /* c = c + [(a1 + a0)*(a1 + a0) << digits] */ c += h; carry = bn_addn_low(c, c, t1, 2 * (h1 + 1)); c += 2 * (h1 + 1); carry = bn_add1_low(c, c, carry, 2 * size - h - 2 * (h1 + 1)); } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { dv_free(t0); dv_free(t1); dv_free(a0a0); dv_free(a1a1); } }