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

refactor!: use big endian and simplify transcript logic #1801

Merged
merged 4 commits into from
Aug 3, 2024

Conversation

alxiong
Copy link
Contributor

@alxiong alxiong commented Jul 30, 2024

Part of #1739
Closes #1797

This PR:

  • Point to the latest release of jf-plonk which modify the PlonkTranscript logic
  • Remove endian reversal for field elements
  • For G1Point, instead of using compressed form via g1Serialize, simply concatenate two base fields to the transcript byte array
  • Update _computeChallenges() to reflect the same change

Gas benchmark

@@ -1,2 +1,2 @@
-LightClient_newFinalizedState_Test:testCorrectUpdateBench() (gas: 594565)
-PlonkVerifier_verify_Test:test_verify_succeeds() (gas: 507806)
\ No newline at end of file
+LightClient_newFinalizedState_Test:testCorrectUpdateBench() (gas: 661721)
+PlonkVerifier_verify_Test:test_verify_succeeds() (gas: 496676)

I'm not 100% sure why the cost of PlonkVerifier goes down but the LightClient cost goes up.
(see if you have any idea @philippecamacho ?)

@alxiong
Copy link
Contributor Author

alxiong commented Jul 31, 2024

⚠️ WIP

I'm still debugging a mysterious failure. Currently computeChallenges test is failing (thus most of proof related tests are also failing). But all tests regarding Transcripts are passing now, which includes all the logic that _computeChallenges uses.

Diving even deeper, the challenge beta and gamma is computed correctly, but alpha, and all subsequent ones are wrong. I still unable to comprehend what could be the source of this inconsistency. Will continue to debug tmr.


Oh wow, this is such an unexpected behavior, somehow adding a console.log line pass all the test

        transcript.transcript = abi.encodePacked(
            transcript.transcript, proof.wire3.x, proof.wire3.y, proof.wire4.x, proof.wire4.y
        );

        // UNCOMMENT THIS LINE, THEN ALL FAILED TEST PASSED
        // console2.logBytes(transcript.transcript);
        res.beta = transcript.getAndAppendChallenge();
        res.gamma = transcript.getAndAppendChallenge();

🤯 investigating why....

@alxiong
Copy link
Contributor Author

alxiong commented Aug 2, 2024

Bug (that I introduce in this PR)

Let me document a minimally reproducible bug (of my own code) that blocks me for days:

import "forge-std/Test.sol";
import { Transcript as T } from "../src/libraries/Transcript.sol";

library Transcript {
    struct TranscriptData {
        bytes transcript;
    }
    function getAndAppendChallenge(TranscriptData memory self) internal pure returns (uint256) {
        bytes memory transcript = self.transcript;
        uint256 ret = uint256(keccak256(transcript)) % BN254.R_MOD;

        assembly {
            let len := mload(transcript)
            let newLen := add(len, 32)
            let dataPtr := add(transcript, 0x20)

            mstore(transcript, newLen)
            mstore(add(dataPtr, len), ret)
        }
        return ret;
    }
}

contract Whatever is Test {
    // run with `forge test --mt whatever -vv`
    function test_whatever() external {
        T.TranscriptData memory transcript;
        transcript.transcript = abi.encodePacked(
            transcript.transcript,
            uint256(0x1234)
        );

        // HERE !! try to comment and uncomment
        console2.log("hi");

        uint256 chal = T.getAndAppendChallenge(transcript);
        console2.log("chal: %x", chal);
        console2.logBytes(transcript.transcript);
        revert(); // deliberately revert to print logs
    }
}

so comment out the console2.log before the getAndAppendChallenge(): you would get:

  chal: 0x21909f356f36b5c053a1b997dd03e86a313c86db7ca957c0840832cca43b62c1
  0x0000000000000000000000000000000000000000000000000000000000001234
    0000000000000000000000000000000000000000000000000000000000000008

uncomment it, you would get the correct/expected transcript bytes:

  chal: 0x21909f356f36b5c053a1b997dd03e86a313c86db7ca957c0840832cca43b62c1
0x0000000000000000000000000000000000000000000000000000000000001234
  21909f356f36b5c053a1b997dd03e86a313c86db7ca957c0840832cca43b62c1

Diagonsis

I couldn't wrap my head around this behavior, and only after went into the debugger and look at stack and memory content and stepping through instruction by instruction do I realize the problem comes from:

my inline assembly code in getAndAppendChallenge() only updated the transcript's length but not the free memory pointer stored at 0x40 memory address. Thus later function call would overwrite the first 32 bytes the outdated free memory points to.
🤦

Reason why injecting a console.log between transcript.transcript = abi.encodePacked(transcript.transcript) and transcript.getAndAppendChallenge() makes things back to correct behavior is:

that console.log() call re-adjust the free memory pointer to point to somewhere much deeper in the memory, thus leaving the entire transcript region untact.

