Skip to content

Commit

Permalink
Feat/sha3 (#2604)
Browse files Browse the repository at this point in the history
* implement Keccak256 Keccak384 Keccak512 with tests

* implement SHA3_256 SHA3_384 SHA3_512 with tests
  • Loading branch information
collinc97 authored Sep 22, 2023
1 parent 82b39a8 commit 9c20f42
Show file tree
Hide file tree
Showing 88 changed files with 3,050 additions and 14 deletions.
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 @@ -233,6 +233,51 @@ pub enum CoreFunction {
Poseidon8HashToU128,
Poseidon8HashToScalar,

SHA3_256HashToAddress,
SHA3_256HashToField,
SHA3_256HashToGroup,
SHA3_256HashToI8,
SHA3_256HashToI16,
SHA3_256HashToI32,
SHA3_256HashToI64,
SHA3_256HashToI128,
SHA3_256HashToU8,
SHA3_256HashToU16,
SHA3_256HashToU32,
SHA3_256HashToU64,
SHA3_256HashToU128,
SHA3_256HashToScalar,

SHA3_384HashToAddress,
SHA3_384HashToField,
SHA3_384HashToGroup,
SHA3_384HashToI8,
SHA3_384HashToI16,
SHA3_384HashToI32,
SHA3_384HashToI64,
SHA3_384HashToI128,
SHA3_384HashToU8,
SHA3_384HashToU16,
SHA3_384HashToU32,
SHA3_384HashToU64,
SHA3_384HashToU128,
SHA3_384HashToScalar,

SHA3_512HashToAddress,
SHA3_512HashToField,
SHA3_512HashToGroup,
SHA3_512HashToI8,
SHA3_512HashToI16,
SHA3_512HashToI32,
SHA3_512HashToI64,
SHA3_512HashToI128,
SHA3_512HashToU8,
SHA3_512HashToU16,
SHA3_512HashToU32,
SHA3_512HashToU64,
SHA3_512HashToU128,
SHA3_512HashToScalar,

MappingGet,
MappingGetOrUse,
MappingSet,
Expand Down Expand Up @@ -464,6 +509,51 @@ impl CoreFunction {
(sym::Poseidon8, sym::hash_to_u128) => Self::Poseidon8HashToU128,
(sym::Poseidon8, sym::hash_to_scalar) => Self::Poseidon8HashToScalar,

(sym::SHA3_256, sym::hash_to_address) => Self::SHA3_256HashToAddress,
(sym::SHA3_256, sym::hash_to_field) => Self::SHA3_256HashToField,
(sym::SHA3_256, sym::hash_to_group) => Self::SHA3_256HashToGroup,
(sym::SHA3_256, sym::hash_to_i8) => Self::SHA3_256HashToI8,
(sym::SHA3_256, sym::hash_to_i16) => Self::SHA3_256HashToI16,
(sym::SHA3_256, sym::hash_to_i32) => Self::SHA3_256HashToI32,
(sym::SHA3_256, sym::hash_to_i64) => Self::SHA3_256HashToI64,
(sym::SHA3_256, sym::hash_to_i128) => Self::SHA3_256HashToI128,
(sym::SHA3_256, sym::hash_to_u8) => Self::SHA3_256HashToU8,
(sym::SHA3_256, sym::hash_to_u16) => Self::SHA3_256HashToU16,
(sym::SHA3_256, sym::hash_to_u32) => Self::SHA3_256HashToU32,
(sym::SHA3_256, sym::hash_to_u64) => Self::SHA3_256HashToU64,
(sym::SHA3_256, sym::hash_to_u128) => Self::SHA3_256HashToU128,
(sym::SHA3_256, sym::hash_to_scalar) => Self::SHA3_256HashToScalar,

(sym::SHA3_384, sym::hash_to_address) => Self::SHA3_384HashToAddress,
(sym::SHA3_384, sym::hash_to_field) => Self::SHA3_384HashToField,
(sym::SHA3_384, sym::hash_to_group) => Self::SHA3_384HashToGroup,
(sym::SHA3_384, sym::hash_to_i8) => Self::SHA3_384HashToI8,
(sym::SHA3_384, sym::hash_to_i16) => Self::SHA3_384HashToI16,
(sym::SHA3_384, sym::hash_to_i32) => Self::SHA3_384HashToI32,
(sym::SHA3_384, sym::hash_to_i64) => Self::SHA3_384HashToI64,
(sym::SHA3_384, sym::hash_to_i128) => Self::SHA3_384HashToI128,
(sym::SHA3_384, sym::hash_to_u8) => Self::SHA3_384HashToU8,
(sym::SHA3_384, sym::hash_to_u16) => Self::SHA3_384HashToU16,
(sym::SHA3_384, sym::hash_to_u32) => Self::SHA3_384HashToU32,
(sym::SHA3_384, sym::hash_to_u64) => Self::SHA3_384HashToU64,
(sym::SHA3_384, sym::hash_to_u128) => Self::SHA3_384HashToU128,
(sym::SHA3_384, sym::hash_to_scalar) => Self::SHA3_384HashToScalar,

(sym::SHA3_512, sym::hash_to_address) => Self::SHA3_512HashToAddress,
(sym::SHA3_512, sym::hash_to_field) => Self::SHA3_512HashToField,
(sym::SHA3_512, sym::hash_to_group) => Self::SHA3_512HashToGroup,
(sym::SHA3_512, sym::hash_to_i8) => Self::SHA3_512HashToI8,
(sym::SHA3_512, sym::hash_to_i16) => Self::SHA3_512HashToI16,
(sym::SHA3_512, sym::hash_to_i32) => Self::SHA3_512HashToI32,
(sym::SHA3_512, sym::hash_to_i64) => Self::SHA3_512HashToI64,
(sym::SHA3_512, sym::hash_to_i128) => Self::SHA3_512HashToI128,
(sym::SHA3_512, sym::hash_to_u8) => Self::SHA3_512HashToU8,
(sym::SHA3_512, sym::hash_to_u16) => Self::SHA3_512HashToU16,
(sym::SHA3_512, sym::hash_to_u32) => Self::SHA3_512HashToU32,
(sym::SHA3_512, sym::hash_to_u64) => Self::SHA3_512HashToU64,
(sym::SHA3_512, sym::hash_to_u128) => Self::SHA3_512HashToU128,
(sym::SHA3_512, sym::hash_to_scalar) => Self::SHA3_512HashToScalar,

(sym::Mapping, sym::get) => Self::MappingGet,
(sym::Mapping, sym::get_or_use) => Self::MappingGetOrUse,
(sym::Mapping, sym::set) => Self::MappingSet,
Expand Down Expand Up @@ -696,6 +786,51 @@ impl CoreFunction {
Self::Poseidon8HashToU128 => 1,
Self::Poseidon8HashToScalar => 1,

Self::SHA3_256HashToAddress => 1,
Self::SHA3_256HashToField => 1,
Self::SHA3_256HashToGroup => 1,
Self::SHA3_256HashToI8 => 1,
Self::SHA3_256HashToI16 => 1,
Self::SHA3_256HashToI32 => 1,
Self::SHA3_256HashToI64 => 1,
Self::SHA3_256HashToI128 => 1,
Self::SHA3_256HashToU8 => 1,
Self::SHA3_256HashToU16 => 1,
Self::SHA3_256HashToU32 => 1,
Self::SHA3_256HashToU64 => 1,
Self::SHA3_256HashToU128 => 1,
Self::SHA3_256HashToScalar => 1,

Self::SHA3_384HashToAddress => 1,
Self::SHA3_384HashToField => 1,
Self::SHA3_384HashToGroup => 1,
Self::SHA3_384HashToI8 => 1,
Self::SHA3_384HashToI16 => 1,
Self::SHA3_384HashToI32 => 1,
Self::SHA3_384HashToI64 => 1,
Self::SHA3_384HashToI128 => 1,
Self::SHA3_384HashToU8 => 1,
Self::SHA3_384HashToU16 => 1,
Self::SHA3_384HashToU32 => 1,
Self::SHA3_384HashToU64 => 1,
Self::SHA3_384HashToU128 => 1,
Self::SHA3_384HashToScalar => 1,

Self::SHA3_512HashToAddress => 1,
Self::SHA3_512HashToField => 1,
Self::SHA3_512HashToGroup => 1,
Self::SHA3_512HashToI8 => 1,
Self::SHA3_512HashToI16 => 1,
Self::SHA3_512HashToI32 => 1,
Self::SHA3_512HashToI64 => 1,
Self::SHA3_512HashToI128 => 1,
Self::SHA3_512HashToU8 => 1,
Self::SHA3_512HashToU16 => 1,
Self::SHA3_512HashToU32 => 1,
Self::SHA3_512HashToU64 => 1,
Self::SHA3_512HashToU128 => 1,
Self::SHA3_512HashToScalar => 1,

Self::MappingGet => 2,
Self::MappingGetOrUse => 3,
Self::MappingSet => 3,
Expand Down Expand Up @@ -918,6 +1053,48 @@ impl CoreFunction {
| CoreFunction::Poseidon8HashToU64
| CoreFunction::Poseidon8HashToU128
| CoreFunction::Poseidon8HashToScalar
| CoreFunction::SHA3_256HashToAddress
| CoreFunction::SHA3_256HashToField
| CoreFunction::SHA3_256HashToGroup
| CoreFunction::SHA3_256HashToI8
| CoreFunction::SHA3_256HashToI16
| CoreFunction::SHA3_256HashToI32
| CoreFunction::SHA3_256HashToI64
| CoreFunction::SHA3_256HashToI128
| CoreFunction::SHA3_256HashToU8
| CoreFunction::SHA3_256HashToU16
| CoreFunction::SHA3_256HashToU32
| CoreFunction::SHA3_256HashToU64
| CoreFunction::SHA3_256HashToU128
| CoreFunction::SHA3_256HashToScalar
| CoreFunction::SHA3_384HashToAddress
| CoreFunction::SHA3_384HashToField
| CoreFunction::SHA3_384HashToGroup
| CoreFunction::SHA3_384HashToI8
| CoreFunction::SHA3_384HashToI16
| CoreFunction::SHA3_384HashToI32
| CoreFunction::SHA3_384HashToI64
| CoreFunction::SHA3_384HashToI128
| CoreFunction::SHA3_384HashToU8
| CoreFunction::SHA3_384HashToU16
| CoreFunction::SHA3_384HashToU32
| CoreFunction::SHA3_384HashToU64
| CoreFunction::SHA3_384HashToU128
| CoreFunction::SHA3_384HashToScalar
| CoreFunction::SHA3_512HashToAddress
| CoreFunction::SHA3_512HashToField
| CoreFunction::SHA3_512HashToGroup
| CoreFunction::SHA3_512HashToI8
| CoreFunction::SHA3_512HashToI16
| CoreFunction::SHA3_512HashToI32
| CoreFunction::SHA3_512HashToI64
| CoreFunction::SHA3_512HashToI128
| CoreFunction::SHA3_512HashToU8
| CoreFunction::SHA3_512HashToU16
| CoreFunction::SHA3_512HashToU32
| CoreFunction::SHA3_512HashToU64
| CoreFunction::SHA3_512HashToU128
| CoreFunction::SHA3_512HashToScalar
| CoreFunction::GroupToXCoordinate
| CoreFunction::GroupToYCoordinate
| CoreFunction::SignatureVerify => false,
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 @@ -339,6 +339,15 @@ impl<'a> CodeGenerator<'a> {
Type::Identifier(Identifier { name: sym::Poseidon8, .. }) => {
construct_simple_function_call(&input.name, "psd8", arguments)
}
Type::Identifier(Identifier { name: sym::SHA3_256, .. }) => {
construct_simple_function_call(&input.name, "sha3_256", arguments)
}
Type::Identifier(Identifier { name: sym::SHA3_384, .. }) => {
construct_simple_function_call(&input.name, "sha3_384", arguments)
}
Type::Identifier(Identifier { name: sym::SHA3_512, .. }) => {
construct_simple_function_call(&input.name, "sha3_512", arguments)
}
Type::Identifier(Identifier { name: sym::Mapping, .. }) => match input.name.name {
sym::get => {
let mut instruction = " get".to_string();
Expand Down
70 changes: 56 additions & 14 deletions compiler/passes/src/type_checking/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToAddress
| CoreFunction::Poseidon2HashToAddress
| CoreFunction::Poseidon4HashToAddress
| CoreFunction::Poseidon8HashToAddress => {
| CoreFunction::Poseidon8HashToAddress
| CoreFunction::SHA3_256HashToAddress
| CoreFunction::SHA3_384HashToAddress
| CoreFunction::SHA3_512HashToAddress => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Address)
Expand All @@ -499,7 +502,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToField
| CoreFunction::Poseidon2HashToField
| CoreFunction::Poseidon4HashToField
| CoreFunction::Poseidon8HashToField => {
| CoreFunction::Poseidon8HashToField
| CoreFunction::SHA3_256HashToField
| CoreFunction::SHA3_384HashToField
| CoreFunction::SHA3_512HashToField => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Field)
Expand All @@ -513,7 +519,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToGroup
| CoreFunction::Poseidon2HashToGroup
| CoreFunction::Poseidon4HashToGroup
| CoreFunction::Poseidon8HashToGroup => {
| CoreFunction::Poseidon8HashToGroup
| CoreFunction::SHA3_256HashToGroup
| CoreFunction::SHA3_384HashToGroup
| CoreFunction::SHA3_512HashToGroup => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Group)
Expand All @@ -527,7 +536,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToI8
| CoreFunction::Poseidon2HashToI8
| CoreFunction::Poseidon4HashToI8
| CoreFunction::Poseidon8HashToI8 => {
| CoreFunction::Poseidon8HashToI8
| CoreFunction::SHA3_256HashToI8
| CoreFunction::SHA3_384HashToI8
| CoreFunction::SHA3_512HashToI8 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::I8))
Expand All @@ -541,7 +553,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToI16
| CoreFunction::Poseidon2HashToI16
| CoreFunction::Poseidon4HashToI16
| CoreFunction::Poseidon8HashToI16 => {
| CoreFunction::Poseidon8HashToI16
| CoreFunction::SHA3_256HashToI16
| CoreFunction::SHA3_384HashToI16
| CoreFunction::SHA3_512HashToI16 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::I16))
Expand All @@ -555,7 +570,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToI32
| CoreFunction::Poseidon2HashToI32
| CoreFunction::Poseidon4HashToI32
| CoreFunction::Poseidon8HashToI32 => {
| CoreFunction::Poseidon8HashToI32
| CoreFunction::SHA3_256HashToI32
| CoreFunction::SHA3_384HashToI32
| CoreFunction::SHA3_512HashToI32 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::I32))
Expand All @@ -569,7 +587,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToI64
| CoreFunction::Poseidon2HashToI64
| CoreFunction::Poseidon4HashToI64
| CoreFunction::Poseidon8HashToI64 => {
| CoreFunction::Poseidon8HashToI64
| CoreFunction::SHA3_256HashToI64
| CoreFunction::SHA3_384HashToI64
| CoreFunction::SHA3_512HashToI64 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::I64))
Expand All @@ -583,7 +604,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToI128
| CoreFunction::Poseidon2HashToI128
| CoreFunction::Poseidon4HashToI128
| CoreFunction::Poseidon8HashToI128 => {
| CoreFunction::Poseidon8HashToI128
| CoreFunction::SHA3_256HashToI128
| CoreFunction::SHA3_384HashToI128
| CoreFunction::SHA3_512HashToI128 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::I128))
Expand All @@ -597,7 +621,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToU8
| CoreFunction::Poseidon2HashToU8
| CoreFunction::Poseidon4HashToU8
| CoreFunction::Poseidon8HashToU8 => {
| CoreFunction::Poseidon8HashToU8
| CoreFunction::SHA3_256HashToU8
| CoreFunction::SHA3_384HashToU8
| CoreFunction::SHA3_512HashToU8 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::U8))
Expand All @@ -611,7 +638,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToU16
| CoreFunction::Poseidon2HashToU16
| CoreFunction::Poseidon4HashToU16
| CoreFunction::Poseidon8HashToU16 => {
| CoreFunction::Poseidon8HashToU16
| CoreFunction::SHA3_256HashToU16
| CoreFunction::SHA3_384HashToU16
| CoreFunction::SHA3_512HashToU16 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::U16))
Expand All @@ -625,7 +655,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToU32
| CoreFunction::Poseidon2HashToU32
| CoreFunction::Poseidon4HashToU32
| CoreFunction::Poseidon8HashToU32 => {
| CoreFunction::Poseidon8HashToU32
| CoreFunction::SHA3_256HashToU32
| CoreFunction::SHA3_384HashToU32
| CoreFunction::SHA3_512HashToU32 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::U32))
Expand All @@ -639,7 +672,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToU64
| CoreFunction::Poseidon2HashToU64
| CoreFunction::Poseidon4HashToU64
| CoreFunction::Poseidon8HashToU64 => {
| CoreFunction::Poseidon8HashToU64
| CoreFunction::SHA3_256HashToU64
| CoreFunction::SHA3_384HashToU64
| CoreFunction::SHA3_512HashToU64 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::U64))
Expand All @@ -653,7 +689,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToU128
| CoreFunction::Poseidon2HashToU128
| CoreFunction::Poseidon4HashToU128
| CoreFunction::Poseidon8HashToU128 => {
| CoreFunction::Poseidon8HashToU128
| CoreFunction::SHA3_256HashToU128
| CoreFunction::SHA3_384HashToU128
| CoreFunction::SHA3_512HashToU128 => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Integer(IntegerType::U128))
Expand All @@ -667,7 +706,10 @@ impl<'a> TypeChecker<'a> {
| CoreFunction::Keccak512HashToScalar
| CoreFunction::Poseidon2HashToScalar
| CoreFunction::Poseidon4HashToScalar
| CoreFunction::Poseidon8HashToScalar => {
| CoreFunction::Poseidon8HashToScalar
| CoreFunction::SHA3_256HashToScalar
| CoreFunction::SHA3_384HashToScalar
| CoreFunction::SHA3_512HashToScalar => {
// Check that the first argument is not a mapping, tuple, err, or unit type.
check_not_mapping_tuple_err_unit(&arguments[0].0, &arguments[0].1);
Some(Type::Scalar)
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 @@ -196,6 +196,9 @@ symbols! {
rand_u128,
remove,
set,
SHA3_256,
SHA3_384,
SHA3_512,
to_x_coordinate,
to_y_coordinate,
verify,
Expand Down
Loading

0 comments on commit 9c20f42

Please sign in to comment.