Electroneum
rctSigs.h
Go to the documentation of this file.
1 // Copyright (c) 2016, Electroneum Research Labs
2 //
3 // Author: Shen Noether <shen.noether@gmx.com>
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without modification, are
8 // permitted provided that the following conditions are met:
9 //
10 // 1. Redistributions of source code must retain the above copyright notice, this list of
11 // conditions and the following disclaimer.
12 //
13 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
14 // of conditions and the following disclaimer in the documentation and/or other
15 // materials provided with the distribution.
16 //
17 // 3. Neither the name of the copyright holder nor the names of its contributors may be
18 // used to endorse or promote products derived from this software without specific
19 // prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
22 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #pragma once
32 
33 //#define DBG
34 
35 #ifndef RCTSIGS_H
36 #define RCTSIGS_H
37 
38 #include <cstddef>
39 #include <mutex>
40 #include <vector>
41 #include <tuple>
42 
43 #include "crypto/generic-ops.h"
44 
45 extern "C" {
46 #include "crypto/random.h"
47 #include "crypto/keccak.h"
48 }
49 #include "crypto/crypto.h"
50 
51 
52 #include "rctTypes.h"
53 #include "rctOps.h"
54 
55 //Define this flag when debugging to get additional info on the console
56 #ifdef DBG
57 #define DP(x) dp(x)
58 #else
59 #define DP(x)
60 #endif
61 
62 
63 
64 using namespace std;
65 using namespace crypto;
66 
67 namespace rct {
68 
69  boroSig genBorromean(const key64 x, const key64 P1, const key64 P2, const bits indices);
70  bool verifyBorromean(const boroSig &bb, const key64 P1, const key64 P2);
71 
72  //Multilayered Spontaneous Anonymous Group Signatures (MLSAG signatures)
73  //These are aka MG signatutes in earlier drafts of the ring ct paper
74  // c.f. http://eprint.iacr.org/2015/1098 section 2.
75  // keyImageV just does I[i] = xx[i] * HashToPoint(xx[i] * G) for each i
76  // Gen creates a signature which proves that for some column in the keymatrix "pk"
77  // the signer knows a secret key for each row in that column
78  // Ver verifies that the MG sig was created correctly
79  keyV keyImageV(const keyV &xx);
80  mgSig MLSAG_Gen(const key &message, const keyM & pk, const keyV & xx, const unsigned int index, size_t dsRows);
81  bool MLSAG_Ver(const key &message, const keyM &pk, const mgSig &sig, size_t dsRows);
82  //mgSig MLSAG_Gen_Old(const keyM & pk, const keyV & xx, const int index);
83 
84  //proveRange and verRange
85  //proveRange gives C, and mask such that \sumCi = C
86  // c.f. http://eprint.iacr.org/2015/1098 section 5.1
87  // and Ci is a commitment to either 0 or 2^i, i=0,...,63
88  // thus this proves that "amount" is in [0, 2^64]
89  // mask is a such that C = aG + bH, and b = amount
90  //verRange verifies that \sum Ci = C and that each Ci is a commitment to 0 or 2^i
91  rangeSig proveRange(key & C, key & mask, const xmr_amount & amount);
92  bool verRange(const key & C, const rangeSig & as);
93 
94  //Ring-ct MG sigs
95  //Prove:
96  // c.f. http://eprint.iacr.org/2015/1098 section 4. definition 10.
97  // This does the MG sig on the "dest" part of the given key matrix, and
98  // the last row is the sum of input commitments from that column - sum output commitments
99  // this shows that sum inputs = sum outputs
100  //Ver:
101  // verifies the above sig is created corretly
102  mgSig proveRctMG(const ctkeyM & pubs, const ctkeyV & inSk, const keyV &outMasks, const ctkeyV & outPk, unsigned int index, key txnFee, const key &message);
103  mgSig proveRctMGSimple(const key & message, const ctkeyV & pubs, const ctkey & inSk, const key &a , const key &Cout, unsigned int index);
104  bool verRctMG(const mgSig &mg, const ctkeyM & pubs, const ctkeyV & outPk, key txnFee, const key &message);
105  bool verRctMGSimple(const key &message, const mgSig &mg, const ctkeyV & pubs, const key & C);
106 
107  //These functions get keys from blockchain
108  //replace these when connecting blockchain
109  //getKeyFromBlockchain grabs a key from the blockchain at "reference_index" to mix with
110  //populateFromBlockchain creates a keymatrix with "mixin" columns and one of the columns is inPk
111  // the return value are the key matrix, and the index where inPk was put (random).
112  void getKeyFromBlockchain(ctkey & a, size_t reference_index);
113  tuple<ctkeyM, xmr_amount> populateFromBlockchain(ctkeyV inPk, int mixin);
114 
115  //RingCT protocol
116  //genRct:
117  // creates an rctSig with all data necessary to verify the rangeProofs and that the signer owns one of the
118  // columns that are claimed as inputs, and that the sum of inputs = sum of outputs.
119  // Also contains masked "amount" and "mask" so the receiver can see how much they received
120  //verRct:
121  // verifies that all signatures (rangeProogs, MG sig, sum inputs = outputs) are correct
122  //decodeRct: (c.f. http://eprint.iacr.org/2015/1098 section 5.1.1)
123  // uses the attached ecdh info to find the amounts represented by each output commitment
124  // must know the destination private key to find the correct amount, else will return a random number
125  rctSig genRct(const key &message, const ctkeyV & inSk, const keyV & destinations, const vector<xmr_amount> & amounts, const ctkeyM &mixRing, const keyV &amount_keys, unsigned int index, ctkeyV &outSk);
126  rctSig genRct(const key &message, const ctkeyV & inSk, const ctkeyV & inPk, const keyV & destinations, const vector<xmr_amount> & amounts, const keyV &amount_keys, const int mixin);
127  rctSig genRctSimple(const key & message, const ctkeyV & inSk, const ctkeyV & inPk, const keyV & destinations, const vector<xmr_amount> & inamounts, const vector<xmr_amount> & outamounts, const keyV &amount_keys, xmr_amount txnFee, unsigned int mixin);
128  rctSig genRctSimple(const key & message, const ctkeyV & inSk, const keyV & destinations, const vector<xmr_amount> & inamounts, const vector<xmr_amount> & outamounts, xmr_amount txnFee, const ctkeyM & mixRing, const keyV &amount_keys, const std::vector<unsigned int> & index, ctkeyV &outSk);
129  bool verRct(const rctSig & rv, bool semantics);
130  static inline bool verRct(const rctSig & rv) { return verRct(rv, true) && verRct(rv, false); }
131  bool verRctSimple(const rctSig & rv, bool semantics);
132  static inline bool verRctSimple(const rctSig & rv) { return verRctSimple(rv, true) && verRctSimple(rv, false); }
133  xmr_amount decodeRct(const rctSig & rv, const key & sk, unsigned int i, key & mask);
134  xmr_amount decodeRct(const rctSig & rv, const key & sk, unsigned int i);
135  xmr_amount decodeRctSimple(const rctSig & rv, const key & sk, unsigned int i, key & mask);
136  xmr_amount decodeRctSimple(const rctSig & rv, const key & sk, unsigned int i);
137 }
138 #endif /* RCTSIGS_H */
139 
bool verRctMG(const mgSig &mg, const ctkeyM &pubs, const ctkeyV &outPk, key txnFeeKey, const key &message)
Definition: rctSigs.cpp:445
boroSig genBorromean(const key64 x, const key64 P1, const key64 P2, const bits indices)
Definition: rctSigs.cpp:47
rangeSig proveRange(key &C, key &mask, const xmr_amount &amount)
Definition: rctSigs.cpp:269
uint64_t xmr_amount
Definition: rctTypes.h:126
vector< ctkey > ctkeyV
Definition: rctTypes.h:104
crypto namespace.
Definition: crypto.cpp:47
Definition: block_queue.cpp:41
Definition: rctOps.cpp:41
mgSig proveRctMGSimple(const key &message, const ctkeyV &pubs, const ctkey &inSk, const key &a, const key &Cout, unsigned int index)
Definition: rctSigs.cpp:417
void getKeyFromBlockchain(ctkey &a, size_t reference_index)
Definition: rctSigs.cpp:512
xmr_amount decodeRctSimple(const rctSig &rv, const key &sk, unsigned int i, key &mask)
Definition: rctSigs.cpp:902
xmr_amount decodeRct(const rctSig &rv, const key &sk, unsigned int i, key &mask)
Definition: rctSigs.cpp:874
static bool verRctSimple(const rctSig &rv)
Definition: rctSigs.h:132
key key64[64]
Definition: rctTypes.h:128
keyV keyImageV(const keyV &xx)
Definition: rctSigs.cpp:97
bool MLSAG_Ver(const key &message, const keyM &pk, const mgSig &rv, size_t dsRows)
Definition: rctSigs.cpp:205
Definition: rctTypes.h:323
static bool verRct(const rctSig &rv)
Definition: rctSigs.h:130
rctSig genRct(const key &message, const ctkeyV &inSk, const keyV &destinations, const vector< xmr_amount > &amounts, const ctkeyM &mixRing, const keyV &amount_keys, unsigned int index, ctkeyV &outSk)
Definition: rctSigs.cpp:567
vector< keyV > keyM
Definition: rctTypes.h:93
mgSig proveRctMG(const ctkeyM &pubs, const ctkeyV &inSk, const keyV &outMasks, const ctkeyV &outPk, unsigned int index, key txnFee, const key &message)
mgSig MLSAG_Gen(const key &message, const keyM &pk, const keyV &xx, const unsigned int index, size_t dsRows)
Definition: rctSigs.cpp:116
string a
Definition: MakeCryptoOps.py:15
bool verRctMGSimple(const key &message, const mgSig &mg, const ctkeyV &pubs, const key &C)
Definition: rctSigs.cpp:484
bool verRange(const key &C, const rangeSig &as)
Definition: rctSigs.cpp:301
vector< key > keyV
Definition: rctTypes.h:92
bool verifyBorromean(const boroSig &bb, const key64 P1, const key64 P2)
Definition: rctSigs.cpp:78
unsigned int bits[ATOMS]
Definition: rctTypes.h:127
rctSig genRctSimple(const key &message, const ctkeyV &inSk, const keyV &destinations, const vector< xmr_amount > &inamounts, const vector< xmr_amount > &outamounts, xmr_amount txnFee, const ctkeyM &mixRing, const keyV &amount_keys, const std::vector< unsigned int > &index, ctkeyV &outSk)
Definition: rctSigs.cpp:627
vector< ctkeyV > ctkeyM
Definition: rctTypes.h:105
tuple< ctkeyM, xmr_amount > populateFromBlockchain(ctkeyV inPk, int mixin)
Definition: rctSigs.cpp:522