Knowing my problem, I find this StackOverflow question that discussed the same issue.

Solution

simply add one line to update the free memory pointer towards the end:

        assembly {
            // ...

            // update free memory pointer since we extend the dynamic array
            // to prevent potential overwrite
            mstore(0x40, add(mload(0x40), 32))
        }

Takeaway

Be careful about memory safety when editing dynamic memory using inline assembly.
Remember to update both the length field in the struct but also the free pointer memory at 0x40

@philippecamacho
Copy link
Contributor

philippecamacho commented Aug 2, 2024

Re benchmarks:

When I comment these three lines the gas consumption is around 170k.

When I do the same in b3ec621 I get 90k. So there is around a 80k gas leakage in this part of the code. Weird.

@philippecamacho
Copy link
Contributor

philippecamacho commented Aug 2, 2024

Re benchmarks:

When I comment these three lines the gas consumption is around 170k.

When I do the same in b3ec621 I get 90k. So there is around a 80k gas leakage in this part of the code. Weird.

@alxiong : #1816 created so that we can come back to it later.

@alxiong alxiong merged commit d3734a0 into commonprefix-patch Aug 3, 2024
15 checks passed
@alxiong alxiong deleted the cp-i02 branch August 3, 2024 04:08
@alxiong
Copy link
Contributor Author

alxiong commented Aug 5, 2024

Also found this on Solidity doc about safe memory management.

Apparently I didn't update myself with this warning. Lessons learned.

philippecamacho added a commit that referenced this pull request Sep 4, 2024
* fix: bug on lagrange coeff computation (#1639)

* Fix the edge case when zeta is one of the evaluation domain elements, and our lagrange coefficient (and also pi_poly_eval) is computed incorrectly.
Tests are also added for these rare edge cases.

* Update script to run gas benchmarks (#1769)

* Update script to run gas benchmarks. Store gas benchmarks for this commit.
* Update lint.yml as CI is complaining.

* Use addmod in function _computeLinPolyConstantTerm. (#1770)

* Point to branch 'commonprefix-patch of solidity-bn254 repository

* use forge snapshot for gas benchmark instead

* chore: update hotshot to 0.1.60, most jf dep to 0.4.5 (#1788)

* refactor!: use big endian and simplify transcript logic (#1801)

* fix transcript and tests

* fix computeChallenges in verifier

* update gas benchmark

* further improve and add comment to inline assembly

* Fix comment in `_linearizationScalarsAndBases` (#1812)

* Fix comment in _linearizationScalarsAndBases.

* Update contracts/src/libraries/PlonkVerifier.sol

Co-authored-by: Alex Xiong <alex.xiong.tech@gmail.com>

---------

Co-authored-by: Alex Xiong <alex.xiong.tech@gmail.com>

* refactor: add G2 point from SRS to verifying key and transcript (#1819)

* add G2 from SRS to VK and append to Transcript

* update with jf

* Remove redundant code (#1821)

* Remove unused function _batchVerifyOpeningProofs.

* Remove redundant variable sumEval.

* attempt: free disk space to avoid ci failure

---------

Co-authored-by: Alex Xiong <alex.xiong.tech@gmail.com>

* Gas optimization for evaluatePiPoly (#1822)

* Gas optimization for evaluatePiPoly

* add credit

---------

Co-authored-by: Alex Xiong <alex.xiong.tech@gmail.com>

* improve challenge generation  (#1829)

* use state-approach for squeeze domain-separation

* memory align word size

* update with merged jf

* feat: Efficient computeChallenges in pure assembly (#1831)

* rewrite computeChallenges in pure assembly

* add more comments

* refactor!: change publicInputs from dynamic to fixed array (#1835)

* change publicInputs from dynamic to fixed array

* Remove redundant fields in struct EvalDomain. (#1840)

* Remove redundant field EvalDomain.groupGenInv

* Remove redundant field EvalDomain.size.

* address comments

---------

Co-authored-by: Alex Xiong <alex.xiong.tech@gmail.com>

* Remove redundant transcript files. (#1836)

* Use of constants COSET_K1,...,COSET_K4.
* Put Transcript.sol and Transcript.t.sol files in some legacy folder.

* Precompute domain elements (#1860)

* Remove unused domain sizes.
* Hardcode domain elements.
* removed domain.groupGen and localDomainElements.

---------

Co-authored-by: Alex Xiong <alex.xiong.tech@gmail.com>

* feat!: fully assemblify preparePcsInfo() and verifyOpeningProof (#1844)

* perf: reuse free mem for each chal (#1943)

* perf: avoid carrying around commScalars and commBases (#1940)

* avoid carrying around commScalars and commBases

* address jakov comment on L01

* Fix justfile

* Small fixes.
Generate bindings.

* More fixes

---------

Co-authored-by: Alex Xiong <alex.xiong.tech@gmail.com>
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

Successfully merging this pull request may close these issues.

2 participants