Skip to content

Simd Models #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
cd25af8
Porting over from cryspen-ext/core-models:simd-debug
satiscugcat Jun 30, 2025
f138cb8
Removing no_models
satiscugcat Jun 30, 2025
c72c1ca
Removed fstar things
satiscugcat Jul 4, 2025
e72512f
Removing hax dependencies
satiscugcat Jul 9, 2025
5dc77bc
Formatting
satiscugcat Jul 9, 2025
34fad17
More axing the hax
satiscugcat Jul 9, 2025
8ded4c5
Moving int_vec_interp
satiscugcat Jul 9, 2025
1607c83
Finished moving things, should be alright now!
satiscugcat Jul 9, 2025
fae168d
Formatting
satiscugcat Jul 9, 2025
178531a
Update testable-simd-models/src/core_arch/arm_shared/models/mod.rs
satiscugcat Jul 10, 2025
2c7db29
Update testable-simd-models/src/core_arch/x86/models/mod.rs
satiscugcat Jul 10, 2025
d5650f2
Update testable-simd-models/src/core_arch/x86/tests/avx2.rs
satiscugcat Jul 10, 2025
e25e336
Update testable-simd-models/src/core_arch/x86/tests/avx.rs
satiscugcat Jul 10, 2025
66d89c7
Update testable-simd-models/src/core_arch/x86/tests/sse2.rs
satiscugcat Jul 10, 2025
af40445
Update testable-simd-models/src/core_arch/x86/tests/ssse3.rs
satiscugcat Jul 10, 2025
90e1550
Update testable-simd-models/src/core_arch/arm_shared/models/mod.rs
satiscugcat Jul 10, 2025
88f870e
Update testable-simd-models/src/core_arch/arm_shared/specs/mod.rs
satiscugcat Jul 10, 2025
9397eb4
Update testable-simd-models/src/core_arch/arm_shared/specs/mod.rs
satiscugcat Jul 10, 2025
aaadf27
Update testable-simd-models/src/core_arch/x86/models/mod.rs
satiscugcat Jul 10, 2025
aa9bd4b
Update testable-simd-models/src/core_arch/x86/models/mod.rs
satiscugcat Jul 10, 2025
2c685c4
Update testable-simd-models/src/core_arch/x86/specs/mod.rs
satiscugcat Jul 10, 2025
3b629bd
Update testable-simd-models/src/abstractions/mod.rs
satiscugcat Jul 10, 2025
af49b73
Update testable-simd-models/src/core_arch/arm_shared/models/mod.rs
satiscugcat Jul 10, 2025
412e11c
Update testable-simd-models/src/core_arch/arm_shared/tests/neon.rs
satiscugcat Jul 10, 2025
c6e921c
Documentation changes
satiscugcat Jul 10, 2025
a8458fa
Adding avx tests
satiscugcat Jul 10, 2025
f05c320
Moving an intrinsic to the right place
satiscugcat Jul 13, 2025
be022bd
File left out from last commit
satiscugcat Jul 13, 2025
307d8be
Upgrading to earliest nightly that includes our fix
satiscugcat Jul 13, 2025
ee1e273
Doc updates
satiscugcat Jul 13, 2025
511b4a2
Update testable-simd-models/src/core_arch/x86/models/sse2.rs
satiscugcat Jul 15, 2025
f7ac042
Update testable-simd-models/src/core_arch/x86/models/sse2.rs
satiscugcat Jul 15, 2025
63c0d92
Import fix
satiscugcat Jul 16, 2025
51d28c8
Added missing tests
satiscugcat Jul 16, 2025
b5ee135
updating README
satiscugcat Jul 16, 2025
fb5b397
Fixing spec
satiscugcat Jul 16, 2025
69189c2
Formatting
satiscugcat Jul 16, 2025
428fd3c
edits
karthikbhargavan Jul 22, 2025
1f4b817
Merge branch 'main' into simd-models
karthikbhargavan Jul 25, 2025
d69c8d1
README
karthikbhargavan Jul 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ package-lock.json
# already existing elements were commented out

#/target
testable-simd-models/target
16 changes: 16 additions & 0 deletions testable-simd-models/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "core-models"
version = "0.0.2"
authors = ["Cryspen"]
license = "Apache-2.0"
homepage = "https://github.com/cryspen-ext/core-models"
edition = "2021"
repository = "https://github.com/cryspen-ext/core-models"
readme = "README.md"

