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  *    MIRACL  C++ Header file ZZn8.h
37  *
38  *    AUTHOR  : M. Scott
39  *
40  *    NOTE:   : Must be used in conjunction with zzn4.cpp zzn2.cpp big.cpp and zzn.cpp
41  *            : This is designed as a "towering extension", so a ZZn8 consists
42  *            : of a pair of ZZn4. An element looks like (a+x^2.b) + x(c+x^2.d)
43  *
44  *    PURPOSE : Definition of class ZZn8  (Arithmetic over n^8)
45  *
46  * WARNING: This class has been cobbled together for a specific use with
47  * the MIRACL library. It is not complete, and may not work in other
48  * applications
49  *
50  */
51 
52 #ifndef ZZN8_H
53 #define ZZN8_H
54 
55 #include "zzn4.h"
56 
57 class ZZn8
58 {
59     ZZn4 a,b;
60     BOOL unitary;
61 public:
ZZn8()62     ZZn8()   {unitary=FALSE;}
ZZn8(int w)63     ZZn8(int w) {a=(ZZn4)w; b=0; if (w==1) unitary=TRUE; else unitary=FALSE;}
ZZn8(const ZZn8 & w)64     ZZn8(const ZZn8& w) {a=w.a; b=w.b; unitary=w.unitary; }
ZZn8(const ZZn4 & x,const ZZn4 & y)65     ZZn8(const ZZn4 &x,const ZZn4& y) {a=x; b=y; unitary=FALSE;}
ZZn8(const ZZn4 & x)66 	ZZn8(const ZZn4 &x) {a=x; b=0; unitary=FALSE; }
ZZn8(const ZZn & x)67     ZZn8(const ZZn &x)    {a=x; b=0; unitary=FALSE;}
ZZn8(const Big & x)68     ZZn8(const Big &x)    {a=(ZZn)x; b=0; unitary=FALSE;}
69 
set(const ZZn4 & x,const ZZn4 & y)70     void set(const ZZn4 &x,const ZZn4 &y) {a=x; b=y; unitary=FALSE; }
set(const ZZn4 & x)71 	void set(const ZZn4 &x)         {a=x; b=(ZZn4)0; unitary=FALSE;}
set(const Big & x)72     void set(const Big &x)          {a=(ZZn)x; b=(ZZn4)0; unitary=FALSE; }
73 
74     void get(ZZn4 &,ZZn4 &) const;
75     void get(ZZn4 &) const;
76 
clear()77     void clear() {a=0; b=0; unitary=FALSE;}
mark_as_unitary()78     void mark_as_unitary() {unitary=TRUE;}
is_unitary()79     BOOL is_unitary() {return unitary;}
80 
iszero()81     BOOL iszero()  const {if (a.iszero() && b.iszero()) return TRUE; return FALSE; }
isunity()82     BOOL isunity() const {if (a.isunity() && b.iszero()) return TRUE; return FALSE; }
83 //    BOOL isminusone() const {if (a.isminusone() && b.iszero()) return TRUE; return FALSE; }
84 
85     ZZn8& powq(const ZZn2&);
86     ZZn8& operator=(int i) {a=i; b=0; if (i==1) unitary=TRUE; else unitary=FALSE; return *this;}
87     ZZn8& operator=(const ZZn& x) {a=x; b=0; unitary=FALSE; return *this; }
88     ZZn8& operator=(const ZZn4& x) {a=x; b=0; unitary=FALSE; return *this; }
89     ZZn8& operator=(const ZZn8& x) {a=x.a; b=x.b; unitary=x.unitary; return *this; }
90     ZZn8& operator+=(const ZZn& x) {a+=x; unitary=FALSE; return *this; }
91     ZZn8& operator+=(const ZZn4& x) {a+=x; unitary=FALSE; return *this; }
92     ZZn8& operator+=(const ZZn8& x) {a+=x.a; b+=x.b; unitary=FALSE; return *this; }
93     ZZn8& operator-=(const ZZn& x)  {a-=x; unitary=FALSE; return *this; }
94     ZZn8& operator-=(const ZZn4& x) {a-=x; unitary=FALSE; return *this; }
95     ZZn8& operator-=(const ZZn8& x) {a-=x.a; b-=x.b; unitary=FALSE; return *this; }
96     ZZn8& operator*=(const ZZn8&);
97     ZZn8& operator*=(const ZZn4& x) {a*=x; b*=x; unitary=FALSE; return *this; }
98     ZZn8& operator*=(const ZZn& x) {a*=x; b*=x; unitary=FALSE; return *this; }
99     ZZn8& operator*=(int x) {a*=x; b*=x; unitary=FALSE; return *this;}
100     ZZn8& operator/=(const ZZn8&);
101     ZZn8& operator/=(const ZZn4&);
102     ZZn8& operator/=(const ZZn&);
103     ZZn8& operator/=(int);
conj()104     ZZn8& conj() {b=-b; return *this;}
105 
106     friend ZZn8 operator+(const ZZn8&,const ZZn8&);
107     friend ZZn8 operator+(const ZZn8&,const ZZn4&);
108     friend ZZn8 operator+(const ZZn8&,const ZZn&);
109     friend ZZn8 operator-(const ZZn8&,const ZZn8&);
110     friend ZZn8 operator-(const ZZn8&,const ZZn4&);
111     friend ZZn8 operator-(const ZZn8&,const ZZn&);
112     friend ZZn8 operator-(const ZZn8&);
113 
114     friend ZZn8 operator*(const ZZn8&,const ZZn8&);
115     friend ZZn8 operator*(const ZZn8&,const ZZn4&);
116     friend ZZn8 operator*(const ZZn8&,const ZZn&);
117     friend ZZn8 operator*(const ZZn&,const ZZn8&);
118     friend ZZn8 operator*(const ZZn4&,const ZZn8&);
119 
120     friend ZZn8 operator*(int,const ZZn8&);
121     friend ZZn8 operator*(const ZZn8&,int);
122 
123     friend ZZn8 operator/(const ZZn8&,const ZZn8&);
124     friend ZZn8 operator/(const ZZn8&,const ZZn4&);
125     friend ZZn8 operator/(const ZZn8&,const ZZn&);
126     friend ZZn8 operator/(const ZZn8&,int);
127 
real(const ZZn8 & x)128     friend ZZn4  real(const ZZn8& x)      {return x.a;}
imaginary(const ZZn8 & x)129     friend ZZn4  imaginary(const ZZn8& x) {return x.b;}
130 
131     friend ZZn8 pow(const ZZn8&,const Big&);
132     friend ZZn8 pow(int,const ZZn8*,const Big*);
133     friend ZZn8 powl(const ZZn8&,const Big&);
134     friend ZZn8 conj(const ZZn8&);
135     friend ZZn8 tx(const ZZn8&);
136 	friend ZZn8 tx2(const ZZn8&);
137     friend ZZn8 inverse(const ZZn8&);
138 #ifndef MR_NO_RAND
139     friend ZZn8 randn8(void);        // random ZZn8
140 #endif
141     friend BOOL qr(const ZZn8&);
142     friend ZZn8 sqrt(const ZZn8&);   // square root - 0 if none exists
143 
144     friend BOOL operator==(const ZZn8& x,const ZZn8& y)
145     {if (x.a==y.a && x.b==y.b) return TRUE; else return FALSE; }
146 
147     friend BOOL operator!=(const ZZn8& x,const ZZn8& y)
148     {if (x.a!=y.a || x.b!=y.b) return TRUE; else return FALSE; }
149 
150 #ifndef MR_NO_STANDARD_IO
151     friend ostream& operator<<(ostream&,const ZZn8&);
152 #endif
153 
~ZZn8()154     ~ZZn8()  {}
155 };
156 #ifndef MR_NO_RAND
157 extern ZZn8 randn8(void);
158 #endif
159 
160 #endif
161 
162