Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aviad/blake2s #576

Open
wants to merge 26 commits into
base: V2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions icicle/include/api/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
#include <cuda_runtime.h>
#include "gpu-utils/device_context.cuh"
#include "hash/keccak/keccak.cuh"
#include "hash/blake2s/blake2s.cuh"
#include "merkle-tree/merkle.cuh"

/* KECCAK */

extern "C" cudaError_t
keccak256_cuda(uint8_t* input, int input_block_size, int number_of_blocks, uint8_t* output, keccak::HashConfig& config);

Expand All @@ -27,4 +30,16 @@ extern "C" cudaError_t build_keccak512_merkle_tree_cuda(
unsigned int height,
unsigned int input_block_len,
const merkle_tree::TreeBuilderConfig& tree_config);

/* BLAKE2S */

extern "C" cudaError_t blake2s_cuda(
BYTE* input, BYTE* output, WORD number_of_blocks, WORD input_block_size, WORD output_block_size, blake2s::HashConfig& config)

extern "C" cudaError_t build_blake2s_merkle_tree_cuda(
const uint8_t* leaves,
uint64_t* digests,
unsigned int height,
unsigned int input_block_len,
const merkle_tree::TreeBuilderConfig& tree_config);
#endif
82 changes: 82 additions & 0 deletions icicle/include/hash/blake2s/blake2s.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* blake2b.cuh CUDA Implementation of BLAKE2B Hashing
*
* Date: 12 June 2019
* Revision: 1
*
* This file is released into the Public Domain.
*/

#pragma once

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "gpu-utils/device_context.cuh"
#include "gpu-utils/error_handler.cuh"
#include "merkle-tree/merkle.cuh"

#include "hash/hash.cuh"
using namespace hash;

#include "matrix/matrix.cuh"
using matrix::Matrix;

namespace blake2s {

typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long long LONG;

#define BLAKE2S_ROUNDS 10
#define BLAKE2S_BLOCK_LENGTH 64
#define BLAKE2S_CHAIN_SIZE 8
#define BLAKE2S_CHAIN_LENGTH (BLAKE2S_CHAIN_SIZE * sizeof(uint32_t))
#define BLAKE2S_STATE_SIZE 16
#define BLAKE2S_STATE_LENGTH (BLAKE2S_STATE_SIZE * sizeof(uint32_t))

class Blake2s : public Hasher<BYTE, BYTE>
{
bool use_iv;
public:
cudaError_t run_hash_many_kernel(
const BYTE* input,
BYTE* output,
WORD number_of_states,
WORD input_len,
WORD output_len,
const device_context::DeviceContext& ctx) const override;

cudaError_t hash_2d(
const Matrix<BYTE>* inputs,
BYTE* states,
unsigned int number_of_inputs,
unsigned int output_len,
uint64_t number_of_rows,
const device_context::DeviceContext& ctx) const override;

cudaError_t compress_and_inject(
const Matrix<BYTE>* matrices_to_inject,
unsigned int number_of_inputs,
uint64_t number_of_rows,
const BYTE* prev_layer,
BYTE* next_layer,
unsigned int digest_elements,
const device_context::DeviceContext& ctx) const override;

Blake2s(bool use_iv=true) : Hasher<BYTE, BYTE>(BLAKE2S_STATE_SIZE * 4, BLAKE2S_STATE_SIZE * 4, BLAKE2S_STATE_SIZE * 4, 0), use_iv(use_iv) {}
};

extern "C" {
cudaError_t
cuda_blake2s_hash_batch(BYTE* key, WORD keylen, BYTE* in, WORD inlen, BYTE* out, WORD output_len, WORD n_batch);

cudaError_t blake2s_mmcs_commit_cuda(
const Matrix<BYTE>* leaves,
unsigned int number_of_inputs,
BYTE* digests,
const merkle_tree::TreeBuilderConfig& tree_config);
}

} // namespace blake2s
7 changes: 6 additions & 1 deletion icicle/src/hash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
set(TARGET icicle_hash)

add_library(${TARGET} STATIC keccak/extern.cu)
set(SRC ${CMAKE_SOURCE_DIR}/src/hash)

set(HASH_SOURCE ${SRC}/keccak/extern.cu)
list(APPEND HASH_SOURCE ${SRC}/blake2s/extern.cu)

add_library(${TARGET} STATIC ${HASH_SOURCE})
target_include_directories(${TARGET} PUBLIC ${CMAKE_SOURCE_DIR}/include/)
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME "ingo_hash")
26 changes: 26 additions & 0 deletions icicle/src/hash/blake2s/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
test_blake2s: test_blake2s.cu blake2s.cu
nvcc -o test_blake2s -I. -I../../../include test_blake2s.cu blake2s.cu -g
./test_blake2s

test_blake2s_batched: test_blake2s_batched.cu blake2s.cu
nvcc -o test_blake2s_batched -I. -I../../../include test_blake2s_batched.cu -g
./test_blake2s_batched ./batched_test_vectors.csv

test_blake2s_integ: test_blake2s_integ.cu blake2s.cu
nvcc -o test_blake2s_integ -I. -I../../../include test_blake2s_integ.cu -g
./test_blake2s_integ

test_blake2s_seq: test_blake2s_seq.cu blake2s.cu
nvcc -o test_blake2s_seq -I. -I../../../include test_blake2s_seq.cu -g
./test_blake2s_seq

test_blake2s_seq_sa: test_blake2s_seq_sa.cu blake2s.cu
nvcc -o test_blake2s_seq_sa -I. -I../../../include test_blake2s_seq_sa.cu -g
./test_blake2s_seq_sa

test_blake2s_tree: test_tree.cu blake2s.cu ../../merkle-tree/merkle.cu
nvcc -DMERKLE_DEBUG -o test_blake2s_tree -I../../../include test_tree.cu
./test_blake2s_tree

clear:
rm test_blake2s test_blake2s_tree test_blake2s_integ test_blake2s_seq test_blake2s_seq_sa test_blake2s_batched
10 changes: 10 additions & 0 deletions icicle/src/hash/blake2s/batched_test_vectors.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
9301876542, 1e95b837356f67e9b456d636dd3d3f55bfff01eb78b375f613db5819f27e5972
5279608431, 000386abd221e7049e78091c4b964719dd45fda6a70ad88194c8ecc7fb5ec4bc
1842976503, b35ce7baa5c5c620be9bf0d03a7a5b43fd18e786e1678ad797a30b50dc48ccc7
6498302715, 489412fcb1a74c14dbe06aaad61cf2d3ed0eaa6a6154afc9f8b58fe92ffcebf1
7023598146, 6b9a45147bd1c61f8d1d3d110cb705ae85ddc31ac7cb18e47306bc51d4d807ba
3150729846, 5d9d597b956a26fd79cd8bdf38e306db068b6089b305268b90fd1a304a5b2224
9583402167, 584cd56b727e14ccc7fcaf406982faab08529b6789748c9ffc74748b033cf44f
8760134295, 2cba2adb552cc89312c614c3d720edaa5cf03bc5fc2a012511cfee013636ee40
5402987631, d6d4920f85f5e286f3add452fab5b19f31e66293ec612b29389643f78b4ace2a
2739810546, 69aee09804f37a477b34f9a4447b39e9caaae49bbc5056b8018dd513c8cec263
Loading
Loading