Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SHA256 in Miden Assembly #123

Merged
merged 3 commits into from
Mar 17, 2022

Conversation

itzmeanjan
Copy link
Contributor

@itzmeanjan itzmeanjan commented Feb 22, 2022

  • SHA256 functions; see here
  • SHA256 message schedule preparation; see here
  • SHA256 digest compute; see here

@itzmeanjan itzmeanjan changed the base branch from main to next February 22, 2022 15:58
@itzmeanjan
Copy link
Contributor Author

Current cost of executing SHA256 related functions on Miden VM

VM cycles Procedure
29 cap_sigma_0
29 cap_sigma_1
13 maj
28 small_sigma_0
28 small_sigma_1
17 ch
3983 prepare_message_schedule

@itzmeanjan
Copy link
Contributor Author

VM Cycles required to compute SHA256 2-to-1 hash: 29861

@itzmeanjan itzmeanjan marked this pull request as ready for review March 5, 2022 08:14
@itzmeanjan
Copy link
Contributor Author

See itzmeanjan#1, where I improve SHA256 implementation

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks great! I left a small comment and also highlighted a few minor optimization opportunities.

I think we can optimize this a bit more, but this can be done in the future PRs.

Comment on lines +336 to +337
popw.mem # write to mem msg[24, 25, 26, 27] #
pushw.local.4
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be replaced by something like:

storew.mem
loadw.local.4

This change should save 8 cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted, will revisit sha256 soon.

Comment on lines +372 to +373
popw.mem # write to mem msg[32, 33, 34, 35] #
pushw.local.4
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be replaced by something like:

storew.mem
loadw.local.4

This change should save 8 cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted

Comment on lines +409 to +410
popw.mem # write to mem msg[40, 41, 42, 43] #
pushw.local.4
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be replaced by something like:

storew.mem
loadw.local.4

This change should save 8 cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted

Comment on lines 46 to 48
let inputs = ProgramInputs::new(&i_words, &[], Vec::new()).unwrap();
let trace = execute(&script, &inputs).unwrap();
let last_state = trace.last_stack_state();
Copy link
Contributor

Choose a reason for hiding this comment

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

You might be able to use the new build_test macro from here to simplify this a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, changed here d46ae2e

Comment on lines +391 to +392
popw.mem # write to mem msg[36, 37, 38, 39] #
pushw.local.4
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be replaced by something like:

storew.mem
loadw.local.4

This change should save 8 cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted

Comment on lines +426 to +427
popw.mem # write to mem msg[44, 45, 46, 47] #
pushw.local.4
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be replaced by something like:

storew.mem
loadw.local.4

This change should save 8 cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted

itzmeanjan added a commit to itzmeanjan/miden that referenced this pull request Mar 15, 2022
@itzmeanjan
Copy link
Contributor Author

Thanks for reviewing it @bobbinth !

I'm keeping a note of optimization suggestions you've shared, in next few weeks I'll revisit both blake3 & sha256. Also I've moved both blake3 & sha256 tests to use new build_test macro.

@bobbinth bobbinth merged commit bc3b3dd into 0xPolygonMiden:next Mar 17, 2022
@itzmeanjan itzmeanjan deleted the itzmeanjan/sha256 branch March 21, 2022 15:19
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