Пример #1
0
/**
 * Test error handling of importing private key.
 */
void test_import_private_key2(void)
{
	keypair pair;
	fmpz_poly_t f, g, priv, priv_inv;
	int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 };
	int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 };
	ntru_params params;

	fmpz_poly_init(priv);
	fmpz_poly_init(priv_inv);

	params.N = 11;
	params.p = 3;
	params.q = 32;

	poly_new(f, f_c, 11);
	poly_new(g, g_c, 11);

	ntru_create_keypair(&pair, f, g, &params);
	export_priv_key("priv.key", pair.priv, &params);
	import_priv_key(priv, priv_inv, ".", &params);

	remove("priv.key");

	CU_ASSERT_NOT_EQUAL(1, fmpz_poly_equal(priv, pair.priv));
}
Пример #2
0
/**
 * Test exporting public key and reading the resulting file.
 */
void test_export_public_key1(void)
{
	keypair pair;
	fmpz_poly_t f, g;
	int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 };
	int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 };
	ntru_params params;
	string *pub_string;
	char *expected_pub_c_str = "CBkWFAwYDxMMExA=";
	char actual_pub_c_str[512] = "\0";

	params.N = 11;
	params.p = 3;
	params.q = 32;

	poly_new(f, f_c, 11);
	poly_new(g, g_c, 11);

	ntru_create_keypair(&pair, f, g, &params);
	export_public_key("pub.key", pair.pub, &params);

	if ((pub_string = read_file("pub.key"))) {
		memcpy(actual_pub_c_str, pub_string->ptr, pub_string->len);
		actual_pub_c_str[pub_string->len] = '\0';
		string_delete(pub_string);
	}

	remove("pub.key");

	CU_ASSERT_EQUAL(strcmp(expected_pub_c_str, actual_pub_c_str), 0);
}
Пример #3
0
/**
 * Test error handling of exporting private key.
 */
void test_export_private_key2(void)
{
	keypair pair;
	fmpz_poly_t f, g;
	int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 };
	int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 };
	ntru_params params;
	string *pub_string;
	char *expected_priv_c_str = "AgEBAgAAAAEAAQE=";
	char actual_priv_c_str[512] = "\0";

	params.N = 11;
	params.p = 3;
	params.q = 32;

	poly_new(f, f_c, 11);
	poly_new(g, g_c, 11);

	ntru_create_keypair(&pair, f, g, &params);
	export_priv_key(".", pair.pub, &params);

	if ((pub_string = read_file("priv.key"))) {
		memcpy(actual_priv_c_str, pub_string->ptr, pub_string->len);
		actual_priv_c_str[pub_string->len] = '\0';
		string_delete(pub_string);
	}

	CU_ASSERT_NOT_EQUAL(strcmp(expected_priv_c_str, actual_priv_c_str), 0);
}
Пример #4
0
/**
 * Test error handling of importing public key.
 */
void test_import_public_key2(void)
{
	keypair pair;
	fmpz_poly_t f, g, pub;
	int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 };
	int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 };
	ntru_params params;

	fmpz_poly_init(pub);

	params.N = 11;
	params.p = 3;
	params.q = 32;

	poly_new(f, f_c, 11);
	poly_new(g, g_c, 11);

	ntru_create_keypair(&pair, f, g, &params);
	export_public_key("pub.key", pair.pub, &params);
	import_public_key(pub, "foo", &params);

	remove("pub.key");

	CU_ASSERT_NOT_EQUAL(1, fmpz_poly_equal(pub, pair.pub));
}
Пример #5
0
/* Compute the derivative of a polynomial */
poly_t *poly_deriv(poly_t *p) {
    if (p->n == 1) {
	/* Derivative is zero */
	return poly_new(1);
    } else {
	poly_t *p_d = poly_new(p->n - 1);
	int i = 0;

	for (i = 0; i < p->n; i++) {
	    p_d->coeffs[i] = (i + 1) * p->coeffs[i + 1];
	}

	return p_d;
    }
}
Пример #6
0
/**
 * Test keypair creation error handling via non-invertible polynomial.
 */
