Skip to content

Commit

Permalink
Blueokanna Update
Browse files Browse the repository at this point in the history
  • Loading branch information
blueokanna committed Nov 18, 2024
1 parent 649093b commit 0df0d32
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 50 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "BlueHash"
description = "A highly secure alternative to existing custom digest algorithms."
authors = ["[email protected]"]
version = "0.1.4"
version = "0.1.5"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
Expand All @@ -13,6 +13,7 @@ keywords = ["Digest", "Algorithms", "Hash", "Crypto"]
rand = "0.8.5"
criterion = { version = "0.5.1", features = ["plotters"] }
rayon = "1.10.0"
hex = "0.4.3"

[[bench]]
name = "bluebench"
Expand Down
18 changes: 9 additions & 9 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 概述

**BlueHash** 算法是一种自定义的加密哈希函数,旨在生成具有不同位数(128256 和 512 位)且安全的哈希值。它利用多个回合的转换,包括状态更新、置换函数和常量生成,这些都有助于最终哈希输出的独特性和安全性。
**BlueHash** 算法是一种自定义的加密哈希函数,旨在生成具有不同位数(128256 和 512 位)且安全的哈希值。它利用多个回合的转换,包括状态更新、置换函数和常量生成,这些都有助于最终哈希输出的独特性和安全性。

本文档提供了该算法核心组件的解释,包括状态大小、回合次数、常量生成和状态更新转换。文中还提供了描述每一步过程的数学公式。

