Skip to content

Alpha to Stable#2

Merged
barrier15300 merged 4 commits into
mainfrom
alpha-develop
Dec 17, 2025
Merged

Alpha to Stable#2
barrier15300 merged 4 commits into
mainfrom
alpha-develop

Conversation

@barrier15300
Copy link
Copy Markdown
Owner

@barrier15300 barrier15300 commented Dec 17, 2025

#1
初めてのPull requestに大変感謝

変更箇所:

  • 定数計算可能な部分でのreinterpret_castをstd::bit_castに変更
  • キャスト漏れの修正
  • グローバル関数とマクロの削減

Summary by CodeRabbit

リリースノート

  • Chores

    • 暗号化ライブラリの内部実装を最新の標準ライブラリに対応させました。カスタム実装から標準ライブラリ関数への移行により、コードの安全性と保守性が向上しました。
  • Bug Fixes

    • ポインタキャストの安全性を改善し、未定義動作の可能性を排除しました。

✏️ Tip: You can customize this high-level summary in your review settings.

@barrier15300 barrier15300 self-assigned this Dec 17, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 17, 2025

Walkthrough

3つの暗号化関連ヘッダーファイルの現代化リファクタリング。カスタム実装(_bit_widthマクロ、RORマクロ)を標準ライブラリ関数(std::bit_widthstd::rotr)に置き換え、constexpr修飾子を追加、安全でないキャストをstd::bit_castに統一。

Changes

Cohort / File(s) Change Summary
AES128最適化
include/Cryptgraphy/AES128.h
カスタム_bit_widthstd::bit_widthに置き換え、RORマクロをstd::rotrに統一。複数の内部関数にconstexpr修飾子を追加(block_tコンストラクタ、Reverse()メソッド)。静的修飾子の順序をstatic constexprに統一。
型安全キャスト統一
include/Cryptgraphy/MultiWordInt.h
reinterpret_caststd::bit_castに置き換え(テンプレートコンストラクタ、bits()アクセッサ)。型安全性を向上。
SHAKE256コンストラクタ拡張
include/Cryptgraphy/SHAKE256.h
byte_viewパラメータを受け取る新規パブリックコンストラクタを追加。既存のbytearrayコンストラクタを委譲パターンに変更。内部キャストをstd::bit_caststatic_castに統一。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • 注意が必要な箇所:
    • AES128::block_tコンストラクタのconstexpr化におけるstd::bit_castの使用方法(メモリレイアウトの安全性確認)
    • MultiWordInt.hにおけるstd::bit_castの適用範囲(ポインタ参照の型安全性)
    • SHAKE256::stateコンストラクタの委譲チェーン(初期化順序と機能等価性)

Possibly related PRs

  • Avoid C-style casts #1: 両PRがinclude/Cryptgraphy/AES128.hを修正し、RORマクロ関連と回転操作を変更。カスタムマクロから標準ライブラリへの移行パターンが共通。

Poem

🐰 標準ライブラリの光に照らされて、
カスタムマクロの古い道を離れ、
std::bit_castで安全に型を変え、
constexprの魔法で暗号化が舞う。
現代の C++ へと歩を進める。

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 3.57% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title "Alpha to Stable" is vague and does not clearly describe the specific technical changes made in the pull request (std::bit_cast replacements, constexpr improvements, etc.). Consider using a more descriptive title that reflects the main technical changes, such as "Replace reinterpret_cast with std::bit_cast and improve constexpr support" or similar.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch alpha-develop

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
include/Cryptgraphy/SHAKE256.h (1)

91-98: std::bit_castはポインタのキャストには使用できません

operator bytearray()std::bit_castがポインタ型のキャストに使用されていますが、これは不適切です。

 		constexpr operator bytearray() const {
 			bytearray ret;
 			ret.reserve(sizeof(m_words));
 			for (size_t i = 0, c = sizeof(m_words); i < c; ++i) {
-				ret.push_back(*(std::bit_cast<const byte_t*>(m_words.data()) + i));
+				ret.push_back(*(reinterpret_cast<const byte_t*>(m_words.data()) + i));
 			}
 			return ret;
 		}
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c1713bf and b802bc9.

