mirror of
https://github.com/logos-blockchain/sponges.git
synced 2026-07-29 18:13:17 +00:00
Add bash-f (STB 34.101.77-2020) (#92)
This commit is contained in:
parent
f205d0efbb
commit
4d778574a6
92
.github/workflows/bash-f.yml
vendored
Normal file
92
.github/workflows/bash-f.yml
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
name: bash-f
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- "bash-f.yml"
|
||||
- "bash-f/**"
|
||||
- "Cargo.*"
|
||||
push:
|
||||
branches: master
|
||||
|
||||
defaults:
|
||||
run:
|
||||
working-directory: bash-f
|
||||
|
||||
env:
|
||||
RUSTFLAGS: "-Dwarnings"
|
||||
CARGO_INCREMENTAL: 0
|
||||
|
||||
jobs:
|
||||
set-msrv:
|
||||
uses: RustCrypto/actions/.github/workflows/set-msrv.yml@master
|
||||
with:
|
||||
msrv: 1.85.0
|
||||
|
||||
build:
|
||||
needs: set-msrv
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
rust:
|
||||
- ${{needs.set-msrv.outputs.msrv}}
|
||||
- stable
|
||||
target:
|
||||
- thumbv7em-none-eabi
|
||||
- wasm32-unknown-unknown
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: RustCrypto/actions/cargo-cache@master
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
targets: ${{ matrix.target }}
|
||||
- run: cargo build --no-default-features --target ${{ matrix.target }}
|
||||
|
||||
minimal-versions:
|
||||
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
|
||||
with:
|
||||
working-directory: ${{ github.workflow }}
|
||||
|
||||
test:
|
||||
needs: set-msrv
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
rust:
|
||||
- ${{needs.set-msrv.outputs.msrv}}
|
||||
- stable
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: RustCrypto/actions/cargo-cache@master
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
- run: cargo test --no-default-features
|
||||
- run: cargo test
|
||||
- run: cargo test --all-features
|
||||
|
||||
miri:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
MIRIFLAGS: "-Zmiri-symbolic-alignment-check -Zmiri-strict-provenance"
|
||||
strategy:
|
||||
matrix:
|
||||
target:
|
||||
- x86_64-unknown-linux-gnu
|
||||
- s390x-unknown-linux-gnu
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: RustCrypto/actions/cargo-cache@master
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: nightly
|
||||
- name: Install Miri
|
||||
run: |
|
||||
rustup component add miri
|
||||
cargo miri setup
|
||||
- name: Test with Miri
|
||||
run: |
|
||||
cargo miri test --target ${{ matrix.target }} --no-default-features
|
||||
cargo miri test --target ${{ matrix.target }}
|
||||
cargo miri test --target ${{ matrix.target }} --all-features
|
||||
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -9,6 +9,10 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bash-f"
|
||||
version = "0.1.0-rc.0"
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.17"
|
||||
|
||||
@ -2,5 +2,6 @@
|
||||
resolver = "2"
|
||||
members = [
|
||||
"ascon",
|
||||
"bash-f",
|
||||
"keccak",
|
||||
]
|
||||
|
||||
11
bash-f/CHANGELOG.md
Normal file
11
bash-f/CHANGELOG.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## 0.1.0 (UNRELEASED)
|
||||
- Initial release ([#92])
|
||||
|
||||
[#92]: (https://github.com/RustCrypto/sponges/pull/92)
|
||||
14
bash-f/Cargo.toml
Normal file
14
bash-f/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "bash-f"
|
||||
version = "0.1.0-rc.0"
|
||||
description = "Pure Rust implementation of the bash-f sponge function defined in STB 34.101.77-2020"
|
||||
license = "Apache-2.0 OR MIT"
|
||||
authors = ["RustCrypto Developers"]
|
||||
documentation = "https://docs.rs/bash"
|
||||
homepage = "https://github.com/RustCrypto/sponges/tree/master/bash"
|
||||
repository = "https://github.com/RustCrypto/sponges"
|
||||
keywords = ["bash", "belt", "permutation"]
|
||||
categories = ["cryptography", "no-std"]
|
||||
readme = "README.md"
|
||||
edition = "2024"
|
||||
rust-version = "1.85"
|
||||
201
bash-f/LICENSE-APACHE
Normal file
201
bash-f/LICENSE-APACHE
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2025 The RustCrypto Project Developers
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
25
bash-f/LICENSE-MIT
Normal file
25
bash-f/LICENSE-MIT
Normal file
@ -0,0 +1,25 @@
|
||||
Copyright (c) 2025 The RustCrypto Project Developers
|
||||
|
||||
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.
|
||||
58
bash-f/README.md
Normal file
58
bash-f/README.md
Normal file
@ -0,0 +1,58 @@
|
||||
# RustCrypto: bash-f
|
||||
|
||||
[![crate][crate-image]][crate-link]
|
||||
[![Docs][docs-image]][docs-link]
|
||||
![Apache2/MIT licensed][license-image]
|
||||
![Rust Version][rustc-image]
|
||||
[![Project Chat][chat-image]][chat-link]
|
||||
[![Build Status][build-image]][build-link]
|
||||
|
||||
# BelHash - [STB 34.101.77-2020] Sponge-Based Cryptographic Algorithms
|
||||
|
||||
This module implements the cryptographic algorithms defined in the Belarusian
|
||||
state standard STB 34.101.77-2020 "Sponge-based cryptographic algorithms".
|
||||
|
||||
## Overview
|
||||
|
||||
The standard defines a family of cryptographic algorithms built on a sponge
|
||||
construction with the `bash-f` sponge function at its core. The sponge function
|
||||
operates on 1536-bit (192-byte) states.
|
||||
|
||||
## Security Notes
|
||||
|
||||
No security audits of this crate have ever been performed, and it has not been thoroughly assessed to ensure its operation is constant-time on common CPU architectures.
|
||||
|
||||
USE AT YOUR OWN RISK!
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of:
|
||||
|
||||
* [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
|
||||
* [MIT license](https://opensource.org/licenses/MIT)
|
||||
|
||||
at your option.
|
||||
|
||||
### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||
dual licensed as above, without any additional terms or conditions.
|
||||
|
||||
[//]: # (badges)
|
||||
|
||||
[crate-image]: https://buildstats.info/crate/bash-f
|
||||
[crate-link]: https://crates.io/crates/bash-f
|
||||
[docs-image]: https://docs.rs/bash-f/badge.svg
|
||||
[docs-link]: https://docs.rs/bash-f/
|
||||
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
|
||||
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
|
||||
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
|
||||
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260038-AEADs
|
||||
[downloads-image]: https://img.shields.io/crates/d/chacha20poly1305.svg
|
||||
[build-image]: https://github.com/RustCrypto/AEADs/workflows/bash-f/badge.svg?branch=master&event=push
|
||||
[build-link]: https://github.com/RustCrypto/AEADs/actions
|
||||
|
||||
[//]: # (general links)
|
||||
|
||||
[STB 34.101.77-2020]: https://apmi.bsu.by/assets/files/std/bash-spec241.pdf
|
||||
36
bash-f/benches/mod.rs
Normal file
36
bash-f/benches/mod.rs
Normal file
@ -0,0 +1,36 @@
|
||||
#![feature(test)]
|
||||
extern crate test;
|
||||
|
||||
use bash_f::{STATE_WORDS, bash_f};
|
||||
use test::Bencher;
|
||||
|
||||
#[bench]
|
||||
fn bench_bash_f(b: &mut Bencher) {
|
||||
let mut state = [0u64; STATE_WORDS];
|
||||
b.iter(|| {
|
||||
bash_f(test::black_box(&mut state));
|
||||
test::black_box(&state);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bash_f_10_rounds(b: &mut Bencher) {
|
||||
let mut state = [0u64; STATE_WORDS];
|
||||
b.iter(|| {
|
||||
for _ in 0..10 {
|
||||
bash_f(test::black_box(&mut state));
|
||||
}
|
||||
test::black_box(&state);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bash_f_100_rounds(b: &mut Bencher) {
|
||||
let mut state = [0u64; STATE_WORDS];
|
||||
b.iter(|| {
|
||||
for _ in 0..100 {
|
||||
bash_f(test::black_box(&mut state));
|
||||
}
|
||||
test::black_box(&state);
|
||||
});
|
||||
}
|
||||
143
bash-f/src/lib.rs
Normal file
143
bash-f/src/lib.rs
Normal file
@ -0,0 +1,143 @@
|
||||
#![no_std]
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![doc(
|
||||
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
|
||||
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
|
||||
)]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
/// Number of 64-bit words in the state
|
||||
pub const STATE_WORDS: usize = 24;
|
||||
|
||||
/// `bash-s` transformation.
|
||||
///
|
||||
/// Implements the S-box transformation defined in Section 6.1 of STB 34.101.77-2020.
|
||||
/// This is the core non-linear transformation used in the `bash-f` sponge function.
|
||||
fn bash_s(
|
||||
mut w0: u64,
|
||||
mut w1: u64,
|
||||
mut w2: u64,
|
||||
m1: u32,
|
||||
n1: u32,
|
||||
m2: u32,
|
||||
n2: u32,
|
||||
) -> (u64, u64, u64) {
|
||||
// 1. T0 ← RotHi^m1(W0)
|
||||
let t0 = w0.rotate_left(m1);
|
||||
|
||||
// 2. W0 ← W0 ⊕ W1 ⊕ W2
|
||||
w0 ^= w1 ^ w2;
|
||||
|
||||
// 3. T1 ← W1 ⊕ RotHi^n1(W0)
|
||||
let t1 = w1 ^ w0.rotate_left(n1);
|
||||
|
||||
// 4. W1 ← T0 ⊕ T1
|
||||
w1 = t0 ^ t1;
|
||||
|
||||
// 5. W2 ← W2 ⊕ RotHi^m2(W2) ⊕ RotHi^n2(T1)
|
||||
w2 ^= w2.rotate_left(m2) ^ t1.rotate_left(n2);
|
||||
|
||||
// 6. T0 ← ¬W2
|
||||
let t0 = !w2;
|
||||
|
||||
// 7. T1 ← W0 ∨ W2
|
||||
let t1 = w0 | w2;
|
||||
|
||||
// 8. T2 ← W0 ∧ W1
|
||||
let t2 = w0 & w1;
|
||||
|
||||
// 9. T0 ← T0 ∨ W1
|
||||
let t0 = t0 | w1;
|
||||
|
||||
// 10. W1 ← W1 ⊕ T1
|
||||
w1 ^= t1;
|
||||
|
||||
// 11. W2 ← W2 ⊕ T2
|
||||
w2 ^= t2;
|
||||
|
||||
// 12. W0 ← W0 ⊕ T0
|
||||
w0 ^= t0;
|
||||
|
||||
// 13. Return (W0, W1, W2)
|
||||
(w0, w1, w2)
|
||||
}
|
||||
|
||||
/// `bash-f` sponge permutation.
|
||||
///
|
||||
/// Implements the core sponge function defined in Section 6.2 of STB 34.101.77-2020.
|
||||
/// This is a cryptographic permutation that operates on 1536-bit states.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// - `state`: Mutable reference to 24 × 64-bit words (1536 bits total)
|
||||
pub fn bash_f(state: &mut [u64; STATE_WORDS]) {
|
||||
// 1. Split S into words (S0, S1, ..., S23)
|
||||
|
||||
// 2. C ← B194BAC80A08F53B (initialize round constant, swapped to little-endian)
|
||||
let mut c: u64 = 0x3BF5080AC8BA94B1;
|
||||
|
||||
// 3. For i = 1, 2, ..., 24 perform 24 rounds
|
||||
for _ in 0..STATE_WORDS {
|
||||
// 3.1. Apply S-box layer with varying rotation parameters
|
||||
// (m1, n1, m2, n2) ← (8, 53, 14, 1)
|
||||
let (mut m1, mut n1, mut m2, mut n2) = (8, 53, 14, 1);
|
||||
|
||||
// 3.2. For j = 0, 1, ..., 7 apply bash-s to each of 8 columns
|
||||
for j in 0..8 {
|
||||
// 3.2.a. (Sj, S8+j, S16+j) ← bash-s(Sj, S8+j, S16+j, m1, n1, m2, n2)
|
||||
let (s0, s1, s2) = bash_s(state[j], state[8 + j], state[16 + j], m1, n1, m2, n2);
|
||||
state[j] = s0;
|
||||
state[8 + j] = s1;
|
||||
state[16 + j] = s2;
|
||||
|
||||
// 3.2.b. (m1, n1, m2, n2) ← (7·m1 mod 64, 7·n1 mod 64, 7·m2 mod 64, 7·n2 mod 64)
|
||||
(m1, n1, m2, n2) = ((7 * m1) % 64, (7 * n1) % 64, (7 * m2) % 64, (7 * n2) % 64);
|
||||
}
|
||||
|
||||
// 3.3. Apply word permutation
|
||||
// S ← S15 ‖ S10 ‖ S9 ‖ S12 ‖ S11 ‖ S14 ‖ S13 ‖ S8 ‖
|
||||
// S17 ‖ S16 ‖ S19 ‖ S18 ‖ S21 ‖ S20 ‖ S23 ‖ S22 ‖
|
||||
// S6 ‖ S3 ‖ S0 ‖ S5 ‖ S2 ‖ S7 ‖ S4 ‖ S1
|
||||
const INDEXES: [usize; STATE_WORDS] = [
|
||||
15, 10, 9, 12, 11, 14, 13, 8, 17, 16, 19, 18, 21, 20, 23, 22, 6, 3, 0, 5, 2, 7, 4, 1,
|
||||
];
|
||||
*state = INDEXES.map(|i| state[i]);
|
||||
|
||||
// 3.4. S23 ← S23 ⊕ C (add round constant)
|
||||
state[23] ^= c;
|
||||
|
||||
// 3.5. Update LFSR (Galois configuration)
|
||||
// if ⌊C⌉ is even, then C ← ShLo(C)
|
||||
// else C ← ShLo(C) ⊕ AED8E07F99E12BDC
|
||||
if c & 1 == 0 {
|
||||
c >>= 1;
|
||||
} else {
|
||||
c = (c >> 1) ^ 0xDC2BE1997FE0D8AE;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Return S - state is modified in place
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Test vector from Table A.1 of STB 34.101.77-2020.
|
||||
#[test]
|
||||
fn test_bash_s_table_a1() {
|
||||
// Constants in the spec are given using LE order
|
||||
// For example, in spec when they write B194BAC80A08F53B, they do not mean 0xB194BAC80A08F53B, but 0x3BF5080AC8BA94B1.
|
||||
// https://github.com/RustCrypto/sponges/pull/92#issuecomment-3433315011
|
||||
let w0 = 0xB194BAC80A08F53Bu64.swap_bytes();
|
||||
let w1 = 0xE12BDC1AE28257ECu64.swap_bytes();
|
||||
let w2 = 0xE9DEE72C8F0C0FA6u64.swap_bytes();
|
||||
|
||||
let (w0_out, w1_out, w2_out) = bash_s(w0, w1, w2, 8, 53, 14, 1);
|
||||
|
||||
assert_eq!(w0_out, 0x479E76129979DC5Fu64.swap_bytes());
|
||||
assert_eq!(w1_out, 0x0F2B2C93ED128EDDu64.swap_bytes());
|
||||
assert_eq!(w2_out, 0x41009B1B112DFEF3u64.swap_bytes());
|
||||
}
|
||||
}
|
||||
70
bash-f/tests/bash.rs
Normal file
70
bash-f/tests/bash.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use bash_f::bash_f;
|
||||
|
||||
/// Test vector from Table A.2 of STB 34.101.77-2020.
|
||||
#[test]
|
||||
fn test_bash_f_table_a2() {
|
||||
let input: [u64; 24] = [
|
||||
0xB194BAC80A08F53B,
|
||||
0x366D008E584A5DE4,
|
||||
0x8504FA9D1BB6C7AC,
|
||||
0x252E72C202FDCE0D,
|
||||
0x5BE3D61217B96181,
|
||||
0xFE6786AD716B890B,
|
||||
0x5CB0C0FF33C356B8,
|
||||
0x35C405AED8E07F99,
|
||||
0xE12BDC1AE28257EC,
|
||||
0x703FCCF095EE8DF1,
|
||||
0xC1AB76389FE678CA,
|
||||
0xF7C6F860D5BB9C4F,
|
||||
0xF33C657B637C306A,
|
||||
0xDD4EA7799EB23D31,
|
||||
0x3E98B56E27D3BCCF,
|
||||
0x591E181F4C5AB793,
|
||||
0xE9DEE72C8F0C0FA6,
|
||||
0x2DDB49F46F739647,
|
||||
0x06075316ED247A37,
|
||||
0x39CBA38303A98BF6,
|
||||
0x92BD9B1CE5D14101,
|
||||
0x5445FBC95E4D0EF2,
|
||||
0x682080AA227D642F,
|
||||
0x2687F93490405511,
|
||||
];
|
||||
|
||||
let expected: [u64; 24] = [
|
||||
0x8FE727775EA7F140,
|
||||
0xB95BB6A200CBB28C,
|
||||
0x7F0809C0C0BC68B7,
|
||||
0xDC5AEDC841BD94E4,
|
||||
0x03630C301FC255DF,
|
||||
0x5B67DB53EF65E376,
|
||||
0xE8A4D797A6172F22,
|
||||
0x71BA48093173D329,
|
||||
0xC3502AC946767326,
|
||||
0xA2891971392D3F70,
|
||||
0x89959F5D61621238,
|
||||
0x655975E00E2132A0,
|
||||
0xD5018CEEDB17731C,
|
||||
0xCD88FC50151D37C0,
|
||||
0xD4A3359506AEDC2E,
|
||||
0x6109511E7703AFBB,
|
||||
0x014642348D8568AA,
|
||||
0x1A5D9868C4C7E6DF,
|
||||
0xA756B1690C7C2608,
|
||||
0xA2DC136F5997AB8F,
|
||||
0xBB3F4D9F033C87CA,
|
||||
0x6070E117F099C409,
|
||||
0x4972ACD9D976214B,
|
||||
0x7CED8E3F8B6E058E,
|
||||
];
|
||||
|
||||
// Constants in the spec are given using LE order
|
||||
// For example, in spec when they write B194BAC80A08F53B, they do not mean 0xB194BAC80A08F53B, but 0x3BF5080AC8BA94B1.
|
||||
// https://github.com/RustCrypto/sponges/pull/92#issuecomment-3433315011
|
||||
let mut state = input.map(|x| x.swap_bytes());
|
||||
|
||||
bash_f(&mut state);
|
||||
|
||||
let output = state.map(|x| x.swap_bytes());
|
||||
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user