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

Implement Cairo0 Class Hash #2111

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Implement Cairo0 Class Hash #2111

wants to merge 12 commits into from

Conversation

weiihann
Copy link
Contributor

@weiihann weiihann commented Sep 2, 2024

Description

This PR replaces the implementation of the Cairo0 class hash function from FFI to non-FFI (i.e. implement it directly in Golang).

Rationale

Why this function was implemented using FFI:

  • Implementing it in Golang is a pain. There's a specific formatting that we need to adhere to, and there are certain edge cases that we need to handle.

Using FFI removes the inconveniences. However, this means that other applications using Juno as a library must also do Rust compilation. It is not library-friendly at all means. While we also have starknet/rust and vm/rust, removing core/rust would at least reduce the surface area of using FFI, making it more library-friendly.

Now that new Cairo0 classes are no longer available, we can rewrite it in a more brute-force manner. That is, do a bunch of string formatting. The performance comparison shows that it is about 27% slower than using FFI (due to a bunch of string formatting), I think it's still worth switching over to the non-FFI implementation.

Test suites

Tested this non-FFI implementation against all cairo0 class hashes on Starknet mainnet and sepolia, all passes.

FFI vs NoFFI

                                                                                  │   ffi.txt    │              noffi.txt               │
                                                                                  │    sec/op    │    sec/op      vs base               │
ClassV0Hash/0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a-11    30.51m ± ∞ ¹    38.95m ± ∞ ¹  +27.65% (p=0.008 n=5)
ClassV0Hash/0x36c7e49a16f8fc760a6fbdf71dde543d98be1fee2eda5daff59a0eeae066ed9-11    18.85m ± ∞ ¹    22.64m ± ∞ ¹  +20.05% (p=0.008 n=5)
ClassV0Hash/0x07db5c2c2676c2a5bfc892ee4f596b49514e3056a0eee8ad125870b4fb1dd909-11   3.426m ± ∞ ¹    4.404m ± ∞ ¹  +28.54% (p=0.008 n=5)
ClassV0Hash/0x0772164c9d6179a89e7f1167f099219f47d752304b16ed01f081b6e0b45c93c3-11   37.59m ± ∞ ¹    47.24m ± ∞ ¹  +25.68% (p=0.008 n=5)
ClassV0Hash/0x028d1671fb74ecb54d848d463cefccffaef6df3ae40db52130e19fe8299a7b43-11   5.424m ± ∞ ¹    6.992m ± ∞ ¹  +28.91% (p=0.008 n=5)
ClassV0Hash/0x4367b26fbb92235e8d1137d19c080e6e650a6889ded726d00658411cc1046f5-11    7.116m ± ∞ ¹    8.472m ± ∞ ¹  +19.07% (p=0.008 n=5)
ClassV0Hash/0x35c753cbb572d7d0cbd6f725e4bc5f631d82003f3ac743160705b9ecd1befa7-11    829.8µ ± ∞ ¹   1157.0µ ± ∞ ¹  +39.44% (p=0.008 n=5)
ClassV0Hash/0x4fda56652f3dfe9c59904dd0fdc0fa5965b1e560e1641a06e132fdf90a96424-11    17.15m ± ∞ ¹    20.92m ± ∞ ¹  +21.96% (p=0.008 n=5)
ClassV0Hash/0x56fcc016a8ef4bd6310fa95de87c479a086fa1bf1934148c2bdf3b4cdbe4ac5-11    1.513m ± ∞ ¹    2.078m ± ∞ ¹  +37.36% (p=0.008 n=5)
geomean                                                                             7.336m          9.351m        +27.46%

Copy link

codecov bot commented Sep 2, 2024

Codecov Report

Attention: Patch coverage is 78.54077% with 50 lines in your changes missing coverage. Please review.

Project coverage is 78.55%. Comparing base (1b89289) to head (3b82452).
Report is 92 commits behind head on main.

Files with missing lines Patch % Lines
core/class.go 72.50% 25 Missing and 19 partials ⚠️
core/program.go 88.88% 2 Missing and 2 partials ⚠️
utils/strings.go 94.28% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2111      +/-   ##
==========================================
+ Coverage   78.41%   78.55%   +0.14%     
==========================================
  Files         102      103       +1     
  Lines        9205     9396     +191     
