Giter Club home page Giter Club logo

circuits's People

Contributors

dependabot[bot] avatar haoyuathz avatar lispc avatar noel2004 avatar silathdiir avatar winedia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

circuits's Issues

long term: split this repo

in fact this repo contains following parts:

  1. circuits itself
  2. some scripts that generate input/output test data for circuits. like class TestPlaceOrder implements SimpleTest
  3. the tester, return true/false with input / output / circuit
  4. the global state manager, which is core of the future rollup module

feature: implement strategy of overwrite order tree in state_manager

# finished orders can never be used again
order_leaf_to_overwrite = `select order_leaf where state = 'finished' order by id asc limit 1;`
if order_leaf_to_overwrite is None:
    # oldest open order is less probable to be used later? heuristic 
    order_leaf_to_overwrite = `select order_leaf order by id asc limit 1;`

increase data segments limit

When testing block.circom in #31

[CompileError: WebAssembly.compile(): data segments count of 104626 exceeds internal limit of 100000 @+650474]

It seems that there are too data segments being used

check signature mechanism

gates profile result of Block(4, 2, 2):

main.processTransfer.newSenderHash.hash.mimcs.invpow.pow5 1716.28
main.processTransfer.oldReceiverHash.hash.mimcs.invpow.pow5 1716.28
main.processTransfer.newReceiverHash.hash.mimcs.invpow.pow5 1716.28
main.processWithdraw.oldAccountHash.hash.mimcs.invpow.pow5 1716.28
main.processWithdraw.newAccountHash.hash.mimcs.invpow.pow5 1716.28
main.processDepositToNew.newAccountHash.hash.mimcs.invpow.pow5 1716.57
main.processTransfer.sigVerifier.mulFix.segments.adders 2354.35
main.processWithdraw.sigVerifier.mulFix.segments.adders 2354.35
main.processTransfer.sigVerifier.hash.mimcs.pow 2415.89
main.processWithdraw.sigVerifier.hash.mimcs.pow 2415.89
main.processTransfer.sigVerifier.hash.mimcs.invpow.pow5 2589.78
main.processWithdraw.sigVerifier.hash.mimcs.invpow.pow5 2589.78
main.processTransfer.sigVerifier.mulAny.segments.bits.selector 3870.84
main.processWithdraw.sigVerifier.mulAny.segments.bits.selector 3870.84
main.processTransfer.sigVerifier.mulFix.segments.windows.mux 4332.03
main.processWithdraw.sigVerifier.mulFix.segments.windows.mux 4332.03
main.processTransfer.sigVerifier.mulAny.segments.bits 5329.30
main.processWithdraw.sigVerifier.mulAny.segments.bits 5329.30
main.processTransfer.sigVerifier.mulAny.segments.bits.doubler 5440.06
main.processWithdraw.sigVerifier.mulAny.segments.bits.doubler 5440.06
main.processTransfer.sigVerifier.mulAny.segments.bits.adder 5616.06
main.processWithdraw.sigVerifier.mulAny.segments.bits.adder 5616.06
one 34781.95

total_gates 274159.00

TODO: check whether the gate cost of sigVerifier is reasonable.

data availability design

The meaning of 'data availability' is to enable proving account has balance of token in the tree. So order details are not needed.

SpotTrade:

AccountID1 4bytes
AccountID2 4bytes
tokenID_1to2 2bytes
tokenID_2to1 2bytes
Amount1to2 3bytes
Amount2to1 3bytes
// NewAccount1OrderRoot 
// NewAccount2OrderRoot
// although hash root is only 1 field element, it consumes many bytes in fact. So we prefer use original data
Order1Pos 2bytes
Order1ID 4bytes
Order1BuyAmount 3bytes
Order1SellAmount 3bytes
// Order1SellFilled 
// Order1BuyFilled
.. and for order2 ..

So total 42 bytes.

DepositToOld:

AccountID1 4bytes
tokenID1 2bytes
Amount1 3bytes

DepositToNew

AccountID1 4bytes
tokenID1 2bytes
Amount1 3bytes
ethAddr: 20bytes
sign 1bit
ay field: 32 bytes

