1 \input texinfo @c -*-texinfo-*-
3 @setfilename g2hecdoc.info
4 @settitle The Genus 2 Crypto C++ Library v0.1
7 @c %** Summary description and copyright. Appears only in info file.
9 This is a help file for the g2hec library version 0.1.
11 Copyright @copyright{} 2008 N. Shang.
13 @c %** end of summary description and copyright
15 @c %** Titlepage, contents, copyright
17 @title The Genus 2 Crypto C++ Library v0.1
20 @c The following comments start the copyright page.
22 @vskip 0pt plus 1filll
24 Copyright @copyright{} 2008 N. Shang.
27 @c Output the table of contents
33 @c %** end of titlepage, contents, copyright
35 @c %** Top node and master menu
37 @node Top, Overview, ,
40 This manual shows how to install and use the G2HEC (Genus 2 HyperElliptic
47 * Overview:: Introduction to the g2hec library
49 * Installation:: Installation instructions
51 * Quick start:: A web demo of genus 2 scalar multiplication
53 * Tutorial:: Toy examples
55 * Genus 2 curve functions:: Curve-related functions
57 * Divisor functions:: Divisor-related functions
59 * Divisor arithmetic:: Arithmetic-related functions
61 * I/O:: Low-level input/output -- what mathematicians care
63 * Randomness functions:: Random curve, random divisor
68 @c %** end of top node and master menu
70 @c %** Body of the document
71 @node Overview, Installation, Top, Top
73 @section What is G2HEC?
74 The G2HEC (Genus 2 HyperElliptic Curve) library is a free portable C++ library
75 providing divisor group operations in the Jacobian of genus 2 (imaginary)
76 hyperelliptic curve. Such curves can be used for discrete-logarithm-based
77 cryptosystems with advantages over elliptic curves (genus 1 curves). Divisor
78 group operations are essential to using genus 2 curves for cryptography.
82 It is built on top of V. Shoup's NTL library, "a high performance, portable C++
83 library providing data structures and algorithms for arbitrary length integers;
84 for vectors, matrices, and polynomials over the integers and over finite
85 fields; and for arbitrary precision floating point arithmetic."
88 It is recommended to build NTL using GMP (the GNU Multi-Precision package) for
89 best performance. However, the G2HEC library can be built and used with and
90 without the existence of GMP.
93 This library does not assume users' familiarity with any non-trivial math
94 background except basic concepts of finite fields and polynomials. No prior
95 knowledge of genus 2 curve is needed to used the library.
98 The G2HEC library is released under the GNU General Public License. See the
99 file COPYING for details.
102 The G2HEC library homepage: @uref{http://www.math.purdue.edu/~nshang/g2hec/}.
105 The NTL library homepage: @uref{http://www.shoup.net/ntl/}.
108 The GMP library homepage: @uref{http://www.swox.com/gmp/}.
110 @section Genus 2 curve basics
111 Our object of interest, a genus 2 curve, is a nonsingular algebraic curve
112 @center @math{C: y^2 + h(x)y = f(x)}
113 over a finite field @math{GF(q)}, where @math{f(x)} is monic (i.e.,
114 has leading coefficient 1) of degree 5, @math{h(x)} has degree not greater
115 than 2, @math{q} is a prime or a power of a prime.
118 Currently (version 0.1) G2HEC only supports odd prime @math{q} by default.
119 You can ``hack'' the header file in @code{include/g2hec_nsfieldtype.h} to
120 switch its support to @math{q} as a power of an odd prime. However, we do not
121 recommend to do so and it should be avoided except in extreme cases. So far
122 the library is tested only for odd prime numbers @math{q}.
125 There is currently no ``hack'' to have field types @code{GF2} and @code{GF2E}
126 supported (curve singularity test does not work for characteristic 2 yet).
127 Support for characteristic 2 maybe added in the future.
130 A pair @math{(x_0, y_0)} with @math{x_0, y_0\in GF(q)} such that
131 @math{x = x_0} and @math{y = y_0} give a solution to the curve equation of
132 @math{C} is called a @b{@math{GF(q)}-rational point of the genus 2 curve
134 Obviously, @math{C} has only finitely many @math{GF(q)}-rational points.
135 Unlike the case of elliptic curves, the set of @math{GF(q)}-rational points
136 of the curve @math{C} does not naturally hold a group structure, therefore they
137 cannot be used directly to do discrete-logarithm-based cryptography.
138 Instead, we choose to use the @b{@math{GF(q)}-rational points of the Jacobian}
140 @math{C}, which is a finite group, to achieve our (cryptographic) goal.
141 It is not necessary for the users of the G2HEC library know how the Jacobian
142 of a genus 2 curve is defined, given the following two facts:
145 The @math{GF(q)}-rational points of the Jacobian of a genus 2 curve @math{C},
146 @math{Jac(C, GF(q))}, is a finite group of size approximately @math{q^2}.
149 An element of @math{Jac(C, GF(q))} is called a @b{divisor class} (@b{divisor}
150 in short) over @math{GF(q)}. A divisor (class) @math{D} is represented by a
151 pair of polynomials @math{[u(x), v(x)]} over @math{GF(q)}. Several conditions
152 need to be satisfied for a pair @math{[u(x), v(x)]} to be a divisor of
157 The unit element of @math{Jac(C, GF(q))} is @math{[1, 0]}.
160 By convention, the group operation in the Jacobian of the curve @math{C} is
161 written additively. We shall write
162 @center @math{D=D_1+D_2, D=D_1-D_2, D=[n]D_1}
163 for @b{divisor addition, divisor subtraction} and @b{divisor scalar
164 multiplication}, respectively, where @math{D=[n]D_1} means
165 @math{D=D_1+D_1+...+D_1} for @math{n} times.
168 More information about the math background of the Jacobian of genus 2 curves
169 can be found in the book ``Handbook of Elliptic And Hyperelliptic Curve
170 Cryptography'' by Cohen et al.
173 In addition to the high-level functions that operates on curves and divisors,
174 the G2HEC library also exports low-level functions that allow knowledgeable
175 users to take control of mathematical factors of curves and divisors. Our
176 intent is to make the library useful for both mathematicians and cryptographers
177 (or the ones who play both roles).
180 @node Installation, Quick start, Overview, Top
181 @chapter Installation
183 The G2HEC library is dependent of V. Shoup's NTL library, which can be found at
184 @uref{http://www.shoup.net/ntl/, NTL: A Library for doing Number Theory}.
187 The following procedure should work on Unix and Unix-like platforms. The
188 installation has been tested on Linux and Solaris.
191 To obtain the source code and documentation for G2HEC, download
192 @uref{http://www.math.purdue.edu/~nshang/g2hec/, g2hec-lib-xxx.tar.gz}.
193 Here ``xxx'' denotes the current version number.
195 @section General installation instructions
196 If the NTL library is installed in a standard system directory, then do the
200 % gunzip g2hec-lib-xxx.tar.gz
201 % tar xvf g2hec-lib-xxx.tar
203 % ./configure --prefix=$HOME/nssw
210 This will build, test and install G2HEC in @code{$HOME/nssw}. You can change
211 to a different location by specifying it with the @code{--prefix=} option (the
212 default is @code{/usr/local}). After installation, you will find the G2HEC
214 @code{$HOME/nssw/include} and the compiled static library @code{libg2hec.a} in
215 @code{$HOME/nssw/lib/}.
217 @section NTL not in default location
218 If you have installed the NTL/GMP libraries into locations which require
219 special include or library paths, you can pass them to @code{LDFLAGS} and
220 @code{CXXFLAGS} variables for the configure script.
221 For example (for bash)
224 % env LDFLAGS=-L$HOME/nssw/lib CXXFLAGS=-I$HOME/nssw/include ./configure
229 Depending on whether GMP is found in the search path or not, the G2HEC library
230 is configured to build with or without GMP.
232 @section After G2HEC is built
233 After the library is built, executing @code{make check} runs some test
237 It is highly recommended to do this to make sure things went well.
240 Executing @code{make install} copies a number of files to a directory
242 that you specify by passing @code{--prefix=<prefix>} as an argument
243 to @code{configure} at configuration time, or as an argument to
244 @code{make install} at installation time. Recall that the default is
248 To uninstall the library, execute @code{make uninstall}.
251 To remove object files, execute @code{make clean}.
254 Note that this installation process is almost standard.
257 Assuming you have installed G2HEC as above, to compile program @code{myprog.C}
261 % g++ -I<prefix>/include -I<ntl_prefix>/include -L<prefix>/lib \
262 -L<ntl_prefix>/lib myprog.c -o myprog -lg2hec -lntl -lm
266 The binary file @code{myprog} is created.
269 If you built NTL using GMP, do
272 % g++ -I<prefix>/include -I<ntl_prefix>/include -L<prefix>/lib \
273 -L<ntl_prefix>/lib -L<gmp_prefix>/lib myprog.c -o myprog -lg2hec \
278 Of course, there is no need to duplicate the flags if some of the locations are
279 the same, and you may leave out the corresponding flags if the locations are
280 standard directories.
282 @node Quick start, Tutorial, Installation, Top
285 @section A web demo of divisor scalar multiplication
286 You can play around with divisor scalar multiplication on the following demo
288 @center @uref{http://www.math.purdue.edu/~nshang/g2hec/demo.html}.
291 This demo page shows an application built on top of the G2HEC library.
292 Although you may smell a bit of mathematics from this demo, in general
293 programmers can build cryptographic algorithms/protocols without knowing
294 any college-level math.
296 @node Tutorial, Genus 2 curve functions, Quick start, Top
298 We shall walk you through several cryptographic applications of G2HEC.
300 @section HEC-Diffie-Hellman key exchange
301 The basic Diffie-Hellman key exchange protocol works as follows:
304 Alice and Bob wish to agree on a secret random element over an insecure channel
305 without having to exchange any information previously.
308 They agree on an element @math{P} in the Jacobian of the genus 2 curve
309 @math{C} with a large (known or unknown) order @math{G}.
312 Alice generates a random integer @math{a \in @{1, \ldots, \#G - 1@}}, then
313 sends to Bob the element (divisor)
314 @center @math{Q_1 = [a] P}.
317 Bob generates a random integer @math{b \in @{1, \ldots, \#G - 1@}}, then
318 sends to Alice the element (divisor)
319 @center @math{Q_2 = [b] P}.
323 @center @math{[ab] P = [a] Q_2}.
326 Similarly, Bob computes
327 @center @math{[ab] P = [b] Q_1}.
331 This element @math{[ab] P} now serves as the secret that only Alice and Bob
335 We illustrate a local version of this protocol using a genus 2 curve over a
336 prime field @math{GF(p)}, for an educational purpose.
339 First we include the header file g2hec_Genus2_ops.h:
341 #include <g2hec_Genus2_ops.h>
345 Then we define the maximum length (in decimal digits) of the prime number
348 #undef MAX_STRING_LEN
349 #define MAX_STRING_LEN 300
353 The following statement invokes the namespace used by the G2HEC library, and
354 must be present for every client program.
360 In the main function, we first set the pseudo-random number generator seed, and
361 allocate a string buffer for receiving a prime number
364 SetSeed(to_ZZ(1234567890));
365 char p[MAX_STRING_LEN];
367 cin.getline(p, MAX_STRING_LEN);
371 We declare and initialize an NTL big integer object @code{pZZ} to store this
378 Now we initialize the underline prime field @math{GF(p)}:
384 We shall declare and initialize several elements to hold the genus 2 curve,
385 system parameters, keys and so on:
393 Generate a random valid genus 2 curve:
399 Set curve for the divisors. A curve needs only to be set once in a program
400 to work for all divisors.
406 To perform Diffie-Hellman key exchange, it is not necessary to know the order
407 of @math{P}. A fact is that this order is close to @math{p^2}. So we choose
408 @math{a} and @math{b} to be two random number bounded by @math{p^2}.
410 RandomBnd(a, pZZ*pZZ);
411 RandomBnd(b, pZZ*pZZ);
415 We generate a random base point @math{P}:
433 A successful key exchange should yield the shared secret
434 @center @math{[ab]P = [a] Q_2 = [b] Q_1}:
436 if ( b * Q1 == a * Q2)
437 cout << "DH key exchange succeeded!" << endl;
439 cout << "DH key exchange failed!" << endl;
443 A complete source file for this example can be found in @code{example/dh.C}.
445 @section HEC-ElGamal encryption
446 This example is similar to the Diffie-Hellman key exchange example. Please
447 refer to the source file in @code{examples/elgamal_enc.C} for details.
449 @section HEC-ElGamal digital signature
450 We shall use G2HEC to build a digital signature scheme: the ElGamal digital
454 Recall how this signature scheme works:
457 Bob chooses a genus 2 curve @math{C} and a prime number @math{p} so that the
458 number of @math{GF(p})-rational points of the Jacobian of @math{C} has a large
459 prime factor @math{N} -- preferably the number itself is prime. He then chooses
460 a divisor @math{g} of order @math{N}, a secret private key @math{x\in\{1,
461 \ldots, N - 1\}}, then computes a divisor @math{h = [x] g}. Bob publishes
462 @math{g} and @math{h} as his public key.
465 To sign a message @math{m \in Z/(N)}, Bob first generates a random integer
466 @math{i \in @{1, \ldots, N - 1@}}, and computes
467 @center @math{a = [k] g}.
470 Bob then computes a solution, @math{b \in Z/(N)} to the congruence
471 @center @math{m @equiv{} x f(a) + b k (mod N) }.
472 Here @math{f} is a map from the Jacobian of @math{C} to @math{Z/(N)} which
476 Bob sends the signature @math{(a, b)} together with the message @math{m}
480 Upon receiving the message and signature from Bob, Alice verifies the signature
482 @center @math{[f(a)] h + [b] a = [m] g}
486 It needs to be point out that a technical aspect of this algorithm involves
487 the choice of the curve @math{C} and the prime number @math{p} so that its
488 Jacobian has an order divisible by a large prime number. This is not a
489 trivial task -- it involves quite amount of mathematics. Fortunately, there
490 are published data that users can use directly to avoid this difficulty.
491 According to the paper ``Construction of Secure Random Curves of Genus 2
492 over Prime Fields'' by Gaudry and Schost, we choose to use in our example
495 $$C: y^2 = x^5 + 2682810822839355644900736 x^3
496 + 226591355295993102902116 x^2
498 $$ + 2547674715952929717899918 x
499 + 4797309959708489673059350$$
501 with @math{p = 5 \cdot 10^{24} + 850349}. The order of the Jacobian of the
502 curve is a prime number
503 @center @math{N = 2499999999999413043860099940220946396619751607569,}
504 which is to our best interest. In this case, we can pick any random non-unit
505 divisor @math{g} as our base point.
508 At this point, we are able present a local version of this signature scheme.
511 As usual we include the header file @code{g2hec_Genus2_ops.h}:
513 #include <g2hec_Genus2_ops.h>
517 Then we define some macros that we are going to use, including the values of
518 coefficients of the polynomial @math{f}, the prime number @math{p}, the group
519 order of the Jacobian, and a function that maps a suitable value to an NTL
523 #define f3 "2682810822839355644900736"
524 #define f2 "226591355295993102902116"
525 #define f1 "2547674715952929717899918"
526 #define f0 "4797309959708489673059350"
527 #define ps "5000000000000000008503491"
528 #define N "24999999999994130438600999402209463966197516075699"
530 #define str_to_ZZ_p(x) to_ZZ_p(to_ZZ(x))
534 Also we issue the statement
540 Recall that we need an almost bijection that maps elements in the Jacobian to
541 an element in @math{@{1, \ldots, N - 1@}}. This map can be chosen as follows:
543 static ZZ from_divisor_to_ZZ(const divisor& div, const ZZ& n)
545 poly_t u = div.get_upoly();
546 ZZ temp = AddMod(sqr(rep(u.rep[0])), sqr(rep(u.rep[1])), n);
547 return ( IsZero(temp) ? to_ZZ(1) : temp );
550 We just mention that we choose this function to take a divisor
551 @center @math{[u(x), v(x)]}
552 to the value defined by the sum of the squares of the degree 0 and degree 1
553 coefficients modulo the group order @math{N} of the Jacobian. Users can
554 define their own such function to use. Security might be affected by bad
555 choice of this almost injective function.
558 We start working on the @code{main} function by initializing the PRNG seed and
559 declaring and define several values:
561 SetSeed(to_ZZ(1234567890));
570 // Private key x, random number k, parameter b, message m
582 Then we manually assign values of polynomial @math{f} and define the
583 corresponding genus 2 curve:
587 SetCoeff(f, 3, str_to_ZZ_p(f3));
588 SetCoeff(f, 2, str_to_ZZ_p(f2));
589 SetCoeff(f, 1, str_to_ZZ_p(f1));
590 SetCoeff(f, 0, str_to_ZZ_p(f0));
595 Then you update the curve information:
599 This will update some flags related to properties of the curve, and needs to
600 be done immediately after manual assignments to the curve's defining elements
601 (e.g., the polynomial @math{f}).
604 Now we are ready to set the curve as the underlying curve of the divisors.
610 We randomly choose the base point @math{g}, message @math{m} to be signed, the
611 private key @math{x}, and the public key @math{h}:
615 @} while (g.is_unit());
621 @} while (IsZero(x));
625 Note that we want the base point @math{g} to be any divisor except the unit
626 divisor, and we do not allow the private key @math{x} to be 0. This is what
627 the @math{do...while} statements do.
630 Now we shall generate the ElGamal signature @math{(a, b)}:
634 @} while (IsZero(k));
638 f_a = from_divisor_to_ZZ(a, order);
640 /* b = (m - x*f(a))/k mod N */
641 b = MulMod(m - x * f_a, InvMod(k, order), order);
643 The element @code{f_a} holds the value @math{f(a)} given by the almost
644 injection applied to the divisor @math{a}.
647 Alright! Now the most exciting moment has arrived -- signature verification:
649 if ( f_a * h + b * a == m * g )
650 cout << "ElGamal signature verification succeeded!" << endl;
652 cout << "ElGamal signature verification failed!" << endl;
657 The complete source file can be found in @code{examples/elgamal_sig.C}.
660 **************************************************************@*
661 In the following chapters, we will give a general overview of the G2HEC's
662 programming interface. This includes:
665 Functions related to genus 2 curve generation and manipulation
668 Functions related to divisor generation and manipulation
671 Divisor arithmetic functions
674 Input and output functions
680 @c Miscellaneous functions
683 All these interfaces are exported by @code{include/g2hec_Genus2_ops.h} unless
686 **************************************************************
688 @node Genus 2 curve functions, Divisor functions, Tutorial, Top
689 @chapter Genus 2 curve functions
690 We only consider the so-called ``imaginary'' genus 2 curves, i.e., curves given
692 @center @math{C: y^2 + h(x) y = f(x),}
693 with @math{\deg{h}\le 2} and @math{\deg{f} = 5}. We also require that
695 be nonsingular and the leading coefficient of @math{f(x)} be 1.
698 Types @code{field_t} and @code{poly_t} are for the field element
699 type and the corresponding polynomial type, respectively. They are defined
700 in the header file @code{include/g2hec_nsfieldtype.h}. Type @code{poly_tc}
701 is the type @code{const poly_t}.
704 A genus 2 curve is stored in the @code{g2hcurve} class.
707 Below are some of the class's public member functions. They are exported in
708 the file @code{include/g2hec_Genus2_ops.h}.
711 @code{g2hcurve()}: @*
712 default constructor; set curve to @math{y^2 = 0}.
715 @code{g2hcurve(const poly_tc& poly1, const poly_tc& poly2)}: @*
716 constructor; sets curve to @math{y^2 + @code{poly2}*y = @code{poly1}}.
719 @code{g2hcurve(const g2hcurve& hcurve)}: @*
723 @code{void set_f(const poly_tc& poly)}: @*
724 sets @math{f(x)} to @code{poly}.
727 @code{void set_h(const poly_tc& poly)}: @*
728 sets @math{h(x)} to @code{poly}.
731 @code{void update()}: @*
732 checks and set internal flags to determine whether
733 the curve is nonsingular and of genus 2. This function MUST be called
734 immediately after the curve has been changed by @code{set_f()},
735 @code{set_h()}, or/and assignment, and before any further operations
736 related to the curve. This is very important to remember, otherwise error
740 @code{const poly_tc& get_f()}: @*
741 returns reference to polynomial @math{f(x)}.
744 @code{const poly_tc& get_h()}: @*
745 returns reference to polynomial @math{h(x)}.
748 @code{bool_t is_valid_curve()}: @*
749 returns TRUE is curve is nonsingular, genus 2, and @math{f(x)} is monic.
752 You can also write @code{curve1 = curve2} naturally to set the value of
753 @code{curve1} to be the same as @code{curve2}. It is not needed that
754 @code{curve2} be valid for this assignment.
757 You can do comparisons like
758 @code{curve1 == curve2} or @code{curve1 != curve2} to test if two curves
759 are the same. Curves do not need to be valid for these comparisons.
761 @node Divisor functions, Divisor arithmetic, Genus 2 curve functions, Top
762 @chapter Divisor functions
763 A divisor (class) is a pair of polynomials @math{[u(x), v(x)]} over
765 with @math{\deg{v}<\deg{u}\le 2} so that they satisfy certain conditions
766 related to the curve @math{C}. Therefore, every divisor (class) has a
767 curve to associate with. G2HEC use a C++ class @code{divisor} to hold
771 For cryptographic purposes, G2HEC does not support the existence of divisors
772 associated with different curves in the same running process. It implements
773 the associated genus 2 curve as a static data member of the @code{divisor}
774 class. The client program only needs to set the curve once for a divisor, and
775 it works for all divisors. A curve change for one divisor in a program will
776 cause all divisors in the same program to switch to the new curve. This is
777 another important convention of G2HEC.
780 We list some of the @code{divisor} class's public member functions as follows.
784 default constructor; sets @math{u(x) = 1, v(x) = 0}, and associated curve
785 as @math{y^2 = 0}. A divisor is not valid by default.
788 @code{divisor(const poly_tc& polyu, const poly_tc& polyv, const g2hcurve& curve)}: @*
789 constructor; set @math{u(x) = }@code{polyu}, @math{v(x) = }@code{polyv}, and
790 associated curve to @code{curve}.
793 @code{divisor(const divisor& div)}:@*
797 @code{void set_upoly(poly_tc& poly)}: @*
798 sets @math{u(x)} to @code{poly} for the divisor.
801 @code{void set_vpoly(poly_tc& poly)}: @*
802 sets @math{v(x)} to @code{poly} for the divisor.
805 @code{void set_curve(const g2hcurve& curve)}: @*
806 sets the associated curve to @code{curve} for divisor. The curve does
807 not need to be a valid genus 2 curve.
810 @code{void update()}: @*
811 This function checks and updates information related to the divisor. It must
812 be called by a client program after any @code{set_} routine is called.
813 Otherwise error make occur when operations on divisor are performed.
816 @code{const poly_tc& get_upoly()}: @*
817 returns @math{u(x)} of the divisor.
820 @code{const poly_tc& get_vpoly()}: @*
821 returns @math{v(x)} of the divisor.
824 @code{const g2hcurve& get_curve()}: @*
825 returns the associated curve of the divisor.
828 @code{bool_t is_valid_divisor()}: @*
829 returns @code{TRUE} is the representation of the divisor is valid, @code{FALSE}
833 @code{bool_t is_unit()}: @*
834 tests if the divisor is the unit divisor @math{[1, 0]}.
837 @code{set_unit()}: @*
838 sets the divisor to the unit divisor @math{[1, 0]}.
841 You can write @code{divisor1 = divisor2} to assign the value of @code{divisor2}
842 to @code{divisor1}. @code{divisor2} can be either valid or invalid.
845 You can do comparisons like @code{divisor1 == divisor2} or
846 @code{divisor1 != divisor2} to test if two divisors are the same. Divisors
847 do not need to be valid for such comparisons.
850 There is also a non-member function
851 @center @code{bool_t on_same_curve(const divisor& a, const divisor& b)}
852 that tests if two divisors @code{a} and @code{b} have the same associated
853 curve. According to the way G2HEC implements the @code{divisor} class,
854 this function always returns @code{TRUE}. Therefore this test is redundant.
856 @node Divisor arithmetic, I/O, Divisor functions, Top
857 @chapter Divisor arithmetic
858 The divisor operations are @b{addition, negation, subtraction} and
859 @b{scalar multiplication}.
862 The divisor arithmetic is implemented using the @b{explicit formulas}, which
863 is supposed to be faster than the generic one via @b{Cantor's algorithm}, for
864 the most common cases of the divisor operations. The Cantor's algorithm is
865 also implemented in G2HEC and used to handle special cases that rarely happen
866 in cryptographic applications.
869 The interfaces for divisor arithmetic are strait-forward. We list them in
870 below in both their procedure and operator version (if both version exist).
873 @code{bool_t add_cantor(divisor& x, const divisor& a, const divisor& b)}:@*
874 This is the Cantor's algorithm which computes @code{x = a + b}.
877 @code{bool_t add((divisor& x, const divisor& a, const divisor& b)},@*
878 @code{divisor operator+(const divisor& a, const divisor& b)}: @*
879 compute @code{x = a + b} via explicit formulas; special cases handled by
880 calling @code{add_cantor()}.
883 @code{bool_t sub((divisor& x, const divisor& a, const divisor& b)},@*
884 @code{divisor operator-(const divisor& a, const divisor& b)}: @*
885 compute @code{x = a - b}.
888 @code{bool_t dnegate(divisor& x, const divisor& a)}, @*
889 @code{divisor operator-(const divisor& a)}: @*
890 compute @code{x = -a}.
893 @code{scalar_mul(divisor& x, const divisor& a, const ZZ& n, bool_t (*method)(divisor&, const divisor&, const ZZ&))}, @*
894 @code{scalar_mul(divisor& x, const divisor& a, long n, bool_t (*method)(divisor&, const divisor&, const ZZ&))}: @*
895 compute @code{x = [n]a} using a method specified by @code{method}. Currently,
896 three methods are provided by G2HEC:
899 @code{SAM}: sum-and-square method
902 @code{NAF}: method using non-adjacent forms
905 @code{ML}: method of the @b{Montgomery's ladder}; used to resist side-channel
909 Setting @code{method} to @code{NULL} performs scalar multiplication via
914 @code{divisor operator*(const ZZ& n, const divisor& a)}, @*
915 @code{divisor operator*(const divisor& a, const ZZ& n)}, @*
916 @code{divisor operator*(long n, const divisor& a)}, @*
917 @code{divisor operator*(const divisor& a, long n)}: @*
918 return @code{[n]a}. @code{SAM} is used for scalar multiplication.
920 @node I/O, Randomness functions, Divisor arithmetic, Top
922 G2HEC exports several functions to output of polynomials, curves and divisors.
923 They might be more useful for mathematics research on genus 2 curves over
924 finite fields than for implementation of cryptographic protocols. For sake of
925 completeness, we briefly introduce them here.
928 @code{void print_poly(poly_t& poly, std::ostream *s)}: @*
929 directs a natural representation of polynomial @code{poly} to @code{s}.
932 For example, calling @code{print_poly(f, cout)} will display in the standard
937 if @code{f} is a @code{poly_t} object representing
938 @math{f(x) = x^5 + 3 x^2 + x}.
941 Calling @code{cout << curve; } will display in the standard output
944 Curve: y^2 + h(x)*y = f(x)
947 Genus 2 curve is nonsingular
950 if @code{curve} is a valid genus 2 curve, or
952 Curve: y^2 + h(x)*y = f(x)
955 Curve is singular, or not genus 2, or f(x) is not monic
958 if @code{curve} is not valid.
961 Calling @code{cout << divisor; } will display in the standard output
965 Divisor [u(x), v(x)] for Jacobian group of curve y^2 + h(x)*y = f(x).
966 Curve: y^2 + h(x)*y = f(x)
969 Genus 2 curve is nonsingular
977 if @code{divisor} is a valid divisor, or
980 Divisor [u(x), v(x)] for Jacobian group of curve y^2 + h(x)*y = f(x).
981 Curve: y^2 + h(x)*y = f(x)
984 Genus 2 curve is nonsingular
992 if @code{divisor} is not valid.
994 @node Randomness functions, Some issues, I/O, Top
995 @chapter Randomness functions
996 The G2HEC library uses NTL's pseudo-random number generator for generating
997 functions that output random values. It is recommended to override the
998 default seed by calling
999 @center @code{NTL::SetSeed( seed )}
1000 to set the PRNG seed using a @code{ZZ} object @code{seed} if you are to
1001 do serious cryptographic applications which use the G2HEC's randomness
1005 The G2HEC library exports in @code{include/g2hec_rand.h} a function
1006 @center @code{ZZ g2hec_rand()},
1007 which obtains a random @code{ZZ} object of 128-bit integer by trying
1008 to use the file /dev/urandom as a source of random bits; if it fails,
1012 User can set its own random seed, or call
1013 @center @code{NTL::SetSeed(g2hec_rand())}
1017 The class @code{g2hcurve} has a member function to generate a random
1018 valid genus 2 curve.
1021 @code{g2hcurve& random()}: @*
1022 generates a random valid genus 2 curve.
1025 The class @code{divisor} has two member functions to generate random
1026 valid divisors, if the associated curve is valid.
1029 @code{divisor& random()}: @*
1030 If the associated curve is valid, sets the divisor to a random valid
1031 divisor of degree 2, i.e., @math{\deg{u} = 2}.
1034 @code{divisor& random(divdeg_t dgr)}: @*
1035 If the associated curve is valid, sets divisor to a random valid divisor
1036 of degree @code{dgr}, where @code{dgr} takes values 1 (DEGREE_1) or
1040 Note that @code{divisor.random()} never returns a unit divisor @math{[1, 0]}.
1042 @node Some issues, , Randomness functions, Top
1043 @chapter Some issues
1045 Most functions in the G2HEC are reentrant, with exception of functions
1046 that set the associate curve for a divisor, such as
1047 @code{divisor::set_curve} and a constructor of the @code{divisor} class.
1048 It is considered an unchecked programming error to change the associated
1049 curve when performing divisor operations.
1051 @c %** end of body of the document
1053 @c %** End of the document
1055 @c %** end of end of the document