Skip to content

Commit

Permalink
Clears some curve methods and adds Poseidon method
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Oct 21, 2024
1 parent d3dca1d commit 5494ea4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
17 changes: 15 additions & 2 deletions curve/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,19 @@ func Pedersen(a, b *felt.Felt) *felt.Felt {
return junoCrypto.Pedersen(a, b)
}

// Poseidon is a function that implements the Poseidon hash.
// NOTE: This function just wraps the Juno implementation
// (ref: https://github.com/NethermindEth/juno/blob/32fd743c774ec11a1bb2ce3dceecb57515f4873e/core/crypto/poseidon_hash.go#L59)
//
// Parameters:
// - a: a pointers to felt.Felt to be hashed.
// - b: a pointers to felt.Felt to be hashed.
// Returns:
// - *felt.Felt: a pointer to a felt.Felt storing the resulting hash.
func Poseidon(a, b *felt.Felt) *felt.Felt {
return junoCrypto.Poseidon(a, b)
}

// PedersenArray is a function that takes a variadic number of felt.Felt pointers as parameters and
// calls the PedersenArray function from the junoCrypto package with the provided parameters.
// NOTE: This function just wraps the Juno implementation
Expand All @@ -590,7 +603,7 @@ func PedersenArray(felts ...*felt.Felt) *felt.Felt {
// - felts: A variadic number of pointers to felt.Felt
// Returns:
// - *felt.Felt: pointer to a felt.Felt
func (sc StarkCurve) PoseidonArray(felts ...*felt.Felt) *felt.Felt {
func PoseidonArray(felts ...*felt.Felt) *felt.Felt {
return junoCrypto.PoseidonArray(felts...)
}

Expand All @@ -603,7 +616,7 @@ func (sc StarkCurve) PoseidonArray(felts ...*felt.Felt) *felt.Felt {
// Returns:
// - *felt.Felt: pointer to a felt.Felt
// - error: An error if any
func (sc StarkCurve) StarknetKeccak(b []byte) *felt.Felt {
func StarknetKeccak(b []byte) *felt.Felt {
return junoCrypto.StarknetKeccak(b)
}

Expand Down
16 changes: 8 additions & 8 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func ClassHash(contract rpc.ContractClass) *felt.Felt {
ConstructorHash := hashEntryPointByType(contract.EntryPointsByType.Constructor)
ExternalHash := hashEntryPointByType(contract.EntryPointsByType.External)
L1HandleHash := hashEntryPointByType(contract.EntryPointsByType.L1Handler)
SierraProgamHash := curve.Curve.PoseidonArray(contract.SierraProgram...)
ABIHash := curve.Curve.StarknetKeccak([]byte(contract.ABI))
SierraProgamHash := curve.PoseidonArray(contract.SierraProgram...)
ABIHash := curve.StarknetKeccak([]byte(contract.ABI))

// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#deploy_account_hash_calculation
return curve.Curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ABIHash, SierraProgamHash)
return curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ABIHash, SierraProgamHash)
}

// hashEntryPointByType calculates the hash of an entry point by type.
Expand All @@ -83,7 +83,7 @@ func hashEntryPointByType(entryPoint []rpc.SierraEntryPoint) *felt.Felt {
for _, elt := range entryPoint {
flattened = append(flattened, elt.Selector, new(felt.Felt).SetUint64(uint64(elt.FunctionIdx)))
}
return curve.Curve.PoseidonArray(flattened...)
return curve.PoseidonArray(flattened...)
}

// CompiledClassHash calculates the hash of a compiled class in the Casm format.
Expand All @@ -97,10 +97,10 @@ func CompiledClassHash(casmClass contracts.CasmClass) *felt.Felt {
ExternalHash := hashCasmClassEntryPointByType(casmClass.EntryPointByType.External)
L1HandleHash := hashCasmClassEntryPointByType(casmClass.EntryPointByType.L1Handler)
ConstructorHash := hashCasmClassEntryPointByType(casmClass.EntryPointByType.Constructor)
ByteCodeHasH := curve.Curve.PoseidonArray(casmClass.ByteCode...)
ByteCodeHasH := curve.PoseidonArray(casmClass.ByteCode...)

// https://github.com/software-mansion/starknet.py/blob/development/starknet_py/hash/casm_class_hash.py#L10
return curve.Curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ByteCodeHasH)
return curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ByteCodeHasH)
}

// hashCasmClassEntryPointByType calculates the hash of a CasmClassEntryPoint array.
Expand All @@ -116,8 +116,8 @@ func hashCasmClassEntryPointByType(entryPoint []contracts.CasmClassEntryPoint) *
for _, builtIn := range elt.Builtins {
builtInFlat = append(builtInFlat, new(felt.Felt).SetBytes([]byte(builtIn)))
}
builtInHash := curve.Curve.PoseidonArray(builtInFlat...)
builtInHash := curve.PoseidonArray(builtInFlat...)
flattened = append(flattened, elt.Selector, new(felt.Felt).SetUint64(uint64(elt.Offset)), builtInHash)
}
return curve.Curve.PoseidonArray(flattened...)
return curve.PoseidonArray(flattened...)
}

0 comments on commit 5494ea4

Please sign in to comment.