📒 Files selected for processing (3)
  • include/Cryptgraphy/AES128.h (6 hunks)
  • include/Cryptgraphy/MultiWordInt.h (2 hunks)
  • include/Cryptgraphy/SHAKE256.h (6 hunks)
🔇 Additional comments (2)
include/Cryptgraphy/AES128.h (2)

252-256: 良好なコード品質の改善

  • Reverse()メソッドへのconstexprの追加(252行目)により、コンパイル時評価が可能になります。
  • 修飾子の順序をstatic constexprに統一したこと(674-808行目)は、C++の慣例に従っており、コードの一貫性を向上させます。

Also applies to: 674-808


18-18: このプロジェクトはCMakeLists.txt(37行目)で既にC++20標準に設定されています。std::bit_widthstd::rotrの使用は完全にサポートされており、追加の検証は必要ありません。

Likely an incorrect or invalid review comment.

Comment on lines +237 to +246
constexpr block_t(byte_view from) noexcept {
auto it = std::bit_cast<byte_t*>(m_bytes.data());
auto end = std::bit_cast<byte_t*>(m_bytes.data() + block_size);
for (auto&& c : from) {
*it = c;
if (++it == end) {
break;
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

std::bit_castはポインタのキャストには使用できません

block_tのコンストラクタでstd::bit_castがポインタ型のキャストに使用されていますが、これは誤った使用法です。std::bit_castは値型間の変換専用であり、ポインタには使用できません。

 		constexpr block_t(byte_view from) noexcept {
-			auto it = std::bit_cast<byte_t*>(m_bytes.data());
-			auto end = std::bit_cast<byte_t*>(m_bytes.data() + block_size);
+			auto it = reinterpret_cast<byte_t*>(m_bytes.data());
+			auto end = reinterpret_cast<byte_t*>(m_bytes.data() + block_size);
 			for (auto&& c : from) {
 				*it = c;
 				if (++it == end) {
 					break;
 				}
 			}
 		}

注: このコンストラクタがconstexprである必要がある場合、ポインタキャストの使用はconstexpr評価を妨げる可能性があります。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constexpr block_t(byte_view from) noexcept {
auto it = std::bit_cast<byte_t*>(m_bytes.data());
auto end = std::bit_cast<byte_t*>(m_bytes.data() + block_size);
for (auto&& c : from) {
*it = c;
if (++it == end) {
break;
}
}
}
constexpr block_t(byte_view from) noexcept {
auto it = reinterpret_cast<byte_t*>(m_bytes.data());
auto end = reinterpret_cast<byte_t*>(m_bytes.data() + block_size);
for (auto&& c : from) {
*it = c;
if (++it == end) {
break;
}
}
}
🤖 Prompt for AI Agents
In include/Cryptgraphy/AES128.h around lines 237 to 246, the constructor uses
std::bit_cast to cast to pointer types which is invalid; replace the pointer
bit_cast with a safe byte-wise copy instead (e.g., use std::copy(from.begin(),
from.end(), m_bytes.begin()) or std::memcpy(m_bytes.data(), from.data(),
std::min(from.size(), block_size))) and remove pointer arithmetic; if the
constructor must remain constexpr implement a constexpr loop that copies
elements into m_bytes up to block_size without pointer casts; keep noexcept and
ensure you only copy up to block_size.

const count_t copycount = (arr.size() * sizeof(T) < totalbytes) ? arr.size() : totalbytes / sizeof(T);
std::fill(words().begin(), words().end(), 0);
auto it = reinterpret_cast<T*>(words().data()); // TODO: resolve potential undefined behavior
auto it = std::bit_cast<T*>(words().data()); // TODO: resolve potential undefined behavior
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

std::bit_castはポインタのキャストには使用できません

std::bit_castは値型のビット単位の変換用であり、ポインタ型には使用できません。ここではポインタ型(word_t*からT*へ)のキャストが必要なため、reinterpret_castを使用するか、constexpr評価が必要な場合は別のアプローチを検討する必要があります。

-		auto it = std::bit_cast<T*>(words().data()); // TODO: resolve potential undefined behavior
+		auto it = reinterpret_cast<T*>(words().data());

注: constexprコンテキストでのポインタキャストは未定義動作の可能性があります。C++20以降ではstd::start_lifetime_as(C++23)やその他の安全な手法を検討してください。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
auto it = std::bit_cast<T*>(words().data()); // TODO: resolve potential undefined behavior
auto it = reinterpret_cast<T*>(words().data());
🤖 Prompt for AI Agents
include/Cryptgraphy/MultiWordInt.h around line 71: the code incorrectly uses
std::bit_cast to cast a pointer (words().data()) to T*, which is invalid and can
cause UB; replace this with a safe approach — if you truly need a pointer
reinterpretation use reinterpret_cast<T*>(words().data()) and ensure alignment
and object lifetime are valid (or use std::launder when reinterpreting storage),
but the safer option is to avoid pointer bit-casts entirely: either memcpy the
data into a properly constructed T object (or use std::bit_cast on value
copies), or, if targeting C++23 and you need constexpr lifetime-starting
semantics, use std::start_lifetime_as; pick the approach appropriate for your
lifetime/alignment requirements and replace the std::bit_cast usage accordingly.

Comment on lines 636 to 643
constexpr bits_t& bits() {
return *reinterpret_cast<bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
return *std::bit_cast<bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
// NOTE: temporary fix
}
constexpr const bits_t& bits() const {
return *reinterpret_cast<const bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
return *std::bit_cast<const bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
// NOTE: temporary fix
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

std::bit_castはポインタのキャストには使用できません

bits()アクセサでstd::bit_castがポインタ型のキャストに使用されていますが、これは不適切です。std::bit_castは値型間の変換専用であり、ポインタには使用できません。

 	constexpr bits_t& bits() {
-		return *std::bit_cast<bits_t*>(m_words->data());  // TODO: resolve potential undefined behavior
-		// NOTE: temporary fix
+		return *reinterpret_cast<bits_t*>(m_words->data());
 	}
 	constexpr const bits_t& bits() const {
-		return *std::bit_cast<const bits_t*>(m_words->data());  // TODO: resolve potential undefined behavior
-		// NOTE: temporary fix
+		return *reinterpret_cast<const bits_t*>(m_words->data());
 	}

注: word_t配列をstd::bitsetとして再解釈することは、厳密な型エイリアスルールに違反する可能性があります。より安全な実装方法(例:std::bit_castで配列全体を値としてコピーするか、ビット操作を明示的に実装する)を検討することをお勧めします。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constexpr bits_t& bits() {
return *reinterpret_cast<bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
return *std::bit_cast<bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
// NOTE: temporary fix
}
constexpr const bits_t& bits() const {
return *reinterpret_cast<const bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
return *std::bit_cast<const bits_t*>(m_words->data()); // TODO: resolve potential undefined behavior
// NOTE: temporary fix
}
constexpr bits_t& bits() {
return *reinterpret_cast<bits_t*>(m_words->data());
}
constexpr const bits_t& bits() const {
return *reinterpret_cast<const bits_t*>(m_words->data());
}
🤖 Prompt for AI Agents
In include/Cryptgraphy/MultiWordInt.h around lines 636 to 643, the accessor uses
std::bit_cast on a pointer which is invalid and may violate strict-aliasing;
replace the pointer bit_cast with a safe value copy: memcpy or std::bit_cast of
the array value into a local bits_t and return a reference or (better) return by
value; implement two overloads that construct a bits_t from the underlying
word_t array (for non-const and const) by copying bytes into a bits_t local
(using std::memcpy or std::bit_cast on the array value) and return that value
(or store it in a member if a reference is required), or alternatively implement
explicit bit-manipulation helpers that produce bits_t without aliasing. Ensure
no pointer reinterpret_cast/bit_cast on pointers is used and preserve
const-correctness.

Comment on lines +21 to 30
constexpr state(byte_view from) {
auto it = std::bit_cast<byte_t*>(m_words.data());
auto end = std::bit_cast<byte_t*>(m_words.data() + b);
for (auto&& c : from) {
*it = c;
if (++it == end) {
break;
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

std::bit_castはポインタのキャストには使用できません

コンストラクタでstd::bit_castがポインタ型のキャストに使用されていますが、これは誤った使用法です。std::bit_castは値型の変換専用であり、ポインタには使用できません。

 		constexpr state(byte_view from) {
-			auto it = std::bit_cast<byte_t*>(m_words.data());
-			auto end = std::bit_cast<byte_t*>(m_words.data() + b);
+			auto it = reinterpret_cast<byte_t*>(m_words.data());
+			auto end = reinterpret_cast<byte_t*>(m_words.data() + b);
 			for (auto&& c : from) {
 				*it = c;
 				if (++it == end) {
 					break;
 				}
 			}
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constexpr state(byte_view from) {
auto it = std::bit_cast<byte_t*>(m_words.data());
auto end = std::bit_cast<byte_t*>(m_words.data() + b);
for (auto&& c : from) {
*it = c;
if (++it == end) {
break;
}
}
}
constexpr state(byte_view from) {
auto it = reinterpret_cast<byte_t*>(m_words.data());
auto end = reinterpret_cast<byte_t*>(m_words.data() + b);
for (auto&& c : from) {
*it = c;
if (++it == end) {
break;
}
}
}
🤖 Prompt for AI Agents
In include/Cryptgraphy/SHAKE256.h around lines 21 to 30, the constructor
incorrectly uses std::bit_cast for pointer casts (bit_cast is only for value
types); replace those std::bit_cast<byte_t*> casts with a proper pointer
conversion such as reinterpret_cast<byte_t*>(m_words.data()) for the start and
reinterpret_cast<byte_t*>(m_words.data() + b) (or compute the byte end as
reinterpret_cast<byte_t*>(m_words.data()) + byteCount) to avoid UB, or
alternatively use std::memcpy into a temporary byte pointer/array; ensure the
end pointer correctly represents the byte limit and update the loop to use these
corrected pointers.

@barrier15300
Copy link
Copy Markdown
Owner Author

barrier15300 commented Dec 17, 2025

TODO:
開発ブランチからのpull requestをする際のタイトルをもう少しわかりやすくする

@barrier15300 barrier15300 merged commit 653dc98 into main Dec 17, 2025
4 checks passed
@yaito3014
Copy link
Copy Markdown
Contributor

yaito3014 commented Dec 17, 2025

CodeRabbit の指摘は正しくて、ポインタの bit_castに意味はありません…

追記:
正確には、ポインタの bit_cast は定数式になりません
https://eel.is/c++draft/bit.cast#3.2

@barrier15300
Copy link
Copy Markdown
Owner Author

barrier15300 commented Dec 18, 2025

なんと...そもそも定数式にならないんですね...
勉強になります

想定していたのは、memcpy(に似た動作)が定数計算可能であることだったので
少々強引ではありますが Cryptgraphy/common.h にバイト列と相互変換可能な関数を用意して、それを実行する形に対応させました
// 例として先程コミットしたAES128のblock_tコンストラクタの部分

m_bytes = constexpr_bytes_cast<decltype(m_bytes)>(from);

これでおそらくすべてのコンストラクタを定数計算に対応できたと思います

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants