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

Modify the packing policy to reduce the number of primary inputs processed by the Verifier #52

Closed
AntoineRondelet opened this issue Aug 16, 2019 · 5 comments
Assignees
Labels
arithmetic-circuit/R1CS Task related to the R1CS programs optimization Optimization task solidity Task related to the Solidity part of the code base

Comments

@AntoineRondelet
Copy link
Contributor

AntoineRondelet commented Aug 16, 2019

In the current state of the project, several primary inputs are digest (of bit-length 256) and are packed into field elements. This set of field elements represent the set of primary inputs sent to the Verifier (contract), which then executes the SNARK verification routine. The verification routine of the SNARK used does a number of scalar multiplications linear in the number of primary inputs.

For now, each 256-bit digest (d) is packed into 2 field elements:

  1. d1: One field element containing 253 bits from the digest
  2. d2: Another field element containing the 3 remaining bits of the digest (and all zeroes afterwards)
    This "packing policy" was chosen to keep things simple, but we clearly see that, if we have the following set of primary inputs:
    {rt, n1, n2, c1, c2, vpub_in, vpub_out}, where {n1,n2,c1,c2} are digests, then, we'll send the set {rt, n11, n12, n21, n22, c11, c12, c21, c22, vpub_in, vpub_out} of field elements as the set of primary inputs, to the Verifier.

This is quite inefficient since we know that {n12, n22, c12, c22} will only contain 3 "meaningful bits" (the other bits in the field element representation will be set to 0's).

It'd be better to represent the information of the set {n12, n22, c12, c22} in a single field element.

Doing so would reduce the cardinality of the set of primary inputs and save a few calls to the bn256ScalarMul precompiled contract, saving gas on the Verifier side.

@AntoineRondelet AntoineRondelet added arithmetic-circuit/R1CS Task related to the R1CS programs optimization Optimization task solidity Task related to the Solidity part of the code base labels Aug 16, 2019
@AntoineRondelet
Copy link
Contributor Author

AntoineRondelet commented Aug 16, 2019

This requires changes in the circuit (to change the way we pack the primary inputs) and changes on the solidity side (to parse and recompose the digests from the packed elements - some "utils" functions will need to be modified/implemented).

It will save constraints in the circuit and save gas cost on the verifier's side.

@rrtoledo
Copy link
Contributor

Issue #55 can be incorporated here.

@rrtoledo
Copy link
Contributor

rrtoledo commented Aug 28, 2019

Could we go a step further in the packing ?
v_in and v_out each needs only 64 bits. A field variable being written over 253 bits, we could pack v_in, v_out and the {n12, n22, c12, c22, hsig2, h02, h12} into a single field variable (2*64+3*(2* NumInput + 1 + NumOutput) = 146 bits < 253 ).

@AntoineRondelet
Copy link
Contributor Author

Yes, I think this is fine to include the v_in and v_out values in the resulting field element @rrtoledo
You'll need to make sure you can extract these values on the contract (like for other public inputs) to carry out all the relevant checks.

@rrtoledo rrtoledo mentioned this issue Sep 24, 2019
@rrtoledo rrtoledo self-assigned this Sep 25, 2019
@AntoineRondelet
Copy link
Contributor Author

Closing this issue since the corresponding PR has been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arithmetic-circuit/R1CS Task related to the R1CS programs optimization Optimization task solidity Task related to the Solidity part of the code base
Projects
None yet
Development

No branches or pull requests

2 participants