==========================================
+ Hits         7218     7381     +163     
- Misses       1351     1368      +17     
- Partials      636      647      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@weiihann weiihann force-pushed the weiihann/cairo0-class-hash branch from 576d121 to f3478de Compare September 13, 2024 21:12
@weiihann weiihann marked this pull request as ready for review September 13, 2024 21:12
utils/strings.go Fixed Show fixed Hide fixed
@weiihann weiihann force-pushed the weiihann/cairo0-class-hash branch from 83460fc to 96cce04 Compare September 13, 2024 21:41
utils/strings.go Dismissed Show dismissed Hide dismissed
@weiihann weiihann force-pushed the weiihann/cairo0-class-hash branch from 96cce04 to c5c3ae4 Compare September 17, 2024 08:19
@weiihann weiihann force-pushed the weiihann/cairo0-class-hash branch from c5c3ae4 to e9692e2 Compare September 26, 2024 12:30
core/program.go Outdated Show resolved Hide resolved
utils/test.go Outdated Show resolved Hide resolved
core/class.go Outdated Show resolved Hide resolved
@weiihann weiihann requested a review from kirugan September 27, 2024 08:03
core/class.go Outdated Show resolved Hide resolved
core/class.go Outdated Show resolved Hide resolved
@weiihann weiihann requested a review from kirugan September 27, 2024 13:53
@weiihann weiihann force-pushed the weiihann/cairo0-class-hash branch from a926956 to be4f41c Compare September 27, 2024 13:54
var wg sync.WaitGroup
var externalEntryPointHash, l1HandlerEntryPointHash, constructorEntryPointHash, builtInsHash, dataHash *felt.Felt
var hintedClassHash *felt.Felt
var hintedClassHashErr error
Copy link
Contributor

@kirugan kirugan Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can improve this logic by:

  1. replace sync.WaitGroup with conc.NewWaitGroup()
  2. move common logic for EntryPoints in lambda and reuse it for External, L1Handler and Constructor slices
  3. as a consequence remove all digest variables below

i.e.:

	var externalEntryPointHash, l1HandlerEntryPointHash, constructorEntryPointHash, builtInsHash, dataHash *felt.Felt
	var hintedClassHash *felt.Felt
	var hintedClassHashErr error

	entryPointsDigest := func(entryPoints []starknet.EntryPoint) *felt.Felt {
		var epDigest crypto.PedersenDigest
		for _, ep := range entryPoints {
			epDigest.Update(ep.Selector, ep.Offset)
		}
		return epDigest.Finish()
	}
	wg := conc.NewWaitGroup()
	wg.Go(func() {
		externalEntryPointHash = entryPointsDigest(definition.EntryPoints.External)
	})
	wg.Go(func() {
		l1HandlerEntryPointHash = entryPointsDigest(definition.EntryPoints.L1Handler)
	})
	wg.Go(func() {
		constructorEntryPointHash = entryPointsDigest(definition.EntryPoints.Constructor)
	})
	wg.Go(func() {
		var builtInsDigest crypto.PedersenDigest
		for _, builtIn := range program.Builtins {
			builtInHex := hex.EncodeToString([]byte(builtIn))
			builtInFelt, err := new(felt.Felt).SetString("0x" + builtInHex)
			if err != nil {
				return
			}
			builtInsDigest.Update(builtInFelt)
		}
		builtInsHash = builtInsDigest.Finish()
	})
	wg.Go(func() {
		hintedClassHash, hintedClassHashErr = computeHintedClassHash(definition.Abi, definition.Program)
	})
	wg.Go(func() {
		var dataDigest crypto.PedersenDigest
		for _, data := range program.Data {
			dataFelt, err := new(felt.Felt).SetString(data)
			if err != nil {
				return
			}
			dataDigest.Update(dataFelt)
		}
		dataHash = dataDigest.Finish()
	})
	wg.Wait()

Copy link
Contributor

@kirugan kirugan Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and btw are you sure that .SetString("0x" + builtInHex) is necessery?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, hex.EncodeToString outputs the hexadecimal representation without 0x prefix. felt.SetString requires the parsed string to have 0x prefix. You can check felt_test.go and verify the usage.

Here's a quick code snippet that you can verify hex encoding output in go playground:

package main

import (
	"encoding/hex"
	"fmt"
)

func main() {
	// Sample data to encode
	data := []byte("Hello, Hex Encoding!")

	// Encode the data to hexadecimal string
	hexString := hex.EncodeToString(data)

	// Print the result
	fmt.Printf("Original: %s\n", data)
	fmt.Printf("Hex encoded: %s\n", hexString)
}

// computeHintedClassHash calculates the hinted class hash by hashing the JSON combination
// of ABI and Program.
func computeHintedClassHash(abi, program json.RawMessage) (*felt.Felt, error) {
var mProgram Program
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does "m" mean in mProgram ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modified program :D

core/class.go Outdated
}

// Combine both ABI and Program JSON strings
var hintedClassHashJSON strings.Builder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to replace it with bytes.Buffer it has almost the same API, but there is no need to cast to []byte from string at the bottom because we can just call buffer.Bytes()

if err := json.Unmarshal(jsonBytes, &jsonData); err != nil {
return "", err
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't jsonData is the same as value at this point ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed value from any to json.RawMessage

@weiihann weiihann force-pushed the weiihann/cairo0-class-hash branch from be4f41c to e493ea1 Compare October 4, 2024 04:40
Copy link
Contributor

This pull request is stale because it has been open 35 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added Stale and removed Stale labels Dec 10, 2024
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