Exemplo n.º 1
0
int sp_removeFirst(SortedPoints *sp, Point *ret)
{

	if (sp->size == 0 || sp->used[0] == 'n')
	{
		if (NULL != ret)
			point_set(ret, 0.0, 0.0);

		return 0;
	}

	else
	{

		int i;

		sp->size = sp->size - 1;

		if (NULL != ret)
			*ret = sp->pointArray[0];

		sp->used[sp->size] = 'n';

		for (i = 0; i < sp->size; i++)
		{
			sp->pointArray[i] = sp->pointArray[i+1];
		}

		point_set(&(sp->pointArray[sp->size]), 0.0, 0.0);
		//free(&(sp->pointArray)[0]);
		return 1;
	}
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
  Point p1;
  Point *p2 = malloc(sizeof(Point));
  assert(p2);
  double distance;
  
  point_set(&p1, 1.0, 1.0);
  point_set(p2, 1.0, 1.0);
  
  distance = point_distance(&p1, p2);
  
  point_translate(&p1, 1.0, 0.0);
  
  distance = point_distance(&p1, p2);

  point_set(&p1, 0.0, 0.0);
  point_set(p2, 3.0, 4.0);
  
  distance = point_distance(&p1, p2);
  
  printf("%f\n", distance);

  free(p2);
  p2 = NULL;

  printf("OK\n");
  return 0;
 }
Exemplo n.º 3
0
int sp_removeLast(SortedPoints *sp, Point *ret)
{
	if (sp->size == 0)
	{
		if (NULL != ret)
			point_set(ret, 0.0, 0.0);

		return 0;
	}

	else if (sp->used[sp->size - 1] == 'n')
	{
		if (NULL != ret)
			point_set(ret, 0.0, 0.0);
		return 0;
	}

	else
	{

		sp->size = sp->size - 1;

		if (NULL != ret)
			*ret = sp->pointArray[sp->size];

		point_set(&(sp->pointArray[sp->size]), 0.0, 0.0);
		//free(&(sp->pointArray[sp->size]));
		sp->used[sp->size] = 'n';
		return 1;
	}
}
Exemplo n.º 4
0
		//-----------------------------------------------------------------//
		void line(int16_t x1, int16_t y1, int16_t x2, int16_t y2, bool c) {
			int16_t dx;
			int16_t dy;
			int16_t sx;
			int16_t sy;
			if(x2 >= x1) { dx = x2 - x1; sx = 1; } else { dx = x1 - x2; sx = -1; }
			if(y2 >= y1) { dy = y2 - y1; sy = 1; } else { dy = y1 - y2; sy = -1; }

			int16_t m = 0;
			int16_t x = x1;
			int16_t y = y1;
			if(dx > dy) {
				for(int16_t i = 0; i <= dx; i++) {
					if(c) point_set(x, y);
					else point_reset(x, y);
					m += dy;
					if(m >= dx) {
						m -= dx;
						y += sy;
					}
					x += sx;
				}
			} else {
				for(int16_t i = 0; i <= dy; i++) {
					if(c) point_set(x, y);
					else point_reset(x, y);
					m += dx;
					if(m >= dy) {
						m -= dy;
						x += sx;
					}
					y += sy;
				}
			}
		}
