virtual BOOL on_mouse(HELEMENT he, HELEMENT target, UINT event_type, POINT pt, UINT mouseButtons, UINT keyboardStates ) { if( mouseButtons != MAIN_MOUSE_BUTTON ) return false; if( event_type == MOUSE_UP) { dom::element el = he; el.release_capture(); return true; } if( event_type != MOUSE_MOVE && event_type != MOUSE_DOWN) return false; // mouse moved and pressed // el is our splitter element; dom::element splitter_el = he; dom::element parent_element = splitter_el.parent(); // what kind of splitter do we have? bool horizontal = equal(parent_element.get_style_attribute("flow"),L"horizontal"); dom::element first = splitter_el.prev_sibling(); dom::element second = splitter_el.next_sibling(); if(!first.is_valid() || !second.is_valid()) return false; // nothing to do bool need_update = horizontal? do_horizontal(event_type, pt, splitter_el,first,second, parent_element): do_vertical(event_type, pt,splitter_el,first,second, parent_element); if(need_update && event_type == MOUSE_MOVE) parent_element.update(true); //done! update changes on the view return true; // it is ours - stop event bubbling }
static void miller(element_t res, element_t P, element_ptr QR, element_ptr R, int n) { // Collate divisions. mp_bitcnt_t m; element_t v, vd; element_t Z; element_t a, b, c; const element_ptr cca = curve_a_coeff(P); const element_ptr Px = curve_x_coord(P); const element_ptr Py = curve_y_coord(P); element_t e0, e1; mpz_t q; element_ptr Zx, Zy; const element_ptr numx = curve_x_coord(QR); const element_ptr numy = curve_y_coord(QR); const element_ptr denomx = curve_x_coord(R); const element_ptr denomy = curve_y_coord(R); #define do_vertical(e, edenom) { \ element_sub(e0, numx, Zx); \ element_mul((e), (e), e0); \ \ element_sub(e0, denomx, Zx); \ element_mul((edenom), (edenom), e0); \ } #define do_tangent(e, edenom) { \ /*a = -slope_tangent(A.x, A.y); \ b = 1; \ c = -(A.y + a * A.x); \ but we multiply by 2*A.y to avoid division*/ \ \ /*a = -Ax * (Ax + Ax + Ax + twicea_2) - a_4; \ Common curves: a2 = 0 (and cc->a is a_4), so \ a = -(3 Ax^2 + cc->a) \ b = 2 * Ay \ c = -(2 Ay^2 + a Ax); */ \ \ if (element_is0(Zy)) { \ do_vertical((e), (edenom)); \ } else { \ element_square(a, Zx); \ element_mul_si(a, a, 3); \ element_add(a, a, cca); \ element_neg(a, a); \ \ element_add(b, Zy, Zy); \ \ element_mul(e0, b, Zy); \ element_mul(c, a, Zx); \ element_add(c, c, e0); \ element_neg(c, c); \ \ element_mul(e0, a, numx); \ element_mul(e1, b, numy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul((e), (e), e0); \ \ element_mul(e0, a, denomx); \ element_mul(e1, b, denomy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul((edenom), (edenom), e0); \ } \ } #define do_line(e, edenom) { \ if (!element_cmp(Zx, Px)) { \ if (!element_cmp(Zy, Py)) { \ do_tangent(e, edenom); \ } else { \ do_vertical(e, edenom); \ } \ } else { \ element_sub(b, Px, Zx); \ element_sub(a, Zy, Py); \ element_mul(c, Zx, Py); \ element_mul(e0, Zy, Px); \ element_sub(c, c, e0); \ \ element_mul(e0, a, numx); \ element_mul(e1, b, numy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(e, e, e0); \ \ element_mul(e0, a, denomx); \ element_mul(e1, b, denomy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(edenom, edenom, e0); \ } \ } element_init(a, res->field); element_init(b, res->field); element_init(c, res->field); element_init(e0, res->field); element_init(e1, res->field); element_init(v, res->field); element_init(vd, res->field); element_init(Z, P->field); element_set(Z, P); Zx = curve_x_coord(Z); Zy = curve_y_coord(Z); element_set1(v); element_set1(vd); mpz_init(q); mpz_set_ui(q, n); m = (mp_bitcnt_t)mpz_sizeinbase(q, 2); m = (m > 2 ? m - 2 : 0); for (;;) { element_square(v, v); element_square(vd, vd); do_tangent(v, vd); element_double(Z, Z); do_vertical(vd, v); if (mpz_tstbit(q, m)) { do_line(v, vd); element_add(Z, Z, P); if (m) { do_vertical(vd, v); } } if (!m) break; m--; } mpz_clear(q); element_invert(vd, vd); element_mul(res, v, vd); element_clear(v); element_clear(vd); element_clear(Z); element_clear(a); element_clear(b); element_clear(c); element_clear(e0); element_clear(e1); #undef do_vertical #undef do_tangent #undef do_line }
static void e_miller_proj(element_t res, element_t P, element_ptr QR, element_ptr R, e_pairing_data_ptr p) { //collate divisions int n; element_t v, vd; element_t v1, vd1; element_t Z, Z1; element_t a, b, c; const element_ptr cca = curve_a_coeff(P); element_t e0, e1; const element_ptr e2 = a, e3 = b; element_t z, z2; int i; element_ptr Zx, Zy; const element_ptr Px = curve_x_coord(P); const element_ptr numx = curve_x_coord(QR); const element_ptr numy = curve_y_coord(QR); const element_ptr denomx = curve_x_coord(R); const element_ptr denomy = curve_y_coord(R); //convert Z from weighted projective (Jacobian) to affine //i.e. (X, Y, Z) --> (X/Z^2, Y/Z^3) //also sets z to 1 #define to_affine() { \ element_invert(z, z); \ element_square(e0, z); \ element_mul(Zx, Zx, e0); \ element_mul(e0, e0, z); \ element_mul(Zy, Zy, e0); \ element_set1(z); \ element_set1(z2); \ } #define proj_double() { \ const element_ptr x = Zx; \ const element_ptr y = Zy; \ /* e0 = 3x^2 + (cc->a) z^4 */ \ element_square(e0, x); \ /* element_mul_si(e0, e0, 3); */ \ element_double(e1, e0); \ element_add(e0, e0, e1); \ element_square(e1, z2); \ element_mul(e1, e1, cca); \ element_add(e0, e0, e1); \ \ /* z_out = 2 y z */ \ element_mul(z, y, z); \ /* element_mul_si(z, z, 2); */ \ element_double(z, z); \ element_square(z2, z); \ \ /* e1 = 4 x y^2 */ \ element_square(e2, y); \ element_mul(e1, x, e2); \ /* element_mul_si(e1, e1, 4); */ \ element_double(e1, e1); \ element_double(e1, e1); \ \ /* x_out = e0^2 - 2 e1 */ \ /* element_mul_si(e3, e1, 2); */ \ element_double(e3, e1); \ element_square(x, e0); \ element_sub(x, x, e3); \ \ /* e2 = 8y^4 */ \ element_square(e2, e2); \ /* element_mul_si(e2, e2, 8); */ \ element_double(e2, e2); \ element_double(e2, e2); \ element_double(e2, e2); \ \ /* y_out = e0(e1 - x_out) - e2 */ \ element_sub(e1, e1, x); \ element_mul(e0, e0, e1); \ element_sub(y, e0, e2); \ } #define do_tangent(e, edenom) { \ /* a = -(3x^2 + cca z^4) */ \ /* b = 2 y z^3 */ \ /* c = -(2 y^2 + x a) */ \ /* a = z^2 a */ \ element_square(a, z2); \ element_mul(a, a, cca); \ element_square(b, Zx); \ /* element_mul_si(b, b, 3); */ \ element_double(e0, b); \ element_add(b, b, e0); \ element_add(a, a, b); \ element_neg(a, a); \ \ /* element_mul_si(e0, Zy, 2); */ \ element_double(e0, Zy); \ element_mul(b, e0, z2); \ element_mul(b, b, z); \ \ element_mul(c, Zx, a); \ element_mul(a, a, z2); \ element_mul(e0, e0, Zy); \ element_add(c, c, e0); \ element_neg(c, c); \ \ element_mul(e0, a, numx); \ element_mul(e1, b, numy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(e, e, e0); \ \ element_mul(e0, a, denomx); \ element_mul(e1, b, denomy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(edenom, edenom, e0); \ } #define do_vertical(e, edenom, Ax) { \ element_mul(e0, numx, z2); \ element_sub(e0, e0, Ax); \ element_mul(e, e, e0); \ \ element_mul(e0, denomx, z2); \ element_sub(e0, e0, Ax); \ element_mul(edenom, edenom, e0); \ } #define do_line(e, edenom, A, B) { \ element_ptr Ax = curve_x_coord(A); \ element_ptr Ay = curve_y_coord(A); \ element_ptr Bx = curve_x_coord(B); \ element_ptr By = curve_y_coord(B); \ \ element_sub(b, Bx, Ax); \ element_sub(a, Ay, By); \ element_mul(c, Ax, By); \ element_mul(e0, Ay, Bx); \ element_sub(c, c, e0); \ \ element_mul(e0, a, numx); \ element_mul(e1, b, numy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(e, e, e0); \ \ element_mul(e0, a, denomx); \ element_mul(e1, b, denomy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(edenom, edenom, e0); \ } element_init(a, res->field); element_init(b, res->field); element_init(c, res->field); element_init(e0, res->field); element_init(e1, res->field); element_init(z, res->field); element_init(z2, res->field); element_set1(z); element_set1(z2); element_init(v, res->field); element_init(vd, res->field); element_init(v1, res->field); element_init(vd1, res->field); element_init(Z, P->field); element_init(Z1, P->field); element_set(Z, P); Zx = curve_x_coord(Z); Zy = curve_y_coord(Z); element_set1(v); element_set1(vd); element_set1(v1); element_set1(vd1); n = p->exp1; for (i=0; i<n; i++) { element_square(v, v); element_square(vd, vd); do_tangent(v, vd); proj_double(); do_vertical(vd, v, Zx); } to_affine(); if (p->sign1 < 0) { element_set(v1, vd); element_set(vd1, v); do_vertical(vd1, v1, Zx); element_neg(Z1, Z); } else { element_set(v1, v); element_set(vd1, vd); element_set(Z1, Z); } n = p->exp2; for (; i<n; i++) { element_square(v, v); element_square(vd, vd); do_tangent(v, vd); proj_double(); do_vertical(vd, v, Zx); } to_affine(); element_mul(v, v, v1); element_mul(vd, vd, vd1); do_line(v, vd, Z, Z1); element_add(Z, Z, Z1); do_vertical(vd, v, Zx); if (p->sign0 > 0) { do_vertical(v, vd, Px); } element_invert(vd, vd); element_mul(res, v, vd); element_clear(v); element_clear(vd); element_clear(v1); element_clear(vd1); element_clear(z); element_clear(z2); element_clear(Z); element_clear(Z1); element_clear(a); element_clear(b); element_clear(c); element_clear(e0); element_clear(e1); #undef to_affine #undef proj_double #undef do_tangent #undef do_vertical #undef do_line }
//TODO: the following code is useless as the Tate pairing is degenerate on singular curves static void sn_miller(element_t res, mpz_t q, element_t P, element_ptr Qx, element_ptr Qy) { //collate divisions int m; element_t v, vd; element_t Z; element_t a, b, c; element_t e0, e1; element_ptr Zx; element_ptr Zy; const element_ptr Px = curve_x_coord(P); const element_ptr Py = curve_y_coord(P); #define do_vertical(e) \ element_sub(e0, Qx, Zx); \ element_mul(e, e, e0); //a = -slope_tangent(Z.x, Z.y); //b = 1; //c = -(Z.y + a * Z.x); //but we multiply by 2*Z.y to avoid division //a = -Zx * (Zx + Zx + Zx + 2) //b = 2 * Zy //c = -(2 Zy^2 + a Zx); #define do_tangent(e) \ element_double(e0, Zx); \ element_add(a, Zx, e0); \ element_set_si(e0, 2); \ element_add(a, a, e0); \ element_mul(a, a, Zx); \ element_neg(a, a); \ element_add(b, Zy, Zy); \ element_mul(e0, b, Zy); \ element_mul(c, a, Zx); \ element_add(c, c, e0); \ element_neg(c, c); \ element_mul(e0, a, Qx); \ element_mul(e1, b, Qy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(e, e, e0); //a = -(B.y - A.y) / (B.x - A.x); //b = 1; //c = -(A.y + a * A.x); //but we'll multiply by B.x - A.x to avoid division #define do_line(e) \ element_sub(b, Px, Zx); \ element_sub(a, Zy, Py); \ element_mul(e0, b, Zy); \ element_mul(c, a, Zx); \ element_add(c, c, e0); \ element_neg(c, c); \ element_mul(e0, a, Qx); \ element_mul(e1, b, Qy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(e, e, e0); element_init(a, Px->field); element_init(b, Px->field); element_init(c, Px->field); element_init(e0, res->field); element_init(e1, res->field); element_init(v, res->field); element_init(vd, res->field); element_init(Z, P->field); element_set(Z, P); Zx = curve_x_coord(Z); Zy = curve_y_coord(Z); element_set1(v); element_set1(vd); m = mpz_sizeinbase(q, 2) - 2; while(m >= 0) { element_mul(v, v, v); element_mul(vd, vd, vd); do_tangent(v); element_double(Z, Z); do_vertical(vd); if (mpz_tstbit(q, m)) { do_line(v); element_add(Z, Z, P); do_vertical(vd); } m--; } #undef do_tangent #undef do_vertical #undef do_line element_invert(vd, vd); element_mul(res, v, vd); element_clear(v); element_clear(vd); element_clear(Z); element_clear(a); element_clear(b); element_clear(c); element_clear(e0); element_clear(e1); }
static void e_miller_affine(element_t res, element_t P, element_ptr QR, element_ptr R, e_pairing_data_ptr p) { //collate divisions int n; element_t v, vd; element_t v1, vd1; element_t Z, Z1; element_t a, b, c; element_t e0, e1; const element_ptr Px = curve_x_coord(P); const element_ptr cca = curve_a_coeff(P); element_ptr Zx, Zy; int i; const element_ptr numx = curve_x_coord(QR); const element_ptr numy = curve_y_coord(QR); const element_ptr denomx = curve_x_coord(R); const element_ptr denomy = curve_y_coord(R); #define do_vertical(e, edenom, Ax) { \ element_sub(e0, numx, Ax); \ element_mul(e, e, e0); \ \ element_sub(e0, denomx, Ax); \ element_mul(edenom, edenom, e0); \ } #define do_tangent(e, edenom) { \ /* a = -slope_tangent(A.x, A.y); */ \ /* b = 1; */ \ /* c = -(A.y + a * A.x); */ \ /* but we multiply by 2*A.y to avoid division */ \ \ /* a = -Ax * (Ax + Ax + Ax + twicea_2) - a_4; */ \ /* Common curves: a2 = 0 (and cc->a is a_4), so */ \ /* a = -(3 Ax^2 + cc->a) */ \ /* b = 2 * Ay */ \ /* c = -(2 Ay^2 + a Ax); */ \ \ element_square(a, Zx); \ element_mul_si(a, a, 3); \ element_add(a, a, cca); \ element_neg(a, a); \ \ element_add(b, Zy, Zy); \ \ element_mul(e0, b, Zy); \ element_mul(c, a, Zx); \ element_add(c, c, e0); \ element_neg(c, c); \ \ element_mul(e0, a, numx); \ element_mul(e1, b, numy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(e, e, e0); \ \ element_mul(e0, a, denomx); \ element_mul(e1, b, denomy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(edenom, edenom, e0); \ } #define do_line(e, edenom, A, B) { \ element_ptr Ax = curve_x_coord(A); \ element_ptr Ay = curve_y_coord(A); \ element_ptr Bx = curve_x_coord(B); \ element_ptr By = curve_y_coord(B); \ \ element_sub(b, Bx, Ax); \ element_sub(a, Ay, By); \ element_mul(c, Ax, By); \ element_mul(e0, Ay, Bx); \ element_sub(c, c, e0); \ \ element_mul(e0, a, numx); \ element_mul(e1, b, numy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(e, e, e0); \ \ element_mul(e0, a, denomx); \ element_mul(e1, b, denomy); \ element_add(e0, e0, e1); \ element_add(e0, e0, c); \ element_mul(edenom, edenom, e0); \ } element_init(a, res->field); element_init(b, res->field); element_init(c, res->field); element_init(e0, res->field); element_init(e1, res->field); element_init(v, res->field); element_init(vd, res->field); element_init(v1, res->field); element_init(vd1, res->field); element_init(Z, P->field); element_init(Z1, P->field); element_set(Z, P); Zx = curve_x_coord(Z); Zy = curve_y_coord(Z); element_set1(v); element_set1(vd); element_set1(v1); element_set1(vd1); n = p->exp1; for (i=0; i<n; i++) { element_square(v, v); element_square(vd, vd); do_tangent(v, vd); element_double(Z, Z); do_vertical(vd, v, Zx); } if (p->sign1 < 0) { element_set(v1, vd); element_set(vd1, v); do_vertical(vd1, v1, Zx); element_neg(Z1, Z); } else { element_set(v1, v); element_set(vd1, vd); element_set(Z1, Z); } n = p->exp2; for (; i<n; i++) { element_square(v, v); element_square(vd, vd); do_tangent(v, vd); element_double(Z, Z); do_vertical(vd, v, Zx); } element_mul(v, v, v1); element_mul(vd, vd, vd1); do_line(v, vd, Z, Z1); element_add(Z, Z, Z1); do_vertical(vd, v, Zx); if (p->sign0 > 0) { do_vertical(v, vd, Px); } element_invert(vd, vd); element_mul(res, v, vd); element_clear(v); element_clear(vd); element_clear(v1); element_clear(vd1); element_clear(Z); element_clear(Z1); element_clear(a); element_clear(b); element_clear(c); element_clear(e0); element_clear(e1); #undef do_vertical #undef do_tangent #undef do_line }