Skip to content

Commit d4ada3e

Browse files
committed
chore(docs): fix docs for linting & update for ~accuracy
1 parent 4d56fc6 commit d4ada3e

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

sha2raw/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! An implementation of the [SHA-2][1] cryptographic hash algorithms.
1+
//! An implementation of the [SHA-2](https://en.wikipedia.org/wiki/SHA-2) cryptographic hash algorithms.
22
33
// Give relevant error messages if the user tries to enable AArch64 asm on unsupported platforms.
44

sha2raw/src/sha256_utils.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -204,75 +204,81 @@ fn sha256_digest_block_u32(state: &mut [u32; 8], block: &[u32; 16]) {
204204
/// digest calculation. The schedule-related instructions allow 4 rounds to be
205205
/// calculated as:
206206
///
207-
/// ```compile_fail
208-
/// use std::simd::u32x4;
209-
/// use self::crypto::sha2::{
210-
/// sha256msg1,
211-
/// sha256msg2,
212-
/// sha256load
213-
/// };
207+
/// ```ignore
208+
/// // Pseudocode showing how Intel SHA extensions would work with SIMD vectors
209+
/// // (not actual sha2raw API - for illustration only)
214210
///
215-
/// fn schedule4_data(work: &mut [u32x4], w: &[u32]) {
211+
/// type u32x4 = [u32; 4]; // Represents a SIMD vector
216212
///
217-
/// // this is to illustrate the data order
218-
/// work[0] = u32x4(w[3], w[2], w[1], w[0]);
219-
/// work[1] = u32x4(w[7], w[6], w[5], w[4]);
220-
/// work[2] = u32x4(w[11], w[10], w[9], w[8]);
221-
/// work[3] = u32x4(w[15], w[14], w[13], w[12]);
213+
/// fn schedule4_data(work: &mut [u32x4], w: &[u32]) {
214+
/// // Intel SHA extensions operate on 4 words at a time
215+
/// work[0] = [w[3], w[2], w[1], w[0]];
216+
/// work[1] = [w[7], w[6], w[5], w[4]];
217+
/// work[2] = [w[11], w[10], w[9], w[8]];
218+
/// work[3] = [w[15], w[14], w[13], w[12]];
222219
/// }
223220
///
224221
/// fn schedule4_work(work: &mut [u32x4], t: usize) {
225-
///
226-
/// // this is the core expression
227-
/// work[t] = sha256msg2(sha256msg1(work[t - 4], work[t - 3]) +
228-
/// sha256load(work[t - 2], work[t - 1]),
229-
/// work[t - 1])
222+
/// // The SHA256MSG1/2 instructions compute 4 message schedule words
223+
/// // This is pseudocode for what the CPU instructions do internally
224+
/// work[t] = sha256msg2(
225+
/// sha256msg1(work[t - 4], work[t - 3]) + work[t - 2],
226+
/// work[t - 1]
227+
/// )
230228
/// }
231229
/// ```
232230
///
233231
/// instead of 4 rounds of:
234232
///
235-
/// ```compile_fail
233+
/// ```ignore
234+
/// // Traditional SHA-256 message schedule (one word at a time)
236235
/// fn schedule_work(w: &mut [u32], t: usize) {
237-
/// w[t] = sigma1!(w[t - 2]) + w[t - 7] + sigma0!(w[t - 15]) + w[t - 16];
236+
/// // sigma0 and sigma1 are the message schedule functions
237+
/// w[t] = sigma1(w[t - 2]) + w[t - 7] + sigma0(w[t - 15]) + w[t - 16];
238238
/// }
239239
/// ```
240240
///
241241
/// and the digest-related instructions allow 4 rounds to be calculated as:
242242
///
243-
/// ```compile_fail
244-
/// use std::simd::u32x4;
245-
/// use self::crypto::sha2::{K32X4,
246-
/// sha256rnds2,
247-
/// sha256swap
248-
/// };
243+
/// ```ignore
244+
/// // Pseudocode showing how Intel SHA extensions process 4 rounds at once
245+
/// // (not actual sha2raw API - for illustration only)
246+
///
247+
/// type u32x4 = [u32; 4]; // Represents a SIMD vector
248+
/// const K32X4: [[u32; 4]; 16] = /* round constants */;
249249
///
250250
/// fn rounds4(state: &mut [u32; 8], work: &mut [u32x4], t: usize) {
251-
/// let [a, b, c, d, e, f, g, h]: [u32; 8] = *state;
251+
/// let [a, b, c, d, e, f, g, h] = *state;
252252
///
253-
/// // this is to illustrate the data order
254-
/// let mut abef = u32x4(a, b, e, f);
255-
/// let mut cdgh = u32x4(c, d, g, h);
256-
/// let temp = K32X4[t] + work[t];
253+
/// // Intel SHA extensions pack state into two vectors
254+
/// let mut abef = [a, b, e, f];
255+
/// let mut cdgh = [c, d, g, h];
256+
/// let temp = add_vectors(K32X4[t], work[t]);
257257
///
258-
/// // this is the core expression
258+
/// // SHA256RNDS2 performs 2 rounds of SHA-256
259259
/// cdgh = sha256rnds2(cdgh, abef, temp);
260260
/// abef = sha256rnds2(abef, cdgh, sha256swap(temp));
261261
///
262-
/// *state = [abef.0, abef.1, cdgh.0, cdgh.1,
263-
/// abef.2, abef.3, cdgh.2, cdgh.3];
262+
/// // Unpack back to state array
263+
/// *state = [abef[0], abef[1], cdgh[0], cdgh[1],
264+
/// abef[2], abef[3], cdgh[2], cdgh[3]];
264265
/// }
265266
/// ```
266267
///
267268
/// instead of 4 rounds of:
268269
///
269-
/// ```compile_fail
270-
/// fn round(state: &mut [u32; 8], w: &mut [u32], t: usize) {
271-
/// let [a, b, c, mut d, e, f, g, mut h]: [u32; 8] = *state;
270+
/// ```ignore
271+
/// // Traditional SHA-256 round function (one round at a time)
272+
/// // This is closer to what sha2raw actually implements
273+
/// fn round(state: &mut [u32; 8], w: &[u32], t: usize) {
274+
/// let [a, b, c, mut d, e, f, g, mut h] = *state;
272275
///
273-
/// h += big_sigma1!(e) + choose!(e, f, g) + K32[t] + w[t]; d += h;
274-
/// h += big_sigma0!(a) + majority!(a, b, c);
276+
/// // SHA-256 round operations using the functions defined in this module
277+
/// h += big_sigma1(e) + choose(e, f, g) + K32[t] + w[t];
278+
/// d += h;
279+
/// h += big_sigma0(a) + majority(a, b, c);
275280
///
281+
/// // Rotate the state
276282
/// *state = [h, a, b, c, d, e, f, g];
277283
/// }
278284
/// ```

0 commit comments

Comments
 (0)