Git Product home page Git Product logo

huffc's Introduction

The Huff Programming Language

πŸ“Œ DEPRECATION WARNING

This repository is considered deprecated and will be archived. For the new version of this tool please go to huff-rs.

You can continue to use this and it should work as-is but any future issues will not be fixed by the community.


Huff logo.

Huff is a low-level programming language designed for developing highly optimized smart contracts that run on the Ethereum Virtual Machine (EVM). Huff does not hide the inner workings of the EVM. Instead, Huff exposes its programming stack to the developer for manual manipulation.

Rather than having functions, Huff has macros - individual blocks of bytecode that can be rigorously tested and evaluated using the Huff runtime testing suite.

Initially developed by the Aztec Protocol team, Huff was created to write Weierstrudel. Weierstrudel is an on-chain elliptical curve arithmetic library that requires incredibly optimized code that neither Solidity nor Yul could provide.

While EVM experts can use Huff to write highly-efficient smart contracts for use in production, it can also serve as a way for beginners to learn more about the EVM.

Examples

For usage examples, see the huff-examples repository.

Installation

Prerequisities

Make sure you have the following programs installed:

Steps

This is how to create the contract bytecode to output Hello, World! in Huff.

  1. Install Huff globally:

    yarn global add huffc

Note: You may need to add yarn to your system's path to access globally installed packages. See the yarn docs on global for more details.

Hello World

  1. Create a file called hello-world.huff and enter the following content:

    #define macro MAIN() = takes (0) returns (0) {
        0x48656c6c6f2c20776f726c6421 0x00 mstore // Store "Hello, World!" in memory.
        0x1a 0x00 return // Return 26 bytes starting from memory pointer 0.
    }
  2. Use huffc to compile the contract and output bytecode:

    huffc hello-world.huff --bytecode

    This will output something like:

    6100168061000d6000396000f36c48656c6c6f2c20776f726c6421600052601a6000f3 
    

More help

Run huffc --help to view a full list of arguments:

huffc --help

> Usage: huffc [options]
> 
> Options:
>   -V, --version                    output the version number
>   -V, --version                    Show the version and exit
>   --base-path <path>               The base path to the contracts (default: "./")
>   --output-directory <output-dir>  The output directory (default: "./")
>   --bytecode                       Generate and log bytecode (default: false)
>   -o, output                       The output file
>   -p, --paste                      Paste the output to the terminal
>   -h, --help                       display help for command

huffc's People

Contributors

0xdoggie avatar d1ll0n avatar dxganta avatar exp-table avatar jetjadeja avatar johnnymatthews avatar jtriley-eth avatar laurencevs avatar refcell avatar saw-mon-and-natalie avatar saxenism avatar sudovirtual avatar zac-williamson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

huffc's Issues

Possible typo in import path

Hi, I tried to compile the ERC20.huff example but it was giving me a Error: ENOENT: no such file or directory, open '/home/AstronautSleuth/huffc/examples/erc20/contracts/contracts/utils/Ownable.huff'. It seems like contracts was repeated twice so I removed the contracts inside the include statement in the ERC20.huff file and compilation is successful.

I was just wondering if this was a typo or if it's just a pathing issue on my computer. I can do a quick PR if this is a typo πŸ˜„

Setup CI and add tests

We should decide on a code style for the repository and enforce consistency through CI rules.

We should also add thorough unit testing and run it on every push to ensure the package maintains consistent behavior between versions.

I'm just starting a discussion here - would love any opinions on what the style should be and any other comments on the CI / testing / linting strategy. I'd appreciate links to any examples of these in other repositories that people like.

feat(compiler): function to push bytecode size onto the stack

In order to support arbitrary-sized arrays as arguments of the constructor, we have to know the bytecode size beforehand, so we can use it along with the CODESIZEΒ opcode to parse the arguments bits.

Following an exchange with @JetJadeja on Twitter, a potential solution is to define a reserved keyword, which the compiler knows to replace with a Β PUSH operation of the bytecode size.

I have a working PoC here. We probably want to change the keyword as I am using scodesize__, along with OperationType.SCODESIZE and the replacement that is done at the end of the compile function.

How to pass arguments to the constructor?

βŠ‚(β—‰β€Ώβ—‰)぀ HI!

I saw Huff and wanted to give it a try, is there any documentation available that explains the takes(0) returns (0) syntax on the macros?

  • How to pass an address to the constructor?

Tried to do it this way:

/* Constructor */
#define constant TEST_LOCATION = FREE_STORAGE_POINTER()

#define macro CONSTRUCTOR() = takes(1) returns (0) {
    [TEST_LOCATION] sstore
    // Set msg.sender as the owner of the contract.
    OWNABLE_CONSTRUCTOR()
}

and

    const Token = await ethers.getContractFactory("ERC20");
    token = await Token.deploy("0x0000000000000000000000000000000000000000");
    await token.deployed();

But it says wrong number of arguments, expecting 0 got 1

bug(compiler): unable to parse arrays as inputs or outputs

The compiler cannot parse functions of the interface if they either use arrays for inputs or outputs.

The error probably lies in the regex definitions here.
They do not account for [].

To recreate the issue, try to compile any file with the following functions

#define function set(uint256,uint256[]) nonpayable returns ()
#define function get(uint256) view returns (uint256[])

`compile` should accept either file path or prebuilt sources object

The compile function takes a HuffCompilerArgs type as input.

type HuffCompilerArgs = {
  filePath: string;
  generateAbi: boolean;
  constructorArgs?: { type: string; value: string }[];
}

The filePath value is then passed to parseFile which passes it to getAllFileContents. getAllFileContents then reads the main file and all its imports and builds an object with the file contents of the main file and all its imports.

We should replace this with a more common pattern for compiler packages, where you can either provide an input file path or a prebuilt sources object containing all the file contents. This will make it possible to use huffc in environments without access to the filesystem, such as browsers.

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.