ref: bd5cf6784d403e5540291a2674336b07010bc54c
parent: dda657081b032dc653f669ca85f2c09984f0cb35
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Mar 11 20:02:02 EDT 2018
Add constant time 'min()' and 'max()'
--- a/lib/crypto/ct.myr
+++ b/lib/crypto/ct.myr
@@ -10,6 +10,8 @@
generic ge : (a : @t, b : @t -> @t) :: integral,numeric @t
generic le : (a : @t, b : @t -> @t) :: integral,numeric @t
generic mux : (x : @t, a : @t, b : @t ->@t) :: integral,numeric @t
+ generic min : (a : @t, b : @t -> @t) :: integral,numeric @t
+ generic max : (a : @t, b : @t -> @t) :: integral,numeric @t
;;
generic not = {a : @t :: integral,numeric @t
@@ -56,4 +58,18 @@
generic mux = {c, a, b
-> b ^ (-c & (a ^ b))
+}
+
+generic min = {a, b
+ var x
+
+ x = lt(a, b)
+ -> mux(x, a, b)
+}
+
+generic max = {a, b
+ var x
+
+ x = lt(a, b)
+ -> mux(x, b, a)
}