HackMD
    • Sharing Link copied
    • /edit
    • View mode
      • Edit mode
      • View mode
      • Book mode
      • Slide mode
      Edit mode View mode Book mode Slide mode
    • 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
    • More (Comment, Invitee)
    • Publishing
    • Commenting Enable
      Disabled Forbidden Owners Signed-in users Everyone
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Invitee
    • No invitee
    • Options
    • Versions and GitHub Sync
    • Transfer ownership
    • Delete this note
    • Template
    • Save as template
    • Insert from template
    • Export
    • Google Drive Export to Google Drive
    • Gist
    • Import
    • Google Drive Import from Google Drive
    • Gist
    • Clipboard
    • Download
    • Markdown
    • HTML
    • Raw HTML
Menu Sharing Help
Menu
Options
Versions and GitHub Sync Transfer ownership Delete this note
Export
Google Drive Export to Google Drive Gist
Import
Google Drive Import from Google Drive Gist Clipboard
Download
Markdown HTML Raw HTML
Back
Sharing
Sharing Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
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
More (Comment, Invitee)
Publishing
More (Comment, Invitee)
Commenting Enable
Disabled Forbidden Owners Signed-in users Everyone
Permission
Owners
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Invitee
No invitee
   owned this note    owned this note      
Published Linked with GitHub
Like BookmarkBookmarked
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Paul's EVMn (Including EVM384) Update THIS DRAFT IS STILL BEING UPDATED. AN EARLY COPY WAS REQUESTED BY CLIENT DEVS. In this update, I share an EIP draft, some recent breakthroughs since [the last update](https://notes.ethereum.org/@poemm/evm384-update5), and a comparison with precompile gas costs and runtimes. ## EIP Draft The current EIP draft: https://notes.ethereum.org/@poemm/eip_draft_evm_modular_arithmetic_extensions I am still working on simplifications, and to make it more future-proof. This is an iterative process which includes implementing heavy crypto. And sometimes, breakthroughs are made. ## Recent Breakthroughs With EVMn **Breakthrough 1. (DoS mitigation and smaller bytecode.)** A [security analysis](https://notes.ethereum.org/@poemm/EVM384SecurityAnalysis) demonstrated a cache thrashing attack. This is mitigated by switching from four-byte to two-byte offsets, which limits addressable memory to 64kb. Smaller offsets resulted in smaller bytecode -- allowing more optimizations like loop unrolling! **Breakthrough 2. (Immediates without versioning.)** It has been [claimed](https://ethereum-magicians.org/t/eip-663-unlimited-swap-and-dup-instructions/3346/10) that multi-byte opcodes (including immediates) require versioning. This has been [refuted](https://discord.com/channels/595666850260713488/706868829900505180/817455521341636649). The solution is elegant: we wrap extra bytes in a `PUSH*` _after_ the opcode. This `PUSH*` is never executed, it just acts as a syntactic guard to prevent clobbering `JUMPDEST`s. Immediates are a major optimization because they allow us to avoid stack overhead. **Breakthrough 3. (Abstraction to arbitrary algorithms and bitlengths.)** A fourth opcode, `SETMOD`, is introduced to store the modulus and precompute some parameters. `SETMOD` adds statefulness, but allows (i) arbitrary algorithms (including subquadratic runtime), (ii) precomputing things to speed up runtime, (iii) trivial bitlength abstraction (including 256-bit, 384-bit and 768-bit), (iv) fewer runtime checks, (v) even moduli, and (vi) smaller bytecode size. `SETMOD` adds about 50 lines of C code, plus client boilerplate. **Breakthrough 4. (Faster memory copying.)** Cryptography is full of memcopy operations. Earlier prototypes did memcopies through the EVM stack, at high gas cost. Jared Wasinger had a breakthrough suggestion to use `ADDMONT` for memcopies. This only works for copying ring elements (smaller than the modulus), which is the common case in crypto! This has reduced gas costs greatly! **Breakthrough 5. (EVM-specific optimizations.)** In EVM, we can precompute things off-chain and pass them as calldata. See [this ethresearch post](https://ethresear.ch/t/surveying-precomputation-methods-in-cryptography-request-for-help/8606). This is still being explored. ## A side-by-side comparison of EIP-2537 precompiles and EVMn Gas Costs EIP-2537 gas costs are from: https://eips.ethereum.org/EIPS/eip-2537 EVMn gas costs are from executing tests in: https://github.com/poemm/EVMcurves/tree/master/tests (These gas costs may have <0.5% error.) (TODO: upload tests for map-to-g1 and ecadds.) --- | | Precompile Gas Cost | EVMn gas costs | Slowdown | Notes | |---| --- | --- | --- | --- | | `BLS12_PAIRINGS` | 108,000 | 240,570* | 2.23x | (*) The EVMn implementation does not include subgroup checks, a 15% overhead, but many cryptosystems don't need these checks. The EVMn miller loop can be optimized by 25% with line precomputation, which many cryptosystems allow e.g. BLS signatures, PLONK, and KZG. Other EVM optimizations may cut another 5%. | | `BLS12_MAP_FP_TO_G1` | 5,500 | 14,513 | 2.64x | The EVMn implementation can be cut to 11,619 if we skip the final jacobian->affine conversion, which many cryptosystems allow. A [precomputation trick for sqrt](https://ethresear.ch/t/surveying-precomputation-methods-in-cryptography-request-for-help/8606) can further reduce costs to ~9,500 gas. Other EVM optimizations may cut another 5%. | | `BLS12_MAP_FP2_TO_G2` | 75,000 | - | - | The algorithm has similar structure to `BLS12_MAP_FP_TO_G1`, so maybe the slowdown will be similar, and similar optimizations will be possible. | | `BLS12_G1ADD` | 500 | 3504 | 7.08x | Most of the EVMn cost is jacobian->affine conversion, which many cryptosystems can omit -- removing this conversion gets us down to 567 gas. Most of this remaining gas is setup cost, which we can amortize to get us _below_(!) the precompile cost. Other optimizations are possible if we know that it is a point doubling (i.e. same input) or an add of distinct points. | | `BLS12_G2ADD` | 800 | 4456 | 5.57x | Similar to `BLS12_G1ADD`. Without the jacobian->affine conversion, it costs 1406 gas, some of which is startup cost. Similar optimizations are possible as with `G1ADD`. | | `BLS12_G1MUL` | 12,000 | - | - | This precompile is not needed, it is a special case of `BLS12_G1MULTIEXP`. | | `BLS12_G2MUL` | 45,000 | - | - | This precompile is not needed, it is a special case of `BLS12_G2MULTIEXP`. | | `BLS12_G1MULTIEXP` | (a formula) | - | - | EVMn can use [precomputation tricks for multi-scalar mul](https://ethresear.ch/t/surveying-precomputation-methods-in-cryptography-request-for-help/8606). Plus GLV optimizations. I expect only a small slowdown relative to the precompile. | | `BLS12_G2MULTIEXP` | (a formula) | - | - | EVMn can use [precomputation tricks for multi-scalar mul](https://ethresear.ch/t/surveying-precomputation-methods-in-cryptography-request-for-help/8606). Plus GLV-GLS optimizations. I expect only a small slowdown relative to the precompile. | **Table 1.** Comparison of gas costs between EIP-2537 and EVM384. --- ## Benchmarks Besides gas, runtimes were also measured. These implementations are "hacky" (dummy `SETMOD`, hard-coded modulus, using blst's asm), but give an idea of what performance clients can expect, relative to the speed-record implementation blst, when they use fast implementations of the montgomery arithmetic. --- | | miller loop | final exponentiation | slowdown to blst | |---|---|---|---| |blst | 379us | 504us | 1x |evmone | 686us | 989us | 1.9x |geth | 1660us | 2190us | 4.3x **Table 2.** Runtimes on an i5-5300u (2.9GHz, launched Q1 2015). Numbers can be reproduced in [geth](https://gist.github.com/poemm/d3fd461da50b8981bfabe4068b82456c), [evmone](https://gist.github.com/poemm/56dc176ff9fe957cb2de4ff93c8eff5e), and [blst](https://gist.github.com/poemm/671dd777e023ab9bf740d4fc70400374). --- EVMn numbers can be improved in the following ways: - The opcode boilerplate (checks for memory bounds, offset preparation) can be optimized. - Cryptosystem-specific EVM optimizations, which aren't available to precompiles. (See table 1.) - Other EVM optimizations, which were not done yet for simplicity. (See table 1.)

Import from clipboard

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 lost 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?

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

Sign in via SAML

or

Sign in via GitHub

Help

  • English
  • 中文
  • 日本語

Documents

Tutorials

Book Mode Tutorial

Slide Example

YAML Metadata

Resources

Releases

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

Versions and GitHub Sync

Sign in to link this note to GitHub Learn more
This note is not linked with GitHub Learn more
 
Add badge Pull Push GitHub Link Settings
Upgrade now

Version named by    

More Less
  • Edit
  • Delete

Note content is identical to the latest version.
Compare with
    Choose a version
    No search result
    Version not found

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. Learn more

       Sign in to GitHub

      HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

      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
      Available push count

      Upgrade

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Upgrade

      Danger Zone

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

      Syncing

      Push failed

      Push successfully