Konstantce
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
2
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Thoughts on Plookup implementation of Sha256 and Keccak One of neat tricks used in [Barretenberg](https://github.com/AztecProtocol/barretenberg/blob/f8d91ed11f9bd6fa07b7c789498f0b93277223f2/barretenberg/src/aztec/stdlib/hash/sha256/sha256_plookup.cpp) implementation of Sha256 table-based constraint system is the special algorithm for handling majority and choose functions: \begin{eqnarray} maj(x, y, z) &=& (x∧y)⊕(x∧z)⊕(y∧z) \\ ch(x, y, z) &=& (x∧y)⊕(¬x∧z). \end{eqnarray} Instead of introducing many separate tables for each type of primitive boolean function (^, ⊕, ¬) and then applying them repeatedly in the right order, we may take the more efficient approach. Consider the following tables: | x | y | z | maj(x, y, z) | x + y + z | |---|---|---|:------------:|:-----------:| | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 1 | 0 | 1 | | 0 | 1 | 0 | 0 | 1 | | 0 | 1 | 1 | 1 | 2 | | 1 | 0 | 0 | 0 | 1 | | 1 | 0 | 1 | 1 | 2 | | 1 | 1 | 0 | 1 | 2 | | 1 | 1 | 1 | 0 | 3 | | x | y | z | choose(x, y, z) | x + 2y + 3z | |---|---|---|:---------------:|:-----------:| | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | 1 | 1 | 3 | | 0 | 1 | 0 | 0 | 2 | | 0 | 1 | 1 | 1 | 5 | | 1 | 0 | 0 | 0 | 1 | | 1 | 0 | 1 | 0 | 4 | | 1 | 1 | 0 | 1 | 3 | | 1 | 1 | 1 | 1 | 6 | In these tables, third column is our logical function *log(x,y z)*, and the last column is the corresponding algebraic encoding *alg(x,y,z)*, chosen so that the transformation $f$ from *alg* $\to$ *log* is well-defined. Note that in the last table, the value 3 appears twice on two different rows but the corresponding value in the third column on those rows is the same (0). This observations suggest introducing the following algorithm for handling complex logical operatiosn (such as **maj** and **ch**: 1) for all arguments c in (x, y, z) do: *convertion* $c = \sum c_i 2^i$ (binary representation) to $c_{sparse} = \sum c_i b^i$ (sparse representation), where base $b$ is chosen in a way that prevents overflowing to the nearby chunk in algebraic encoding (b = 4 for **maj** and b = 7 for **ch**). In practice this can be achieved via spliting $c$ into chunks of appropriate size, mapping each chunk to sparse representation separately (via table query), and taking linear combination of transformed chunks. 2) instead of doing logical operations on (x, y, z) apply *algebraic operations* on their sparse encodings ($x_{sparse}, y_{sparse}, z_{sparse}$). For **maj** and **ch** these algebraic operations are simply linear combinations and fit into a single PLONK gate! 3) *normalization*: convert the result back from sparse form into binary, where each chunk $[0; b-1]$ is mapped to $\{0, 1\}$ via one-to-one function $f$ discussed above. This trick leads to more compact constrain systems in two ways: 1) it reduces the total number of tables used - instead of separate tables for each basic logic operations, we require only two: the one which converts number to sparse from, and another converting it back. 2) it reduces the total number of gates used. However we have simplified things a little.The actual Sha256 hash mixes complex logical operations with circular rotations. Our sparse representation is friendly to the first (by design), but not to the second. Barretenberg authors solve this problem by introducing several different tables which do rotation *and* convertion to sparse form in one fell swoop. This strategy is fine to Sha256 (though not ideal) where the number of different rotation moduli is rather small, but carries serious obstacles for Keccak, where each of 25 lanes is shifted by unique modulus. Let's see, if we could do better here. Assume, that our sparse algebraic encoding for x in base $b$ is $$ x = \sum_i^nx_i b^i,$$ where each $x_i$ is $\in [0, c-1]$ . In fact, the only actual restriction for the choice of base $b$ is that all separate chunks $x_i$ should be uniqely decodable and retrived from $x$, or in other words: The map $([0, c-1] \times n) \to \mathbb{F}: (x_0, \dots, x_{n}) \to \sum_i^nx_i b^i$ is injective. The most simplest and natural choice is $b = c$, and we may also take $b = c+1$ or $b=2c$, or any larger base $b$ as long as $x = \sum_i^nx_i b^i$ doesn't wrap around modulus p of field $\mathbb{F}$. In this case, injectivity is trivial. However, there is a special choice of base $b$ which forces x to *wrap around* $p$, but super friendly to rotations. Take $b=g$ - root of unity of order n: $$g^{n} = 1, g^i \neq 1, \forall i <= n.$$ If we still posess *injectivity* property for such choice of $b=g$, then both goals are achieved: logical operations are encoded as algebraic relations and are done on chunk-to-chunk basis and numbers are rotated by multiplication by corresponding powers of $g$. Using this, we may pack both rotations and arithmetic in the same Plonk gates. In practice, we are mostly interested for the following special cases of this injectivity problem: 1) $c = 8, n = 32$ - for efficient implementation of Sha256 gadget. 2) $c = 8$(or at least $c=4$), $n = 64$ - for Keccak. Note that, for Sha256 with $c = 8, n = 32$ the total number of all possible linear combinations $\sum_i x_ig^i$ is $8^{32} = 2^{3 \cdot 32} = 2^{96}$, so that injectivity cannot be checked by brute-force. If there exists rigorous *positive* solution to this injectivity problem, it will help to further reduce the number of gates and tables in Sha256 and Keccak Plookup implementations.

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully