int ec2np_mul(ec2np_ptr d, ec2np_ptr a, b2n_ptr e, ec2ng_ptr g) { int i, j, bits, start; b2n_t h, k; ec2np_t q, mina; if (!b2n_cmp_null(e)) { d->inf = 1; if (b2n_set_null(d->x)) return -1; return b2n_set_null(d->y); } b2n_init(h); b2n_init(k); ec2np_init(q); ec2np_init(mina); if (ec2np_set(q, a)) goto fail; /* Create the point -a. */ if (ec2np_set(mina, a)) goto fail; if (b2n_add(mina->y, mina->y, mina->x)) goto fail; if (b2n_set(k, e)) goto fail; if (b2n_3mul(h, k)) goto fail; if (b2n_resize(k, h->chunks)) goto fail; /* * This is low level but can not be avoided, since we have to do single * bit checks on h and k. */ bits = b2n_sigbit(h); if ((bits & CHUNK_MASK) == 1) { start = ((CHUNK_MASK + bits) >> CHUNK_SHIFTS) - 2; bits = CHUNK_BITS; } else {
int main (void) { b2n_t k; ec2np_t p, q, r; ec2ng_t g; char buf[BUFSIZE]; b2n_init (k); ec2np_init (p); ec2np_init (q); ec2np_init (r); ec2ng_init (g); printf ("Testing: ec2ng_set* :"); /* Init Group */ ec2ng_set_p_str (g, "0x0800000000000000000000004000000000000001"); CMP_FAIL (g->p, "0x0800000000000000000000004000000000000001"); ec2ng_set_a_ui (g, 0); CMP_FAIL (g->a, "0x00"); ec2ng_set_b_str (g, "0x07338f"); CMP_FAIL (g->b, "0x07338f"); printf ("\nTesting: ec2np_find_y: "); /* Init Point */ ec2np_set_x_ui (p, 0x7b); ec2np_find_y (p, g); CMP_FAIL (p->y, "0x01c8"); printf ("\nTesting: ec2np_ison: "); if (ec2np_ison (p, g)) printf ("OKAY "); else printf ("FAILED "); ec2np_set_x_ui (q, 0x4); ec2np_find_y (q, g); if (ec2np_ison (q, g)) printf ("OKAY "); else printf ("FAILED "); printf ("\nTesting: ec2np_add: "); ec2np_set (r, p); b2n_add (r->y, r->y, r->x); ec2np_add (r, r, p, g); if (!r->inf) printf ("FAILED "); else printf ("OKAY "); ec2np_add (q, p, q, g); CMP_FAIL (q->x, "0x06f32d7cc82cec8612a87a86e026350fb7595469"); CMP_FAIL (q->y, "0x4ab92e21e51358ca8deab3fbbc9f7d8a7d1575"); if (ec2np_ison (q, g)) printf ("OKAY "); else printf ("FAILED "); ec2np_add (p, q, q, g); CMP_FAIL (p->x, "0x0390001461385559a22ac9b6181c1e1889b38451"); CMP_FAIL (p->y, "0x0188e61f38d747d7813c6a8b33d14dfb7418b04c"); if (ec2np_ison (p, g)) printf ("OKAY "); else printf ("FAILED "); printf ("\nTesting: ec2np_mul: "); b2n_set_ui (k, 57); ec2np_set (q, p); ec2np_mul (q, q, k, g); if (ec2np_ison (q, g)) printf ("OKAY "); else printf ("FAILED "); CMP_FAIL (q->x, "0x06bcf88caab88f99399350c46559da3b91afbf9d"); b2n_set_str (k, "0x0800000000000000000057db5698537193aef943"); ec2np_set (q, p); ec2np_mul (q, q, k, g); if (ec2np_ison (q, g)) printf ("OKAY "); else printf ("FAILED "); CMP_FAIL (q->x, "0x0390001461385559a22ac9b6181c1e1889b38451"); printf ("\n"); ec2np_clear (p); ec2np_clear (q); ec2np_clear (r); ec2ng_clear (g); b2n_clear (k); return 1; }
int ec2np_add(ec2np_ptr d, ec2np_ptr a, ec2np_ptr b, ec2ng_ptr g) { b2n_t lambda, temp; ec2np_t pn; /* Check for Neutral Element */ if (b->inf) return ec2np_set(d, a); if (a->inf) return ec2np_set(d, b); if (!b2n_cmp(a->x, b->x) && (b2n_cmp(a->y, b->y) || !b2n_cmp_null(a->x))) { d->inf = 1; if (b2n_set_null(d->x)) return -1; return b2n_set_null(d->y); } b2n_init(lambda); b2n_init(temp); ec2np_init(pn); if (b2n_cmp(a->x, b->x)) { if (b2n_add(temp, a->x, b->x)) goto fail; if (b2n_add(lambda, a->y, b->y)) goto fail; if (b2n_div_mod(lambda, lambda, temp, g->p)) goto fail; if (b2n_square(pn->x, lambda)) goto fail; if (b2n_mod(pn->x, pn->x, g->p)) goto fail; if (b2n_add(pn->x, pn->x, lambda)) goto fail; if (b2n_add(pn->x, pn->x, g->a)) goto fail; if (b2n_add(pn->x, pn->x, a->x)) goto fail; if (b2n_add(pn->x, pn->x, b->x)) goto fail; } else { if (b2n_div_mod(lambda, b->y, b->x, g->p)) goto fail; if (b2n_add(lambda, lambda, b->x)) goto fail; if (b2n_square(pn->x, lambda)) goto fail; if (b2n_mod(pn->x, pn->x, g->p)) goto fail; if (b2n_add(pn->x, pn->x, lambda)) goto fail; if (b2n_add(pn->x, pn->x, g->a)) goto fail; } if (b2n_add(pn->y, b->x, pn->x)) goto fail; if (b2n_mul(pn->y, pn->y, lambda)) goto fail; if (b2n_mod(pn->y, pn->y, g->p)) goto fail; if (b2n_add(pn->y, pn->y, pn->x)) goto fail; if (b2n_add(pn->y, pn->y, b->y)) goto fail; EC2NP_SWAP(d, pn); ec2np_clear(pn); b2n_clear(lambda); b2n_clear(temp); return 0; fail: ec2np_clear(pn); b2n_clear(lambda); b2n_clear(temp); return -1; }