diff options
Diffstat (limited to 'src/gp')
| -rw-r--r-- | src/gp/equation.gp | 28 | ||||
| -rw-r--r-- | src/gp/field.gp | 33 | ||||
| -rw-r--r-- | src/gp/gp.gp | 7 | ||||
| -rw-r--r-- | src/gp/invalid.gp | 59 | ||||
| -rw-r--r-- | src/gp/utils.gp | 52 |
5 files changed, 179 insertions, 0 deletions
diff --git a/src/gp/equation.gp b/src/gp/equation.gp new file mode 100644 index 0000000..c1483e4 --- /dev/null +++ b/src/gp/equation.gp @@ -0,0 +1,28 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + +/** + * Constructs an elliptic curve in the form E: + * y^2 = x^3 + ax + b, over a prime field + * @param a + * @param b + * @param p + * @returns elliptic curve + */ +prime_weierstrass(a:int, b:int, field:gen) = { + return(ellinit([a,b], field)); +} + +/** + * Constructs an elliptic curve in the form E: + * y^2 + xy = x^3 + ax + b, over a binary field. + * @param a + * @param b + * @param field + * @returns elliptic curve + */ +binary_weierstrass(a:int, b:int, field:gen) = { + return(ellinit([1,0,0,a,b], field)); +} diff --git a/src/gp/field.gp b/src/gp/field.gp new file mode 100644 index 0000000..c428abd --- /dev/null +++ b/src/gp/field.gp @@ -0,0 +1,33 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + +/** + * Extract a field representation from a field. + * - char(field) == 2: + * returns the vector of powers of middle coefficients of the reduction polynomial. + * - char(field) != 2: + * returns the field characteristic(p). + * + * @return field representation + */ +field_params(field:gen) = { + if(type(field) == "t_INT", + return([field]); + ); + + local(out:vec, j:int, c:int); + out = vector(3); + + j = 1; + for(i=2, length(field.mod) - 2, + c = polcoeff(field.mod, i):int; + if(c != 0, + out[j] = i; + j++; + ); + ); + + return(out); +}
\ No newline at end of file diff --git a/src/gp/gp.gp b/src/gp/gp.gp new file mode 100644 index 0000000..0124958 --- /dev/null +++ b/src/gp/gp.gp @@ -0,0 +1,7 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + +\r gp/utils +\r gp/invalid
\ No newline at end of file diff --git a/src/gp/invalid.gp b/src/gp/invalid.gp new file mode 100644 index 0000000..4970014 --- /dev/null +++ b/src/gp/invalid.gp @@ -0,0 +1,59 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + +/** + * Computes primes upto some upper bound. + * + * @param bound an upper bound on primes + * @return a vector of primes up to bound^2 + */ +prime_upto(bound:int) = { + local(p:list, product:int, last:int, result:vec); + p = List(); + + bound = bound^2; + listput(p, 2); + product = 2; + last = 2; + + while(product < bound, + last = nextprime(last + 1); + listput(p, last); + product = product * last; + ); + + result = list_to_vec(p); + listkill(p); + return(result); +} + +/** + * + */ +invalid(coeffs:vec, field:pol, primes:vec, bits:int) = { + local(bs:vec, cs:vec, eq:vec, e:ell, b, n, c, o):int; + n = length(primes); + bs = vector(n); + eq = coeffs; + c = 0; + + while(c < n, + b = random_int(bits):int; + eq[4] = b; /* Times field? */ + + iferr(e = ellinit(eq,field):ell, E, next()); + + o = ellsea(e):int; + for(i=1,n, + if((o % primes[i]) == 0 && bs[i] == 0, + bs[i] = b; + cs[i] = e; + c = c + 1; + ); + ); + ); + + return(cs); +}
\ No newline at end of file diff --git a/src/gp/utils.gp b/src/gp/utils.gp new file mode 100644 index 0000000..932a44a --- /dev/null +++ b/src/gp/utils.gp @@ -0,0 +1,52 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + + random_primer(range:vec) = { + local(p:int); + until(isprime(p), + p = randomprime(range):int; + ); + return(p); + } + +/** + * Calculates a random prime of bit size bits. + * + * @param bits bit size of the requested prime + * @return random prime between 2^(bits - 1) and 2^bits + */ +random_prime(bits:small) = { + return(random_primer([2^(bits-1), 2^bits])); +} + +random_intr(range:vec) = { + return(random(range)); +} + +/** + * Generates a random integer with bit size bits. + * + * @param bits bit size of the requested integer + * @return random int between 2^(bits - 1) and 2^bits + */ +random_int(bits:small) = { + return(random_intr([2^(bits-1), 2^bits])); +} + +/** + * Converts a list to a vector. + * + * @param l list to convert + * @return a vector of the lists values + */ +list_to_vec(l:list) = { + local(v:vec, n:int); + n = length(l); + v = vector(n); + for(i=1, n, + v[i] = l[i]; + ); + return(v); +}
\ No newline at end of file |
