cairo_lang_sierra/extensions/modules/
pedersen.rs

1use super::felt252::Felt252Type;
2use crate::define_libfunc_hierarchy;
3use crate::extensions::lib_func::{
4    DeferredOutputKind, LibfuncSignature, OutputVarInfo, ParamSignature, SierraApChange,
5    SignatureSpecializationContext,
6};
7use crate::extensions::{
8    NamedType, NoGenericArgsGenericLibfunc, NoGenericArgsGenericType, OutputVarReferenceInfo,
9    SpecializationError,
10};
11use crate::ids::GenericTypeId;
12
13/// Type representing the Pedersen hash builtin.
14#[derive(Default)]
15pub struct PedersenType {}
16impl NoGenericArgsGenericType for PedersenType {
17    const ID: GenericTypeId = GenericTypeId::new_inline("Pedersen");
18    const STORABLE: bool = true;
19    const DUPLICATABLE: bool = false;
20    const DROPPABLE: bool = false;
21    const ZERO_SIZED: bool = false;
22}
23
24define_libfunc_hierarchy! {
25    pub enum PedersenLibfunc {
26        PedersenHash(PedersenHashLibfunc),
27    }, PedersenConcreteLibfunc
28}
29
30/// Libfunc for computing the Pedersen hash of two felt252s.
31/// Returns a felt252 (and the updated builtin pointer).
32#[derive(Default)]
33pub struct PedersenHashLibfunc {}
34impl NoGenericArgsGenericLibfunc for PedersenHashLibfunc {
35    const STR_ID: &'static str = "pedersen";
36
37    fn specialize_signature(
38        &self,
39        context: &dyn SignatureSpecializationContext,
40    ) -> Result<LibfuncSignature, SpecializationError> {
41        let pedersen_ty = context.get_concrete_type(PedersenType::id(), &[])?;
42        let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
43        let felt252_param = ParamSignature::new(felt252_ty.clone());
44        Ok(LibfuncSignature::new_non_branch_ex(
45            vec![
46                ParamSignature::new(pedersen_ty.clone()).with_allow_add_const(),
47                felt252_param.clone(),
48                felt252_param,
49            ],
50            vec![OutputVarInfo::new_builtin(pedersen_ty, 0), OutputVarInfo {
51                ty: felt252_ty,
52                ref_info: OutputVarReferenceInfo::Deferred(DeferredOutputKind::Generic),
53            }],
54            SierraApChange::Known { new_vars_only: true },
55        ))
56    }
57}