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

Add SHA256 Tutorial #387

Merged
merged 1 commit into from
May 15, 2023
Merged

Add SHA256 Tutorial #387

merged 1 commit into from
May 15, 2023

Conversation

RasoulAM
Copy link

A tutorial on implementing SHA256 as part of the bounty program has been added, utilizing concrete operations. The accompanying notebook details all necessary steps and implements required operations such as 32-bit additions, rotations, and shifts. Some assumptions are:

  • Data can be of variable length
  • The client splits data into small pieces before encryption
  • Bitlength of the pieces can be adjusted to get the best performance
  • Added support for 32-bit addition, rotation, and shifts
  • Test cases available in the code to show the correctness
  • Compilation and runtime generally take a long time, but smaller examples with fewer iterations are included in the notebook

@cla-bot
Copy link

cla-bot bot commented Apr 25, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @m-meidani on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@rudy-6-4 rudy-6-4 self-requested a review April 25, 2023 17:39
@cla-bot
Copy link

cla-bot bot commented Apr 25, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @m-meidani on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
docs/tutorial/sha256_tutorial.ipynb Outdated Show resolved Hide resolved
@cla-bot
Copy link

cla-bot bot commented Apr 28, 2023

Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: Rasoul Akhavan Mahdavi.
This is most likely caused by a git client misconfiguration; please make sure to:

  1. check if your git client is configured with an email to sign commits git config --list | grep email
  2. If not, set it up using git config --global user.email email@example.com
  3. Make sure that the git commit email is configured in your GitHub account settings, see https://github.com/settings/emails

@cla-bot
Copy link

cla-bot bot commented Apr 28, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @m-meidani, @RasoulAM on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

1 similar comment
@cla-bot
Copy link

cla-bot bot commented Apr 28, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @m-meidani, @RasoulAM on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@aquint-zama
Copy link
Contributor

@cla-bot check

@cla-bot cla-bot bot added the cla-signed label May 5, 2023
@cla-bot
Copy link

cla-bot bot commented May 5, 2023

The cla-bot has been summoned, and re-checked this pull request!

Copy link
Contributor

@umut-sahin umut-sahin left a comment

Choose a reason for hiding this comment

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

Could you use chunks terminology instead of slices?

@RasoulAM
Copy link
Author

RasoulAM commented May 9, 2023

Could you use chunks terminology instead of slices?

Done. 88a0847

@umut-sahin
Copy link
Contributor

umut-sahin commented May 9, 2023

Can you replace explicit tables:

extract_one_bit_carry = fhe.LookupTable([x // (2 ** WIDTH) for x in range(2**(WIDTH+1))])
extract_chunk_from_two_operand_sum = fhe.LookupTable([x % (2 ** WIDTH) for x in range(2**(WIDTH+1))])

extract_two_bit_carry = fhe.LookupTable([x // (2 ** WIDTH) for x in range(2**(WIDTH+2))])
extract_chunk_from_four_operand_sum = fhe.LookupTable([x % (2 ** WIDTH) for x in range(2**(WIDTH+2))])

with regular operations

    carries = extract_two_bit_carry[added]
    # to
    carries = added // (2**WIDTH)
    # or even better
    carries = added >> WIDTH  # extract the carry bit

@RasoulAM
Copy link
Author

RasoulAM commented May 9, 2023

Are those operations implemented? When we were implementing they were not.

@umut-sahin
Copy link
Contributor

Constant ones are implemented a long time ago, only all operands encrypted version was not :)

@RasoulAM
Copy link
Author

RasoulAM commented May 9, 2023

Done. b8d19a0

@umut-sahin
Copy link
Contributor

Still two remaining:

extract_one_bit_carry = fhe.LookupTable([x // (2 ** WIDTH) for x in range(2**(WIDTH+1))])
extract_chunk_from_two_operand_sum = fhe.LookupTable([x % (2 ** WIDTH) for x in range(2**(WIDTH+1))])

@RasoulAM
Copy link
Author

RasoulAM commented May 10, 2023

Thanks for pointing those out. Does that mean these can be replaced as well?

# Extracts the [shift] lower bits of a [WIDTH]-bit number and places them at the top
table_extract_low_bits_and_raise = {
    shift : fhe.LookupTable([2**(WIDTH-shift) * (x %2**shift) for x in range(2**WIDTH)])
    for shift in range(WIDTH)
}

# Extracts the [WIDTH-shift] higher bits of a [WIDTH]-bit number
table_extract_high_bits = {
    shift : fhe.LookupTable([x//2**shift  for x in range(2**WIDTH)])
    for shift in range(WIDTH)
}

@umut-sahin
Copy link
Contributor

Yes, they can be replaced, I'd replace the first one with fhe.univariate and second one with a direct division.

@umut-sahin
Copy link
Contributor

Lastly, could you:

  • remove get_sha256
  • change def sha256(data) to def sha256(data, number_of_rounds=64)
  • compile using compiler = fhe.Compiler(lambda data: sha256(data, number_of_rounds), {"data": "encrypted"}) instead of compiler = fhe.Compiler(get_sha256(self.number_of_rounds), {"data": "encrypted"})

Then it's good to go for me :)

@RasoulAM
Copy link
Author

We decided not to give number_of_rounds as an argument to the function because it complicates many other parts of the code. For example, the inputset must be adjusted to include the number of rounds as well, mainly when you are less than 64 rounds.

@umut-sahin
Copy link
Contributor

umut-sahin commented May 11, 2023

We decided not to give number_of_rounds as an argument to the function because it complicates many other parts of the code. For example, the inputset must be adjusted to include the number of rounds as well, mainly when you are less than 64 rounds.

This is not the case with the changes I've suggested. You can wrap it in a lambda with a single argument :)

@RasoulAM
Copy link
Author

Done. Thanks for the suggestion :D
d4a357b

@umut-sahin
Copy link
Contributor

Lovely, could you squash your commits into a single one with the message:

docs(frontend-python): add sha256 tutorial

@m-meidani
Copy link
Contributor

Done. Does that looks good?

@umut-sahin umut-sahin merged commit a5c679f into zama-ai:main May 15, 2023
@aquint-zama
Copy link
Contributor

close zama-ai/bounty-program#39

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

Successfully merging this pull request may close these issues.

5 participants