mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 23:23:50 +03:00
Add CoreFunction to AST
This commit is contained in:
parent
1c05df3529
commit
0bc04791b6
88
compiler/ast/src/functions/core_function.rs
Normal file
88
compiler/ast/src/functions/core_function.rs
Normal file
@ -0,0 +1,88 @@
|
||||
// Copyright (C) 2019-2023 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use leo_span::{sym, Symbol};
|
||||
|
||||
/// A core instruction that maps directly to an AVM bytecode instruction.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum CoreFunction {
|
||||
BHP256Commit,
|
||||
BHP256Hash,
|
||||
BHP512Commit,
|
||||
BHP512Hash,
|
||||
BHP768Commit,
|
||||
BHP768Hash,
|
||||
BHP1024Commit,
|
||||
BHP1024Hash,
|
||||
|
||||
Pedersen64Commit,
|
||||
Pedersen64Hash,
|
||||
Pedersen128Commit,
|
||||
Pedersen128Hash,
|
||||
|
||||
Poseidon2Hash,
|
||||
Poseidon4Hash,
|
||||
Poseidon8Hash,
|
||||
}
|
||||
|
||||
impl CoreFunction {
|
||||
/// Returns a `CoreFunction` from the given module and method symbols.
|
||||
pub fn from_symbols(module: Symbol, function: Symbol) -> Option<Self> {
|
||||
Some(match (module, function) {
|
||||
(sym::BHP256, sym::commit) => Self::BHP256Commit,
|
||||
(sym::BHP256, sym::hash) => Self::BHP256Hash,
|
||||
(sym::BHP512, sym::commit) => Self::BHP512Commit,
|
||||
(sym::BHP512, sym::hash) => Self::BHP512Hash,
|
||||
(sym::BHP768, sym::commit) => Self::BHP768Commit,
|
||||
(sym::BHP768, sym::hash) => Self::BHP768Hash,
|
||||
(sym::BHP1024, sym::commit) => Self::BHP1024Commit,
|
||||
(sym::BHP1024, sym::hash) => Self::BHP1024Hash,
|
||||
|
||||
(sym::Pedersen64, sym::commit) => Self::Pedersen64Commit,
|
||||
(sym::Pedersen64, sym::hash) => Self::Pedersen64Hash,
|
||||
(sym::Pedersen128, sym::commit) => Self::Pedersen128Commit,
|
||||
(sym::Pedersen128, sym::hash) => Self::Pedersen128Hash,
|
||||
|
||||
(sym::Poseidon2, sym::hash) => Self::Poseidon2Hash,
|
||||
(sym::Poseidon4, sym::hash) => Self::Poseidon4Hash,
|
||||
(sym::Poseidon8, sym::hash) => Self::Poseidon8Hash,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the number of arguments required by the instruction.
|
||||
pub fn num_args(&self) -> usize {
|
||||
match self {
|
||||
Self::BHP256Commit => 2,
|
||||
Self::BHP256Hash => 1,
|
||||
Self::BHP512Commit => 2,
|
||||
Self::BHP512Hash => 1,
|
||||
Self::BHP768Commit => 2,
|
||||
Self::BHP768Hash => 1,
|
||||
Self::BHP1024Commit => 2,
|
||||
Self::BHP1024Hash => 1,
|
||||
|
||||
Self::Pedersen64Commit => 2,
|
||||
Self::Pedersen64Hash => 1,
|
||||
Self::Pedersen128Commit => 2,
|
||||
Self::Pedersen128Hash => 1,
|
||||
|
||||
Self::Poseidon2Hash => 1,
|
||||
Self::Poseidon4Hash => 1,
|
||||
Self::Poseidon8Hash => 1,
|
||||
}
|
||||
}
|
||||
}
|
@ -17,6 +17,9 @@
|
||||
pub mod annotation;
|
||||
pub use annotation::*;
|
||||
|
||||
pub mod core_function;
|
||||
pub use core_function::*;
|
||||
|
||||
pub mod variant;
|
||||
pub use variant::*;
|
||||
|
||||
|
@ -20,10 +20,6 @@
|
||||
//! The [`Ast`] type is intended to be parsed and modified by different passes
|
||||
//! of the Leo compiler. The Leo compiler can generate a set of R1CS constraints from any [`Ast`].
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
#![doc = include_str!("../README.md")]
|
||||
extern crate core;
|
||||
|
||||
pub mod access;
|
||||
pub use self::access::*;
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
[package]
|
||||
name = "leo-core"
|
||||
version = "1.6.3"
|
||||
version = "1.5.3"
|
||||
authors = [ "The Aleo Team <hello@aleo.org>" ]
|
||||
description = "Core library functions for the Leo programming language"
|
||||
description = "The Leo programming language"
|
||||
homepage = "https://aleo.org"
|
||||
repository = "https://github.com/AleoHQ/leo"
|
||||
keywords = [
|
||||
@ -12,23 +12,22 @@ keywords = [
|
||||
"programming-language",
|
||||
"zero-knowledge"
|
||||
]
|
||||
categories = [ "compilers", "cryptography", "web-programming" ]
|
||||
categories = [ "cryptography::cryptocurrencies", "web-programming" ]
|
||||
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ]
|
||||
license = "GPL-3.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.65"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies.leo-ast]
|
||||
path = "../ast"
|
||||
version = "1.6.3"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-errors]
|
||||
path = "../../errors"
|
||||
version = "1.6.3"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-span]
|
||||
path = "../span"
|
||||
version = "1.6.3"
|
||||
version = "1.5.3"
|
@ -1,146 +0,0 @@
|
||||
// Copyright (C) 2019-2023 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::algorithms::CoreFunction;
|
||||
use leo_ast::Type;
|
||||
|
||||
pub struct BHP256Hash;
|
||||
|
||||
impl CoreFunction for BHP256Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BHP256Commit;
|
||||
|
||||
impl CoreFunction for BHP256Commit {
|
||||
const NUM_ARGS: usize = 2;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn second_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Scalar)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BHP512Hash;
|
||||
|
||||
impl CoreFunction for BHP512Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BHP512Commit;
|
||||
|
||||
impl CoreFunction for BHP512Commit {
|
||||
const NUM_ARGS: usize = 2;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn second_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Scalar)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BHP768Hash;
|
||||
|
||||
impl CoreFunction for BHP768Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BHP768Commit;
|
||||
|
||||
impl CoreFunction for BHP768Commit {
|
||||
const NUM_ARGS: usize = 2;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn second_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Scalar)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BHP1024Hash;
|
||||
|
||||
impl CoreFunction for BHP1024Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BHP1024Commit;
|
||||
|
||||
impl CoreFunction for BHP1024Commit {
|
||||
const NUM_ARGS: usize = 2;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn second_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Scalar)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
@ -1,181 +0,0 @@
|
||||
// Copyright (C) 2019-2023 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
mod bhp;
|
||||
pub use bhp::*;
|
||||
|
||||
mod pedersen;
|
||||
pub use pedersen::*;
|
||||
|
||||
mod poseidon;
|
||||
pub use poseidon::*;
|
||||
|
||||
use leo_ast::Type;
|
||||
use leo_span::{sym, Symbol};
|
||||
|
||||
/// A core instruction that maps directly to an AVM bytecode instruction.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum CoreInstruction {
|
||||
BHP256Commit,
|
||||
BHP256Hash,
|
||||
BHP512Commit,
|
||||
BHP512Hash,
|
||||
BHP768Commit,
|
||||
BHP768Hash,
|
||||
BHP1024Commit,
|
||||
BHP1024Hash,
|
||||
|
||||
Pedersen64Commit,
|
||||
Pedersen64Hash,
|
||||
Pedersen128Commit,
|
||||
Pedersen128Hash,
|
||||
|
||||
Poseidon2Hash,
|
||||
Poseidon4Hash,
|
||||
Poseidon8Hash,
|
||||
}
|
||||
|
||||
impl CoreInstruction {
|
||||
/// Returns a `CoreInstruction` from the given module and method symbols.
|
||||
pub fn from_symbols(module: Symbol, function: Symbol) -> Option<Self> {
|
||||
Some(match (module, function) {
|
||||
(sym::BHP256, sym::commit) => Self::BHP256Commit,
|
||||
(sym::BHP256, sym::hash) => Self::BHP256Hash,
|
||||
(sym::BHP512, sym::commit) => Self::BHP512Commit,
|
||||
(sym::BHP512, sym::hash) => Self::BHP512Hash,
|
||||
(sym::BHP768, sym::commit) => Self::BHP768Commit,
|
||||
(sym::BHP768, sym::hash) => Self::BHP768Hash,
|
||||
(sym::BHP1024, sym::commit) => Self::BHP1024Commit,
|
||||
(sym::BHP1024, sym::hash) => Self::BHP1024Hash,
|
||||
|
||||
(sym::Pedersen64, sym::commit) => Self::Pedersen64Commit,
|
||||
(sym::Pedersen64, sym::hash) => Self::Pedersen64Hash,
|
||||
(sym::Pedersen128, sym::commit) => Self::Pedersen128Commit,
|
||||
(sym::Pedersen128, sym::hash) => Self::Pedersen128Hash,
|
||||
|
||||
(sym::Poseidon2, sym::hash) => Self::Poseidon2Hash,
|
||||
(sym::Poseidon4, sym::hash) => Self::Poseidon4Hash,
|
||||
(sym::Poseidon8, sym::hash) => Self::Poseidon8Hash,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the number of arguments required by the instruction.
|
||||
pub fn num_args(&self) -> usize {
|
||||
match self {
|
||||
Self::BHP256Commit => BHP256Commit::NUM_ARGS,
|
||||
Self::BHP256Hash => BHP256Hash::NUM_ARGS,
|
||||
Self::BHP512Commit => BHP512Commit::NUM_ARGS,
|
||||
Self::BHP512Hash => BHP512Hash::NUM_ARGS,
|
||||
Self::BHP768Commit => BHP768Commit::NUM_ARGS,
|
||||
Self::BHP768Hash => BHP768Hash::NUM_ARGS,
|
||||
Self::BHP1024Commit => BHP1024Commit::NUM_ARGS,
|
||||
Self::BHP1024Hash => BHP1024Hash::NUM_ARGS,
|
||||
|
||||
Self::Pedersen64Commit => Pedersen64Commit::NUM_ARGS,
|
||||
Self::Pedersen64Hash => Pedersen64Hash::NUM_ARGS,
|
||||
Self::Pedersen128Commit => Pedersen128Commit::NUM_ARGS,
|
||||
Self::Pedersen128Hash => Pedersen128Hash::NUM_ARGS,
|
||||
|
||||
Self::Poseidon2Hash => Poseidon2Hash::NUM_ARGS,
|
||||
Self::Poseidon4Hash => Poseidon4Hash::NUM_ARGS,
|
||||
Self::Poseidon8Hash => Poseidon8Hash::NUM_ARGS,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether or not the first argument is an allowed type.
|
||||
pub fn first_arg_is_allowed_type(&self, type_: &Type) -> bool {
|
||||
match self {
|
||||
CoreInstruction::BHP256Commit => BHP256Commit::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP256Hash => BHP256Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP512Commit => BHP512Commit::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP512Hash => BHP512Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP768Commit => BHP768Commit::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP768Hash => BHP768Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP1024Commit => BHP1024Commit::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP1024Hash => BHP1024Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen64Commit => Pedersen64Commit::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen64Hash => Pedersen64Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen128Commit => Pedersen128Commit::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen128Hash => Pedersen128Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Poseidon2Hash => Poseidon2Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Poseidon4Hash => Poseidon4Hash::first_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Poseidon8Hash => Poseidon8Hash::first_arg_is_allowed_type(type_),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether or not the second argument is an allowed type.
|
||||
pub fn second_arg_is_allowed_type(&self, type_: &Type) -> bool {
|
||||
match self {
|
||||
CoreInstruction::BHP256Commit => BHP256Commit::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP256Hash => BHP256Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP512Commit => BHP512Commit::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP512Hash => BHP512Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP768Commit => BHP768Commit::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP768Hash => BHP768Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP1024Commit => BHP1024Commit::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::BHP1024Hash => BHP1024Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen64Commit => Pedersen64Commit::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen64Hash => Pedersen64Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen128Commit => Pedersen128Commit::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Pedersen128Hash => Pedersen128Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Poseidon2Hash => Poseidon2Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Poseidon4Hash => Poseidon4Hash::second_arg_is_allowed_type(type_),
|
||||
CoreInstruction::Poseidon8Hash => Poseidon8Hash::second_arg_is_allowed_type(type_),
|
||||
}
|
||||
}
|
||||
|
||||
/// The type of the instruction output.
|
||||
pub fn return_type(&self) -> Type {
|
||||
match self {
|
||||
Self::BHP256Commit => BHP256Commit::return_type(),
|
||||
Self::BHP256Hash => BHP256Hash::return_type(),
|
||||
Self::BHP512Commit => BHP512Commit::return_type(),
|
||||
Self::BHP512Hash => BHP512Hash::return_type(),
|
||||
Self::BHP768Commit => BHP768Commit::return_type(),
|
||||
Self::BHP768Hash => BHP768Hash::return_type(),
|
||||
Self::BHP1024Commit => BHP1024Commit::return_type(),
|
||||
Self::BHP1024Hash => BHP1024Hash::return_type(),
|
||||
|
||||
Self::Pedersen64Commit => Pedersen64Commit::return_type(),
|
||||
Self::Pedersen64Hash => Pedersen64Hash::return_type(),
|
||||
Self::Pedersen128Commit => Pedersen128Commit::return_type(),
|
||||
Self::Pedersen128Hash => Pedersen128Hash::return_type(),
|
||||
|
||||
Self::Poseidon2Hash => Poseidon2Hash::return_type(),
|
||||
Self::Poseidon4Hash => Poseidon4Hash::return_type(),
|
||||
Self::Poseidon8Hash => Poseidon8Hash::return_type(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A core function of a core struct, e.g. `hash` or `commit`
|
||||
/// Provides required type information to the type checker.
|
||||
trait CoreFunction {
|
||||
const NUM_ARGS: usize;
|
||||
|
||||
/// Returns whether or not the first argument is an allowed type.
|
||||
fn first_arg_is_allowed_type(_: &Type) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns whether or not the second argument is an allowed type.
|
||||
fn second_arg_is_allowed_type(_: &Type) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// The return type of the core function.
|
||||
fn return_type() -> Type;
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
// Copyright (C) 2019-2023 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::algorithms::CoreFunction;
|
||||
use leo_ast::{IntegerType, Type};
|
||||
|
||||
pub struct Pedersen64Hash;
|
||||
|
||||
impl CoreFunction for Pedersen64Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(
|
||||
type_,
|
||||
Type::Boolean
|
||||
| Type::Integer(IntegerType::I8)
|
||||
| Type::Integer(IntegerType::I16)
|
||||
| Type::Integer(IntegerType::I32)
|
||||
| Type::Integer(IntegerType::I64)
|
||||
| Type::Integer(IntegerType::U8)
|
||||
| Type::Integer(IntegerType::U16)
|
||||
| Type::Integer(IntegerType::U32)
|
||||
| Type::Integer(IntegerType::U64)
|
||||
| Type::String
|
||||
)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Pedersen64Commit;
|
||||
|
||||
impl CoreFunction for Pedersen64Commit {
|
||||
const NUM_ARGS: usize = 2;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(
|
||||
type_,
|
||||
Type::Boolean
|
||||
| Type::Integer(IntegerType::I8)
|
||||
| Type::Integer(IntegerType::I16)
|
||||
| Type::Integer(IntegerType::I32)
|
||||
| Type::Integer(IntegerType::I64)
|
||||
| Type::Integer(IntegerType::U8)
|
||||
| Type::Integer(IntegerType::U16)
|
||||
| Type::Integer(IntegerType::U32)
|
||||
| Type::Integer(IntegerType::U64)
|
||||
| Type::String
|
||||
)
|
||||
}
|
||||
|
||||
fn second_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Scalar)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Group
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Pedersen128Hash;
|
||||
|
||||
impl CoreFunction for Pedersen128Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Boolean | Type::Integer(_) | Type::String)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Pedersen128Commit;
|
||||
|
||||
impl CoreFunction for Pedersen128Commit {
|
||||
const NUM_ARGS: usize = 2;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Boolean | Type::Integer(_) | Type::String)
|
||||
}
|
||||
|
||||
fn second_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
matches!(type_, Type::Scalar)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Group
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
// Copyright (C) 2019-2023 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::algorithms::CoreFunction;
|
||||
use leo_ast::Type;
|
||||
|
||||
pub struct Poseidon2Hash;
|
||||
|
||||
impl CoreFunction for Poseidon2Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Poseidon4Hash;
|
||||
|
||||
impl CoreFunction for Poseidon4Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Poseidon8Hash;
|
||||
|
||||
impl CoreFunction for Poseidon8Hash {
|
||||
const NUM_ARGS: usize = 1;
|
||||
|
||||
fn first_arg_is_allowed_type(type_: &Type) -> bool {
|
||||
!matches!(type_, Type::Mapping(_) | Type::Tuple(_) | Type::Err | Type::Unit)
|
||||
}
|
||||
|
||||
fn return_type() -> Type {
|
||||
Type::Field
|
||||
}
|
||||
}
|
@ -19,3 +19,86 @@
|
||||
|
||||
mod algorithms;
|
||||
pub use algorithms::*;
|
||||
|
||||
mod utilities;
|
||||
pub use utilities::*;
|
||||
|
||||
use leo_ast::Type;
|
||||
use leo_span::{sym, Symbol};
|
||||
|
||||
/// A core instruction that maps directly to an AVM bytecode instruction.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum CoreInstruction {
|
||||
BHP256Commit,
|
||||
BHP256Hash,
|
||||
BHP512Commit,
|
||||
BHP512Hash,
|
||||
BHP768Commit,
|
||||
BHP768Hash,
|
||||
BHP1024Commit,
|
||||
BHP1024Hash,
|
||||
|
||||
Pedersen64Commit,
|
||||
Pedersen64Hash,
|
||||
Pedersen128Commit,
|
||||
Pedersen128Hash,
|
||||
|
||||
Poseidon2Hash,
|
||||
Poseidon4Hash,
|
||||
Poseidon8Hash,
|
||||
}
|
||||
|
||||
impl CoreInstruction {
|
||||
/// Returns a `CoreInstruction` from the given module and method symbols.
|
||||
pub fn from_symbols(module: Symbol, function: Symbol) -> Option<Self> {
|
||||
Some(match (module, function) {
|
||||
(sym::BHP256, sym::commit) => Self::BHP256Commit,
|
||||
(sym::BHP256, sym::hash) => Self::BHP256Hash,
|
||||
(sym::BHP512, sym::commit) => Self::BHP512Commit,
|
||||
(sym::BHP512, sym::hash) => Self::BHP512Hash,
|
||||
(sym::BHP768, sym::commit) => Self::BHP768Commit,
|
||||
(sym::BHP768, sym::hash) => Self::BHP768Hash,
|
||||
(sym::BHP1024, sym::commit) => Self::BHP1024Commit,
|
||||
(sym::BHP1024, sym::hash) => Self::BHP1024Hash,
|
||||
|
||||
(sym::Pedersen64, sym::commit) => Self::Pedersen64Commit,
|
||||
(sym::Pedersen64, sym::hash) => Self::Pedersen64Hash,
|
||||
(sym::Pedersen128, sym::commit) => Self::Pedersen128Commit,
|
||||
(sym::Pedersen128, sym::hash) => Self::Pedersen128Hash,
|
||||
|
||||
(sym::Poseidon2, sym::hash) => Self::Poseidon2Hash,
|
||||
(sym::Poseidon4, sym::hash) => Self::Poseidon4Hash,
|
||||
(sym::Poseidon8, sym::hash) => Self::Poseidon8Hash,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the number of arguments required by the instruction.
|
||||
pub fn num_args(&self) -> usize {
|
||||
match self {
|
||||
Self::BHP256Commit => BHP256Commit::NUM_ARGS,
|
||||
Self::BHP256Hash => BHP256Hash::NUM_ARGS,
|
||||
Self::BHP512Commit => BHP512Commit::NUM_ARGS,
|
||||
Self::BHP512Hash => BHP512Hash::NUM_ARGS,
|
||||
Self::BHP768Commit => BHP768Commit::NUM_ARGS,
|
||||
Self::BHP768Hash => BHP768Hash::NUM_ARGS,
|
||||
Self::BHP1024Commit => BHP1024Commit::NUM_ARGS,
|
||||
Self::BHP1024Hash => BHP1024Hash::NUM_ARGS,
|
||||
|
||||
Self::Pedersen64Commit => Pedersen64Commit::NUM_ARGS,
|
||||
Self::Pedersen64Hash => Pedersen64Hash::NUM_ARGS,
|
||||
Self::Pedersen128Commit => Pedersen128Commit::NUM_ARGS,
|
||||
Self::Pedersen128Hash => Pedersen128Hash::NUM_ARGS,
|
||||
|
||||
Self::Poseidon2Hash => Poseidon2Hash::NUM_ARGS,
|
||||
Self::Poseidon4Hash => Poseidon4Hash::NUM_ARGS,
|
||||
Self::Poseidon8Hash => Poseidon8Hash::NUM_ARGS,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A core function of a core struct, e.g. `hash` or `commit`
|
||||
/// Provides required type information to the type checker.
|
||||
trait CoreFunction {
|
||||
const NUM_INPUTS: usize;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user