Skip to main content

Shift

Struct Shift 

Source
pub struct Shift {
    pub variant: ShiftVariant,
    pub amount: u8,
}
Expand description

One shift: an operation paired with the distance it moves by.

The amount is always below the variant’s max_amount, so a Shift that exists denotes the same operation wherever it is read.

Every variant is the identity at amount 0, so the amount alone does not fix how the identity is spelled. Shift::IDENTITY is the canonical spelling, and Self::is_canonical is what says which spelling a constraint system may carry.

The amount is stored as a byte to keep the struct small: constraint systems hold millions of these.

use binius_core::{constraint_system::Shift, word::Word};

let word = Word::from_u64(0xf0);
assert_eq!(Shift::srl(4).apply(word), Word::from_u64(0x0f));
assert_eq!(Shift::IDENTITY.apply(word), word);

// Every variant is the identity at amount 0, but only one spelling is canonical.
assert!(Shift::rotr(0).is_identity());
assert!(!Shift::rotr(0).is_canonical());

Fields§

§variant: ShiftVariant

The operation this shift performs.

§amount: u8

The number of bits to shift by, below the variant’s upper bound.

Implementations§

Source§

impl Shift

Source

pub const IDENTITY: Self

The canonical shift that leaves a word untouched.

Source

pub const fn new(variant: ShiftVariant, amount: usize) -> Self

A shift of amount bits by variant.

§Panics

Panics if the amount is not below the variant’s max_amount: 32 for the half-word (*32) variants, 64 for the rest.

Source

pub const fn sll(amount: usize) -> Self

Shift Left Logical.

§Panics

Panics if the shift amount is greater than or equal to 64.

Source

pub const fn srl(amount: usize) -> Self

Shift Right Logical.

§Panics

Panics if the shift amount is greater than or equal to 64.

Source

pub const fn sar(amount: usize) -> Self

Shift Right Arithmetic.

This is similar to the Shift Right Logical but instead of shifting in 0 bits it will replicate the sign bit.

§Panics

Panics if the shift amount is greater than or equal to 64.

Source

pub const fn rotr(amount: usize) -> Self

Rotate Right.

Rotates bits to the right, with bits shifted off the right end wrapping around to the left.

§Panics

Panics if the shift amount is greater than or equal to 64.

Source

pub const fn sll32(amount: usize) -> Self

Shift Left Logical on 32-bit halves.

Performs independent logical left shifts on the upper and lower 32-bit halves.

§Panics

Panics if the shift amount is greater than or equal to 32.

Source

pub const fn srl32(amount: usize) -> Self

Shift Right Logical on 32-bit halves.

Performs independent logical right shifts on the upper and lower 32-bit halves.

§Panics

Panics if the shift amount is greater than or equal to 32.

Source

pub const fn sra32(amount: usize) -> Self

Shift Right Arithmetic on 32-bit halves.

Performs independent arithmetic right shifts on the upper and lower 32-bit halves, sign extending each half independently.

§Panics

Panics if the shift amount is greater than or equal to 32.

Source

pub const fn rotr32(amount: usize) -> Self

Rotate Right on 32-bit halves.

Performs independent rotate right operations on the upper and lower 32-bit halves.

§Panics

Panics if the shift amount is greater than or equal to 32.

Source

pub const fn is_identity(self) -> bool

Whether this shift leaves every word untouched.

Every variant is the identity at amount 0, so this holds for more shifts than Shift::IDENTITY alone.

Source

pub const fn is_canonical(self) -> bool

Whether this is the canonical spelling of the operation it denotes.

Only the identity has more than one spelling, and Shift::IDENTITY is the one to use. Constraint systems carry canonical shifts only, so that two terms denoting the same shifted word compare equal.

Source

pub const fn index(self) -> usize

Where this shift sits in the enumeration of every (variant, amount) spelling.

The variant indexes runs of Word::BITS, and the amount indexes within a run:

    [ Sll 0 .. Sll 63 | Slr 0 .. Slr 63 | ... | Rotr32 0 .. Rotr32 63 ]
      0          63     64         127          448             511

A reduction keying one table entry per spelling addresses it by this index. So does the prover’s multilinear over the same axis pair, which is what lets the two agree.

§Panics

Panics if the amount is not below Word::BITS. Above it a shift would index into the next variant’s run, sharing an entry with another shift.

Source

pub fn apply(self, word: Word) -> Word

Applies this shift to a word and returns the result.

§Performance

Which operation to run is decided on every call. To shift many words by one fixed shift, resolve the variant once instead — see ShiftVariant::write_shifted and ShiftVariant::xor_shifted.

Source

pub fn compose(inner: Shift, outer: Shift) -> Composition

Classifies the composition of two shifts, outer applied to the result of inner.

This is the merge rule for a shift sequence: it says whether the two collapse to one shift, clear the word, or genuinely need both slots.

Collapsing is not just a matter of adding amounts. Two shifts collapse when the second continues the first — which happens for more pairs than sharing a variant, since a shift that has already cleared the sign bit or carried every bit past the halfway point leaves the next shift nothing to distinguish. chained enumerates those, and degenerate the cases where one shift has flattened the word past the other’s notice.

Reporting Composition::Pair where a collapse exists would cost a shift slot but never a wrong answer; the tests check against an independent bit-level model so that does not happen silently.

§Arguments
  • inner: the shift applied first.
  • outer: the shift applied to its result.

Trait Implementations§

Source§

impl Clone for Shift

Source§

fn clone(&self) -> Shift

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Shift

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl DeserializeBytes for Shift

Source§

fn deserialize(read_buf: impl Buf) -> Result<Self, SerializationError>
where Self: Sized,

Source§

impl Hash for Shift

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Shift

Source§

fn cmp(&self, other: &Shift) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Shift

Source§

fn eq(&self, other: &Shift) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Shift

Source§

fn partial_cmp(&self, other: &Shift) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl SerializeBytes for Shift

Source§

fn serialize(&self, write_buf: impl BufMut) -> Result<(), SerializationError>

Source§

impl Copy for Shift

Source§

impl Eq for Shift

Source§

impl StructuralPartialEq for Shift

Auto Trait Implementations§

§

impl Freeze for Shift

§

impl RefUnwindSafe for Shift

§

impl Send for Shift

§

impl Sync for Shift

§

impl Unpin for Shift

§

impl UnsafeUnpin for Shift

§

impl UnwindSafe for Shift

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more