Electroneum
rctOps.h
Go to the documentation of this file.
1 //#define DBG
2 // Copyright (c) 2016, Electroneum Research Labs
3 //
4 // Author: Shen Noether <shen.noether@gmx.com>
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without modification, are
9 // permitted provided that the following conditions are met:
10 //
11 // 1. Redistributions of source code must retain the above copyright notice, this list of
12 // conditions and the following disclaimer.
13 //
14 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
15 // of conditions and the following disclaimer in the documentation and/or other
16 // materials provided with the distribution.
17 //
18 // 3. Neither the name of the copyright holder nor the names of its contributors may be
19 // used to endorse or promote products derived from this software without specific
20 // prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 #pragma once
33 
34 #ifndef RCTOPS_H
35 #define RCTOPS_H
36 
37 #include <cstddef>
38 #include <mutex>
39 #include <vector>
40 #include <tuple>
41 
42 #include "crypto/generic-ops.h"
43 
44 extern "C" {
45 #include "crypto/random.h"
46 #include "crypto/keccak.h"
47 #include "rctCryptoOps.h"
48 }
49 #include "crypto/crypto.h"
50 
51 #include "rctTypes.h"
52 
53 //Define this flag when debugging to get additional info on the console
54 #ifdef DBG
55 #define DP(x) dp(x)
56 #else
57 #define DP(x)
58 #endif
59 
60 using namespace std;
61 using namespace crypto;
62 
63 namespace rct {
64 
65  //Various key initialization functions
66 
67  static const key Z = { {0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 } };
68  static const key I = { {0x01, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 } };
69  static const key L = { {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 } };
70 
71  //Creates a zero scalar
72  inline key zero() { return Z; }
73  inline void zero(key &z) { memset(&z, 0, 32); }
74  //Creates a zero elliptic curve point
75  inline key identity() { return I; }
76  inline void identity(key &Id) { memcpy(&Id, &I, 32); }
77  //Creates a key equal to the curve order
78  inline key curveOrder() { return L; }
79  inline void curveOrder(key &l) { l = L; }
80  //copies a scalar or point
81  inline void copy(key &AA, const key &A) { memcpy(&AA, &A, 32); }
82  inline key copy(const key & A) { key AA; memcpy(&AA, &A, 32); return AA; }
83 
84  //initializes a key matrix;
85  //first parameter is rows,
86  //second is columns
87  keyM keyMInit(size_t rows, size_t cols);
88 
89  //Various key generation functions
90 
91  //generates a random scalar which can be used as a secret key or mask
92  key skGen();
93  void skGen(key &);
94 
95  //generates a vector of secret keys of size "int"
96  keyV skvGen(size_t rows );
97 
98  //generates a random curve point (for testing)
99  key pkGen();
100  //generates a random secret and corresponding public key
101  void skpkGen(key &sk, key &pk);
102  tuple<key, key> skpkGen();
103  //generates a <secret , public> / Pedersen commitment to the amount
104  tuple<ctkey, ctkey> ctskpkGen(xmr_amount amount);
105  //generates C =aG + bH from b, a is random
106  void genC(key & C, const key & a, xmr_amount amount);
107  //this one is mainly for testing, can take arbitrary amounts..
108  tuple<ctkey, ctkey> ctskpkGen(const key &bH);
109  // make a pedersen commitment with given key
110  key commit(xmr_amount amount, const key &mask);
111  // make a pedersen commitment with zero key
112  key zeroCommit(xmr_amount amount);
113  //generates a random uint long long
114  xmr_amount randXmrAmount(xmr_amount upperlimit);
115 
116  //Scalar multiplications of curve points
117 
118  //does a * G where a is a scalar and G is the curve basepoint
119  void scalarmultBase(key & aG, const key &a);
120  key scalarmultBase(const key & a);
121  //does a * P where a is a scalar and P is an arbitrary point
122  void scalarmultKey(key &aP, const key &P, const key &a);
123  key scalarmultKey(const key &P, const key &a);
124  //Computes aH where H= toPoint(cn_fast_hash(G)), G the basepoint
125  key scalarmultH(const key & a);
126 
127  //Curve addition / subtractions
128 
129  //for curve points: AB = A + B
130  void addKeys(key &AB, const key &A, const key &B);
131  //aGB = aG + B where a is a scalar, G is the basepoint, and B is a point
132  void addKeys1(key &aGB, const key &a, const key & B);
133  //aGbB = aG + bB where a, b are scalars, G is the basepoint and B is a point
134  void addKeys2(key &aGbB, const key &a, const key &b, const key &B);
135  //Does some precomputation to make addKeys3 more efficient
136  // input B a curve point and output a ge_dsmp which has precomputation applied
137  void precomp(ge_dsmp rv, const key &B);
138  //aAbB = a*A + b*B where a, b are scalars, A, B are curve points
139  //B must be input after applying "precomp"
140  void addKeys3(key &aAbB, const key &a, const key &A, const key &b, const ge_dsmp B);
141  //AB = A - B where A, B are curve points
142  void subKeys(key &AB, const key &A, const key &B);
143  //checks if A, B are equal as curve points
144  bool equalKeys(const key & A, const key & B);
145 
146  //Hashing - cn_fast_hash
147  //be careful these are also in crypto namespace
148  //cn_fast_hash for arbitrary l multiples of 32 bytes
149  void cn_fast_hash(key &hash, const void * data, const size_t l);
150  void hash_to_scalar(key &hash, const void * data, const size_t l);
151  //cn_fast_hash for a 32 byte key
152  void cn_fast_hash(key &hash, const key &in);
153  void hash_to_scalar(key &hash, const key &in);
154  //cn_fast_hash for a 32 byte key
155  key cn_fast_hash(const key &in);
156  key hash_to_scalar(const key &in);
157  //for mg sigs
158  key cn_fast_hash128(const void * in);
159  key hash_to_scalar128(const void * in);
160  key cn_fast_hash(const ctkeyV &PC);
161  key hash_to_scalar(const ctkeyV &PC);
162  //for mg sigs
163  key cn_fast_hash(const keyV &keys);
164  key hash_to_scalar(const keyV &keys);
165  //for ANSL
166  key cn_fast_hash(const key64 keys);
167  key hash_to_scalar(const key64 keys);
168 
169  //returns hashToPoint as described in https://github.com/ShenNoether/ge_fromfe_writeup
170  key hashToPointSimple(const key &in);
171  key hashToPoint(const key &in);
172  void hashToPoint(key &out, const key &in);
173 
174  //sums a vector of curve points (for scalars use sc_add)
175  void sumKeys(key & Csum, const key &Cis);
176 
177  //Elliptic Curve Diffie Helman: encodes and decodes the amount b and mask a
178  // where C= aG + bH
179  void ecdhEncode(ecdhTuple & unmasked, const key & sharedSec);
180  void ecdhDecode(ecdhTuple & masked, const key & sharedSec);
181 }
182 #endif /* RCTOPS_H */
void genC(key &C, const key &a, xmr_amount amount)
Definition: rctOps.cpp:108
keyV skvGen(size_t rows)
Definition: rctOps.cpp:77
void precomp(ge_dsmp rv, const key &B)
Definition: rctOps.cpp:243
void addKeys1(key &aGB, const key &a, const key &B)
Definition: rctOps.cpp:226
void identity(key &Id)
Definition: rctOps.h:76
key pkGen()
Definition: rctOps.cpp:88
void curveOrder(key &l)
Definition: rctOps.h:79
key commit(xmr_amount amount, const key &mask)
Definition: rctOps.cpp:143
list B
Definition: base.py:26
uint64_t xmr_amount
Definition: rctTypes.h:126
void addKeys3(key &aAbB, const key &a, const key &A, const key &b, const ge_dsmp B)
Definition: rctOps.cpp:252
void scalarmultKey(key &aP, const key &P, const key &a)
Definition: rctOps.cpp:177
vector< ctkey > ctkeyV
Definition: rctTypes.h:104
void ecdhEncode(ecdhTuple &unmasked, const key &sharedSec)
Definition: rctOps.cpp:429
tuple< ctkey, ctkey > ctskpkGen(xmr_amount amount)
Definition: rctOps.cpp:114
crypto namespace.
Definition: crypto.cpp:47
key hash_to_scalar128(const void *in)
Definition: rctOps.cpp:329
Definition: block_queue.cpp:41
Definition: rctOps.cpp:41
key cn_fast_hash128(const void *in)
Definition: rctOps.cpp:323
key zeroCommit(xmr_amount amount)
Definition: rctOps.cpp:134
void sumKeys(key &Csum, const key &Cis)
keyM keyMInit(size_t rows, size_t cols)
Definition: rctOps.cpp:48
void zero(key &z)
Definition: rctOps.h:73
static const key I
Definition: rctOps.h:68
key key64[64]
Definition: rctTypes.h:128
void cn_fast_hash(key &hash, const void *data, const size_t l)
Definition: rctTypes.h:82
int b
Definition: base.py:1
xmr_amount randXmrAmount(xmr_amount upperlimit)
Definition: rctOps.cpp:152
key scalarmultH(const key &a)
Definition: rctOps.cpp:198
#define L(m0, m1, m2, m3, m4, m5, m6, m7)
Definition: jh.c:116
int l
Definition: base.py:3
vector< keyV > keyM
Definition: rctTypes.h:93
void skGen(key &sk)
Definition: rctOps.cpp:63
void scalarmultBase(key &aG, const key &a)
Definition: rctOps.cpp:159
ge_cached ge_dsmp[8]
Definition: crypto-ops.h:79
key copy(const key &A)
Definition: rctOps.h:82
static const key Z
Definition: rctOps.h:67
void ecdhDecode(ecdhTuple &masked, const key &sharedSec)
Definition: rctOps.cpp:436
string a
Definition: MakeCryptoOps.py:15
void subKeys(key &AB, const key &A, const key &B)
Definition: rctOps.cpp:263
void hash_to_scalar(key &hash, const void *data, const size_t l)
void addKeys2(key &aGbB, const key &a, const key &b, const key &B)
Definition: rctOps.cpp:233
void skpkGen(key &sk, key &pk)
Definition: rctOps.cpp:95
vector< key > keyV
Definition: rctTypes.h:92
key hashToPoint(const key &hh)
Definition: rctOps.cpp:394
POD_CLASS hash
Definition: hash.h:46
void addKeys(key &AB, const key &A, const key &B)
Definition: rctOps.cpp:211
bool equalKeys(const key &a, const key &b)
Definition: rctOps.cpp:277
key hashToPointSimple(const key &hh)
Definition: rctOps.cpp:380
int rows
Definition: crypto.h:77