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

[Proposal] alt_bn128 curve math #98

Closed
snjax opened this issue Jul 21, 2020 · 5 comments
Closed

[Proposal] alt_bn128 curve math #98

snjax opened this issue Jul 21, 2020 · 5 comments
Assignees

Comments

@snjax
Copy link

snjax commented Jul 21, 2020

Proposal

To implement a set of zkSNAKs verifiers (like Groth16 or PLONK) I suggest to add alt_bn128 math functions into VM:
To learn more about alt_bn128 and subgroups G1 and G2 see EIP-196 and EIP-197.

Functions

All formulas below are defined in additive notation. If data is wrong serialized, the function returns Error.

  • alt_bn128_g1_multiexp(items:&[G1, Fr]) -> Result<G1>

Compute \sum s_i g_i with Pippenger's algorithm, where s_i are Fr scalars and g_i are G1 group elements.

Bad data: If a s_i is more or equal then Fr order or g_i is not in G1 group, function returns Error.

Complexity: O(\frac{n}{\log(n))}) . I propose to use regularization \frac{n}{\max(\log_2(n),1) )} for n<=1.

Gas formula:

fn log2floor(x:u64) -> u64 {
    let mut t = x;
    let mut r = 0;
    for offset in [32, 16, 8, 4, 2, 1].iter() {
        if t >> offset != 0 {
            t >>= offset;
            r += offset;
        }
    }
    r
}

let n = (n_bytes+item_size-1)/item_size;
let gas_consumed = A+B*n+C * if n > 1 {n / log2floor(n)} else {n};

B is linear component, corresponding to deserialization complexity.

  • alt_bn128_g1_sum(items:&[G1, bool]) -> Result<G1>

Compute \sum (-1)^{s_i} g_i.

Bad data: If a s_i is not one or zero or g_i is not in G1 group, function returns Error.

Complexity: linear

Gas formula:

let n = (n_bytes+item_size-1)/item_size;
let gas_consumed = A+B*n;
  • alt_bn128_pairing_check(items:&[G1,G2]) -> Result<bool>

Compute \sum e(g_{1 i}, g_{2 i}) \stackrel{?}{=} 1

Bad data: If g_1i is not in G1 group or g_2i is not in G2 subgroup, function returns Error.

Complexity: linear

Gas formula:

let n = (n_bytes+item_size-1)/item_size;
let gas_consumed = A+B*n;

Data encoding

G1 is serialized as two U256 (x and y) in LE.
G2 is serialized as four U256 (re(x), im(x), re(y), im(y)) in LE.
bool is serialized as one byte.
Tuple is serialized as concatenated chunks of serialized elements.
Slice is serialized as concatenated chunks of serialized elements.

Implementation

alt_bn128 functions implemented as fork of parity-bn with minor updates:

  • serialization, deserialization
  • pippenger multiexp
  • API, described above

The crate should be published and moved to nearprotocol.

@MaksymZavershynskyi
Copy link
Contributor

@akhi3030 Is this implemented and can be closed?

@akhi3030
Copy link
Contributor

akhi3030 commented Nov 8, 2022

@nearmax: in near/nearcore#6824, we stabalised alt_bn128_g1_multiexp, alt_bn128_g1_sum, alt_bn128_pairing_check.
However, I do not see any NEPs or spec for these function in this repo. Maybe a NEP is not strictly necessary as the bulk of the work was done before the NEP process was standardised. However, we should probably add something to the spec. We can probably create a separate issue to track the remaining work and close this issue.

@frol
Copy link
Collaborator

frol commented Nov 8, 2022

@akhi3030 Yes, please, create a new issue and close this one. Could you take ownership of getting the spec updated? 🙏

@akhi3030
Copy link
Contributor

@akhi3030 Yes, please, create a new issue and close this one. Could you take ownership of getting the spec updated? 🙏

Happy to do so!

@akhi3030
Copy link
Contributor

The remaining work is now tracked in #426. Please prefer continuing discussions on that issue.

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

No branches or pull requests

6 participants