Exemplo n.º 5
0
int main(int argc, char **argv)
{
	struct point p1, *p2;

	point_set(&p1, 1.0, 1.0);

	p2 = malloc(sizeof(struct point));
	assert(p2);

	point_set(p2, 1.0, 1.0);
	assert(point_distance(&p1, p2) == 0.0);

	point_translate(&p1, 1.0, 0.0);
	assert(point_distance(&p1, p2) == 1.0);

	point_set(&p1, 0.0, 0.0);
	point_set(p2, 3.0, 4.0);
	assert(point_distance(&p1, p2) == 5.0);

	free(p2);
	p2 = NULL;

	printf("OK\n");
	return 0;
}
Exemplo n.º 6
0
static void point_add(element_t c, element_t a, element_t b) {
    point_ptr p1 = DATA(a), p2 = DATA(b), p3 = DATA(c);
    int inf1 = p1->isinf, inf2 = p2->isinf;
    element_ptr x1 = p1->x, y1 = p1->y, x2 = p2->x, y2 = p2->y;
    field_ptr f = FIELD(x1);
    if (inf1) {
        point_set(c, b);
        return;
    }
    if (inf2) {
        point_set(c, a);
        return;
    }
    element_t v0, v1, v2, v3, v4, ny2;
    element_init(v0, f);
    element_init(v1, f);
    element_init(v2, f);
    element_init(v3, f);
    element_init(v4, f);
    element_init(ny2, f);
    if (!element_cmp(x1, x2)) { // x1 == x2
        element_neg(ny2, y2); // ny2 == -y2
        if (!element_cmp(y1, ny2)) {
            p3->isinf = 1;
            goto end;
        }
        if (!element_cmp(y1, y2)) { // y1 == y2
            element_invert(v0, y1); // v0 == y1^{-1}
            element_mul(v1, v0, v0); // v1 == [y1^{-1}]^2
            element_add(p3->x, v1, x1); // v1 == [y1^{-1}]^2 + x1
            element_cubic(v2, v0); // v2 == [y1^{-1}]^3
            element_add(v2, v2, y1); // v2 == [y1^{-1}]^3 + y1
            element_neg(p3->y, v2); // p3 == -([y1^{-1}]^3 + y1)
            p3->isinf = 0;
            goto end;
        }
    }
    // $P1 \ne \pm P2$
    element_sub(v0, x2, x1); // v0 == x2-x1
    element_invert(v1, v0); // v1 == (x2-x1)^{-1}
    element_sub(v0, y2, y1); // v0 == y2-y1
    element_mul(v2, v0, v1); // v2 == (y2-y1)/(x2-x1)
    element_mul(v3, v2, v2); // v3 == [(y2-y1)/(x2-x1)]^2
    element_cubic(v4, v2); // v4 == [(y2-y1)/(x2-x1)]^3
    element_add(v0, x1, x2); // v0 == x1+x2
    element_sub(v3, v3, v0); // v3 == [(y2-y1)/(x2-x1)]^2 - (x1+x2)
    element_add(v0, y1, y2); // v0 == y1+y2
    element_sub(v4, v0, v4); // v4 == (y1+y2) - [(y2-y1)/(x2-x1)]^3
    p3->isinf = 0;
    element_set(p3->x, v3);
    element_set(p3->y, v4);
end:
    element_clear(v0);
    element_clear(v1);
    element_clear(v2);
    element_clear(v3);
    element_clear(v4);
    element_clear(ny2);
}
Exemplo n.º 7
0
int main(int argc, char *argv[]) {
  Image *src;
  int i, j;
  float x, y, z;
  Vector N;
  Lighting *l;
  Point p;
  Point V;
  Point lp;
  Color BlueGrey;
  Color Sun;
  Color Cb;
  Color Cs;
  Color c;
  int rows = 500, cols = 500;

	color_set(&BlueGrey, 0.2, 0.25, 0.3);
	color_set(&Sun, 0.9, 0.85, 0.8);
	color_set(&Cb, 0.7, 0.2, 0.1);
	color_set(&Cs, 0.3, 0.3, 0.3);

	point_set(&V, 0.0, 4.0, 0.0, 1.0);
	point_set(&lp, 1.0, 5.0, 1.0, 1.0);

  src = image_create(rows, cols);

  // add an ambient light and a point light, slightly below and to the right of the viewer
  l = lighting_create();
  lighting_add( l, LightAmbient, &BlueGrey, NULL, NULL, 0.0, 0.0 );
  lighting_add( l, LightPoint, &Sun, NULL, &lp, 0.0, 0.0 );

  // for each pixel in the image
  for(i=0;i<rows;i++) {
	  // the z value is defined by the pixel (faux parallel projections)
	  z = -1 + i * (2.0/(rows-1));
	  for(j=0;j<cols;j++) {
		  // the x value is defined by the pixel (faux parallel projection)
		  x = -1 + j * (2.0/(cols-1));
		  y = 1.0 - x*x - z*z;
		  if( y <= 0.0 )
			  continue;

		  y = sqrt(y);

		  // the surface point and normal vector are the same since the sphere is centered on 0
		  p.val[0] = x;
		  p.val[1] = y;
		  p.val[2] = z;
		  vector_set( &N, x, y, z );
		  lighting_shading( l, &N, &V, &p, &Cb, &Cs, 32, 1, &c);
		  image_setColor( src, i, j, c );
	  }
  }
  image_write( src, "test9d.ppm");

  return(0);
}
Exemplo n.º 8
0
 bool alias_set(GridT &to, U source, Point<double> &dest_point){
     size_t x1 = dest_point.x;
     size_t y1 = dest_point.y;
     size_t x2 = dest_point.x + 1;
     size_t y2 = dest_point.y + 1;
     double xofs = dest_point.x - x1;
     double yofs = dest_point.y - y1;
     if (point_set(to, source, xofs * yofs, x1, y1)) return true;
     if (point_set(to, source, xofs * (1-yofs), x1, y2)) return true;
     if (point_set(to, source, (1-xofs) * yofs, x2, y1)) return true;
     if (point_set(to, source, (1-xofs) * (1-yofs), x2, y2)) return true;
     return false;
 }
