+static int eccheck(ec_curve *c, const ec *p)
+{
+ field *f = c->f;
+ mp *l, *x, *r;
+ int rc;
+ if (EC_ATINF(p)) return (0);
+ l = F_SQR(f, MP_NEW, p->y);
+ x = F_SQR(f, MP_NEW, p->x);
+ r = F_MUL(f, MP_NEW, x, p->x);
+ x = F_MUL(f, x, c->a, p->x);
+ r = F_ADD(f, r, r, x);
+ r = F_ADD(f, r, r, c->b);
+ rc = MP_EQ(l, r) ? 0 : -1;
+ mp_drop(l);
+ mp_drop(x);
+ mp_drop(r);
+ return (rc);
+}
+
+static int ecprojcheck(ec_curve *c, const ec *p)
+{
+ ec t = EC_INIT;
+ int rc;
+
+ c->ops->fix(c, &t, p);
+ rc = eccheck(c, &t);
+ EC_DESTROY(&t);
+ return (rc);
+}
+
+static void ecdestroy(ec_curve *c)
+{
+ MP_DROP(c->a);
+ MP_DROP(c->b);
+ DESTROY(c);
+}
+
+/* --- @ec_prime@, @ec_primeproj@ --- *
+ *
+ * Arguments: @field *f@ = the underlying field for this elliptic curve
+ * @mp *a, *b@ = the coefficients for this curve
+ *
+ * Returns: A pointer to the curve, or null.
+ *
+ * Use: Creates a curve structure for an elliptic curve defined over
+ * a prime field. The @primeproj@ variant uses projective
+ * coordinates, which can be a win.
+ */
+
+extern ec_curve *ec_prime(field *f, mp *a, mp *b)
+{
+ ec_curve *c = CREATE(ec_curve);
+ c->ops = &ec_primeops;
+ c->f = f;
+ c->a = F_IN(f, MP_NEW, a);
+ c->b = F_IN(f, MP_NEW, b);
+ return (c);
+}
+
+extern ec_curve *ec_primeproj(field *f, mp *a, mp *b)
+{
+ ec_curve *c = CREATE(ec_curve);
+ mp *ax;
+
+ ax = mp_add(MP_NEW, a, MP_THREE);
+ ax = F_IN(f, ax, ax);
+ if (F_ZEROP(f, ax))
+ c->ops = &ec_primeprojxops;
+ else
+ c->ops = &ec_primeprojops;
+ MP_DROP(ax);
+ c->f = f;
+ c->a = F_IN(f, MP_NEW, a);
+ c->b = F_IN(f, MP_NEW, b);
+ return (c);
+}
+
+static const ec_ops ec_primeops = {
+ "prime",
+ ecdestroy, ec_stdsamep, ec_idin, ec_idout, ec_idfix,
+ ecfind, ecneg, ecadd, ec_stdsub, ecdbl, eccheck
+};
+
+static const ec_ops ec_primeprojops = {
+ "primeproj",
+ ecdestroy, ec_stdsamep, ec_projin, ec_projout, ec_projfix,
+ ecfind, ecneg, ecprojadd, ec_stdsub, ecprojdbl, ecprojcheck
+};
+
+static const ec_ops ec_primeprojxops = {
+ "primeproj",
+ ecdestroy, ec_stdsamep, ec_projin, ec_projout, ec_projfix,
+ ecfind, ecneg, ecprojadd, ec_stdsub, ecprojxdbl, ecprojcheck
+};
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+#define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
+
+int main(int argc, char *argv[])
+{
+ field *f;
+ ec_curve *c;
+ ec g = EC_INIT, d = EC_INIT;
+ mp *p, *a, *b, *r;
+ int i, n = argc == 1 ? 1 : atoi(argv[1]);
+
+ printf("ec-prime: ");
+ fflush(stdout);
+ a = MP(-3);
+ b = MP(0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef);
+ p = MP(39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319);
+ r = MP(39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942642);
+
+ f = field_niceprime(p);
+ c = ec_primeproj(f, a, b);
+
+ g.x = MP(0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7);
+ g.y = MP(0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f);
+
+ for (i = 0; i < n; i++) {
+ ec_mul(c, &d, &g, r);
+ if (EC_ATINF(&d)) {
+ fprintf(stderr, "zero too early\n");
+ return (1);
+ }
+ ec_add(c, &d, &d, &g);
+ if (!EC_ATINF(&d)) {
+ fprintf(stderr, "didn't reach zero\n");
+ MP_EPRINT("d.x", d.x);
+ MP_EPRINT("d.y", d.y);
+ return (1);
+ }
+ ec_destroy(&d);
+ }
+ ec_destroy(&g);
+ ec_destroycurve(c);
+ F_DESTROY(f);
+ MP_DROP(p); MP_DROP(a); MP_DROP(b); MP_DROP(r);
+ assert(!mparena_count(&mparena_global));
+ printf("ok\n");
+ return (0);
+}
+
+#endif
+