-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from itzmeanjan/sha3-256
Implementing SHA3-256 based Binary Merklization
- Loading branch information
Showing
32 changed files
with
2,311 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "sha3_224.hpp" | ||
#include <cassert> | ||
|
||
// This example attempts to show how to use 2-to-1 SHA3-224 hash function ! | ||
int | ||
main(int argc, char** argv) | ||
{ | ||
// $ python3 | ||
// >>> a = [0xff] * 28 | ||
// | ||
// first input digest | ||
constexpr sycl::uchar digest_0[28] = { | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
}; | ||
|
||
// >>> b = [0x0f] * 28 | ||
// | ||
// second input digest | ||
constexpr sycl::uchar digest_1[28] = { | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
}; | ||
|
||
// >>> c = a + b | ||
// >>> import hashlib | ||
// >>> list(hashlib.sha3_224(bytes(c)).digest()) | ||
// | ||
// final output digest after merging two input digests | ||
constexpr sycl::uchar digest_2[28] = { 248, 78, 65, 145, 150, 163, 134, | ||
170, 248, 171, 53, 130, 25, 148, | ||
39, 171, 106, 173, 59, 81, 105, | ||
249, 104, 41, 206, 226, 62, 243 }; | ||
|
||
sycl::default_selector s{}; | ||
sycl::device d{ s }; | ||
sycl::context c{ d }; | ||
sycl::queue q{ c, d }; | ||
|
||
// so that input digests can be transferred from host to device ( by runtime ) | ||
sycl::uchar* in = static_cast<sycl::uchar*>( | ||
sycl::malloc_shared(sizeof(digest_0) + sizeof(digest_1), q)); | ||
|
||
// so that output digest can be transferred from device to host ( by runtime ) | ||
sycl::uchar* out = | ||
static_cast<sycl::uchar*>(sycl::malloc_shared(sizeof(digest_2), q)); | ||
|
||
// copy both input digests to device memory | ||
q.memcpy(in + 0, digest_0, sizeof(digest_0)).wait(); | ||
q.memcpy(in + sizeof(digest_0), digest_1, sizeof(digest_1)).wait(); | ||
|
||
// compute 2-to-1 hash | ||
q.single_task<class kernelExampleSHA3_224>( | ||
[=]() { sha3_224::hash(in, out); }); | ||
q.wait(); | ||
|
||
// finally assert ! | ||
for (size_t i = 0; i < sizeof(digest_2); i++) { | ||
assert(*(out + i) == digest_2[i]); | ||
} | ||
|
||
// deallocate resources ! | ||
sycl::free(in, q); | ||
sycl::free(out, q); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "sha3_256.hpp" | ||
#include <cassert> | ||
|
||
// This example attempts to show how to use 2-to-1 SHA3-256 hash function ! | ||
int | ||
main(int argc, char** argv) | ||
{ | ||
// $ python3 | ||
// >>> a = [0xff] * 32 | ||
// | ||
// first input digest | ||
constexpr sycl::uchar digest_0[32] = { | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 | ||
}; | ||
|
||
// >>> b = [0x0f] * 32 | ||
// | ||
// second input digest | ||
constexpr sycl::uchar digest_1[32] = { 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15 }; | ||
|
||
// >>> c = a + b | ||
// >>> import hashlib | ||
// >>> list(hashlib.sha3_256(bytes(c)).digest()) | ||
// | ||
// final output digest after merging two input digests | ||
constexpr sycl::uchar digest_2[32] = { | ||
121, 136, 237, 222, 17, 197, 60, 82, 161, 87, 52, 66, 251, 235, 8, 125, | ||
1, 95, 88, 134, 1, 235, 132, 182, 114, 55, 207, 202, 17, 104, 74, 95 | ||
}; | ||
|
||
sycl::default_selector s{}; | ||
sycl::device d{ s }; | ||
sycl::context c{ d }; | ||
sycl::queue q{ c, d }; | ||
|
||
// so that input digests can be transferred from host to device ( by runtime ) | ||
sycl::uchar* in = static_cast<sycl::uchar*>( | ||
sycl::malloc_shared(sizeof(digest_0) + sizeof(digest_1), q)); | ||
|
||
// so that output digest can be transferred from device to host ( by runtime ) | ||
sycl::uchar* out = | ||
static_cast<sycl::uchar*>(sycl::malloc_shared(sizeof(digest_2), q)); | ||
|
||
// copy both input digests to device memory | ||
q.memcpy(in + 0, digest_0, sizeof(digest_0)).wait(); | ||
q.memcpy(in + sizeof(digest_0), digest_1, sizeof(digest_1)).wait(); | ||
|
||
// compute 2-to-1 hash | ||
q.single_task<class kernelExampleSHA3_256>( | ||
[=]() { sha3_256::hash(in, out); }); | ||
q.wait(); | ||
|
||
// finally assert ! | ||
for (size_t i = 0; i < sizeof(digest_2); i++) { | ||
assert(*(out + i) == digest_2[i]); | ||
} | ||
|
||
// deallocate resources ! | ||
sycl::free(in, q); | ||
sycl::free(out, q); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "sha3_384.hpp" | ||
#include <cassert> | ||
|
||
// This example attempts to show how to use 2-to-1 SHA3-384 hash function ! | ||
int | ||
main(int argc, char** argv) | ||
{ | ||
// $ python3 | ||
// >>> a = [0xff] * 48 | ||
// | ||
// first input digest | ||
constexpr sycl::uchar digest_0[48] = { | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 | ||
}; | ||
|
||
// >>> b = [0x0f] * 48 | ||
// | ||
// second input digest | ||
constexpr sycl::uchar digest_1[48] = { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15 }; | ||
|
||
// >>> c = a + b | ||
// >>> import hashlib | ||
// >>> list(hashlib.sha3_384(bytes(c)).digest()) | ||
// | ||
// final output digest after merging two input digests | ||
constexpr sycl::uchar digest_2[48] = { | ||
25, 254, 93, 230, 2, 191, 78, 51, 238, 228, 239, 160, | ||
231, 101, 38, 216, 38, 8, 135, 59, 34, 169, 154, 20, | ||
221, 245, 50, 59, 27, 9, 21, 234, 249, 223, 45, 73, | ||
214, 0, 146, 51, 25, 83, 0, 0, 111, 210, 47, 206 | ||
}; | ||
|
||
sycl::default_selector s{}; | ||
sycl::device d{ s }; | ||
sycl::context c{ d }; | ||
sycl::queue q{ c, d }; | ||
|
||
// so that input digests can be transferred from host to device ( by runtime ) | ||
sycl::uchar* in = static_cast<sycl::uchar*>( | ||
sycl::malloc_shared(sizeof(digest_0) + sizeof(digest_1), q)); | ||
|
||
// so that output digest can be transferred from device to host ( by runtime ) | ||
sycl::uchar* out = | ||
static_cast<sycl::uchar*>(sycl::malloc_shared(sizeof(digest_2), q)); | ||
|
||
// copy both input digests to device memory | ||
q.memcpy(in + 0, digest_0, sizeof(digest_0)).wait(); | ||
q.memcpy(in + sizeof(digest_0), digest_1, sizeof(digest_1)).wait(); | ||
|
||
// compute 2-to-1 hash | ||
q.single_task<class kernelExampleSHA3_384>( | ||
[=]() { sha3_384::hash(in, out); }); | ||
q.wait(); | ||
|
||
// finally assert ! | ||
for (size_t i = 0; i < sizeof(digest_2); i++) { | ||
assert(*(out + i) == digest_2[i]); | ||
} | ||
|
||
// deallocate resources ! | ||
sycl::free(in, q); | ||
sycl::free(out, q); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "sha3_512.hpp" | ||
#include <cassert> | ||
|
||
// This example attempts to show how to use 2-to-1 SHA3-512 hash function ! | ||
int | ||
main(int argc, char** argv) | ||
{ | ||
// $ python3 | ||
// >>> a = [0xff] * 64 | ||
// | ||
// first input digest | ||
constexpr sycl::uchar digest_0[64] = { | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 | ||
}; | ||
|
||
// >>> b = [0x0f] * 64 | ||
// | ||
// second input digest | ||
constexpr sycl::uchar digest_1[64] = { | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 | ||
}; | ||
|
||
// >>> c = a + b | ||
// >>> import hashlib | ||
// >>> list(hashlib.sha3_512(bytes(c)).digest()) | ||
// | ||
// final output digest after merging two input digests | ||
constexpr sycl::uchar digest_2[64] = { | ||
73, 228, 11, 92, 59, 196, 139, 212, 163, 66, 229, 66, 106, | ||
155, 168, 55, 241, 215, 241, 253, 75, 61, 91, 215, 172, 186, | ||
250, 212, 10, 12, 61, 253, 80, 236, 57, 238, 27, 53, 53, | ||
20, 81, 55, 63, 196, 104, 93, 94, 74, 19, 36, 181, 15, | ||
41, 21, 198, 35, 60, 3, 65, 232, 15, 78, 220, 61 | ||
}; | ||
|
||
sycl::default_selector s{}; | ||
sycl::device d{ s }; | ||
sycl::context c{ d }; | ||
sycl::queue q{ c, d }; | ||
|
||
// so that input digests can be transferred from host to device ( by runtime ) | ||
sycl::uchar* in = static_cast<sycl::uchar*>( | ||
sycl::malloc_shared(sizeof(digest_0) + sizeof(digest_1), q)); | ||
|
||
// so that output digest can be transferred from device to host ( by runtime ) | ||
sycl::uchar* out = | ||
static_cast<sycl::uchar*>(sycl::malloc_shared(sizeof(digest_2), q)); | ||
|
||
// copy both input digests to device memory | ||
q.memcpy(in + 0, digest_0, sizeof(digest_0)).wait(); | ||
q.memcpy(in + sizeof(digest_0), digest_1, sizeof(digest_1)).wait(); | ||
|
||
// compute 2-to-1 hash | ||
q.single_task<class kernelExampleSHA3_512>( | ||
[=]() { sha3_512::hash(in, out); }); | ||
q.wait(); | ||
|
||
// finally assert ! | ||
for (size_t i = 0; i < sizeof(digest_2); i++) { | ||
assert(*(out + i) == digest_2[i]); | ||
} | ||
|
||
// deallocate resources ! | ||
sycl::free(in, q); | ||
sycl::free(out, q); | ||
|
||
return EXIT_SUCCESS; | ||
} |
Oops, something went wrong.