Fabcoin Core  0.16.2
P2P Digital Currency
xtrcrypt.cpp
Go to the documentation of this file.
1 // xtrcrypt.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #include "asn.h"
6 #include "integer.h"
7 #include "xtrcrypt.h"
8 #include "nbtheory.h"
9 #include "modarith.h"
10 #include "argnames.h"
11 
13 
14 XTR_DH::XTR_DH(const Integer &p, const Integer &q, const GFP2Element &g)
15  : m_p(p), m_q(q), m_g(g)
16 {
17 }
18 
19 XTR_DH::XTR_DH(RandomNumberGenerator &rng, unsigned int pbits, unsigned int qbits)
20 {
21  XTR_FindPrimesAndGenerator(rng, m_p, m_q, m_g, pbits, qbits);
22 }
23 
25 {
26  BERSequenceDecoder seq(bt);
27  m_p.BERDecode(seq);
28  m_q.BERDecode(seq);
29  m_g.c1.BERDecode(seq);
30  m_g.c2.BERDecode(seq);
31  seq.MessageEnd();
32 }
33 
35 {
36  DERSequenceEncoder seq(bt);
37  m_p.DEREncode(seq);
38  m_q.DEREncode(seq);
39  m_g.c1.DEREncode(seq);
40  m_g.c2.DEREncode(seq);
41  seq.MessageEnd();
42 }
43 
44 bool XTR_DH::Validate(RandomNumberGenerator &rng, unsigned int level) const
45 {
46  bool pass = true;
47  pass = pass && m_p > Integer::One() && m_p.IsOdd();
48  pass = pass && m_q > Integer::One() && m_q.IsOdd();
49  GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3);
50  pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three);
51  if (level >= 1)
52  pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero();
53  if (level >= 2)
54  {
55  pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
56  pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three;
57  pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three;
58  }
59  return pass;
60 }
61 
62 bool XTR_DH::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
63 {
64  return GetValueHelper(this, name, valueType, pValue).Assignable()
66  CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupOrder)
67  CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupGenerator)
68  ;
69 }
70 
72 {
73  AssignFromHelper(this, source)
75  CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupOrder)
76  CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupGenerator)
77  ;
78 }
79 
81 {
82  Integer x(rng, Integer::Zero(), m_q-1);
83  x.Encode(privateKey, PrivateKeyLength());
84 }
85 
86 void XTR_DH::GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
87 {
88  CRYPTOPP_UNUSED(rng);
89  Integer x(privateKey, PrivateKeyLength());
91  y.Encode(publicKey, PublicKeyLength());
92 }
93 
94 bool XTR_DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const
95 {
96  GFP2Element w(otherPublicKey, PublicKeyLength());
97  if (validateOtherPublicKey)
98  {
100  GFP2Element three = gfp2.ConvertIn(3);
101  if (w.c1.IsNegative() || w.c2.IsNegative() || w.c1 >= m_p || w.c2 >= m_p || w == three)
102  return false;
103  if (XTR_Exponentiate(w, m_q, m_p) != three)
104  return false;
105  }
106  Integer s(privateKey, PrivateKeyLength());
108  z.Encode(agreedValue, AgreedValueLength());
109  return true;
110 }
111 
unsigned int PublicKeyLength() const
Provides the size of the public key.
Definition: xtrcrypt.h:33
Standard names for retrieving values by name when working with NameValuePairs.
uint8_t byte
Definition: Common.h:57
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: xtrcrypt.cpp:62
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Definition: integer.cpp:3369
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:345
Integer m_q
Definition: xtrcrypt.h:50
void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
Creates primes p,q and generator g for XTR.
Definition: xtr.cpp:19
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: xtrcrypt.cpp:71
void MessageEnd()
Definition: asn.cpp:524
GFP2Element m_g
Definition: xtrcrypt.h:51
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: xtrcrypt.cpp:44
#define g(i)
Definition: sha.cpp:735
Interface for random number generators.
Definition: cryptlib.h:1188
BER Sequence Decoder.
Definition: asn.h:303
Interface for buffered transformations.
Definition: cryptlib.h:1352
void Encode(byte *encodedElement, unsigned int size)
Definition: xtr.h:25
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
bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const
Derive agreed value.
Definition: xtrcrypt.cpp:94
an element of GF(p^2)
Definition: xtr.h:17
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a public key from a private key in this domain.
Definition: xtrcrypt.cpp:86
#define x(i)
void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate private key in this domain.
Definition: xtrcrypt.cpp:80
const char * source
Definition: rpcconsole.cpp:60
Integer Squared() const
Multiply this integer by itself.
Definition: integer.h:570
XTR-DH with key validation.
Definition: xtrcrypt.h:16
XTR_DH(const Integer &p, const Integer &q, const GFP2Element &g)
Definition: xtrcrypt.cpp:14
bool IsNegative() const
Determines if the Integer is negative.
Definition: integer.h:330
const char * name
Definition: rest.cpp:36
unsigned int AgreedValueLength() const
Provides the size of the agreed value.
Definition: xtrcrypt.h:31
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
void DEREncode(BufferedTransformation &domainParams) const
Definition: xtrcrypt.cpp:34
AssignFromHelperClass< T, BASE > AssignFromHelper(T *pObject, const NameValuePairs &source)
Definition: algparam.h:286
Classes and functions for working with ANS.1 objects.
Integer m_p
Definition: xtrcrypt.h:50
void MessageEnd()
Definition: asn.cpp:456
#define CRYPTOPP_SET_FUNCTION_ENTRY(name)
Definition: algparam.h:505
Classes and functions for number theoretic operations.
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Definition: integer.cpp:3391
DER Sequence Encoder.
Definition: asn.h:313
"The XTR public key system" by Arjen K.
#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
Multiple precision integer with arithmetic operations.
static const Integer &CRYPTOPP_API Zero()
Integer representing 0.
Definition: integer.cpp:3027
#define NAMESPACE_END
Definition: config.h:201
Integer c2
Definition: xtr.h:42
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Definition: integer.cpp:3398
GFP2Element ConvertIn(const Integer &a) const
Definition: xtr.h:61
Class file for performing modular arithmetic.
#define z(i)
GF(p^2), optimal normal basis.
Definition: xtr.h:48
GFP2Element XTR_Exponentiate(const GFP2Element &b, const Integer &e, const Integer &p)
Definition: xtr.cpp:56
Integer c1
Definition: xtr.h:42
unsigned int PrivateKeyLength() const
Provides the size of the private key.
Definition: xtrcrypt.h:32
Interface for retrieving values given their names.
Definition: cryptlib.h:279
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