Exemplo n.º 9
0
void point_add(struct affine_point *p1, const struct affine_point *p2,
	       const struct domain_params *dp)
{
  if (! point_is_zero(p2)) {
    if (! point_is_zero(p1)) {
      if (! gcry_mpi_cmp(p1->x, p2->x)) {
	if (! gcry_mpi_cmp(p1->y, p2->y))
	  point_double(p1, dp);
	else
	  point_load_zero(p1);
      }
      else {
	gcry_mpi_t t;
	t = gcry_mpi_snew(0);
	gcry_mpi_subm(t, p1->y, p2->y, dp->m);
	gcry_mpi_subm(p1->y, p1->x, p2->x, dp->m);
	gcry_mpi_invm(p1->y, p1->y, dp->m);
	gcry_mpi_mulm(p1->y, t, p1->y, dp->m);
	gcry_mpi_mulm(t, p1->y, p1->y, dp->m);
	gcry_mpi_addm(p1->x, p1->x, p2->x, dp->m);
	gcry_mpi_subm(p1->x, t, p1->x, dp->m);
	gcry_mpi_subm(t, p2->x, p1->x, dp->m);
	gcry_mpi_mulm(p1->y, p1->y, t, dp->m);
	gcry_mpi_subm(p1->y, p1->y, p2->y, dp->m);
	gcry_mpi_release(t);
      }
    }
    else
      point_set(p1, p2);
  }
}
TEST(UnscentedTransformTest, covariance_recovery_dynamic_dynamic)
{
    constexpr size_t dim = 10;
    typedef Eigen::Matrix<double, Eigen::Dynamic, 1> Point;

    fl::PointSet<Point> point_set(dim);
    test_covariance_transform(point_set, dim);
}
Exemplo n.º 11
0
extern si_t send_widget_move_message(struct egui_uds* uds_ptr, union message* msg, si_t window_descripter, si_t x_offset, si_t y_offset)
{
    union message msg_to_be_sent;
    struct point p = {0};
    point_set(&p, x_offset, y_offset);
    copy_message(msg, &msg_to_be_sent);
    message_set_widget_move(&msg_to_be_sent, window_descripter, &p);
    return comm_send_message(uds_ptr, &msg_to_be_sent);
}
Exemplo n.º 12
0
		//-----------------------------------------------------------------//
		void frame(int16_t x, int16_t y, int16_t w, int16_t h, bool c) {
			for(int16_t i = 0; i < w; ++i) {
				if(c) {
					point_set(x + i, y);
					point_set(x + i, y + h - 1);
				} else {
					point_reset(x + i, y);
					point_reset(x + i, y + h - 1);
				}
			}
			for(int16_t i = 0; i < h; ++i) {
				if(c) {
					point_set(x, y + i);
					point_set(x + w - 1, y + i);
				} else {
					point_reset(x, y + i);
					point_reset(x + w - 1, y + i);
				}
			}
		}
