1 /* rmint/basic/arith/inv.h - Invert arithmetic functions for rmint
2 
3    Copyright Université Joseph Fourier - Grenoble
4 Contributors :
5 Alexis BREUST (alexis.breust@gmail.com 2014)
6 Christophe CHABOT (christophechabotcc@gmail.com 2011)
7 
8 
9 This software is a computer program whose purpose is to provide an fixed precision arithmetic library.
10 
11 This software is governed by the CeCILL-B license under French law and
12 abiding by the rules of distribution of free software.  You can  use,
13 modify and/ or redistribute the software under the terms of the CeCILL-B
14 license as circulated by CEA, CNRS and INRIA at the following URL
15 "http://www.cecill.info".
16 
17 As a counterpart to the access to the source code and  rights to copy,
18 modify and redistribute granted by the license, users are provided only
19 with a limited warranty  and the software's author,  the holder of the
20 economic rights,  and the successive licensors  have only  limited
21 liability.
22 
23 In this respect, the user's attention is drawn to the risks associated
24 with loading,  using,  modifying and/or developing or reproducing the
25 software by the user in light of its specific status of free software,
26 that may mean  that it is complicated to manipulate,  and  that  also
27 therefore means  that it is reserved for developers  and  experienced
28 professionals having in-depth computer knowledge. Users are therefore
29 encouraged to load and test the software's suitability as regards their
30 requirements in conditions enabling the security of their systems and/or
31 data to be ensured and,  more generally, to use and operate it in the
32 same conditions as regards security.
33 
34 The fact that you are presently reading this means that you have had
35 knowledge of the CeCILL-B license and that you accept its terms.
36 */
37 
38 
39 #ifndef RMINT_BASIC_ARITH_INV_H
40 #define RMINT_BASIC_ARITH_INV_H
41 
42 #include "ruinvmod.h" /* inv_mod() */
43 #include "rmbrmint.h"
44 #include "rmbreduc.h"
45 
46 // --------------------------------------------------------------
47 // ----------------------- DEFINTIONS ---------------------------
48 
49 namespace RecInt
50 {
51     template <size_t K> rmint<K, MGI>& inv(rmint<K, MGI>&, const rmint<K, MGI>&);
52     template <size_t K> rmint<K, MGI>& inv(rmint<K, MGI>&);
53 
54     template <size_t K, typename T> __RECINT_IS_ARITH(T, rmint<K, MGI>&) inv(rmint<K, MGI>&, const T&);
55 }
56 
57 
58 // --------------------------------------------------------------
59 // --------------------- Implementation -------------------------
60 
61 namespace RecInt
62 {
63     // a = b^(-1) mod a.p or a = 0 if b not invertible
64     template <size_t K>
inv(rmint<K,MGI> & a,const rmint<K,MGI> & b)65     inline rmint<K, MGI>& inv(rmint<K, MGI>& a, const rmint<K, MGI>& b) {
66         inv_mod(a.Value, b.Value, a.p);
67         return a;
68     }
69 
70     // a = a^(-1) mod a.p or a = 0 if a not invertible
71     template <size_t K>
inv(rmint<K,MGI> & a)72     inline rmint<K, MGI>& inv(rmint<K, MGI>& a) {
73         inv_mod(a.Value, a.Value, a.p);
74         return a;
75     }
76 
77     // a = b^(-1) mod a.p or a = 0 if b not invertible
78     template <size_t K, typename T>
__RECINT_IS_ARITH(T,rmint<K,MGI> &)79     inline __RECINT_IS_ARITH(T, rmint<K, MGI>&) inv(rmint<K, MGI>& a, const T& b) {
80         ruint<K> br(b);
81         inv_mod(a.Value, br, a.p);
82         return a;
83     }
84 }
85 
86 #endif
87 
88 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
89 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
90