Chris Pondoc
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # CS 251 - 2020 Practice Exam See the link to the exam [here](https://cs251.stanford.edu/hw/final2020.pdf). ## Problem 1 (a.) An Ethereum re-entrancy attack occurs when a smart contract transfer an asset -- whether it be ETH, ERC-20, ERC-721, etc. -- to another contract. If this contract does not make the proper `require` checks at a certain location around the actual transfer, an adversarial contract can implement a `fallback` or `ERCReceived` function in order to repeatedly call the function and drain the main function of ETH, NFTs, etc. Summary [here](https://solidity-by-example.org/hacks/re-entrancy/): re-entrancy allows a contract to call back into another contract before they are finished executing. (b.) Pertinent slide is slide 34 [here](https://cs251.stanford.edu/lectures/lecture3.pdf). My initial hunch here would be no: you might be able to rederive the public keys with the information here, but you can't rederive the secret keys, which are equal to $sk_i = k_1 + HMAC(k_2, i)$ for all $i = 1, 2, ...$. (c.) I don't like any of the answers given in the doc. My initial hunch would be that if you were only checking if all the inputs were positive, this would represent some sort of deposit/transfer, indicating you have these funds. However, a negative output could mean you are removing money from someone, not sending them money. So if only the inputs had to positive, and the sum of the outputs has to be equal to the sum of the inputs, then couldn't you have two inputs equal to two outputs, but one output be very positive (i.e. a transfer) and another output be negative (i.e. losing money) as an attack? (d.) We did not learn about Zcash in this class, so I would refer to the Final Exam [collab](https://docs.google.com/document/d/1O506czTwpKeZHw6W-7vIBpYWBEiVc4g5iQDzM1uFZs0/edit#). (e.) On this question, my first thought process was that for UTXOs that are about to expire, transaction fees would be incredibly high since there would be high demand to spend them. I also like the alternative answer in the [collab](https://docs.google.com/document/d/1O506czTwpKeZHw6W-7vIBpYWBEiVc4g5iQDzM1uFZs0/edit#): there's potential that there will be more txs since you can't just hold your bitcoin: it has to be used in a transaction. (f.) Off-chain collateral systems, like USDC and USDT, work by sending your crypto to a company/regulated custodian, and then they mint the amount of the stablecoin on the blockchain. On the contrary, on-chain collateral systems work be locking up your crypto in a vault, typically overcollateralized, and then unlocking said stablecoin in return. You have to pay interest, and your position can also get liquidated. Due to the public nature of the blockchain and smart contracts, only the on-chain collateral verifies that the collateral is properly managed and maintained [here](https://docs.google.com/document/d/1O506czTwpKeZHw6W-7vIBpYWBEiVc4g5iQDzM1uFZs0/edit#). ## Problem 2 (a.) Similar to one of the problems on 2021 exam -- you want to wait for the blocks to confirmed. Note that the confirmation rule in Nakamoto Consensus is that each miner confirms the block (along with its prefix) that is $k$-deep within the longest chain in its view. Thus, the merchant should wait until the block is confirmed. There could also be a fork. (b.) Note that in Nakamoto Consensus, at any given time, each honest miner attempts to extend the heaviest chain, or that with the highest difficulty. Thus, longest chain will not matter. ## Problem 3 (a.) Note that the functionality lies in the code `uint256 maxAmount = (totalSupply - this.balance + (1 ether))`. The key is that the producer can only withdraw 1 more ether than has ever been withdrawn. At the first month, we know that we have withdrawn none, so we can get 1. Then, we see that after the second month, we've only withdrawn 1, so we can withdraw 2. After the third month, we've withdrawn 3 so far, so we can withdraw 4. Finally, in the past, we've withdrawn 4+2+1 = 7, so we can withdraw 8. (b.) Recall that an unsigned int works such that a negative value is an arbitrarily large positive value. Thus, imagine if film director was able to send over funds to the address of the contract before initializing the contract. This would make `this.balance > 0`. Then, when they initialize `MovieToken`, they can set `totalSupply = 0` by not supplying ETH to the contract. Then, when calculating `maxAmount`, we know that `totalSupply - this.balance` would result in a negative number, leading to a high positive number. They can then call `withdraw` with any `amount` value less than to redeem all the funds. (c.) Same as above, but now, we just self-destruct a contract that pushes ETH into the `MovieToken` contract after `totalSupply` has been initialized. This will cause the same underflow bug. (d.) The answer from the collab would be to keep a global `totalWithdrawals` variable that would determine how much you could withdraw. You could also just update `totalSupply` continually any time you receive ETH (i.e. `onETHRecived`, `fallback`, etc.) # Problem 4 (b.) Optimistic rollups use fraud proofs to prevent against a fraudulent rollup coordinator: the coordinator submits an update to the blockchain without proof. Within 7 days, if a user can prove (by showing a divergence in the Merkle tree) the coordinator submitted fraudulently, the update is cancelled and the coordinator's stake is slashed. If the user submitted an incorrect proof, the user pays a penalty. (c.) Only thing ever mentioned - Dapps live on different chains, so hard to interoperate/compose between different Dapps now # Problem 5 (a.) The vulnerability occurs when Alice tries to register a domain name `alice.eth` using a transaction calling the NameCoin contract. Bob sees the transaction posted to the chain, albeit not confirmed, and immediately posts his own transaction registering `alice.eth` but attaching a higher `maxPriorityFee` (EIP1559 tip) such that his transaction would be considered before Alice's in the mempool. This would allow Bob to register `alice.eth` even though he called the NameCoin contract after Alice did. (b.) ```solidity contract NameCoin { struct NameEntry { address owner; bytes32 value; } mapping (bytes32 => nameEntry) data; mapping (bytes32 => bytes32) commitments; function NameNewBegin(bytes32 commitment) { if (msg.value >= 100 && commitments[msg.sender] == 0) { commitments[msg.sender] = commitment; emit Register(msg.sender, name); } } function NameNewDo(bytes32 name, bytes32 r) { if ( data[name] == 0 && commitments[msg.sender] == sha3(name, r) ) { data[name].owner = msg.sender; } } } ``` (c.) (a) This would once again allow front-running: from part (a), Bob can see that Alice has submitted a transaction for `alice.eth`, and can front-run this with his own transaction if he can get `alice.eth` out of Alice's commitment. (c.) (b) This would allow Bob to forge a transaction for Alice as having bought the arbitrary domain name.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully