|
| 1 | +pub const STATE_WORDS: usize = 32; |
| 2 | + |
| 3 | +pub const HASH_WORDS: usize = 8; |
| 4 | + |
| 5 | +pub const SCHEDULE_WORDS: usize = 64; |
| 6 | + |
| 7 | +pub const INITIAL_HASH: Sha256Hash = [ |
| 8 | + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, |
| 9 | +]; |
| 10 | + |
| 11 | +pub const K: [u32; 64] = [ |
| 12 | + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 13 | + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 14 | + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 15 | + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 16 | + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 17 | + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 18 | + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 19 | + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, |
| 20 | +]; |
| 21 | + |
| 22 | +pub type Sha256Hash = [u32; HASH_WORDS]; |
| 23 | + |
| 24 | +pub type MessageSchedule = [u32; SCHEDULE_WORDS]; |
| 25 | + |
| 26 | +pub type WorkingVariables = [u32; 8]; |
| 27 | + |
| 28 | +pub fn init_working_variables(hash: &Sha256Hash) -> WorkingVariables { |
| 29 | + [ |
| 30 | + hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], |
| 31 | + ] |
| 32 | +} |
| 33 | + |
| 34 | +pub fn prepare_schedule(block: &[u32; 16]) -> MessageSchedule { |
| 35 | + let mut schedule = [0u32; SCHEDULE_WORDS]; |
| 36 | + |
| 37 | + // The first 16 words of the message schedule are the block itself. |
| 38 | + for i in 0..16 { |
| 39 | + schedule[i] = block[i]; |
| 40 | + } |
| 41 | + // The remaining words are generated using the sigma functions. |
| 42 | + for i in 16..SCHEDULE_WORDS { |
| 43 | + schedule[i] = schedule[i - 16] |
| 44 | + .wrapping_add(sig0(schedule[i - 15])) |
| 45 | + .wrapping_add(schedule[i - 7]) |
| 46 | + .wrapping_add(sig1(schedule[i - 2])); |
| 47 | + } |
| 48 | + |
| 49 | + schedule |
| 50 | +} |
| 51 | + |
| 52 | +pub fn compress_schedule(schedule: &MessageSchedule, v: &mut WorkingVariables) { |
| 53 | + for i in 0..SCHEDULE_WORDS { |
| 54 | + let t1 = v[7] |
| 55 | + .wrapping_add(big_sig1(v[4])) |
| 56 | + .wrapping_add(schedule[i]) |
| 57 | + .wrapping_add(K[i]); |
| 58 | + |
| 59 | + let t2 = big_sig0(v[0]).wrapping_add(v[0] & v[1] ^ v[0] & v[2] ^ v[1] & v[2]); |
| 60 | + |
| 61 | + v[7] = v[6]; |
| 62 | + v[6] = v[5]; |
| 63 | + v[5] = v[4]; |
| 64 | + v[4] = v[3].wrapping_add(t1); |
| 65 | + v[3] = v[2]; |
| 66 | + v[2] = v[1]; |
| 67 | + v[1] = v[0]; |
| 68 | + v[0] = t1.wrapping_add(t2); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +pub fn update_hash(hash: &mut Sha256Hash, v: &WorkingVariables) { |
| 73 | + for (h, w) in hash.iter_mut().zip(v.iter()) { |
| 74 | + *h = h.wrapping_add(*w); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +pub fn sig0(x: u32) -> u32 { |
| 79 | + (x >> 7) ^ (x >> 18) ^ (x >> 3) |
| 80 | +} |
| 81 | + |
| 82 | +pub fn sig1(x: u32) -> u32 { |
| 83 | + (x >> 17) ^ (x >> 19) ^ (x >> 10) |
| 84 | +} |
| 85 | + |
| 86 | +pub fn big_sig0(x: u32) -> u32 { |
| 87 | + (x >> 2) ^ (x >> 13) ^ (x >> 22) |
| 88 | +} |
| 89 | + |
| 90 | +pub fn big_sig1(x: u32) -> u32 { |
| 91 | + (x >> 6) ^ (x >> 11) ^ (x >> 25) |
| 92 | +} |
0 commit comments