Expand Down Expand Up @@ -41,9 +41,9 @@ fn main() {
```
### 你会得到输出结果如下:
```
The full 128-bit hash result is: 0x6a2f8f3572697616894a8a0d02eb42f3
The full 256-bit hash result is: 0xb668bc23ecf63a45071829d992140992985c7323b43387cc617f0c7537764025
The full 512-bit hash result is: 0x73c2ef6642efeda5bb00b3385b6a644e9791ff29a06c559b4723a76f168312010b8cc657b130b720d77cccad2c2137b2ba5c5efdd817d5d1743b6420d065a29d
The full 128-bit hash result is: e68e6528271d5623a8e195bb6ac7cff3
The full 256-bit hash result is: c472cbe52b0f1b44f3aa1cec8d56dc578eb75048be19ca5edc6d349c2b5c7ceb
The full 512-bit hash result is: 46ae2678b8ad6bf066313512f26ceba12211c6087b9f6d7b6223dbcc18687440699b65b333db95b978aba1440c27b5ad5833bbd796380f66028ffa6a9a44482e
```

## 关键组件
Expand All @@ -53,7 +53,7 @@ The full 512-bit hash result is: 0x73c2ef6642efeda5bb00b3385b6a644e9791ff29a06c5
**BlueHash** 算法使用固定的状态大小,并根据所需的输出大小选择不同的摘要长度。

- **状态大小**:25 个 64 位字(即 \( STATE\_SIZE = 25 \))。
- **摘要长度**:根据摘要大小,可能是 128、256 或 512 位。
- **摘要长度**:根据摘要大小,可能是 128位,256位 或 512 位。

每个摘要大小的轮次数和输出长度如下:

Expand Down Expand Up @@ -177,10 +177,10 @@ $$
### **SHA3-256**相比,**BlueHash**算法有以下优点:

1. **抗量子攻击**
通过 LWE 噪声和恒定生成机制,可以更好地抵御量子攻击
通过 LWE 噪声和恒定生成机制,可能可以更好地抵御量子攻击

2. **更高的随机性和复杂性**
利用动态生成常数,增加哈希算法的不可预测性。这增加了复杂性,并使算法更能抵御差分攻击。
利用动态生成常数,增加哈希算法的不可预测性。这增加了攻破的复杂性,并使算法更能抵御差分攻击。

3. **更强的状态更新和替换**
通过更多样化的比特运算和混合运算,提高了对传统攻击和量子攻击的抵御能力。
Expand All @@ -196,13 +196,13 @@ $$
### BlueHash 的潜在局限性:

1. **较高的性能开销**
多轮复杂运算和 LWE 噪声生成会增加计算开销,可能会影响大数据处理的效率。
多轮复杂计算运算和 LWE 噪声生成会增加计算开销,可能会影响大数据处理的效率。

2. **更高的内存消耗**
更多的本地变量和状态存储要求可能会导致低内存环境下的性能瓶颈。

3. **缺乏标准化和审计**
与 SHA3-256 相比**BlueHash** 缺乏广泛的安全审计。
SHA2 或 SHA3 摘要算法相比**BlueHash** 缺乏广泛的安全审计。

----
> **BlueHash** 算法是一种自定义的加密哈希函数,利用多个回合的复杂转换生成不同长度的安全哈希。它使用固定的状态大小、基于回合的转换和常量生成来确保最终输出的独特性和安全性。
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ fn main() {
```
### You will get output for example code
```
The full 128-bit hash result is: 0x6a2f8f3572697616894a8a0d02eb42f3
The full 256-bit hash result is: 0xb668bc23ecf63a45071829d992140992985c7323b43387cc617f0c7537764025
The full 512-bit hash result is: 0x73c2ef6642efeda5bb00b3385b6a644e9791ff29a06c559b4723a76f168312010b8cc657b130b720d77cccad2c2137b2ba5c5efdd817d5d1743b6420d065a29d
The full 128-bit hash result is: e68e6528271d5623a8e195bb6ac7cff3
The full 256-bit hash result is: c472cbe52b0f1b44f3aa1cec8d56dc578eb75048be19ca5edc6d349c2b5c7ceb
The full 512-bit hash result is: 46ae2678b8ad6bf066313512f26ceba12211c6087b9f6d7b6223dbcc18687440699b65b333db95b978aba1440c27b5ad5833bbd796380f66028ffa6a9a44482e
```

## Key Components
Expand Down
106 changes: 71 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
//! resist quantum attacks while maintaining high security. It includes state manipulation,
//! constant generation, and noise-based perturbations inspired by lattice-based cryptography.
//!
//! You can check github: https://github.com/blueokanna/BlueHash for details.
//! Full details and source code: https://github.com/blueokanna/BlueHash.
pub mod constants;
pub mod noise;
pub mod utils;

use std::collections::VecDeque;
pub use constants::generate_constants;
pub use noise::generate_lwe_noise;
pub use utils::to_u64;

/// State size used for the internal state array of BlueHash.
pub const STATE_SIZE: usize = 25;

/// Represents the available digest sizes for BlueHash.
#[derive(Debug, Copy, Clone)]
#[warn(non_snake_case)]
pub enum DigestSize {
Bit128,
Bit256,
Bit512,
}

impl DigestSize {
/// Returns the number of rounds based on the digest size.
pub fn round_count(&self) -> usize {
match self {
DigestSize::Bit128 => 56,
Expand All @@ -34,6 +35,7 @@ impl DigestSize {
}
}

/// Returns the output length of the digest in bytes.
pub fn digest_length(&self) -> usize {
match self {
DigestSize::Bit128 => 16,
Expand All @@ -43,15 +45,33 @@ impl DigestSize {
}
}

#[derive(Debug, Clone, Copy)]
/// The `Digest` trait for cryptographic hash functions.
pub trait Digest {
/// Updates the internal state with the given input data.
fn update(&mut self, data: &[u8]);

/// Finalizes the computation and returns the resulting hash as a byte vector.
fn finalize(&self) -> Vec<u8>;

/// Resets the internal state, making the hasher ready for reuse.
fn reset(&mut self);
}

/// BlueHash structure implementing the cryptographic hash algorithm.
#[derive(Debug, Clone)]
pub struct BlueHash {
/// The internal state array of the hasher.
state: [u64; STATE_SIZE],

/// The number of rounds for this instance of BlueHash.
round_count: usize,

/// The digest size configuration (e.g., 128-bit, 256-bit).
digest_size: DigestSize,
}

impl BlueHash {
/// Constructs a new BlueHash instance with the given digest size and key.
/// Constructs a new BlueHash instance with the given digest size.
///
/// # Arguments
///
Expand All @@ -68,33 +88,20 @@ impl BlueHash {
}
}

/// Updates the hash state with new data in chunks of 8 bytes.
///
/// # Arguments
///
/// * `data` - Input data to update the hash state.
pub fn update(&mut self, data: &[u8]) {
for chunk in data.chunks(8) {
let block = to_u64(chunk);
self.state[0] ^= block;
self.permute(data);
}
}

/// Applies a permutation to the internal state based on the input data.
///
/// # Arguments
///
/// * `input_data` - The data used to perturb the state.
fn permute(&mut self, input_data: &[u8]) {
let mut queue: VecDeque<u64> = VecDeque::new();
// Optimize by replacing VecDeque with a fixed-size array
let mut tmp_state = [0u64; STATE_SIZE];

// Using the idea of Merkle tree to reduce repeated calculations
for round in 0..self.round_count {
// Generate a constant value for this round using input data
let constant = generate_constants(round, input_data, self.round_count);
let state = &mut self.state;

// Change the state update to a divide-and-conquer calculation on a tree structure
for i in 0..STATE_SIZE {
let local_vars = [
state[(i + 1) % STATE_SIZE],
Expand All @@ -104,30 +111,35 @@ impl BlueHash {
state[(i + 5) % STATE_SIZE],
];

let tmp = state[i]
// Update temporary state with computed value
tmp_state[i] = state[i]
.wrapping_add(constant)
.wrapping_add(local_vars[2])
.rotate_left(29)
.wrapping_add(local_vars[0] & local_vars[1])
.wrapping_add(local_vars[3].rotate_right(17))
.rotate_left(23);

queue.push_back(tmp);
}

// After each round of calculation, the update of the state value is optimized through the queue
for i in 0..STATE_SIZE {
self.state[i] = queue.pop_front().unwrap_or(0);
}
// Update the main state array from the temporary state
self.state.copy_from_slice(&tmp_state);
}
}
}

/// Finalizes the hash calculation and returns the hash output as a byte vector.
///
/// # Returns
///
/// The final hash output as a vector of bytes.
pub fn finalize(&self) -> Vec<u8> {
/// Implements the `Digest` trait for BlueHash.
impl Digest for BlueHash {
/// Updates the internal state with the given input data.
fn update(&mut self, data: &[u8]) {
for chunk in data.chunks(8) {
let block = to_u64(chunk);
self.state[0] ^= block;
self.permute(data);
}
}

/// Finalizes the computation and returns the resulting hash as a byte vector.
fn finalize(&self) -> Vec<u8> {
let digest_size = self.digest_size.digest_length();
let mut result = vec![0u8; digest_size];
let mut output_idx = 0;
Expand All @@ -144,4 +156,28 @@ impl BlueHash {

result
}
}

/// Resets the internal state to its initial value.
fn reset(&mut self) {
self.state = [0u64; STATE_SIZE];
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_bluehash() {
let mut hasher = BlueHash::new(DigestSize::Bit256);
hasher.update(b"Hello, world! This is a test message for BlueHash");
let result = hasher.finalize();

// Adjust expected values to match algorithm logic
assert_eq!(result.len(), 32);
assert_eq!(
hex::encode(result.clone()),
"c472cbe52b0f1b44f3aa1cec8d56dc578eb75048be19ca5edc6d349c2b5c7ceb"
);
}
}
2 changes: 0 additions & 2 deletions src/noise.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/// Generates LWE noise based on the input data, round number, and secret key.
/// The noise is designed to enhance resistance against quantum attacks by using
/// a combination of multiplicative and additive operations, with bit rotations
Expand All @@ -16,7 +15,6 @@
/// # Returns
///
/// A 64-bit unsigned integer representing the generated noise value.
pub fn generate_lwe_noise(input_data: &[u8], round: usize, prime: u64) -> u64 {
let mut noise = prime;

Expand Down

0 comments on commit 0df0d32

Please sign in to comment.