Skip to content

Instantly share code, notes, and snippets.

@ruescasd
Created February 7, 2025 18:02
Show Gist options
  • Save ruescasd/8c223f32dc4aefe5a9ccf8ab33e6fcb2 to your computer and use it in GitHub Desktop.
Save ruescasd/8c223f32dc4aefe5a9ccf8ab33e6fcb2 to your computer and use it in GitHub Desktop.
#!cryptol
#![allow(unused_variables)]
#![allow(unused_imports)]
use cry_rts::trait_methods::*;
/**
Returns the smaller of two comparable arguments.
Bitvectors are compared using unsigned arithmetic.
```cryptol
min : {a} (Cmp a) => a -> a -> a
```
*/
// drb: fixed to use trait bounds rather than the impl syntax
// see also
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=85c59dd2ab53b3eeb344c846ed660659
pub fn min_inst_iternb<A, B: cry_rts::Stream<A>>(
a: usize,
x: B,
y: B,
) -> B
where
A: cry_rts::Type,
A: cry_rts::Cmp,
{
if <Vec<A> as cry_rts::Cmp>::lt(
x.clone().to_vec().as_arg(),
y.clone().to_vec().as_arg(),
) { x } else { y }
}
/**
Returns the smaller of two comparable arguments.
Bitvectors are compared using unsigned arithmetic.
```cryptol
min : {a} (Cmp a) => a -> a -> a
```
*/
pub fn min_inst_ty<A>(x: A::Arg<'_>, y: A::Arg<'_>) -> A
where
A: cry_rts::Type,
A: cry_rts::Cmp,
{
if <A as cry_rts::Cmp>::lt(x, y) { x.clone_arg() } else { y.clone_arg() }
}
/**
Map a function over a sequence.
```cryptol
map : {n, a, b} (a -> b) -> [n]a -> [n]b
```
*/
pub fn map_inst_sz_val_val<A, B>(
n: usize,
f: &cry_rts::Fn1<cry_rts::O<A>, B>,
xs: &[A],
) -> Vec<B>
where
A: cry_rts::Type,
B: cry_rts::Type,
{
cry_rts::stream!(
forall = [
SI1: [ cry_rts::Stream<A> ], A: [ cry_rts::Type ], B: [ cry_rts::Type ]
], element = B, capture = [
f: cry_rts::Fn1<cry_rts::O<A>, B> = f.clone(), anon: SI1 = xs
.clone_arg()
.into_iter()
], step = |this|{ let x = this.anon.next()?; (&this.f)(x) }
)
.to_vec()
}
/**
Map a function over a sequence.
```cryptol
map : {n, a, b} (a -> b) -> [n]a -> [n]b
```
*/
pub fn map_inst_sz_val_bit<A>(
n: usize,
f: &cry_rts::Fn1<cry_rts::O<A>, bool>,
xs: &[A],
) -> cry_rts::DWord
where
A: cry_rts::Type,
{
cry_rts::DWord::from_stream_msb(
n,
cry_rts::stream!(
forall = [
SI1: [ cry_rts::Stream<A> ], A: [ cry_rts::Type ]
], element = bool, capture = [
f: cry_rts::Fn1<cry_rts::O<A>, bool> = f.clone(), anon: SI1 = xs
.clone_arg()
.into_iter()
], step = |this|{ let x = this.anon.next()?; (&this.f)(x) }
),
)
}
/**
Map a function over a sequence.
```cryptol
map : {n, a, b} (a -> b) -> [n]a -> [n]b
```
*/
pub fn map_inst_sz_bit_val<B>(
n: usize,
f: &cry_rts::Fn1<cry_rts::O<bool>, B>,
xs: cry_rts::DWordRef<'_>,
) -> Vec<B>
where
B: cry_rts::Type,
{
cry_rts::stream!(
forall = [
SI1: [ cry_rts::Stream<bool> ], B: [ cry_rts::Type ]
], element = B, capture = [
f: cry_rts::Fn1<cry_rts::O<bool>, B> = f.clone(), anon: SI1 = xs
.clone_arg()
.into_iter_bits_msb()
], step = |this|{ let x = this.anon.next()?; (&this.f)(x) }
)
.to_vec()
}
/**
Map a function over a sequence.
```cryptol
map : {n, a, b} (a -> b) -> [n]a -> [n]b
```
*/
pub fn map_inst_sz_bit_bit(
n: usize,
f: &cry_rts::Fn1<cry_rts::O<bool>, bool>,
xs: cry_rts::DWordRef<'_>,
) -> cry_rts::DWord {
cry_rts::DWord::from_stream_msb(
n,
cry_rts::stream!(
forall = [ SI1: [ cry_rts::Stream<bool> ] ], element = bool, capture = [
f: cry_rts::Fn1<cry_rts::O<bool>, bool> = f.clone(), anon: SI1 = xs
.clone_arg()
.into_iter_bits_msb()
], step = |this|{ let x = this.anon.next()?; (&this.f)(x) }
),
)
}
/**
Map a function over a sequence.
```cryptol
map : {n, a, b} (a -> b) -> [n]a -> [n]b
```
*/
pub fn map_inst_inf_ty_ty<A, B>(
f: &cry_rts::Fn1<cry_rts::O<A>, B>,
xs: impl cry_rts::Stream<A>,
) -> impl cry_rts::Stream<B>
where
A: cry_rts::Type,
B: cry_rts::Type,
{
cry_rts::stream!(
forall = [
SI1: [ cry_rts::Stream<A> ], A: [ cry_rts::Type ], B: [ cry_rts::Type ]
], element = B, capture = [
f: cry_rts::Fn1<cry_rts::O<A>, B> = f.clone(), anon: SI1 = xs
], step = |this|{ let x = this.anon.next()?; (&this.f)(x) }
)
}
/**
Map a function iteratively over a seed value, producing an infinite
list of successive function applications.
```cryptol
iterate : {a} (a -> a) -> a -> [inf]a
```
*/
pub fn iterate_inst_ty<A>(
f: &cry_rts::Fn1<cry_rts::O<A>, A>,
z: A::Arg<'_>,
) -> impl cry_rts::Stream<A>
where
A: cry_rts::Type,
{
cry_rts::cry_scanl(
<cry_rts::Fn2<cry_rts::O<A>, cry_rts::O<()>, _>>::new({
let f = f.clone();
move |x, __p9| { let f = &f; (f)(x) }
}),
z.clone_arg(),
cry_rts::str_zero_inf::<()>(()),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment