tasm_lib/arithmetic/u64/or.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
use std::collections::HashMap;
use triton_vm::prelude::*;
use crate::prelude::*;
use crate::traits::basic_snippet::Reviewer;
use crate::traits::basic_snippet::SignOffFingerprint;
/// [Bitwise “or”][bitor] (“`|`”) for `u64`s.
///
/// ### Behavior
///
/// ```text
/// BEFORE: _ [right: u64] [left: u64]
/// AFTER: _ [left | right: u64]
/// ```
///
/// ### Preconditions
///
/// - all input arguments are properly [`BFieldCodec`] encoded
///
/// ### Postconditions
///
/// - the output is the bitwise “or” of the input
/// - the output is properly [`BFieldCodec`] encoded
///
/// [bitor]: core::ops::BitOr
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Or;
impl BasicSnippet for Or {
fn inputs(&self) -> Vec<(DataType, String)> {
["right", "left"]
.map(|s| (DataType::U64, s.to_string()))
.to_vec()
}
fn outputs(&self) -> Vec<(DataType, String)> {
vec![(DataType::U64, "left | right".to_string())]
}
fn entrypoint(&self) -> String {
"tasmlib_arithmetic_u64_or".to_string()
}
fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
let entrypoint = self.entrypoint();
triton_asm!(
// BEFORE: _ r_hi r_lo l_hi l_lo
// AFTER: _ (l | r)_hi (l | r)_lo
{entrypoint}:
dup 2
dup 1
xor // _ r_hi r_lo l_hi l_lo (r_lo ^ l_lo)
swap 3
and // _ r_hi (r_lo ^ l_lo) l_hi (l_lo & r_lo)
swap 3 // _ (l_lo & r_lo) (r_lo ^ l_lo) l_hi r_hi
dup 1
dup 1
xor // _ (l_lo & r_lo) (r_lo ^ l_lo) l_hi r_hi (l_hi ^ r_hi)
place 2
and // _ (l_lo & r_lo) (r_lo ^ l_lo) (l_hi ^ r_hi) (r_hi & l_hi)
add // _ (l_lo & r_lo) (r_lo ^ l_lo) (l_hi | r_hi)
place 2
add // _ (l_hi | r_hi) (r_lo | l_lo)
return
)
}
fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
let mut sign_offs = HashMap::new();
sign_offs.insert(Reviewer("ferdinand"), 0x9304a37ae367ed6.into());
sign_offs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::arithmetic::u64::and::And;
use crate::test_prelude::*;
impl Closure for Or {
type Args = (u64, u64);
fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
let (right, left) = pop_encodable::<Self::Args>(stack);
let or = left | right;
push_encodable(stack, &or);
}
fn pseudorandom_args(
&self,
seed: [u8; 32],
bench_case: Option<BenchmarkCase>,
) -> Self::Args {
match bench_case {
Some(BenchmarkCase::CommonCase) => (u32::MAX.into(), u32::MAX.into()),
Some(BenchmarkCase::WorstCase) => (u64::MAX, u64::MAX),
None => StdRng::from_seed(seed).random(),
}
}
fn corner_case_args(&self) -> Vec<Self::Args> {
And.corner_case_args()
}
}
#[test]
fn rust_shadow() {
ShadowedClosure::new(Or).test();
}
}
#[cfg(test)]
mod benches {
use super::*;
use crate::test_prelude::*;
#[test]
fn benchmark() {
ShadowedClosure::new(Or).bench();
}
}