void test_create_keypair2(void)
{
	keypair pair;
	fmpz_poly_t f, g, priv_inv;
	int f_c[] = { 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, -1 };
	int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 };
	int priv_inv_c[] = { 1, 2, 0, 2, 2, 1, 0, 2, 1, 2, 0 };
	ntru_params params;
	params.N = 11;
	params.p = 3;
	params.q = 32;

	poly_new(f, f_c, 11);
	poly_new(g, g_c, 11);
	poly_new(priv_inv, priv_inv_c, 11);

	CU_ASSERT_EQUAL(false, ntru_create_keypair(&pair, f, g, &params));
}
Пример #7
0
static poly poly_clip(poly sub, poly clip)
{
    int i;
    poly p1 = poly_new(), p2 = poly_new(), tmp;
 
    int dir = poly_winding(clip);
    poly_edge_clip(sub, clip->v + clip->len - 1, clip->v, dir, p2);
    for (i = 0; i < clip->len - 1; i++) {
        tmp = p2; p2 = p1; p1 = tmp;
        if(p1->len == 0) {
            p2->len = 0;
            break;
        }
        poly_edge_clip(p1, clip->v + i, clip->v + i + 1, dir, p2);
    }
 
    poly_free(p1);
    return p2;
}
Пример #8
0
Poly *poly_term_multi(Poly *poly, Term *term)
{
	Poly *retPoly;
	Term *iter;   //iterator
	retPoly = poly_new(); // the return polynomial
	for (iter = poly->head; iter != NULL; iter = iter->next)
	{
		poly_add_term(retPoly, term_new(poly_data(iter)->coef * poly_data(term)->coef, poly_data(iter)->expo + poly_data(term)->expo));
	}
	return retPoly;
}
Пример #9
0
/* Subtract two polynomials */
poly_t *poly_diff(poly_t *p, poly_t *q) {
    int n = MAX(p->n, q->n);
    poly_t *r = poly_new(n);
    int i;

    for (i = 0; i < n + 1; i++) {
	double c = poly_get_coeff(p, i) - poly_get_coeff(q, i);
	poly_set_coeff(r, i, c);
    }

    return r;
}
Пример #10
0
/* Multiply two polynomials and return the result */
poly_t *poly_product(poly_t *p, poly_t *q) {
    int n = p->n + q->n;
    poly_t *r = poly_new(n);
    int i, j;
    
    /* Multiply */
    for (i = 0; i < p->n + 1; i++)
	for (j = 0; j < q->n + 1; j++)
	    r->coeffs[i+j] += p->coeffs[i] * q->coeffs[j];

    return r;
}
Пример #11
0
/**
 * Test keypair creation.
 */
