1
1
// ---------------------------------------------------------------------------//
2
2
// Copyright (c) 2023 Elena Tatuzova <[email protected] >
3
+ // Copyright (c) 2024 Vasiliy Olekhov <[email protected] >
3
4
//
4
5
// MIT License
5
6
//
@@ -44,37 +45,40 @@ namespace nil {
44
45
using transcript_type = transcript::fiat_shamir_heuristic_sequential<transcript_hash_type>;
45
46
using output_type = OutType;
46
47
47
- static inline OutType generate (transcript_type &transcript, OutType mask=0xFFFF ) {
48
+ static inline std::array<std::uint8_t , sizeof (OutType)>
49
+ int_be (OutType v) {
50
+ std::array<std::uint8_t , sizeof (OutType)> bytes;
51
+ for (int i = sizeof (v)-1 ; i>=0 ; --i) {
52
+ bytes[i] = v & 0xFF ;
53
+ v >>= 8 ;
54
+ }
55
+ return bytes;
56
+ }
57
+
58
+ static inline OutType generate (transcript_type &transcript, std::size_t GrindingBits = 16 ) {
59
+ BOOST_ASSERT_MSG (GrindingBits < 64 , " Grinding parameter should be bits, not mask" );
60
+ output_type mask = GrindingBits > 0 ? ( 1ULL << GrindingBits ) - 1 : 0 ;
48
61
output_type proof_of_work = std::rand ();
49
62
output_type result;
50
- std::vector<std::uint8_t > bytes (4 );
51
63
52
64
while ( true ) {
53
65
transcript_type tmp_transcript = transcript;
54
- bytes[0 ] = std::uint8_t ((proof_of_work&0xFF000000 )>>24 );
55
- bytes[1 ] = std::uint8_t ((proof_of_work&0x00FF0000 )>>16 );
56
- bytes[2 ] = std::uint8_t ((proof_of_work&0x0000FF00 )>>8 );
57
- bytes[3 ] = std::uint8_t (proof_of_work&0x000000FF );
58
-
59
- tmp_transcript (bytes);
66
+ tmp_transcript (int_be (proof_of_work));
60
67
result = tmp_transcript.template int_challenge <output_type>();
61
68
if ((result & mask) == 0 )
62
69
break ;
63
70
proof_of_work++;
64
71
}
65
- transcript (bytes );
72
+ transcript (int_be (proof_of_work) );
66
73
result = transcript.template int_challenge <output_type>();
67
74
return proof_of_work;
68
75
}
69
76
70
- static inline bool verify (transcript_type &transcript, output_type proof_of_work, OutType mask=0xFFFF ) {
71
- std::vector<std::uint8_t > bytes (4 );
72
- bytes[0 ] = std::uint8_t ((proof_of_work&0xFF000000 )>>24 );
73
- bytes[1 ] = std::uint8_t ((proof_of_work&0x00FF0000 )>>16 );
74
- bytes[2 ] = std::uint8_t ((proof_of_work&0x0000FF00 )>>8 );
75
- bytes[3 ] = std::uint8_t (proof_of_work&0x000000FF );
76
- transcript (bytes);
77
+ static inline bool verify (transcript_type &transcript, output_type proof_of_work, std::size_t GrindingBits = 16 ) {
78
+ BOOST_ASSERT_MSG (GrindingBits < 64 , " Grinding parameter should be bits, not mask" );
79
+ transcript (int_be (proof_of_work));
77
80
output_type result = transcript.template int_challenge <output_type>();
81
+ output_type mask = GrindingBits > 0 ? ( 1ULL << GrindingBits ) - 1 : 0 ;
78
82
return ((result & mask) == 0 );
79
83
}
80
84
};
@@ -91,7 +95,7 @@ namespace nil {
91
95
using value_type = typename FieldType::value_type;
92
96
using integral_type = typename FieldType::integral_type;
93
97
94
- static inline value_type generate (transcript_type &transcript, std::size_t GrindingBits= 16 ) {
98
+ static inline value_type generate (transcript_type &transcript, std::size_t GrindingBits = 16 ) {
95
99
static boost::random ::random_device dev;
96
100
static nil::crypto3::random ::algebraic_engine<FieldType> random_engine (dev);
97
101
value_type proof_of_work = random_engine ();
@@ -115,7 +119,7 @@ namespace nil {
115
119
return proof_of_work;
116
120
}
117
121
118
- static inline bool verify (transcript_type &transcript, value_type proof_of_work, std::size_t GrindingBits= 16 ) {
122
+ static inline bool verify (transcript_type &transcript, value_type proof_of_work, std::size_t GrindingBits = 16 ) {
119
123
transcript (proof_of_work);
120
124
integral_type mask =
121
125
(GrindingBits > 0 ?
0 commit comments