Exemplo n.º 13
0
	//-----------------------------------------------------------------//
	void monograph::frame(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t c)
	{
		for(int16_t i = 0; i < w; ++i) {
			if(c) {
				point_set(x + i, y);
				point_set(x + i, y + h - 1);
			} else {
				point_reset(x + i, y);
				point_reset(x + i, y + h - 1);
			}
		}
		for(int16_t i = 0; i < h; ++i) {
			if(c) {
				point_set(x, y + i);
				point_set(x + w - 1, y + i);
			} else {
				point_reset(x, y + i);
				point_reset(x + w - 1, y + i);
			}
		}
	}
// dynamic dimension, dynamic number of points
TEST(UnscentedTransformTest, mean_recovery_dynamic_dynamic)
{
    constexpr size_t dim = 10;
    typedef Eigen::Matrix<double, Eigen::Dynamic, 1> Point;
    typedef fl::PointSet<Point> PointSet;

    PointSet point_set(dim);
    test_mean_transform(point_set, dim);

    EXPECT_EQ(point_set.count_points(),
              fl::UnscentedTransform::number_of_points(dim));
}
Exemplo n.º 15
0
polyhedron_integrator_t* midpoint_polyhedron_integrator_new()
{
  midpt_t* midpt = polymec_malloc(sizeof(midpt_t));
  midpt->cell_volume = 0.0;
  point_set(&midpt->cell_center, 0.0, 0.0, 0.0);

  polyhedron_integrator_vtable vtable = {.set_domain = midpt_set_domain,
                                         .next_volume_point = midpt_next_volume_point,
                                         .next_surface_point = midpt_next_surface_point,
                                         .dtor = midpt_dtor};
  return polyhedron_integrator_new("Midpoint", midpt, vtable);
}
Exemplo n.º 16
0
/*
  * Remove the point that appears in position
  * <index> on the sorted list, storing its 
  * value in *ret. Returns 1 on success
  * and 0 on failure (too short list).
  *
  * The first item on the list is at index 0.
  */
int sp_removeIndex(SortedPoints *sp, int index, Point *ret)
{
	if (sp->size == 0 || index < 0 || index > 10)
	{
		if (NULL != ret)
			point_set(ret, 0.0, 0.0);
		return 0;
	}

	else if (sp->used[index] == 'n')
	{
		if (NULL != ret)
			point_set(ret, 0.0, 0.0);
		return 0;
	}

	else
	{
		int i;

		sp->size = sp->size - 1;

		if (NULL != ret)
			*ret = sp->pointArray[index];

		sp->used[sp->size] = 'n';

		for (i = index; i < sp->size; i++)
		{
			sp->pointArray[i] = sp->pointArray[i+1];
		}

		point_set(&(sp->pointArray[sp->size]), 0.0, 0.0);
		//free(&(sp->pointArray[sp->size]));
		return 1;

	}
}
Exemplo n.º 17
0
		//-----------------------------------------------------------------//
		void draw_holizontal_level(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t l) {
			frame(x, y, w, h, 1);
		  	if(w <= 2 || h <= 2) return;
			++x;
			h -= 2;
			++y;
			w -= 2;
			for(uint8_t j = 0; j < h; ++j) {
				for(uint8_t i = 0; i < w; ++i) {
					if(i < l) {
						if((i ^ j) & 1) {
							point_set(x + i, y + j);
						} else {
							point_reset(x + i, y + j);
						}
					} else if(i == l && i != 0) {
						point_set(x + i, y + j);
					} else {
						point_reset(x + i, y + j);
					}
				}
			}
		}