[dependencies]
rand = "0.9"
pastey = "0.1.0"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(hax)'] }
2 changes: 2 additions & 0 deletions testable-simd-models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# core-models
Rust models for the Core Library (extending work from libcrux/minicore)
91 changes: 91 additions & 0 deletions testable-simd-models/hax.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
set -e

function extract_all() {
go_to "./"
cargo hax into fstar --z3rlimit 80
}

function prove() {
case "$1" in
--admit)
shift 1
export OTHERFLAGS="--admit_smt_queries true";;
*);;
esac
go_to "./"
JOBS="${JOBS:-$(nproc --all)}"
JOBS="${JOBS:-4}"
make -C proofs/fstar/extraction -j $JOBS "$@"
}

function init_vars() {
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
SCRIPT_PATH="${SCRIPT_DIR}/${SCRIPT_NAME}"

if [ -t 1 ]; then
BLUE='\033[34m'
GREEN='\033[32m'
BOLD='\033[1m'
RESET='\033[0m'
else
BLUE=''
GREEN=''
BOLD=''
RESET=''
fi
}

function go_to() {
ROOT="$SCRIPT_DIR"
cd "$ROOT"
cd "$1"
}

function msg() {
echo -e "$1[$SCRIPT_NAME]$RESET $2"
}

function help() {
echo "Libcrux script to extract Rust to F* via hax."
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Comands:"
echo ""
grep '[#]>' "$SCRIPT_PATH" | sed 's/[)] #[>]/\t/g'
echo ""
}

function cli() {
if [ -z "$1" ]; then
help
exit 1
fi
# Check if an argument was provided

case "$1" in
--help) #> Show help message
help;;
extract) #> Extract the F* code for the proofs.
extract_all
msg "$GREEN" "done"
;;
prove) #> Run F*. This typechecks the extracted code. To lax-typecheck use --admit.
shift 1
prove "$@";;
extract+prove) #> Equivalent to extracting and proving.
shift 1
extract_all
prove "$@";;
*)
echo "Invalid option: $1"
help
exit 1;;
esac
}

init_vars

cli "$@"
204 changes: 204 additions & 0 deletions testable-simd-models/src/abstractions/bit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//! # Bit Manipulation and Machine Integer Utilities
//!
//! This module provides utilities for working with individual bits and machine integer types.
//! It defines a [`Bit`] enum to represent a single bit (`0` or `1`) along with convenient
//! conversion implementations between `Bit`, [`bool`], and various primitive integer types.
//!
//! In addition, the module introduces the [`MachineInteger`] trait which abstracts over
//! integer types, providing associated constants:
//!
//! - `BITS`: The size of the integer type in bits.
//! - `SIGNED`: A flag indicating whether the type is signed.
//!
//! The [`Bit`] type includes methods for extracting the value of a specific bit from an integer.
//! For example, [`Bit::of_int`] returns the bit at a given position for a provided integer,
//! handling both positive and negative values (assuming a two's complement representation).
//!
//! # Examples
//!
//! ```rust
//! use core_models::abstractions::bit::{Bit, MachineInteger};
//!
//! // Extract the 3rd bit (0-indexed) from an integer.
//! let bit = Bit::of_int(42, 2);
//! println!("The extracted bit is: {:?}", bit);
//!
//! // Convert Bit to a primitive integer type.
//! let num: u8 = bit.into();
//! println!("As an integer: {}", num);
//! ```
//!
//! [`bool`]: https://doc.rust-lang.org/std/primitive.bool.html
//! [`Bit::of_int`]: enum.Bit.html#method.of_int

/// Represent a bit: `0` or `1`.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Bit {
Zero,
One,
}
impl std::ops::BitAnd for Bit {
type Output = Self;
fn bitand(self, rhs: Self) -> Self {
match self {
Bit::Zero => Bit::Zero,
Bit::One => rhs,
}
}
}

impl std::ops::BitOr for Bit {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
match self {
Bit::Zero => rhs,
Bit::One => Bit::One,
}
}
}

impl std::ops::BitXor for Bit {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self {
match (self, rhs) {
(Bit::Zero, Bit::Zero) => Bit::Zero,
(Bit::One, Bit::One) => Bit::Zero,
_ => Bit::One,
}
}
}

impl std::ops::Neg for Bit {
type Output = Self;
fn neg(self) -> Self {
match self {
Bit::One => Bit::Zero,
Bit::Zero => Bit::One,
}
}
}
macro_rules! generate_from_bit_impls {
($($ty:ident),*) => {
$(impl From<Bit> for $ty {
fn from(bit: Bit) -> Self {
bool::from(bit) as $ty
}
})*
};
}
generate_from_bit_impls!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);

