summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2016-12-21 22:39:27 +0100
committerJ08nY2016-12-21 22:39:27 +0100
commitde1f5bccb47bc847b182b9f7497e0b077f86361b (patch)
tree6540e1787435134838d4d48652ada81ca9d6fd59
parentadb0d5368c5972260a2617f60407301134e73ab4 (diff)
downloadecgen-de1f5bccb47bc847b182b9f7497e0b077f86361b.tar.gz
ecgen-de1f5bccb47bc847b182b9f7497e0b077f86361b.tar.zst
ecgen-de1f5bccb47bc847b182b9f7497e0b077f86361b.zip
-rw-r--r--Makefile4
-rw-r--r--ecgen.c5
-rw-r--r--gp.c (renamed from sea.c)190
-rw-r--r--gp.gp2
-rw-r--r--gp.h35
-rw-r--r--points.c181
-rw-r--r--points.h23
-rw-r--r--sea.gp7
-rw-r--r--sea.h19
9 files changed, 223 insertions, 243 deletions
diff --git a/Makefile b/Makefile
index e614309..d63a093 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ GPFLAGS=-g
LIBS=-lpari -lreadline -ltermcap
-GP = points sea
+GP = gp
GPC = $(addsuffix .c, $(GP))
GPO = $(addsuffix .o, $(GP))
GPH = $(addsuffix .h, $(GP))
@@ -18,7 +18,7 @@ GPH = $(addsuffix .h, $(GP))
all: ecgen
ecgen: ecgen.o $(GPO)
- $(CC) $(CFLAGS) -o $@ $^ $(LIBS)
+ $(CC) $(CFLAGS) -o $@ $^ $(LIBS)
gp2c: $(GPC)
diff --git a/ecgen.c b/ecgen.c
index bf8c1ca..455aaaa 100644
--- a/ecgen.c
+++ b/ecgen.c
@@ -4,8 +4,7 @@
#include <stdio.h>
#include <readline/readline.h>
-#include "points.h"
-#include "sea.h"
+#include "gp.h"
char *readHex() {
char *r = readline(NULL);
@@ -35,7 +34,7 @@ int main(int argc, char * argv[]) {
}
}
- init_sea();
+ init_gp();
pari_init( 1e9, 1e8 );
//default0("datadir","./data");
diff --git a/sea.c b/gp.c
index 946d4f1..722d864 100644
--- a/sea.c
+++ b/gp.c
@@ -1,13 +1,184 @@
-#include "sea.h"
+#include "gp.h"
void
-init_sea(void) /* void */
+init_gp(void) /* void */
{
pari_sp ltop = avma;
avma = ltop;
return;
}
+/* Finds random point of order n on curve e of order o.
+* @returns [[P.x, P.y], n, h]
+* @param e curve
+* @param o curve order
+* @param n desired point order
+*/
+GEN
+find_point(GEN e, GEN o, GEN n) /* vec */
+{
+ pari_sp ltop = avma;
+ GEN h = gen_0, P = gen_0;
+ GEN p1 = gen_0; /* vec */
+ h = gdivent(o, n);
+ {
+ pari_sp btop = avma;
+ do
+ {
+ P = genrand(e);
+ if (gc_needed(btop, 1))
+ P = gerepilecopy(btop, P);
+ } while(gequal0(ellmul(e, P, n)));
+ }
+ p1 = cgetg(4, t_VEC);
+ gel(p1, 1) = gcopy(P);
+ gel(p1, 2) = gcopy(n);
+ gel(p1, 3) = gcopy(h);
+ p1 = gerepilecopy(ltop, p1);
+ return p1;
+}
+
+/* Finds random points of orders given by vector p.
+* @returns vector of points in format [[P.x, P.y], n, h]
+* @param e curve
+* @param o curve order
+* @param p vector of point orders
+*/
+GEN
+find_points(GEN e, GEN o, GEN p) /* vec */
+{
+ pari_sp ltop = avma;
+ long l1;
+ GEN p2 = gen_0; /* vec */
+ l1 = glength(p);
+ {
+ long X;
+ p2 = cgetg(l1+1, t_VEC);
+ for (X = 1; X <= l1; ++X)
+ gel(p2, X) = find_point(e, o, gel(p, X));
+ }
+ p2 = gerepilecopy(ltop, p2);
+ return p2;
+}
+
+/*####################################################################*/
+
+GEN
+maxprime_order(GEN e, GEN o)
+{
+ pari_sp ltop = avma;
+ if (!gequal0(gisprime(o, 0)))
+ {
+ o = gerepilecopy(ltop, o);
+ return o;
+ }
+ else
+ {
+ GEN p1 = gen_0;
+ p1 = vecmax(factor(o));
+ p1 = gerepilecopy(ltop, p1);
+ return p1;
+ }
+ avma = ltop;
+ return gen_0;
+}
+
+GEN
+minprime_order(GEN e, GEN o)
+{
+ pari_sp ltop = avma;
+ if (!gequal0(gisprime(o, 0)))
+ {
+ o = gerepilecopy(ltop, o);
+ return o;
+ }
+ else
+ {
+ GEN p1 = gen_0;
+ p1 = gcopy(gcoeff(factor(o), 1, 1));
+ p1 = gerepilecopy(ltop, p1);
+ return p1;
+ }
+ avma = ltop;
+ return gen_0;
+}
+
+GEN
+max_order(GEN e, GEN o)
+{
+ pari_sp ltop = avma;
+ o = gerepilecopy(ltop, o);
+ return o;
+}
+
+/* Finds a random point of order given by f(o).
+* @returns [[P.x, P.y], n, h]
+* with P being the point with order f(o).
+* @param e curve
+* @param o curve order
+* @param f function returning the point order, \in maxprime_order,
+* minprime_order, max_order
+*/
+GEN
+get_point(GEN e, GEN o, GEN f) /* vec */
+{
+ pari_sp ltop = avma;
+ GEN p1 = gen_0; /* vec */
+ p1 = find_point(e, o, closure_callgen1(f, o));
+ p1 = gerepilecopy(ltop, p1);
+ return p1;
+}
+
+/*####################################################################*/
+
+GEN
+prime_orders(GEN e, GEN o)
+{
+ pari_sp ltop = avma;
+ GEN f = gen_0;
+ if (!gequal0(gisprime(o, 0)))
+ {
+ GEN p1 = gen_0; /* vec */
+ p1 = cgetg(2, t_VEC);
+ gel(p1, 1) = gcopy(o);
+ p1 = gerepilecopy(ltop, p1);
+ return p1;
+ }
+ else
+ {
+ long l2;
+ GEN p3 = gen_0; /* vec */
+ f = factor(o);
+ l2 = glength(f);
+ {
+ long X;
+ p3 = cgetg(l2+1, t_VEC);
+ for (X = 1; X <= l2; ++X)
+ gel(p3, X) = gcopy(gcoeff(f, X, 1));
+ }
+ p3 = gerepilecopy(ltop, p3);
+ return p3;
+ }
+ avma = ltop;
+ return gen_0;
+}
+
+/* Finds random points of orders given by f(o).
+* @returns vector of points in format [[P.x, P.y], n, h]
+* @param e curve
+* @param o curve order
+* @param f function returning a vector of point orders
+*/
+GEN
+get_points(GEN e, GEN o, GEN f) /* vec */
+{
+ pari_sp ltop = avma;
+ GEN p1 = gen_0; /* vec */
+ p1 = find_points(e, o, closure_callgen1(f, o));
+ p1 = gerepilecopy(ltop, p1);
+ return p1;
+}
+
/* E(Fp): y^2 = x^3 + ax + b mod p
* @returns [p, a, b, [G.x, G.y], n, h]
* @param p
@@ -19,9 +190,7 @@ largest_prime(GEN p, GEN a, GEN b, long prec)
{
pari_sp ltop = avma;
GEN e = gen_0, o = gen_0, G = gen_0;
- GEN p1 = gen_0; /* vec */
- GEN maxprime_point = pol_x(fetch_user_var("maxprime_point"));
- GEN p2 = gen_0; /* vec */
+ GEN p1 = gen_0, p2 = gen_0; /* vec */
p1 = cgetg(3, t_VEC);
gel(p1, 1) = gcopy(a);
gel(p1, 2) = gcopy(b);
@@ -32,7 +201,7 @@ largest_prime(GEN p, GEN a, GEN b, long prec)
avma = ltop;
return gen_0;
}
- G = get_point(e, o, maxprime_point);
+ G = find_point(e, o, maxprime_order(o, gen_0));
p2 = cgetg(8, t_VEC);
gel(p2, 1) = gcopy(p);
gel(p2, 2) = gcopy(a);
@@ -56,9 +225,7 @@ smallest_prime(GEN p, GEN a, GEN b, long prec)
{
pari_sp ltop = avma;
GEN e = gen_0, o = gen_0, G = gen_0;
- GEN p1 = gen_0; /* vec */
- GEN minprime_point = pol_x(fetch_user_var("minprime_point"));
- GEN p2 = gen_0; /* vec */
+ GEN p1 = gen_0, p2 = gen_0; /* vec */
p1 = cgetg(3, t_VEC);
gel(p1, 1) = gcopy(a);
gel(p1, 2) = gcopy(b);
@@ -69,7 +236,7 @@ smallest_prime(GEN p, GEN a, GEN b, long prec)
avma = ltop;
return gen_0;
}
- G = get_point(e, o, minprime_point);
+ G = find_point(e, o, minprime_order(o, gen_0));
p2 = cgetg(8, t_VEC);
gel(p2, 1) = gcopy(p);
gel(p2, 2) = gcopy(a);
@@ -94,7 +261,6 @@ all_prime(GEN p, GEN a, GEN b, long prec)
pari_sp ltop = avma;
GEN e = gen_0, o = gen_0, G = gen_0;
GEN p1 = gen_0; /* vec */
- GEN prime_orders = pol_x(fetch_user_var("prime_orders"));
long l2;
GEN p3 = gen_0; /* vec */
p1 = cgetg(3, t_VEC);
@@ -107,7 +273,7 @@ all_prime(GEN p, GEN a, GEN b, long prec)
avma = ltop;
return gen_0;
}
- G = get_points(e, o, prime_orders);
+ G = find_points(e, o, prime_orders(o, gen_0));
l2 = glength(G);
{
long X;
diff --git a/gp.gp b/gp.gp
new file mode 100644
index 0000000..a7a996a
--- /dev/null
+++ b/gp.gp
@@ -0,0 +1,2 @@
+\rpoints
+\rsea
diff --git a/gp.h b/gp.h
new file mode 100644
index 0000000..740b4c2
--- /dev/null
+++ b/gp.h
@@ -0,0 +1,35 @@
+/*-*- compile-command: "cc -c -o gp.gp.o -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -I"/usr/include/x86_64-linux-gnu" gp.gp.c && cc -o gp.gp.so -shared -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -Wl,-shared -Wl,-z,relro gp.gp.o -lc -lm -L/usr/lib/x86_64-linux-gnu -lpari"; -*-*/
+#include <pari/pari.h>
+/*
+GP;install("init_gp","v","init_gp","./gp.gp.so");
+GP;install("find_point","D0,G,D0,G,D0,G,","find_point","./gp.gp.so");
+GP;install("find_points","D0,G,D0,G,D0,G,","find_points","./gp.gp.so");
+GP;install("maxprime_order","D0,G,D0,G,","maxprime_order","./gp.gp.so");
+GP;install("minprime_order","D0,G,D0,G,","minprime_order","./gp.gp.so");
+GP;install("max_order","D0,G,D0,G,","max_order","./gp.gp.so");
+GP;install("get_point","D0,G,D0,G,D0,G,","get_point","./gp.gp.so");
+GP;install("prime_orders","D0,G,D0,G,","prime_orders","./gp.gp.so");
+GP;install("get_points","D0,G,D0,G,D0,G,","get_points","./gp.gp.so");
+GP;install("largest_prime","D0,G,D0,G,D0,G,p","largest_prime","./gp.gp.so");
+GP;install("smallest_prime","D0,G,D0,G,D0,G,p","smallest_prime","./gp.gp.so");
+GP;install("all_prime","D0,G,D0,G,D0,G,p","all_prime","./gp.gp.so");
+GP;install("small_pubkey","D0,G,D0,G,D0,G,p","small_pubkey","./gp.gp.so");
+GP;install("print_params","vD0,G,","print_params","./gp.gp.so");
+GP;install("print_params_pub","vD0,G,","print_params_pub","./gp.gp.so");
+*/
+void init_gp(void);
+GEN find_point(GEN e, GEN o, GEN n);
+GEN find_points(GEN e, GEN o, GEN p);
+GEN maxprime_order(GEN e, GEN o);
+GEN minprime_order(GEN e, GEN o);
+GEN max_order(GEN e, GEN o);
+GEN get_point(GEN e, GEN o, GEN f);
+GEN prime_orders(GEN e, GEN o);
+GEN get_points(GEN e, GEN o, GEN f);
+GEN largest_prime(GEN p, GEN a, GEN b, long prec);
+GEN smallest_prime(GEN p, GEN a, GEN b, long prec);
+GEN all_prime(GEN p, GEN a, GEN b, long prec);
+GEN small_pubkey(GEN p, GEN a, GEN b, long prec);
+void print_params(GEN curve);
+void print_params_pub(GEN curve);
+/*End of prototype*/
diff --git a/points.c b/points.c
deleted file mode 100644
index 9fe5a56..0000000
--- a/points.c
+++ /dev/null
@@ -1,181 +0,0 @@
-#include "points.h"
-
-void
-init_points(void) /* void */
-{
- pari_sp ltop = avma;
- avma = ltop;
- return;
-}
-
-/* Finds random point of order n on curve e of order o.
-* @returns [[P.x, P.y], n, h]
-* @param e curve
-* @param o curve order
-* @param n desired point order
-*/
-GEN
-find_point(GEN e, GEN o, GEN n) /* vec */
-{
- pari_sp ltop = avma;
- GEN h = gen_0, P = gen_0;
- GEN p1 = gen_0; /* vec */
- h = gdivent(o, n);
- {
- pari_sp btop = avma;
- do
- {
- P = genrand(e);
- if (gc_needed(btop, 1))
- P = gerepilecopy(btop, P);
- } while(gequal0(ellmul(e, P, n)));
- }
- p1 = cgetg(4, t_VEC);
- gel(p1, 1) = gcopy(P);
- gel(p1, 2) = gcopy(n);
- gel(p1, 3) = gcopy(h);
- p1 = gerepilecopy(ltop, p1);
- return p1;
-}
-
-/* Finds random points of orders given by vector p.
-* @returns vector of points in format [[P.x, P.y], n, h]
-* @param e curve
-* @param o curve order
-* @param p vector of point orders
-*/
-GEN
-find_points(GEN e, GEN o, GEN p) /* vec */
-{
- pari_sp ltop = avma;
- long l1;
- GEN p2 = gen_0; /* vec */
- l1 = glength(p);
- {
- long X;
- p2 = cgetg(l1+1, t_VEC);
- for (X = 1; X <= l1; ++X)
- gel(p2, X) = find_point(e, o, gel(p, X));
- }
- p2 = gerepilecopy(ltop, p2);
- return p2;
-}
-
-/*####################################################################*/
-
-GEN
-maxprime_order(GEN e, GEN o)
-{
- pari_sp ltop = avma;
- if (!gequal0(gisprime(o, 0)))
- {
- o = gerepilecopy(ltop, o);
- return o;
- }
- else
- {
- GEN p1 = gen_0;
- p1 = vecmax(factor(o));
- p1 = gerepilecopy(ltop, p1);
- return p1;
- }
- avma = ltop;
- return gen_0;
-}
-
-GEN
-minprime_order(GEN e, GEN o)
-{
- pari_sp ltop = avma;
- if (!gequal0(gisprime(o, 0)))
- {
- o = gerepilecopy(ltop, o);
- return o;
- }
- else
- {
- GEN p1 = gen_0;
- p1 = gcopy(gcoeff(factor(o), 1, 1));
- p1 = gerepilecopy(ltop, p1);
- return p1;
- }
- avma = ltop;
- return gen_0;
-}
-
-GEN
-max_order(GEN e, GEN o)
-{
- pari_sp ltop = avma;
- o = gerepilecopy(ltop, o);
- return o;
-}
-
-/* Finds a random point of order given by f(o).
-* @returns [[P.x, P.y], n, h]
-* with P being the point with order f(o).
-* @param e curve
-* @param o curve order
-* @param f function returning the point order, \in maxprime_order,
-* minprime_order, max_order
-*/
-GEN
-get_point(GEN e, GEN o, GEN f) /* vec */
-{
- pari_sp ltop = avma;
- GEN p1 = gen_0; /* vec */
- p1 = find_point(e, o, closure_callgen1(f, o));
- p1 = gerepilecopy(ltop, p1);
- return p1;
-}
-
-/*####################################################################*/
-
-GEN
-prime_orders(GEN e, GEN o)
-{
- pari_sp ltop = avma;
- GEN f = gen_0;
- if (!gequal0(gisprime(o, 0)))
- {
- GEN p1 = gen_0; /* vec */
- p1 = cgetg(2, t_VEC);
- gel(p1, 1) = gcopy(o);
- p1 = gerepilecopy(ltop, p1);
- return p1;
- }
- else
- {
- long l2;
- GEN p3 = gen_0; /* vec */
- f = factor(o);
- l2 = glength(f);
- {
- long X;
- p3 = cgetg(l2+1, t_VEC);
- for (X = 1; X <= l2; ++X)
- gel(p3, X) = gcopy(gcoeff(f, X, 1));
- }
- p3 = gerepilecopy(ltop, p3);
- return p3;
- }
- avma = ltop;
- return gen_0;
-}
-
-/* Finds random points of orders given by f(o).
-* @returns vector of points in format [[P.x, P.y], n, h]
-* @param e curve
-* @param o curve order
-* @param f function returning a vector of point orders
-*/
-GEN
-get_points(GEN e, GEN o, GEN f) /* vec */
-{
- pari_sp ltop = avma;
- GEN p1 = gen_0; /* vec */
- p1 = find_points(e, o, closure_callgen1(f, o));
- p1 = gerepilecopy(ltop, p1);
- return p1;
-}
-
diff --git a/points.h b/points.h
deleted file mode 100644
index 302a222..0000000
--- a/points.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/*-*- compile-command: "cc -c -o points.gp.o -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -I"/usr/include/x86_64-linux-gnu" points.gp.c && cc -o points.gp.so -shared -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -Wl,-shared -Wl,-z,relro points.gp.o -lc -lm -L/usr/lib/x86_64-linux-gnu -lpari"; -*-*/
-#include <pari/pari.h>
-/*
-GP;install("init_points","v","init_points","./points.gp.so");
-GP;install("find_point","D0,G,D0,G,D0,G,","find_point","./points.gp.so");
-GP;install("find_points","D0,G,D0,G,D0,G,","find_points","./points.gp.so");
-GP;install("maxprime_order","D0,G,D0,G,","maxprime_order","./points.gp.so");
-GP;install("minprime_order","D0,G,D0,G,","minprime_order","./points.gp.so");
-GP;install("max_order","D0,G,D0,G,","max_order","./points.gp.so");
-GP;install("get_point","D0,G,D0,G,D0,G,","get_point","./points.gp.so");
-GP;install("prime_orders","D0,G,D0,G,","prime_orders","./points.gp.so");
-GP;install("get_points","D0,G,D0,G,D0,G,","get_points","./points.gp.so");
-*/
-void init_points(void);
-GEN find_point(GEN e, GEN o, GEN n);
-GEN find_points(GEN e, GEN o, GEN p);
-GEN maxprime_order(GEN e, GEN o);
-GEN minprime_order(GEN e, GEN o);
-GEN max_order(GEN e, GEN o);
-GEN get_point(GEN e, GEN o, GEN f);
-GEN prime_orders(GEN e, GEN o);
-GEN get_points(GEN e, GEN o, GEN f);
-/*End of prototype*/
diff --git a/sea.gp b/sea.gp
index 6679b73..5f5cf2c 100644
--- a/sea.gp
+++ b/sea.gp
@@ -1,3 +1,4 @@
+\rpoints
/* E(Fp): y^2 = x^3 + ax + b mod p
* @returns [p, a, b, [G.x, G.y], n, h]
* @param p
@@ -10,7 +11,7 @@ largest_prime(p, a, b) = {
o = ellsea(e);
if(!o, return);
- G = get_point(e, o, maxprime_point);
+ G = find_point(e, o, maxprime_order(o));
return([p, a, b, lift(G[1][1]), lift(G[1][2]), G[2], G[3]]);
}
@@ -26,7 +27,7 @@ smallest_prime(p, a, b) = {
o = ellsea(e);
if(!o, return);
- G = get_point(e, o, minprime_point);
+ G = find_point(e, o, minprime_order(o));
return([p, a, b, lift(G[1][1]), lift(G[1][2]), G[2], G[3]]);
}
@@ -42,7 +43,7 @@ all_prime(p, a, b) = {
o = ellsea(e);
if(!o, return);
- G = get_points(e, o, prime_orders);
+ G = find_points(e, o, prime_orders(o));
return(vector(length(G),X,[p, a, b, lift(G[X][1][1]), lift(G[X][1][2]), G[X][2], G[X][3]]));
}
diff --git a/sea.h b/sea.h
deleted file mode 100644
index be27ed6..0000000
--- a/sea.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*-*- compile-command: "cc -c -o sea.gp.o -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -I"/usr/include/x86_64-linux-gnu" sea.gp.c && cc -o sea.gp.so -shared -g -O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC -Wl,-shared -Wl,-z,relro sea.gp.o -lc -lm -L/usr/lib/x86_64-linux-gnu -lpari"; -*-*/
-#include <pari/pari.h>
-/*
-GP;install("init_sea","v","init_sea","./sea.gp.so");
-GP;install("largest_prime","D0,G,D0,G,D0,G,p","largest_prime","./sea.gp.so");
-GP;install("smallest_prime","D0,G,D0,G,D0,G,p","smallest_prime","./sea.gp.so");
-GP;install("all_prime","D0,G,D0,G,D0,G,p","all_prime","./sea.gp.so");
-GP;install("small_pubkey","D0,G,D0,G,D0,G,p","small_pubkey","./sea.gp.so");
-GP;install("print_params","vD0,G,","print_params","./sea.gp.so");
-GP;install("print_params_pub","vD0,G,","print_params_pub","./sea.gp.so");
-*/
-void init_sea(void);
-GEN largest_prime(GEN p, GEN a, GEN b, long prec);
-GEN smallest_prime(GEN p, GEN a, GEN b, long prec);
-GEN all_prime(GEN p, GEN a, GEN b, long prec);
-GEN small_pubkey(GEN p, GEN a, GEN b, long prec);
-void print_params(GEN curve);
-void print_params_pub(GEN curve);
-/*End of prototype*/