Exemplo n.º 18
0
/*
 * Return a copy of a curve object.
 */
static elliptic_curve_t
curve_copy (elliptic_curve_t E)
{
  elliptic_curve_t R;

  R.p = mpi_copy (E.p);
  R.a = mpi_copy (E.a);
  R.b = mpi_copy (E.b);
  point_init (&R.G);
  point_set (&R.G, &E.G);
  R.n = mpi_copy (E.n);

  return R;
}
Exemplo n.º 19
0
/* Return a copy of POINT.  */
static gcry_mpi_point_t
point_copy (gcry_mpi_point_t point)
{
  gcry_mpi_point_t newpoint;

  if (point)
    {
      newpoint = gcry_mpi_point_new (0);
      point_set (newpoint, point);
    }
  else
    newpoint = NULL;
  return newpoint;
}
/**
 * 处理控件的移动
 *
 * 标记标题栏是否被按下
 *
 * @param msg 必须是鼠标移动消息
 *
 * @return 0
 **/
static si_t accumulate_widget_move(union message * msg)
{
	struct point p;

	/* 无包含窗口的用户应用程序 */
	if(global_wm.active_app_info_ptr == NULL && global_wm.active_win_info_ptr == NULL)
	{
		return 0;
	}
	if(title_bar_press != 1)
	{
		return 0;
	}

	screen_flush
		(global_wm.active_win_info_ptr->area.x + widget_move_old_x_offset,
		 global_wm.active_win_info_ptr->area.y + widget_move_old_y_offset,
		 global_wm.active_win_info_ptr->area.width,
		 global_wm.active_win_info_ptr->area.height);

	/* keep the cursor in desktop workarea */
	ensure_point_in_area(&global_wm.new_cursor, &global_wm.work_area);

	point_set(&p, 
		global_wm.active_win_info_ptr->area.x + widget_move_new_x_offset, global_wm.active_win_info_ptr->area.y + widget_move_new_y_offset);
	/* make sure window did not run away */
	ensure_point_in_area(&p, &global_wm.work_area);
	widget_move_old_x_offset = p.x - global_wm.active_win_info_ptr->area.x;
	widget_move_old_y_offset = p.y - global_wm.active_win_info_ptr->area.y;

	/* 绘制活动窗口的边框 */
	engine_draw_rectangle
		(global_wm.gd_handler,
		 global_wm.active_win_info_ptr->area.x + widget_move_old_x_offset,
		 global_wm.active_win_info_ptr->area.y + widget_move_old_y_offset,
		 global_wm.active_win_info_ptr->area.width,
		 global_wm.active_win_info_ptr->area.height);

	if(msg->mouse.code == INPUT_CODE_MOUSE_X_OFFSET)
	{
		widget_move_new_x_offset += global_wm.new_cursor.x - global_wm.old_cursor.x;
	}
	else if(msg->mouse.code == INPUT_CODE_MOUSE_Y_OFFSET)
	{
		widget_move_new_y_offset += global_wm.new_cursor.y - global_wm.old_cursor.y;
	}

	return 0;
}
Exemplo n.º 21
0
		//-----------------------------------------------------------------//
		void fill(int16_t x, int16_t y, int16_t w, int16_t h, bool c) {
			if(c) {
				for(int16_t i = y; i < (y + h); ++i) {
					for(int16_t j = x; j < (x + w); ++j) {
						point_set(j, i);
					}
				}
			} else {
				for(int16_t i = y; i < (y + h); ++i) {
					for(int16_t j = x; j < (x + w); ++j) {
						point_reset(j, i);
					}
				}
			}
		}
