Skip to content

Commit

Permalink
implement Keccak256 Keccak384 Keccak512 with tests (#2603)
Browse files Browse the repository at this point in the history
  • Loading branch information
collinc97 authored Sep 22, 2023
1 parent ac0e474 commit 82b39a8
Show file tree
Hide file tree
Showing 89 changed files with 3,036 additions and 19 deletions.
19 changes: 0 additions & 19 deletions compiler/ast/src/core/mod.rs

This file was deleted.

177 changes: 177 additions & 0 deletions compiler/ast/src/functions/core_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,51 @@ pub enum CoreFunction {
ChaChaRandU128,
ChaChaRandScalar,

Keccak256HashToAddress,
Keccak256HashToField,
Keccak256HashToGroup,
Keccak256HashToI8,
Keccak256HashToI16,
Keccak256HashToI32,
Keccak256HashToI64,
Keccak256HashToI128,
Keccak256HashToU8,
Keccak256HashToU16,
Keccak256HashToU32,
Keccak256HashToU64,
Keccak256HashToU128,
Keccak256HashToScalar,

Keccak384HashToAddress,
Keccak384HashToField,
Keccak384HashToGroup,
Keccak384HashToI8,
Keccak384HashToI16,
Keccak384HashToI32,
Keccak384HashToI64,
Keccak384HashToI128,
Keccak384HashToU8,
Keccak384HashToU16,
Keccak384HashToU32,
Keccak384HashToU64,
Keccak384HashToU128,
Keccak384HashToScalar,

Keccak512HashToAddress,
Keccak512HashToField,
Keccak512HashToGroup,
Keccak512HashToI8,
Keccak512HashToI16,
Keccak512HashToI32,
Keccak512HashToI64,
Keccak512HashToI128,
Keccak512HashToU8,
Keccak512HashToU16,
Keccak512HashToU32,
Keccak512HashToU64,
Keccak512HashToU128,
Keccak512HashToScalar,

Pedersen64CommitToAddress,
Pedersen64CommitToField,
Pedersen64CommitToGroup,
Expand Down Expand Up @@ -293,6 +338,51 @@ impl CoreFunction {
(sym::ChaCha, sym::rand_u64) => Self::ChaChaRandU64,
(sym::ChaCha, sym::rand_u128) => Self::ChaChaRandU128,

(sym::Keccak256, sym::hash_to_address) => Self::Keccak256HashToAddress,
(sym::Keccak256, sym::hash_to_field) => Self::Keccak256HashToField,
(sym::Keccak256, sym::hash_to_group) => Self::Keccak256HashToGroup,
(sym::Keccak256, sym::hash_to_i8) => Self::Keccak256HashToI8,
(sym::Keccak256, sym::hash_to_i16) => Self::Keccak256HashToI16,
(sym::Keccak256, sym::hash_to_i32) => Self::Keccak256HashToI32,
(sym::Keccak256, sym::hash_to_i64) => Self::Keccak256HashToI64,
(sym::Keccak256, sym::hash_to_i128) => Self::Keccak256HashToI128,
(sym::Keccak256, sym::hash_to_u8) => Self::Keccak256HashToU8,
(sym::Keccak256, sym::hash_to_u16) => Self::Keccak256HashToU16,
(sym::Keccak256, sym::hash_to_u32) => Self::Keccak256HashToU32,
(sym::Keccak256, sym::hash_to_u64) => Self::Keccak256HashToU64,
(sym::Keccak256, sym::hash_to_u128) => Self::Keccak256HashToU128,
(sym::Keccak256, sym::hash_to_scalar) => Self::Keccak256HashToScalar,

(sym::Keccak384, sym::hash_to_address) => Self::Keccak384HashToAddress,
(sym::Keccak384, sym::hash_to_field) => Self::Keccak384HashToField,
(sym::Keccak384, sym::hash_to_group) => Self::Keccak384HashToGroup,
(sym::Keccak384, sym::hash_to_i8) => Self::Keccak384HashToI8,
(sym::Keccak384, sym::hash_to_i16) => Self::Keccak384HashToI16,
(sym::Keccak384, sym::hash_to_i32) => Self::Keccak384HashToI32,
(sym::Keccak384, sym::hash_to_i64) => Self::Keccak384HashToI64,
(sym::Keccak384, sym::hash_to_i128) => Self::Keccak384HashToI128,
(sym::Keccak384, sym::hash_to_u8) => Self::Keccak384HashToU8,
(sym::Keccak384, sym::hash_to_u16) => Self::Keccak384HashToU16,
(sym::Keccak384, sym::hash_to_u32) => Self::Keccak384HashToU32,
(sym::Keccak384, sym::hash_to_u64) => Self::Keccak384HashToU64,
(sym::Keccak384, sym::hash_to_u128) => Self::Keccak384HashToU128,
(sym::Keccak384, sym::hash_to_scalar) => Self::Keccak384HashToScalar,

(sym::Keccak512, sym::hash_to_address) => Self::Keccak512HashToAddress,
(sym::Keccak512, sym::hash_to_field) => Self::Keccak512HashToField,
(sym::Keccak512, sym::hash_to_group) => Self::Keccak512HashToGroup,
(sym::Keccak512, sym::hash_to_i8) => Self::Keccak512HashToI8,
(sym::Keccak512, sym::hash_to_i16) => Self::Keccak512HashToI16,
(sym::Keccak512, sym::hash_to_i32) => Self::Keccak512HashToI32,
(sym::Keccak512, sym::hash_to_i64) => Self::Keccak512HashToI64,
(sym::Keccak512, sym::hash_to_i128) => Self::Keccak512HashToI128,
(sym::Keccak512, sym::hash_to_u8) => Self::Keccak512HashToU8,
(sym::Keccak512, sym::hash_to_u16) => Self::Keccak512HashToU16,
(sym::Keccak512, sym::hash_to_u32) => Self::Keccak512HashToU32,
(sym::Keccak512, sym::hash_to_u64) => Self::Keccak512HashToU64,
(sym::Keccak512, sym::hash_to_u128) => Self::Keccak512HashToU128,
(sym::Keccak512, sym::hash_to_scalar) => Self::Keccak512HashToScalar,

(sym::Pedersen64, sym::commit_to_address) => Self::Pedersen64CommitToAddress,
(sym::Pedersen64, sym::commit_to_field) => Self::Pedersen64CommitToField,
(sym::Pedersen64, sym::commit_to_group) => Self::Pedersen64CommitToGroup,
Expand Down Expand Up @@ -480,6 +570,51 @@ impl CoreFunction {
Self::ChaChaRandU128 => 0,
Self::ChaChaRandScalar => 0,

Self::Keccak256HashToAddress => 1,
Self::Keccak256HashToField => 1,
Self::Keccak256HashToGroup => 1,
Self::Keccak256HashToI8 => 1,
Self::Keccak256HashToI16 => 1,
Self::Keccak256HashToI32 => 1,
Self::Keccak256HashToI64 => 1,
Self::Keccak256HashToI128 => 1,
Self::Keccak256HashToU8 => 1,
Self::Keccak256HashToU16 => 1,
Self::Keccak256HashToU32 => 1,
Self::Keccak256HashToU64 => 1,
Self::Keccak256HashToU128 => 1,
Self::Keccak256HashToScalar => 1,

Self::Keccak384HashToAddress => 1,
Self::Keccak384HashToField => 1,
Self::Keccak384HashToGroup => 1,
Self::Keccak384HashToI8 => 1,
Self::Keccak384HashToI16 => 1,
Self::Keccak384HashToI32 => 1,
Self::Keccak384HashToI64 => 1,
Self::Keccak384HashToI128 => 1,
Self::Keccak384HashToU8 => 1,
Self::Keccak384HashToU16 => 1,
Self::Keccak384HashToU32 => 1,
Self::Keccak384HashToU64 => 1,
Self::Keccak384HashToU128 => 1,
Self::Keccak384HashToScalar => 1,

Self::Keccak512HashToAddress => 1,
Self::Keccak512HashToField => 1,
Self::Keccak512HashToGroup => 1,
Self::Keccak512HashToI8 => 1,
Self::Keccak512HashToI16 => 1,
Self::Keccak512HashToI32 => 1,
Self::Keccak512HashToI64 => 1,
Self::Keccak512HashToI128 => 1,
Self::Keccak512HashToU8 => 1,
Self::Keccak512HashToU16 => 1,
Self::Keccak512HashToU32 => 1,
Self::Keccak512HashToU64 => 1,
Self::Keccak512HashToU128 => 1,
Self::Keccak512HashToScalar => 1,

Self::Pedersen64CommitToAddress => 2,
Self::Pedersen64CommitToField => 2,
Self::Pedersen64CommitToGroup => 2,
Expand Down Expand Up @@ -665,6 +800,48 @@ impl CoreFunction {
| CoreFunction::BHP1024HashToU64
| CoreFunction::BHP1024HashToU128
| CoreFunction::BHP1024HashToScalar
| CoreFunction::Keccak256HashToAddress
| CoreFunction::Keccak256HashToField
| CoreFunction::Keccak256HashToGroup
| CoreFunction::Keccak256HashToI8
| CoreFunction::Keccak256HashToI16
| CoreFunction::Keccak256HashToI32
| CoreFunction::Keccak256HashToI64
| CoreFunction::Keccak256HashToI128
| CoreFunction::Keccak256HashToU8
| CoreFunction::Keccak256HashToU16
| CoreFunction::Keccak256HashToU32
| CoreFunction::Keccak256HashToU64
| CoreFunction::Keccak256HashToU128
| CoreFunction::Keccak256HashToScalar
| CoreFunction::Keccak384HashToAddress
| CoreFunction::Keccak384HashToField
| CoreFunction::Keccak384HashToGroup
| CoreFunction::Keccak384HashToI8
| CoreFunction::Keccak384HashToI16
| CoreFunction::Keccak384HashToI32
| CoreFunction::Keccak384HashToI64
| CoreFunction::Keccak384HashToI128
| CoreFunction::Keccak384HashToU8
| CoreFunction::Keccak384HashToU16
| CoreFunction::Keccak384HashToU32
| CoreFunction::Keccak384HashToU64
| CoreFunction::Keccak384HashToU128
| CoreFunction::Keccak384HashToScalar
| CoreFunction::Keccak512HashToAddress
| CoreFunction::Keccak512HashToField
| CoreFunction::Keccak512HashToGroup
| CoreFunction::Keccak512HashToI8
| CoreFunction::Keccak512HashToI16
| CoreFunction::Keccak512HashToI32
| CoreFunction::Keccak512HashToI64
| CoreFunction::Keccak512HashToI128
| CoreFunction::Keccak512HashToU8
| CoreFunction::Keccak512HashToU16
| CoreFunction::Keccak512HashToU32
| CoreFunction::Keccak512HashToU64
| CoreFunction::Keccak512HashToU128
| CoreFunction::Keccak512HashToScalar
| CoreFunction::Pedersen64CommitToAddress
| CoreFunction::Pedersen64CommitToField
| CoreFunction::Pedersen64CommitToGroup
Expand Down
9 changes: 9 additions & 0 deletions compiler/passes/src/code_generation/visit_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@ impl<'a> CodeGenerator<'a> {
Type::Identifier(Identifier { name: sym::BHP1024, .. }) => {
construct_simple_function_call(&input.name, "bhp1024", arguments)
}
Type::Identifier(Identifier { name: sym::Keccak256, .. }) => {
construct_simple_function_call(&input.name, "keccak256", arguments)
}
Type::Identifier(Identifier { name: sym::Keccak384, .. }) => {
construct_simple_function_call(&input.name, "keccak384", arguments)
}
Type::Identifier(Identifier { name: sym::Keccak512, .. }) => {
construct_simple_function_call(&input.name, "keccak512", arguments)
}
Type::Identifier(Identifier { name: sym::Pedersen64, .. }) => {
construct_simple_function_call(&input.name, "ped64", arguments)
}
Expand Down
42 changes: 42 additions & 0 deletions compiler/passes/src/type_checking/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToAddress
| CoreFunction::BHP768HashToAddress
| CoreFunction::BHP1024HashToAddress
| CoreFunction::Keccak256HashToAddress
| CoreFunction::Keccak384HashToAddress
| CoreFunction::Keccak512HashToAddress
| CoreFunction::Poseidon2HashToAddress
| CoreFunction::Poseidon4HashToAddress
| CoreFunction::Poseidon8HashToAddress => {
Expand All @@ -491,6 +494,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToField
| CoreFunction::BHP768HashToField
| CoreFunction::BHP1024HashToField
| CoreFunction::Keccak256HashToField
| CoreFunction::Keccak384HashToField
| CoreFunction::Keccak512HashToField
| CoreFunction::Poseidon2HashToField
| CoreFunction::Poseidon4HashToField
| CoreFunction::Poseidon8HashToField => {
Expand All @@ -502,6 +508,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToGroup
| CoreFunction::BHP768HashToGroup
| CoreFunction::BHP1024HashToGroup
| CoreFunction::Keccak256HashToGroup
| CoreFunction::Keccak384HashToGroup
| CoreFunction::Keccak512HashToGroup
| CoreFunction::Poseidon2HashToGroup
| CoreFunction::Poseidon4HashToGroup
| CoreFunction::Poseidon8HashToGroup => {
Expand All @@ -513,6 +522,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToI8
| CoreFunction::BHP768HashToI8
| CoreFunction::BHP1024HashToI8
| CoreFunction::Keccak256HashToI8
| CoreFunction::Keccak384HashToI8
| CoreFunction::Keccak512HashToI8
| CoreFunction::Poseidon2HashToI8
| CoreFunction::Poseidon4HashToI8
| CoreFunction::Poseidon8HashToI8 => {
Expand All @@ -524,6 +536,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToI16
| CoreFunction::BHP768HashToI16
| CoreFunction::BHP1024HashToI16
| CoreFunction::Keccak256HashToI16
| CoreFunction::Keccak384HashToI16
| CoreFunction::Keccak512HashToI16
| CoreFunction::Poseidon2HashToI16
| CoreFunction::Poseidon4HashToI16
| CoreFunction::Poseidon8HashToI16 => {
Expand All @@ -535,6 +550,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToI32
| CoreFunction::BHP768HashToI32
| CoreFunction::BHP1024HashToI32
| CoreFunction::Keccak256HashToI32
| CoreFunction::Keccak384HashToI32
| CoreFunction::Keccak512HashToI32
| CoreFunction::Poseidon2HashToI32
| CoreFunction::Poseidon4HashToI32
| CoreFunction::Poseidon8HashToI32 => {
Expand All @@ -546,6 +564,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToI64
| CoreFunction::BHP768HashToI64
| CoreFunction::BHP1024HashToI64
| CoreFunction::Keccak256HashToI64
| CoreFunction::Keccak384HashToI64
| CoreFunction::Keccak512HashToI64
| CoreFunction::Poseidon2HashToI64
| CoreFunction::Poseidon4HashToI64
| CoreFunction::Poseidon8HashToI64 => {
Expand All @@ -557,6 +578,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToI128
| CoreFunction::BHP768HashToI128
| CoreFunction::BHP1024HashToI128
| CoreFunction::Keccak256HashToI128
| CoreFunction::Keccak384HashToI128
| CoreFunction::Keccak512HashToI128
| CoreFunction::Poseidon2HashToI128
| CoreFunction::Poseidon4HashToI128
| CoreFunction::Poseidon8HashToI128 => {
Expand All @@ -568,6 +592,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToU8
| CoreFunction::BHP768HashToU8
| CoreFunction::BHP1024HashToU8
| CoreFunction::Keccak256HashToU8
| CoreFunction::Keccak384HashToU8
| CoreFunction::Keccak512HashToU8
| CoreFunction::Poseidon2HashToU8
| CoreFunction::Poseidon4HashToU8
| CoreFunction::Poseidon8HashToU8 => {
Expand All @@ -579,6 +606,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToU16
| CoreFunction::BHP768HashToU16
| CoreFunction::BHP1024HashToU16
| CoreFunction::Keccak256HashToU16
| CoreFunction::Keccak384HashToU16
| CoreFunction::Keccak512HashToU16
| CoreFunction::Poseidon2HashToU16
| CoreFunction::Poseidon4HashToU16
| CoreFunction::Poseidon8HashToU16 => {
Expand All @@ -590,6 +620,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToU32
| CoreFunction::BHP768HashToU32
| CoreFunction::BHP1024HashToU32
| CoreFunction::Keccak256HashToU32
| CoreFunction::Keccak384HashToU32
| CoreFunction::Keccak512HashToU32
| CoreFunction::Poseidon2HashToU32
| CoreFunction::Poseidon4HashToU32
| CoreFunction::Poseidon8HashToU32 => {
Expand All @@ -601,6 +634,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToU64
| CoreFunction::BHP768HashToU64
| CoreFunction::BHP1024HashToU64
| CoreFunction::Keccak256HashToU64
| CoreFunction::Keccak384HashToU64
| CoreFunction::Keccak512HashToU64
| CoreFunction::Poseidon2HashToU64
| CoreFunction::Poseidon4HashToU64
| CoreFunction::Poseidon8HashToU64 => {
Expand All @@ -612,6 +648,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToU128
| CoreFunction::BHP768HashToU128
| CoreFunction::BHP1024HashToU128
| CoreFunction::Keccak256HashToU128
| CoreFunction::Keccak384HashToU128
| CoreFunction::Keccak512HashToU128
| CoreFunction::Poseidon2HashToU128
| CoreFunction::Poseidon4HashToU128
| CoreFunction::Poseidon8HashToU128 => {
Expand All @@ -623,6 +662,9 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::BHP512HashToScalar
| CoreFunction::BHP768HashToScalar
| CoreFunction::BHP1024HashToScalar
| CoreFunction::Keccak256HashToScalar
| CoreFunction::Keccak384HashToScalar
| CoreFunction::Keccak512HashToScalar
| CoreFunction::Poseidon2HashToScalar
| CoreFunction::Poseidon4HashToScalar
| CoreFunction::Poseidon8HashToScalar => {
Expand Down
3 changes: 3 additions & 0 deletions compiler/span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ symbols! {
hash_to_u64,
hash_to_u128,
hash_to_scalar,
Keccak256,
Keccak384,
Keccak512,
Mapping,
Pedersen64,
Pedersen128,
Expand Down
Loading

0 comments on commit 82b39a8

Please sign in to comment.