1 
2 /***************************************************************************
3                                                                            *
4 Copyright 2012 CertiVox IOM Ltd.                                           *
5                                                                            *
6 This file is part of CertiVox MIRACL Crypto SDK.                           *
7                                                                            *
8 The CertiVox MIRACL Crypto SDK provides developers with an                 *
9 extensive and efficient set of cryptographic functions.                    *
10 For further information about its features and functionalities please      *
11 refer to http://www.certivox.com                                           *
12                                                                            *
13 * The CertiVox MIRACL Crypto SDK is free software: you can                 *
14   redistribute it and/or modify it under the terms of the                  *
15   GNU Affero General Public License as published by the                    *
16   Free Software Foundation, either version 3 of the License,               *
17   or (at your option) any later version.                                   *
18                                                                            *
19 * The CertiVox MIRACL Crypto SDK is distributed in the hope                *
20   that it will be useful, but WITHOUT ANY WARRANTY; without even the       *
21   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
22   See the GNU Affero General Public License for more details.              *
23                                                                            *
24 * You should have received a copy of the GNU Affero General Public         *
25   License along with CertiVox MIRACL Crypto SDK.                           *
26   If not, see <http://www.gnu.org/licenses/>.                              *
27                                                                            *
28 You can be released from the requirements of the license by purchasing     *
29 a commercial license. Buying such a license is mandatory as soon as you    *
30 develop commercial activities involving the CertiVox MIRACL Crypto SDK     *
31 without disclosing the source code of your own applications, or shipping   *
32 the CertiVox MIRACL Crypto SDK with a closed source product.               *
33                                                                            *
34 ***************************************************************************/
35 /*
36  *
37  *    MIRACL  C++ Header file ecn.h
38  *
39  *    AUTHOR  : M. Scott
40  *
41  *    PURPOSE : Definition of class ECn  (Arithmetic on an Elliptic Curve,
42  *               mod n)
43  *
44  *    NOTE    : Must be used in conjunction with ecn.cpp and big.cpp
45  *              The active curve is set dynamically (via the Big ecurve()
46  *              routine) - so beware the pitfalls implicit in declaring
47  *              static or global ECn's (which are initialised before the
48  *              curve is set!). Uninitialised data is OK
49  *
50  */
51 
52 #ifndef ECN_H
53 #define ECN_H
54 
55 #include <cstring>
56 #include "big.h"
57 
58 #ifdef ZZNS
59 #define MR_INIT_ECN memset(mem,0,mr_ecp_reserve(1,ZZNS)); p=(epoint *)epoint_init_mem_variable(mem,0,ZZNS);
60 #else
61 #define MR_INIT_ECN mem=(char *)ecp_memalloc(1); p=(epoint *)epoint_init_mem(mem,0);
62 #endif
63 
64 class ECn
65 {
66     epoint *p;
67 #ifdef ZZNS
68     char mem[mr_ecp_reserve(1,ZZNS)];
69 #else
70     char *mem;
71 #endif
72 public:
ECn()73     ECn()                           {MR_INIT_ECN }
74 
ECn(const Big & x,const Big & y)75     ECn(const Big &x,const Big& y)  {MR_INIT_ECN
76                                    epoint_set(x.getbig(),y.getbig(),0,p); }
77 
78   // This next constructor restores a point on the curve from "compressed"
79   // data, that is the full x co-ordinate, and the LSB of y  (0 or 1)
80 
81 #ifndef MR_SUPPORT_COMPRESSION
ECn(const Big & x,int cb)82     ECn(const Big& x,int cb)             {MR_INIT_ECN
83                                    epoint_set(x.getbig(),x.getbig(),cb,p); }
84 #endif
85 
ECn(const ECn & b)86     ECn(const ECn &b)                   {MR_INIT_ECN epoint_copy(b.p,p);}
87 
88     epoint *get_point() const;
get_status()89     int get_status() {return p->marker;}
90     ECn& operator=(const ECn& b)  {epoint_copy(b.p,p);return *this;}
91 
92     ECn& operator+=(const ECn& b) {ecurve_add(b.p,p); return *this;}
93 
94     int add(const ECn&,big *,big *ex1=NULL,big *ex2=NULL) const;
95                                   // returns line slope as a big
96     int sub(const ECn&,big *,big *ex1=NULL,big *ex2=NULL) const;
97 
98     ECn& operator-=(const ECn& b) {ecurve_sub(b.p,p); return *this;}
99 
100   // Multiplication of a point by an integer.
101 
102     ECn& operator*=(const Big& k) {ecurve_mult(k.getbig(),p,p); return *this;}
103 
clear()104     void clear() {epoint_set(NULL,NULL,0,p);}
set(const Big & x,const Big & y)105     BOOL set(const Big& x,const Big& y)    {return epoint_set(x.getbig(),y.getbig(),0,p);}
106 #ifndef MR_AFFINE_ONLY
107 // use with care if at all
setz(const Big & z)108 	void setz(const Big& z) {nres(z.getbig(),p->Z); p->marker=MR_EPOINT_GENERAL;}
109 #endif
110     BOOL iszero() const;
111     int get(Big& x,Big& y) const;
112 
113   // This gets the point in compressed form. Return value is LSB of y-coordinate
114     int get(Big& x) const;
115 
116   // get raw coordinates
117     void getx(Big &x) const;
118     void getxy(Big &x,Big &y) const;
119     void getxyz(Big &x,Big &y,Big &z) const;
120 
121   // point compression
122 
123   // This sets the point from compressed form. cb is LSB of y coordinate
124 #ifndef MR_SUPPORT_COMPRESSION
125     BOOL set(const Big& x,int cb=0)  {return epoint_set(x.getbig(),x.getbig(),cb,p);}
126 #endif
127     friend ECn operator-(const ECn&);
128     friend void multi_add(int,ECn *,ECn *);
129     friend void double_add(ECn&,ECn&,ECn&,ECn&,big&,big&);
130 
131     friend ECn mul(const Big&, const ECn&, const Big&, const ECn&);
132     friend ECn mul(int, const Big *, ECn *);
133 
normalise(ECn & e)134     friend void normalise(ECn &e) {epoint_norm(e.p);}
135     friend void multi_norm(int,ECn *);
136 
137     friend BOOL operator==(const ECn& a,const ECn& b)
138                                   {return epoint_comp(a.p,b.p);}
139     friend BOOL operator!=(const ECn& a,const ECn& b)
140                                   {return (!epoint_comp(a.p,b.p));}
141 
142     friend ECn operator*(const Big &,const ECn&);
143 
144 #ifndef MR_NO_STANDARD_IO
145 
146     friend ostream& operator<<(ostream&,const ECn&);
147 
148 #endif
149 
~ECn()150     ~ECn() {
151 #ifndef ZZNS
152         mr_free(mem);
153 #endif
154  }
155 
156 };
157 
158 #endif
159 
160