Exemplo n.º 22
0
	//-----------------------------------------------------------------//
	void monograph::fill(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t c)
	{
		if(c) {
			for(int16_t i = y; i < (y + h); ++i) {
				for(int16_t j = x; j < (x + w); ++j) {
					point_set(j, i);
				}
			}
		} else {
			for(int16_t i = y; i < (y + h); ++i) {
				for(int16_t j = x; j < (x + w); ++j) {
					point_reset(j, i);
				}
			}
		}
	}
Exemplo n.º 23
0
/*
 * Return a copy of a curve object.
 */
elliptic_curve_t
_gcry_ecc_curve_copy (elliptic_curve_t E)
{
  elliptic_curve_t R;

  R.model = E.model;
  R.dialect = E.dialect;
  R.name = E.name;
  R.p = mpi_copy (E.p);
  R.a = mpi_copy (E.a);
  R.b = mpi_copy (E.b);
  _gcry_mpi_point_init (&R.G);
  point_set (&R.G, &E.G);
  R.n = mpi_copy (E.n);

  return R;
}
Exemplo n.º 24
0
	//-----------------------------------------------------------------//
	void monograph::draw_image(int16_t x, int16_t y, const uint8_t* img, uint8_t w, uint8_t h)
	{
		uint8_t k = 1;
		uint8_t c = *img++;

		for(uint8_t i = 0; i < h; ++i) {
			int16_t xx = x;
			for(uint8_t j = 0; j < w; ++j) {
				if(c & k) point_set(xx, y);
				k <<= 1;
				if(k == 0) {
					k = 1;
					c = *img++;
				}
				++xx;
			}
			++y;
		}
	}
Exemplo n.º 25
0
/****************
 * To verify correct skey it use a random information.
 * First, encrypt and decrypt this dummy value,
 * test if the information is recuperated.
 * Second, test with the sign and verify functions.
 */
static void
test_keys (ECC_secret_key *sk, unsigned int nbits)
{
  ECC_public_key pk;
  gcry_mpi_t test = mpi_new (nbits);
  mpi_point_t R_;
  gcry_mpi_t c = mpi_new (nbits);
  gcry_mpi_t out = mpi_new (nbits);
  gcry_mpi_t r = mpi_new (nbits);
  gcry_mpi_t s = mpi_new (nbits);

  if (DBG_CIPHER)
    log_debug ("Testing key.\n");

  point_init (&R_);

  pk.E = curve_copy (sk->E);
  point_init (&pk.Q);
  point_set (&pk.Q, &sk->Q);

  gcry_mpi_randomize (test, nbits, GCRY_WEAK_RANDOM);

  if (sign (test, sk, r, s) )
    log_fatal ("ECDSA operation: sign failed\n");

  if (verify (test, &pk, r, s))
    {
      log_fatal ("ECDSA operation: sign, verify failed\n");
    }

  if (DBG_CIPHER)
    log_debug ("ECDSA operation: sign, verify ok.\n");

  point_free (&pk.Q);
  curve_free (&pk.E);

  point_free (&R_);
  mpi_free (s);
  mpi_free (r);
  mpi_free (out);
  mpi_free (c);
  mpi_free (test);
}
Exemplo n.º 26
0
		//-----------------------------------------------------------------//
		void draw_image(int16_t x, int16_t y, const uint8_t* img, uint8_t w, uint8_t h)
		{
			if(img == nullptr) return;

			uint8_t k = 1;
			uint8_t c = *img++;
			for(uint8_t i = 0; i < h; ++i) {
				int16_t xx = x;
				for(uint8_t j = 0; j < w; ++j) {
					if(c & k) point_set(xx, y);
					k <<= 1;
					if(k == 0) {
						k = 1;
						c = *img++;
					}
					++xx;
				}
				++y;
			}
		}
