forked from NoahBz/Easy-BigInt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HardPoly1305.cpp
323 lines (263 loc) · 10.7 KB
/
HardPoly1305.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
MIT License
Copyright (c) 2024-2050 Twilight-Dream & With-Sky
https://github.com/Twilight-Dream-Of-Magic/
https://github.com/With-Sky
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <iomanip>
#include <cassert>
#include "HardPoly1305.hpp"
std::vector<uint8_t> SubByteArray( const std::vector<uint8_t>& data, ptrdiff_t start, ptrdiff_t end, ptrdiff_t step = 1 )
{
if ( step == 0 )
{
return {};
}
ptrdiff_t dataSize = static_cast<ptrdiff_t>( data.size() );
if ( start < 0 )
{
start += dataSize;
}
if ( end < 0 )
{
end += dataSize;
}
start = std::max<ptrdiff_t>( 0, start );
end = std::min<ptrdiff_t>( end, dataSize );
if ( start >= end )
{
return {};
}
std::vector<uint8_t> sub_array;
if ( step > 0 )
{
auto first = data.begin() + start;
auto last = data.begin() + end;
std::copy_if( first, last, std::back_inserter( sub_array ), [ n = 0, step ]( const uint8_t& ) mutable { return n++ % step == 0; } );
}
else
{
auto first = data.rbegin() + ( dataSize - end );
auto last = data.rbegin() + ( dataSize - start );
std::copy_if( first, last, std::back_inserter( sub_array ), [ n = 0, step ]( const uint8_t& ) mutable { return n++ % -step == 0; } );
}
return sub_array;
}
std::string BytesToHexString( const std::vector<uint8_t>& bytes )
{
std::ostringstream oss;
for ( const auto& byte : bytes )
{
oss << std::hex << std::setw( 2 ) << std::setfill( '0' ) << static_cast<int>( byte );
}
return oss.str();
}
std::vector<uint8_t> generate_random_bytes( size_t size )
{
std::vector<uint8_t> bytes( size );
std::random_device rd;
std::mt19937 gen( rd() );
std::uniform_int_distribution<> dis( 0, 255 );
for ( size_t i = 0; i < size; ++i )
{
bytes[ i ] = static_cast<uint8_t>( dis( gen ) );
}
return bytes;
}
HardPoly1305::BigSignedInteger HardPoly1305::generate_unpredictable_value( const BigSignedInteger& hash_value, const BigSignedInteger& key )
{
//std::cout << "\n--- generate_unpredictable_value ---\n";
//std::cout << "Initial hash_value: " << hash_value.ToString( 10 ) << '\n';
//std::cout << "Initial key: " << key.ToString( 10 ) << '\n';
const BigSignedInteger a = BigSignedInteger( hash_value ) + BigSignedInteger( key );
const BigSignedInteger b = BigSignedInteger( key ) - BigSignedInteger( hash_value );
// 复杂公式计算 alpha
BigSignedInteger alpha = ( ( a * a ) + ( b * b * b ) );
//std::cout << "Computed alpha: " << alpha.ToString( 10 ) << '\n';
// 应用 模数 p2
if ( alpha < 0 )
{
//std::cout << "Alpha is negative: " << alpha.ToString( 10 ) << '\n';
alpha = p2 + alpha; //Adjustment of negative numbers to a positive range [0, p2]
//std::cout << "Adjusted alpha by adding p2: " << alpha.ToString( 10 ) << '\n';
}
if ( alpha >= p2 )
{
//std::cout << "Alpha exceeds p2: " << alpha.ToString( 10 ) << '\n';
alpha = alpha % p2;
//std::cout << "Alpha reduced modulo p2: " << alpha.ToString( 10 ) << '\n';
}
size_t key_bit_length = key.BitLength();
size_t hash_value_bit_length = hash_value.BitLength();
//std::cout << "key_bit_length: " << key_bit_length << '\n';
//std::cout << "hash_value_bit_length: " << hash_value_bit_length << '\n';
const BigSignedInteger& u0 = a;
//std::cout << "Computed u0 (hash_value + key): " << u0.ToString( 10 ) << '\n';
const BigSignedInteger u1 = hash_value - key;
//std::cout << "Computed u1 (hash_value - key): " << u1.ToString( 10 ) << '\n';
uint64_t left_shift_amount = ( u0 % key_bit_length ).ToUnsignedInt();
uint64_t right_shift_amount = ( u1 % hash_value_bit_length ).ToUnsignedInt();
BigSignedInteger u2 = key << left_shift_amount;
//std::cout << "Computed u2 (key << (u0 % key_bit_length)): " << u2.ToString( 10 ) << '\n';
BigSignedInteger u3 = hash_value >> right_shift_amount;
//std::cout << "hash_value_bit_length: " << hash_value_bit_length << "\n";
//std::cout << "right_shift_amount: " << right_shift_amount << "\n";
//std::cout << "Computed u3 (hash_value >> (u1 % hash_value_bit_length)): " << u3.ToString( 10 ) << '\n';
BigSignedInteger u4 = hash_value * ( u2 + u3 + alpha );
//std::cout << "Computed u4 (hash_value * (u2 + u3 + alpha)): " << u4.ToString( 10 ) << '\n';
u4 = u4 % p;
//std::cout << "Final u4 modulo p: " << u4.ToString( 10 ) << '\n';
//std::cout << "--- End of generate_unpredictable_value ---\n";
// 应用 模数 p 规约大小
return u4;
}
std::vector<uint8_t> HardPoly1305::mix_key_and_message( const std::vector<uint8_t>& message, const std::vector<uint8_t>& key )
{
// Mix the message and key
std::vector<uint8_t> mixed_data( message.size(), 0 );
size_t key_index = 0;
for ( size_t i = 0; i < message.size(); ++i )
{
mixed_data[ i ] = ( message[ i ] + key[ key_index ] ) % 256;
key_index = ( key_index + 1 ) % key.size();
}
if ( mixed_data.size() < 32 )
{
//mixed_data = mixed_data concatenation key
mixed_data.reserve( 32 );
for ( size_t i = mixed_data.size(); i < key.size(); i++ )
{
mixed_data.push_back( key[ i ] );
}
}
return mixed_data;
}
std::vector<uint8_t> HardPoly1305::hard_poly1305_core( const std::vector<uint8_t>& mixed_data, const std::vector<uint8_t>& key )
{
// 初始化混合消息状态
BigSignedInteger mixed_number = 0;
// 获取中间字节部分的密钥
BigSignedInteger key_number = 0;
key_number.ImportData( false, SubByteArray( key, 7, 23 ) );
std::cout << "\n--- hard_poly1305_core ---\n";
std::cout << "Initial key_number (from key[7:23]): " << key_number.ToString( 10 ) << '\n';
key_number *= 5;
std::cout << "After multiplying by 5, key_number: " << key_number.ToString( 10 ) << '\n';
// 初始化(Aaccumulator) hash_value 为 0
BigSignedInteger hash_value = 0;
// 初始化秘密状态 Secret status
r.ImportData( false, SubByteArray( key, 0, 15 ) );
s.ImportData( false, SubByteArray( key, 16, 31 ) );
std::cout << "Initial r: ";
r.Print( 10 );
std::cout << "Initial s: ";
s.Print( 10 );
// 计算 loop_count
size_t loop_count = ( mixed_data.size() + 15 ) / 16 + 1;
//std::cout << "Total loop_count: " << loop_count << '\n';
BigSignedInteger mixed_data_number = 0;
mixed_data_number.ImportData( false, mixed_data );
//std::cout << "Initial mixed_data_number : ";
mixed_data_number.Print( 10 );
std::vector<uint8_t> mixed_data_span;
// HardPoly1305 算法核心计算循环
for ( size_t i = 1; i <= loop_count - 1; ++i )
{
//std::cout << "\n--- Loop " << i << " ---\n";
// 将 r 进行 clamp
r &= clamp_bit_mask;
//std::cout << "Clamped r: " << r.ToString( 10 ) << '\n';
// 获取 mixed_data 的字节切片
mixed_data_span = SubByteArray( mixed_data, ( i - 1 ) * 16, i * 16 );
// 重新分配 bytes 大小并拼接 Byte 0x01
mixed_data_span.push_back( 0x01 );
// 打印 mixed_data_span 的16进制表示
//std::cout << "Mixed data span (hex): " << BytesToHexString( mixed_data_span ) << '\n';
// 计算 mixed_number
mixed_number.ImportData( false, mixed_data_span );
//std::cout << "Computed mixed_number from mixed_data_span: " << mixed_number.ToString( 10 ) << '\n';
// 更新 hash_value
hash_value += mixed_number;
//std::cout << "Updated hash_value after adding mixed_number: " << hash_value.ToString( 10 ) << '\n';
// 首先 应用 秘密部分 r 进行倍增 然后 应用 模数 p
hash_value = ( r * hash_value ) % p;
//std::cout << "Updated hash_value after multiplying by r and mod p: " << hash_value.ToString( 10 ) << '\n';
// 最后 应用 秘密部分 s
hash_value += s;
//std::cout << "Updated hash_value after adding s: " << hash_value.ToString( 10 ) << '\n';
// 更新秘密状态 r 和 s
r = generate_unpredictable_value( hash_value, mixed_number );
s = generate_unpredictable_value( hash_value, key_number );
}
// 规约大小 hash = hash (mod 2^128)
hash_value = hash_value % hash_max_number;
std::cout << "\nFinal hash_value mod 2^128: " << hash_value.ToString( 10 ) << '\n';
std::cout << "--- End of hard_poly1305_core ---\n";
// 返回结果
std::vector<uint8_t> hash_result;
bool is_negative = false;
hash_value.ExportData( is_negative, hash_result, 16 );
std::cout << "Final Tag/Hash Bytes Data:\n";
for ( const auto& byte : hash_result )
{
std::cout << std::hex << std::setw( 2 ) << std::setfill( '0' ) << static_cast<unsigned int>( byte );
}
std::cout << '\n';
return hash_result;
}
void test_hard_poly1305()
{
using BigInteger = TwilightDream::BigInteger::BigInteger;
// 创建 HardPoly1305 对象
HardPoly1305 hard_poly1305;
// 生成消息和随机密钥
std::string string_message = std::string( "Hello, world!" );
std::vector<uint8_t> message( string_message.begin(), string_message.end() );
std::vector<uint8_t> key( 32, 'A' );
// 计算混合消息数据 将消息与密钥混合
std::vector<uint8_t> mixed_data = hard_poly1305.mix_key_and_message( message, key );
auto format_flags = std::cout.flags();
// 输出密钥、消息和标签
std::cout << "Message: ";
for ( const auto& byte : message )
{
std::cout << std::hex << std::setw( 2 ) << std::setfill( '0' ) << static_cast<unsigned int>( byte );
}
std::cout << '\n';
std::cout.flags( format_flags );
std::cout << "message byte length: " << message.size() << std::endl;
std::cout << "Key: ";
for ( const auto& byte : key )
{
std::cout << std::hex << std::setw( 2 ) << std::setfill( '0' ) << static_cast<unsigned int>( byte );
}
std::cout << '\n';
std::cout.flags( format_flags );
std::cout << "key byte length: " << key.size() << std::endl;
std::cout << "MixData: ";
for ( const auto& byte : mixed_data )
{
std::cout << std::hex << std::setw( 2 ) << std::setfill( '0' ) << static_cast<unsigned int>( byte );
}
std::cout << '\n';
std::cout.flags( format_flags );
std::cout << "mixed_data byte length: " << mixed_data.size() << std::endl;
// 计算消息的标签
std::vector<uint8_t> tag = hard_poly1305.hard_poly1305_core( mixed_data, key );
std::cout.flags( format_flags );
}