miden_stdlib_sys/intrinsics/
crypto.rs

1//! Cryptographic intrinsics for the Miden VM.
2//!
3//! This module provides Rust bindings for cryptographic operations available in the Miden VM.
4#![allow(warnings)]
5
6use crate::intrinsics::{Felt, Word};
7
8/// A cryptographic digest representing a 256-bit hash value.
9///
10/// This is a wrapper around `Word` which contains 4 field elements.
11#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
12#[repr(transparent)]
13pub struct Digest {
14    pub inner: Word,
15}
16
17impl Digest {
18    /// Creates a new `Digest` from a `[Felt; 4]` array.
19    #[inline]
20    pub fn new(felts: [Felt; 4]) -> Self {
21        Self {
22            inner: Word::from(felts),
23        }
24    }
25
26    /// Creates a new `Digest` from a `Word`.
27    #[inline]
28    pub const fn from_word(word: Word) -> Self {
29        Self { inner: word }
30    }
31}
32
33impl From<Word> for Digest {
34    #[inline]
35    fn from(word: Word) -> Self {
36        Self::from_word(word)
37    }
38}
39
40impl From<Digest> for Word {
41    #[inline]
42    fn from(digest: Digest) -> Self {
43        digest.inner
44    }
45}
46
47impl From<[Felt; 4]> for Digest {
48    #[inline]
49    fn from(felts: [Felt; 4]) -> Self {
50        Self::new(felts)
51    }
52}
53
54impl From<Digest> for [Felt; 4] {
55    #[inline]
56    fn from(digest: Digest) -> Self {
57        digest.inner.into()
58    }
59}
60
61// Remove WIT import module and resolve via a linker stub instead. The stub will export
62// the MASM symbol `intrinsics::crypto::hmerge`, and the frontend will lower its
63// unreachable body to a MASM exec.
64unsafe extern "C" {
65    /// Computes the hash of two digests using the Rescue Prime Optimized (RPO)
66    /// permutation in 2-to-1 mode.
67    ///
68    /// This is the `hmerge` instruction in the Miden VM.
69    ///
70    /// Input: Pointer to an array of two digests (8 field elements total)
71    /// Output: One digest (4 field elements) written to the result pointer
72    #[link_name = "intrinsics::crypto::hmerge"]
73    fn extern_hmerge(
74        // Pointer to array of two digests
75        digests_ptr: *const Felt,
76        // Result pointer
77        result_ptr: *mut Felt,
78    );
79}
80
81/// Computes the hash of two digests using the Rescue Prime Optimized (RPO)
82/// permutation in 2-to-1 mode.
83///
84/// This directly maps to the `hmerge` VM instruction.
85///
86/// # Arguments
87/// * `digests` - An array of two digests to be merged. The function internally
88///   reorders them as required by the VM instruction (from [A, B] to [B, A] on the stack).
89#[inline]
90pub fn merge(digests: [Digest; 2]) -> Digest {
91    unsafe {
92        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
93        let result_ptr = ret_area.as_mut_ptr().addr() as u32;
94
95        let digests_ptr = digests.as_ptr().addr() as u32;
96        extern_hmerge(digests_ptr as *const Felt, result_ptr as *mut Felt);
97
98        Digest::from_word(ret_area.assume_init())
99    }
100}