Exemplo n.º 27
0
	//-----------------------------------------------------------------//
	void monograph::draw_image_P(int16_t x, int16_t y, const uint8_t* img, uint8_t w, uint8_t h)
	{
		uint8_t k = 1;
		uint8_t c = static_cast<uint8_t>(pgm_read_byte_near(img));
		++img;

		for(uint8_t i = 0; i < h; ++i) {
			int16_t xx = x;
			for(uint8_t j = 0; j < w; ++j) {
				if(c & k) point_set(xx, y);
				k <<= 1;
				if(k == 0) {
					k = 1;
					c = static_cast<uint8_t>(pgm_read_byte_near(img));
					++img;
				}
				++xx;
			}
			++y;
		}
	}
Exemplo n.º 28
0
//returns an average point of the "point" array
Point point_average(Point* point, int size){
    if(NULL != point){
        int i;
        Point p;
        float a=0;
        float b=0;
        float c=0;
        float d=0;
        for(i=0; i<size; i++){
            a += point[i].val[0];
            b += point[i].val[1];
            c += point[i].val[2];
            d += point[i].val[3];
        }
        a /= size;
        b /= size;
        c /= size;
        d /= size;
        point_set(&p, a, b, c, d);
        return p;
    }    
    printf("point_average >> point pointer is NULL\n");
    exit(-1);
}
Exemplo n.º 29
0
/* Scalar point multiplication - the main function for ECC.  If takes
   an integer SCALAR and a POINT as well as the usual context CTX.
   RESULT will be set to the resulting point. */
void
_gcry_mpi_ec_mul_point (mpi_point_t result,
                        gcry_mpi_t scalar, mpi_point_t point,
                        mpi_ec_t ctx)
{
#if 0
  /* Simple left to right binary method.  GECC Algorithm 3.27 */
  unsigned int nbits;
  int i;

  nbits = mpi_get_nbits (scalar);
  mpi_set_ui (result->x, 1);
  mpi_set_ui (result->y, 1);
  mpi_set_ui (result->z, 0);

  for (i=nbits-1; i >= 0; i--)
    {
      _gcry_mpi_ec_dup_point (result, result, ctx);
      if (mpi_test_bit (scalar, i) == 1)
        _gcry_mpi_ec_add_points (result, result, point, ctx);
    }

#else
  gcry_mpi_t x1, y1, z1, k, h, yy;
  unsigned int i, loops;
  mpi_point_struct p1, p2, p1inv;

  x1 = mpi_alloc_like (ctx->p);
  y1 = mpi_alloc_like (ctx->p);
  h  = mpi_alloc_like (ctx->p);
  k  = mpi_copy (scalar);
  yy = mpi_copy (point->y);

  if ( mpi_is_neg (k) )
    {
      k->sign = 0;
      ec_invm (yy, yy, ctx);
    }

  if (!mpi_cmp_ui (point->z, 1))
    {
      mpi_set (x1, point->x);
      mpi_set (y1, yy);
    }
  else
    {
      gcry_mpi_t z2, z3;

      z2 = mpi_alloc_like (ctx->p);
      z3 = mpi_alloc_like (ctx->p);
      ec_mulm (z2, point->z, point->z, ctx);
      ec_mulm (z3, point->z, z2, ctx);
      ec_invm (z2, z2, ctx);
      ec_mulm (x1, point->x, z2, ctx);
      ec_invm (z3, z3, ctx);
      ec_mulm (y1, yy, z3, ctx);
      mpi_free (z2);
      mpi_free (z3);
    }
  z1 = mpi_copy (mpi_const (MPI_C_ONE));

  mpi_mul (h, k, mpi_const (MPI_C_THREE)); /* h = 3k */
  loops = mpi_get_nbits (h);
  if (loops < 2)
    {
      /* If SCALAR is zero, the above mpi_mul sets H to zero and thus
         LOOPs will be zero.  To avoid an underflow of I in the main
         loop we set LOOP to 2 and the result to (0,0,0).  */
      loops = 2;
      mpi_clear (result->x);
      mpi_clear (result->y);
      mpi_clear (result->z);
    }
  else
    {
      mpi_set (result->x, point->x);
      mpi_set (result->y, yy);
      mpi_set (result->z, point->z);
    }
  mpi_free (yy); yy = NULL;

  p1.x = x1; x1 = NULL;
  p1.y = y1; y1 = NULL;
  p1.z = z1; z1 = NULL;
  point_init (&p2);
  point_init (&p1inv);

  for (i=loops-2; i > 0; i--)
    {
      _gcry_mpi_ec_dup_point (result, result, ctx);
      if (mpi_test_bit (h, i) == 1 && mpi_test_bit (k, i) == 0)
        {
          point_set (&p2, result);
          _gcry_mpi_ec_add_points (result, &p2, &p1, ctx);
        }
      if (mpi_test_bit (h, i) == 0 && mpi_test_bit (k, i) == 1)
        {
          point_set (&p2, result);
          /* Invert point: y = p - y mod p  */
          point_set (&p1inv, &p1);
          ec_subm (p1inv.y, ctx->p, p1inv.y, ctx);
          _gcry_mpi_ec_add_points (result, &p2, &p1inv, ctx);
        }
    }

  point_free (&p1);
  point_free (&p2);
  point_free (&p1inv);
  mpi_free (h);
  mpi_free (k);
#endif
}
Exemplo n.º 30
0
/*
 * First obtain the setup.  Over the finite field randomize an scalar
 * secret value, and calculate the public point.
 */
