MCPcopy Create free account
hub / github.com/BitVM/BitVM / sha256

Function sha256

bitvm/src/hash/sha256_u4.rs:260–467  ·  view source on GitHub ↗
(num_bytes: u32)

Source from the content-addressed store, hash-verified

258}
259
260pub fn sha256(num_bytes: u32) -> Script {
261 // up to 55 is one block and always supports add table
262 // probably up to 68 bytes I can afford to load the add tables for the first chunk (but have I would have to unload it)
263
264 let (mut padding_scripts, chunks) = double_padding(num_bytes);
265 let mut bytes_per_chunk: Vec<u32> = Vec::new();
266 let mut bytes_remaining = num_bytes;
267 while bytes_remaining > 0 {
268 if bytes_remaining > 64 {
269 bytes_per_chunk.push(64);
270 bytes_remaining -= 64;
271 } else {
272 bytes_per_chunk.push(bytes_remaining);
273 bytes_remaining = 0;
274 }
275 }
276 if bytes_per_chunk.len() < chunks as usize {
277 bytes_per_chunk.push(0);
278 }
279 println!("{:?}", bytes_per_chunk);
280 println!("{:?}", padding_scripts);
281
282 let add_size = 128;
283 let sched_size = 128;
284 let rrot_size = 16 * 5;
285 let half_logic_size = 136 + 16;
286 let mut tables_size = rrot_size + half_logic_size;
287 let use_add_table = chunks == 1;
288 if use_add_table {
289 tables_size += add_size;
290 }
291
292 let sched_loop_offset_and = sched_size;
293 let sched_loop_offset_rrot = sched_loop_offset_and + half_logic_size;
294 let sched_loop_offset_add = sched_loop_offset_rrot + rrot_size;
295
296 let full_sched_size = 512;
297 let temp_vars_size = 8 * 8;
298
299 let vars_top = temp_vars_size + full_sched_size;
300 let main_loop_offset_and = vars_top;
301 let main_loop_offset_rrot = main_loop_offset_and + half_logic_size;
302 let main_loop_offset_add = main_loop_offset_rrot + rrot_size;
303
304 script! {
305
306 if use_add_table {
307 { u4_push_add_tables() }
308 }
309 { u4_push_rrot_tables() } // rshiftn 16*6= 96
310 { u4_push_half_xor_table() } // 136
311 { u4_push_half_lookup() } // 16
312 // total : 136 + 16 + 96 = 248
313
314 for c in 0..chunks {
315
316 if c > 0 {
317 //change and with xor

Callers 1

test_sizesFunction · 0.70

Calls 3

lenMethod · 0.80
double_paddingFunction · 0.70
pushMethod · 0.45

Tested by 1

test_sizesFunction · 0.56