impl From<Bit> for bool {
fn from(bit: Bit) -> Self {
match bit {
Bit::Zero => false,
Bit::One => true,
}
}
}

impl From<bool> for Bit {
fn from(b: bool) -> Bit {
match b {
false => Bit::Zero,
true => Bit::One,
}
}
}

/// A trait for types that represent machine integers.
pub trait MachineInteger {
/// The size of this integer type in bits.
fn bits() -> u32;

/// The signedness of this integer type.
const SIGNED: bool;
/// Element of the integer type with every bit as 0.
const ZEROS: Self;
/// Element of the integer type with every bit as 1.
const ONES: Self;
/// Minimum value of the integer type.
const MIN: Self;
/// Maximum value of the integer type.
const MAX: Self;

/// Implements functionality for `simd_add` in `crate::abstractions::simd`.
fn wrapping_add(self, rhs: Self) -> Self;
/// Implements functionality for `simd_sub` in `crate::abstractions::simd`.
fn wrapping_sub(self, rhs: Self) -> Self;
/// Implements functionality for `simd_mul` in `crate::abstractions::simd`.
fn overflowing_mul(self, rhs: Self) -> Self;
/// Implements functionality for `simd_saturating_add` in `crate::abstractions::simd`.
fn saturating_add(self, rhs: Self) -> Self;
/// Implements functionality for `simd_saturating_sub` in `crate::abstractions::simd`.
fn saturating_sub(self, rhs: Self) -> Self;
/// Implements functionality for `simd_abs_diff` in `crate::abstractions::simd`.
fn absolute_diff(self, rhs: Self) -> Self;
/// Implements functionality for `simd_abs` in `crate::abstractions::simd`.
fn absolute_val(self) -> Self;
}

macro_rules! generate_imachine_integer_impls {
($($ty:ident),*) => {
$(
impl MachineInteger for $ty {
const SIGNED: bool = true;
const ZEROS: $ty = 0;
const ONES: $ty = -1;
const MIN: $ty = $ty::MIN;
const MAX: $ty = $ty::MAX;
fn bits() -> u32 { $ty::BITS }
fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) }
fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) }
fn overflowing_mul(self, rhs: Self) -> Self { self.overflowing_mul(rhs).0 }
fn saturating_add(self, rhs: Self) -> Self { self.saturating_add(rhs)}
fn saturating_sub(self, rhs: Self) -> Self { self.saturating_sub(rhs) }
fn absolute_diff(self, rhs: Self) -> Self {if self > rhs {$ty::wrapping_sub(self, rhs)} else {$ty::wrapping_sub(rhs, self)}}
fn absolute_val(self) -> Self {if self == $ty::MIN {self} else {self.abs()}}
})*
};
}

macro_rules! generate_umachine_integer_impls {
($($ty:ident),*) => {
$(
impl MachineInteger for $ty {
const SIGNED: bool = false;
const ZEROS: $ty = 0;
const ONES: $ty = $ty::MAX;
const MIN: $ty = $ty::MIN;
const MAX: $ty = $ty::MAX;


fn bits() -> u32 { $ty::BITS }
fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) }
fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) }
fn overflowing_mul(self, rhs: Self) -> Self { self.overflowing_mul(rhs).0 }
fn saturating_add(self, rhs: Self) -> Self { self.saturating_add(rhs)}
fn saturating_sub(self, rhs: Self) -> Self { self.saturating_sub(rhs)}
fn absolute_diff(self, rhs: Self) -> Self {if self > rhs {self - rhs} else {rhs - self}}
fn absolute_val(self) -> Self {self}
})*
};
}
generate_imachine_integer_impls!(i8, i16, i32, i64, i128);
generate_umachine_integer_impls!(u8, u16, u32, u64, u128);

impl Bit {
fn of_raw_int(x: u128, nth: u32) -> Self {
if x / 2u128.pow(nth) % 2 == 1 {
Self::One
} else {
Self::Zero
}
}

pub fn of_int<T: Into<i128> + MachineInteger>(x: T, nth: u32) -> Bit {
let x: i128 = x.into();
if x >= 0 {
Self::of_raw_int(x as u128, nth)
} else {
Self::of_raw_int((2i128.pow(T::bits()) + x) as u128, nth)
}
}
}
Loading
Loading