static gpg_err_code_t
generate_key (ECC_secret_key *sk, unsigned int nbits, const char *name,
              gcry_mpi_t g_x, gcry_mpi_t g_y,
              gcry_mpi_t q_x, gcry_mpi_t q_y)
{
  gpg_err_code_t err;
  elliptic_curve_t E;
  gcry_mpi_t d;
  mpi_point_t Q;
  mpi_ec_t ctx;

  err = generate_curve (nbits, name, &E, &nbits);
  if (err)
    return err;

  if (DBG_CIPHER)
    {
      log_mpidump ("ecc generation   p", E.p);
      log_mpidump ("ecc generation   a", E.a);
      log_mpidump ("ecc generation   b", E.b);
      log_mpidump ("ecc generation   n", E.n);
      log_mpidump ("ecc generation  Gx", E.G.x);
      log_mpidump ("ecc generation  Gy", E.G.y);
      log_mpidump ("ecc generation  Gz", E.G.z);
    }

  if (DBG_CIPHER)
    log_debug ("choosing a random x of size %u\n", nbits);
  d = gen_k (E.n, GCRY_VERY_STRONG_RANDOM); 

  /* Compute Q.  */
  point_init (&Q);
  ctx = _gcry_mpi_ec_init (E.p, E.a);
  _gcry_mpi_ec_mul_point (&Q, d, &E.G, ctx);

  /* Copy the stuff to the key structures. */
  sk->E.p = mpi_copy (E.p);
  sk->E.a = mpi_copy (E.a);
  sk->E.b = mpi_copy (E.b);
  point_init (&sk->E.G);
  point_set (&sk->E.G, &E.G);
  sk->E.n = mpi_copy (E.n);
  point_init (&sk->Q);
  point_set (&sk->Q, &Q);
  sk->d    = mpi_copy (d);
  /* We also return copies of G and Q in affine coordinates if
     requested.  */
  if (g_x && g_y)
    {
      if (_gcry_mpi_ec_get_affine (g_x, g_y, &sk->E.G, ctx))
        log_fatal ("ecc generate: Failed to get affine coordinates\n");
    }
  if (q_x && q_y)
    {
      if (_gcry_mpi_ec_get_affine (q_x, q_y, &sk->Q, ctx))
        log_fatal ("ecc generate: Failed to get affine coordinates\n");
    }
  _gcry_mpi_ec_free (ctx);

  point_free (&Q);
  mpi_free (d);
  curve_free (&E);

  /* Now we can test our keys (this should never fail!). */
  test_keys (sk, nbits - 64);

  return 0;
}