Fabcoin Core  0.16.2
P2P Digital Currency
luc.cpp
Go to the documentation of this file.
1 // luc.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "luc.h"
5 #include "asn.h"
6 #include "sha.h"
7 #include "integer.h"
8 #include "nbtheory.h"
9 #include "algparam.h"
10 
12 
13 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
14 void LUC_TestInstantiations()
15 {
19 }
20 #endif
21 
22 void DL_Algorithm_LUC_HMP::Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
23 {
24  const Integer &q = params.GetSubgroupOrder();
25  r = params.ExponentiateBase(k);
26  s = (k + x*(r+e)) % q;
27 }
28 
29 bool DL_Algorithm_LUC_HMP::Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
30 {
31  const Integer p = params.GetGroupOrder()-1;
32  const Integer &q = params.GetSubgroupOrder();
33 
34  Integer Vsg = params.ExponentiateBase(s);
35  Integer Vry = publicKey.ExponentiatePublicElement((r+e)%q);
36  return (Vsg*Vsg + Vry*Vry + r*r) % p == (Vsg * Vry * r + 4) % p;
37 }
38 
40 {
41  return Lucas(exponent, m_g, static_cast<const DL_GroupPrecomputation_LUC &>(group).GetModulus());
42 }
43 
44 void DL_GroupParameters_LUC::SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
45 {
46  for (unsigned int i=0; i<exponentsCount; i++)
47  results[i] = Lucas(exponents[i], base, GetModulus());
48 }
49 
51 {
52  BERSequenceDecoder seq(bt);
53  m_n.BERDecode(seq);
54  m_e.BERDecode(seq);
55  seq.MessageEnd();
56 }
57 
59 {
60  DERSequenceEncoder seq(bt);
61  m_n.DEREncode(seq);
62  m_e.DEREncode(seq);
63  seq.MessageEnd();
64 }
65 
67 {
68  DoQuickSanityCheck();
69  return Lucas(m_e, x, m_n);
70 }
71 
72 bool LUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
73 {
74  CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
75  bool pass = true;
76  pass = pass && m_n > Integer::One() && m_n.IsOdd();
77  pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
78  return pass;
79 }
80 
81 bool LUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
82 {
83  return GetValueHelper(this, name, valueType, pValue).Assignable()
85  CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
86  ;
87 }
88 
90 {
91  AssignFromHelper(this, source)
93  CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
94  ;
95 }
96 
97 // *****************************************************************************
98 // private key operations:
99 
101 {
102 public:
103  LUCPrimeSelector(const Integer &e) : m_e(e) {}
104  bool IsAcceptable(const Integer &candidate) const
105  {
106  return RelativelyPrime(m_e, candidate+1) && RelativelyPrime(m_e, candidate-1);
107  }
109 };
110 
112 {
113  int modulusSize = 2048;
114  alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
115 
116  if (modulusSize < 16)
117  throw InvalidArgument("InvertibleLUCFunction: specified modulus size is too small");
118 
119  m_e = alg.GetValueWithDefault("PublicExponent", Integer(17));
120 
121  if (m_e < 5 || m_e.IsEven())
122  throw InvalidArgument("InvertibleLUCFunction: invalid public exponent");
123 
124  LUCPrimeSelector selector(m_e);
126  ("PointerToPrimeSelector", selector.GetSelectorPointer());
127  m_p.GenerateRandom(rng, primeParam);
128  m_q.GenerateRandom(rng, primeParam);
129 
130  m_n = m_p * m_q;
131  m_u = m_q.InverseMod(m_p);
132 }
133 
134 void InvertibleLUCFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
135 {
136  GenerateRandom(rng, MakeParameters("ModulusSize", (int)keybits)("PublicExponent", e));
137 }
138 
140 {
141  BERSequenceDecoder seq(bt);
142 
143  Integer version(seq);
144  if (!!version) // make sure version is 0
145  BERDecodeError();
146 
147  m_n.BERDecode(seq);
148  m_e.BERDecode(seq);
149  m_p.BERDecode(seq);
150  m_q.BERDecode(seq);
151  m_u.BERDecode(seq);
152  seq.MessageEnd();
153 }
154 
156 {
157  DERSequenceEncoder seq(bt);
158 
159  const byte version[] = {INTEGER, 1, 0};
160  seq.Put(version, sizeof(version));
161  m_n.DEREncode(seq);
162  m_e.DEREncode(seq);
163  m_p.DEREncode(seq);
164  m_q.DEREncode(seq);
165  m_u.DEREncode(seq);
166  seq.MessageEnd();
167 }
168 
170 {
171  // not clear how to do blinding with LUC
172  CRYPTOPP_UNUSED(rng);
173  DoQuickSanityCheck();
174  return InverseLucas(m_e, x, m_q, m_p, m_u);
175 }
176 
177 bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
178 {
179  bool pass = LUCFunction::Validate(rng, level);
180  pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
181  pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
182  pass = pass && m_u.IsPositive() && m_u < m_p;
183  if (level >= 1)
184  {
185  pass = pass && m_p * m_q == m_n;
186  pass = pass && RelativelyPrime(m_e, m_p+1);
187  pass = pass && RelativelyPrime(m_e, m_p-1);
188  pass = pass && RelativelyPrime(m_e, m_q+1);
189  pass = pass && RelativelyPrime(m_e, m_q-1);
190  pass = pass && m_u * m_q % m_p == 1;
191  }
192  if (level >= 2)
193  pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
194  return pass;
195 }
196 
197 bool InvertibleLUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
198 {
199  return GetValueHelper<LUCFunction>(this, name, valueType, pValue).Assignable()
202  CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
203  ;
204 }
205 
207 {
208  AssignFromHelper<LUCFunction>(this, source)
211  CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
212  ;
213 }
214 
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17)
Create a LUC private key.
Definition: luc.cpp:134
An invalid argument was detected.
Definition: cryptlib.h:184
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.cpp:81
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: luc.cpp:111
Classes for working with NameValuePairs.
uint8_t byte
Definition: Common.h:57
AlgorithmParameters MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength)
Definition: nbtheory.cpp:267
Integer m_e
Definition: luc.cpp:108
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:345
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:350
Definition: asn.h:30
LUCPrimeSelector(const Integer &e)
Definition: luc.cpp:103
void MessageEnd()
Definition: asn.cpp:524
bool Verify(const DL_GroupParameters< Integer > &params, const DL_PublicKey< Integer > &publicKey, const Integer &e, const Integer &r, const Integer &s) const
Definition: luc.cpp:29
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
void BERDecode(BufferedTransformation &bt)
Definition: luc.cpp:50
CRYPTOPP_DLL bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition: cryptlib.h:373
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: luc.cpp:89
virtual Element ExponentiatePublicElement(const Integer &exponent) const
Definition: pubkey.h:1012
Interface for random number generators.
Definition: cryptlib.h:1188
Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u)
Definition: nbtheory.cpp:1000
Integer Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const
Definition: luc.cpp:39
BER Sequence Decoder.
Definition: asn.h:303
Interface for buffered transformations.
Definition: cryptlib.h:1352
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: luc.cpp:66
static const Integer &CRYPTOPP_API One()
Integer representing 1.
Definition: integer.cpp:3035
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level)
Verifies a prime number.
Definition: nbtheory.cpp:249
Interface for Discrete Log (DL) public keys.
Definition: pubkey.h:992
void version()
Definition: main.cpp:53
Classes for the LUC cryptosystem.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.cpp:197
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: luc.cpp:169
#define x(i)
const char * source
Definition: rpcconsole.cpp:60
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1376
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:498
const char * name
Definition: rest.cpp:36
#define t3
Application callback to signal suitability of a cabdidate prime.
Definition: nbtheory.h:80
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
Integer Lucas(const Integer &e, const Integer &pIn, const Integer &n)
Definition: nbtheory.cpp:814
#define t1
AssignFromHelperClass< T, BASE > AssignFromHelper(T *pObject, const NameValuePairs &source)
Definition: algparam.h:286
void BERDecodeError()
Raises a BERDecodeErr.
Definition: asn.h:69
#define t2
Classes and functions for working with ANS.1 objects.
The LUC inverse function.
Definition: luc.h:75
Classes for SHA-1 and SHA-2 family of message digests.
void MessageEnd()
Definition: asn.cpp:456
#define CRYPTOPP_SET_FUNCTION_ENTRY(name)
Definition: algparam.h:505
void Sign(const DL_GroupParameters< Integer > &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
Definition: luc.cpp:22
Classes and functions for number theoretic operations.
void DEREncode(BufferedTransformation &bt) const
Definition: luc.cpp:155
DER Sequence Encoder.
Definition: asn.h:313
#define pass(a, b, c, mul, X)
#define CRYPTOPP_GET_FUNCTION_ENTRY(name)
Definition: algparam.h:504
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
Exponentiates a base to multiple exponents.
Definition: luc.cpp:44
bool RelativelyPrime(const Integer &a, const Integer &b)
Definition: nbtheory.h:107
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: luc.cpp:206
An object that implements NameValuePairs.
Definition: algparam.h:433
Multiple precision integer with arithmetic operations.
void BERDecode(BufferedTransformation &bt)
Definition: luc.cpp:139
#define NAMESPACE_END
Definition: config.h:201
The LUC function.
Definition: luc.h:36
#define e(i)
Definition: sha.cpp:733
bool IsAcceptable(const Integer &candidate) const
Definition: luc.cpp:104
const PrimeSelector * GetSelectorPointer() const
Definition: nbtheory.h:83
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
Definition: pubkey.h:803
void DEREncode(BufferedTransformation &bt) const
Definition: luc.cpp:58
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: luc.cpp:72
virtual Integer GetGroupOrder() const
Retrieves the order of the group.
Definition: pubkey.h:843
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:1989
GetValueHelperClass< T, BASE > GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
Definition: algparam.h:224
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: luc.cpp:177
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.