void test_create_keypair1(void)
{
	keypair pair;
	fmpz_poly_t f, g, pub, priv_inv;
	int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 };
	int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 };
	int pub_c[] = { 8, 25, 22, 20, 12, 24, 15, 19, 12, 19, 16 };
	int priv_inv_c[] = { 1, 2, 0, 2, 2, 1, 0, 2, 1, 2, 0 };
	ntru_params params;
	params.N = 11;
	params.p = 3;
	params.q = 32;

	poly_new(f, f_c, 11);
	poly_new(g, g_c, 11);
	poly_new(pub, pub_c, 11);
	poly_new(priv_inv, priv_inv_c, 11);

	CU_ASSERT_EQUAL(true, ntru_create_keypair(&pair, f, g, &params));
	CU_ASSERT_EQUAL(1, fmpz_poly_equal(pub, pair.pub));
	CU_ASSERT_EQUAL(1, fmpz_poly_equal(priv_inv, pair.priv_inv));
	CU_ASSERT_EQUAL(1, fmpz_poly_equal(f, pair.priv));
}
Пример #12
0
Poly *poly_parse(int size)
{
	Poly *poly;
	double coef;
	int expo, i;

	poly = poly_new();

	for (i = 0; i<size; i++)
	{
		scanf("%lf%d", &coef, &expo);
		poly_add_term(poly, term_new(coef, expo));
	}

	return poly;
}
Пример #13
0
Poly *poly_sub(Poly *poly1, Poly *poly2)    //return poly1-poly2
{
	Poly *poly;
	Term *term1, *term2;
	term1 = poly1->head;
	term2 = poly2->head;
	poly = poly_new();
	while (term1 != NULL && term2 != NULL)
	{
		if (poly_data(term1)->expo > poly_data(term2)->expo)
		{
			poly_add_term(poly, term_new(poly_data(term1)->coef, poly_data(term1)->expo));
			term1 = term1->next;
		}
		else if (poly_data(term1)->expo == poly_data(term2)->expo)
		{
			if (poly_data(term1)->coef - poly_data(term2)->coef != 0)
				poly_add_term(poly, term_new(poly_data(term1)->coef - poly_data(term2)->coef, poly_data(term1)->expo));
			term1 = term1->next;
			term2 = term2->next;
		}
		else
		{
			poly_add_term(poly, term_new(-poly_data(term2)->coef, poly_data(term2)->expo));
			term2 = term2->next;
		}
	}

	if (term1 == NULL)
	{
		while (term2 != NULL)
		{
			poly_add_term(poly, term_new(-poly_data(term2)->coef, poly_data(term2)->expo));
			term2 = term2->next;
		}
	}
	else if (term2 == NULL)
	{
		while (term1 != NULL)
		{
			poly_add_term(poly, term_new(poly_data(term1)->coef, poly_data(term1)->expo));
			term1 = term1->next;
		}
	}
	return poly;
}
Пример #14
0
Poly *poly_multi(Poly *poly1, Poly *poly2)
{
	Poly *retPoly, *tmpM, *tmpA;
	// to ensure that no memory leaking during the operation, we use two temporary pointers for memory management purposes
	// tmpM is used to track the poly object during multiplication
	// tmpA is used to track the poly object during addition
	Term *iter;
	retPoly = poly_new();
	for (iter = poly2->head; iter != NULL; iter = iter->next)
	{
		tmpM = poly_term_multi(poly1, iter);
		tmpA = retPoly;
		retPoly = poly_add(retPoly, tmpM);
		poly_destroy(tmpM);
		poly_destroy(tmpA);
	}
	return retPoly;
}
Пример #15
0
void gui_init (gui *g) {
  g->poly     = poly_new ();
  g->selected = NULL;
  g->hovered  = NULL;
  g->gc       = NULL;
  g->canvas   = NULL;
  g->pixmap   = NULL;
  g->ctrl     = false;
  g->pix_w    = 0;
  g->pix_h    = 0;

  /* Main window */
  g->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (g->window, "[:Polytool:]");

  /* Widget creation */
  g->vbox      = gtk_vbox_new         (false, 0);
  g->box       = gtk_hbox_new         (false, 4);
  g->status    = gtk_statusbar_new    ();
  g->draw_zone = gtk_drawing_area_new ();


  /* Context menu */
  g->menu = gtk_menu_new ();

  g->m_split = gtk_menu_item_new_with_label ("Split left/right chains");
  g->m_mtr   = gtk_menu_item_new_with_label ("Triangulate (monotone)");
  g->m_rtr   = gtk_menu_item_new_with_label ("Triangulate (generic)");
  g->m_hull  = gtk_menu_item_new_with_label ("Convex hull (Graham)");
  g->m_sep1  = gtk_separator_menu_item_new  ();
  g->m_clear = gtk_menu_item_new_with_label ("Clear");
  g->m_draw  = gtk_menu_item_new_with_label ("Redraw");
  g->m_sep2  = gtk_separator_menu_item_new  ();
  g->m_quit  = gtk_menu_item_new_with_label ("Quit");

  gtk_menu_append (g->menu, g->m_split);
  gtk_menu_append (g->menu, g->m_mtr);
  gtk_menu_append (g->menu, g->m_rtr);
  gtk_menu_append (g->menu, g->m_hull);
  gtk_menu_append (g->menu, g->m_sep1);
  gtk_menu_append (g->menu, g->m_clear);
  gtk_menu_append (g->menu, g->m_draw);
  gtk_menu_append (g->menu, g->m_sep2);
  gtk_menu_append (g->menu, g->m_quit);

  gtk_widget_show_all (g->menu);


  /* Drawing area */
  gtk_drawing_area_size (g->draw_zone, 200, 200);
  gtk_widget_set_events (g->draw_zone, GDK_EXPOSURE_MASK
                         | GDK_LEAVE_NOTIFY_MASK
                         | GDK_BUTTON_PRESS_MASK
                         | GDK_BUTTON_RELEASE_MASK
                         | GDK_POINTER_MOTION_MASK
                         | GDK_POINTER_MOTION_HINT_MASK);

  /* Packing */
  gtk_container_set_border_width (g->box, 4);

  gtk_box_pack_start (g->box,    g->draw_zone, true,  true,  0);
  gtk_box_pack_start (g->vbox,   g->box,       true,  true,  0);
  gtk_box_pack_start (g->vbox,   g->status,    false, false, 0);
  gtk_container_add  (g->window, g->vbox);

  g->cid = gtk_statusbar_get_context_id (g->status, "default");

  gui_connect_signals (g);
}