total: 61bytes

Transfer

AccountID1 4bytes
AccountID2 4bytes
tokenID2bytes
Amount 3bytes

Withdraw

AccountID1 4bytes
tokenID1 2bytes
Amount1 3bytes

TestGenesisOrderRoot: Signal assigned twice error using wasm_tester

ERROR:  6 Signal assigned twice 0 0 0 0
Error: Signal assigned twice 0 0 0 0
    at error (/home/chris/gitfiles/Fluidex/circuits/node_modules/circom_runtime/js/witness_calculator.js:51:23)
    at setSignal (wasm://wasm/000f95ca:0:27522)
    at null.<anonymous> (wasm://wasm/000f95ca:0:28963)
    at null.<anonymous> (wasm://wasm/000f95ca:0:26916)
    at init (wasm://wasm/000f95ca:0:27116)
    at WitnessCalculator._doCalculateWitness (/home/chris/gitfiles/Fluidex/circuits/node_modules/circom_runtime/js/witness_calculator.js:130:31)
    at WitnessCalculator.calculateWitness (/home/chris/gitfiles/Fluidex/circuits/node_modules/circom_runtime/js/witness_calculator.js:158:20)
    at WasmTester.calculateWitness (/home/chris/gitfiles/Fluidex/circuits/node_modules/circom/ports/wasm/tester.js:57:45)
    at Object.testWithInputOutput (/home/chris/gitfiles/Fluidex/circuits/test/tester/wasm.ts:31:33)
    at main (/home/chris/gitfiles/Fluidex/circuits/test/test_all.ts:23:5)

c_tester works good

basic benchmark

Choose a circuit ( either spotTrade or Block including spotTrade ), get a table with following schema:

Both one core and multi core ( 4 core or 8 core ) ( or real time / user time ) results are useful :

time groth16-zkutil groth16-rapidsnark plonk-plonkit
rescue hash ?? ?? ??
poseidon hash ?? ?? ??

plonkit should be upgraded to bellman beta branch ( If fully upgrading including solidity stuff costs much time, we can create a branch rather than push unfinished code to plonkit master )

The bench script code may be executed later so it is needed to be commited into the repo. ( We don't need a full auto script. Some manual operations are ok )

https://github.com/Fluidex/circuits/blob/master/tools/replace_hash.sh may be useful.

rapidsnark need a zkey, we may have to generate it.

amount compression

we should encode amount to Scientific notation (Mantisa & Exponent) for compression

refactor order circuit

decouple order id and merkle tree idx

Order id is like nonce, always incremental. User signed this id.

Meanwhile trade circuit uses idx of order merkle tree to fetch the order info then check trade price/amount against order info.

They are not same thing. After decoupling these two items, we can implement this #104

rewrite the order state transition login

These are the only valid transition methods:

  1. from empty slot to a new order
  2. from an old slot to a new order. Order with bigger id is newer.

The above two can be combined if we assume order_id:0 means empty order, real order starts with order id 1.

meta issue: towards 300 tps

Our recent goal is to run a 300tps rollup test on testnet ( for 1 hour? ), including matching engine, witness generation, proving, contract verification.

here I list some must-to-do for this goal:

  1. design bitwise tx encoding mechanism to save space and gas
  2. sha the inputs and make all other circuit input private. Only the hash in public input.
  3. rewrite global state manager in rust.
  4. implement parallel merkle tree update
  5. we may even need to use poseidon ( seems faster than rescue for offline cpu calculation?)

Better to do:

  1. Reuse order tree slot
  2. Encode big int, signficand + exponent

Depedency of other repo:

  1. prover cluster
  2. matchengine can generate rollup event

For all things above, we can support 'SpotTrade' first. Implement 'Withdraw/DepositToNew' etc later.

research: will recursion help?

We have different types of op DepositToNew DepositToOld Transfer etc.

As we all know the if else in circuits is not same to if else in common programming languages where either if branch or else branch will run. if else in circuits run both branch. It will take more resources ( constraints/gates here ).

So there may be a method to split the op by type then combine them using recursion... More research is needed here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.