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 support for alt_bn128 syscalls (old) #25464

Closed
wants to merge 8 commits into from

Conversation

at5500
Copy link

@at5500 at5500 commented May 22, 2022

Problem

Contracts written in the Solidity language cannot work in Solana if they contain calls to the following precompiled contracts:

bn256Add — Performs addition on the elliptic curve operations.
bn256ScalarMult — Performs scalar multiplication on the elliptic curve operations.
bn256Pairing — Elliptic curve pairing operations to perform zkSTARKs verification within the block gas limit.

https://github.com/ethereum/EIPs/blob/master/EIPS/eip-196.md
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-197.md

The Neon EVM requires the implementation of system calls in Solana for these contracts.

Solution

In order for the precompiled contracts to be used in Solana, it is proposed to implement sys-calls inside the Solana core. That is, to perform an implementation similar to the erc-recover implementation.

Summary of Changes

This merge request adds implementation of bn256 (alt_bn128) precompiles.

About compute budget costs

We need to know, how much BPF instructions will be used for every operation on elliptic curves. We can't to know it directly, but we can compare the time needed for these operations and time, needed for Keccak hash for the same data.

We added benchmarks, that allow us to approximately define the amount of data in bytes for elliptic curves operations and Keccak hash and compare result times.

The repository with benchmarks is here: https://github.com/neonlabsorg/alt-bn128-bench

@mergify mergify bot added the community Community contribution label May 22, 2022
@mergify mergify bot requested a review from a team May 22, 2022 18:39
@at5500 at5500 changed the base branch from master to v1.9 May 22, 2022 18:40
@samkim-crypto
Copy link
Contributor

@Lichtso These are the crypto syscalls that I mentioned a couple weeks back. When you get a chance, can you help with computing the syscall compute budgets here? I am also curious to know what the process is for computing costs for syscalls.

These set of crypto operations are extremely useful/fundamental and should enable many advanced crypto operations on Solana.

@at5500 at5500 force-pushed the alt_bn128_precompiles branch from aee1a2b to ee55495 Compare May 26, 2022 09:36
@Lichtso
Copy link
Contributor

Lichtso commented May 26, 2022

We roughly charge one CU per byte copied (over the syscall interface edges) or CPU cycle used.

ALT_BN128_ADDITION_INPUT_LEN=128
ALT_BN128_ADDITION_OUTPUT_LEN=64
And at least 32 arithmetic operations (probably more, would have to benchmark), in total 128+64+32 = 224.

So you are currently more on the too cheap side I would say. Question is if this makes sense at all, because the data moved over the syscall boundary might out-weight the actual calculation.

@jackcmay Thoughts?

@at5500
Copy link
Author

at5500 commented May 27, 2022

So you are currently more on the too cheap side I would say.

You're right! We are currently recalculating compute budget costs and will update them in code ASAP (today or tomorrow).

@Lichtso
Copy link
Contributor

Lichtso commented May 31, 2022

@samkim-crypto Quoting Tao on Discord we have a rough idea of how long 1 CU should take (about 33 ns):

However CU to micro-sec conversion rate of 30 CU per 1 us is a cluster average - it meant to be a generous one, it should allow most validators to replay 48MCu block within 400ms.

@stale
Copy link

stale bot commented Jul 10, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale [bot only] Added to stale content; results in auto-close after a week. label Jul 10, 2022
@at5500 at5500 changed the base branch from v1.9 to v1.10 July 14, 2022 18:11
@stale stale bot removed the stale [bot only] Added to stale content; results in auto-close after a week. label Jul 14, 2022
@at5500 at5500 changed the base branch from v1.10 to v1.9 July 14, 2022 18:17
@at5500 at5500 marked this pull request as draft July 14, 2022 21:17
@at5500 at5500 changed the title Add support for alt_bn128 syscalls Add support for alt_bn128 syscalls (old) Jul 14, 2022
@stale
Copy link

stale bot commented Jul 31, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale [bot only] Added to stale content; results in auto-close after a week. label Jul 31, 2022
@stale
Copy link

stale bot commented Aug 13, 2022

This stale pull request has been automatically closed. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Community contribution stale [bot only] Added to stale content; results in auto-close after a week.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants