Skip to content

Useful FaCT Patterns

scauligi edited this page Mar 17, 2018 · 1 revision

All of the following snippets are valid FaCT code, and thus will compile to constant time instructions.

// Assignment based on a secret condition
void selecting_things(secret bool cond) {
  // mut because we reassign later
  secret mut int32 x = 0;
  if (cond) {
    x = 42;
  } else {
    x = 137;
  }

  // or even more succinctly (and without mut)
  secret int32 y = cond ? 42 : 137;
}
// Accessing a secret index in a buffer
void use_secret_index(secret uint8[] buf, secret uint32 index) {
  for (uint32 i = 0; i < len buf; i += 1) {
    if (i == index) {
      // ... do something with buf[i] ...
    }
  }
}
// Ending computation "early"
secret bool contains_17(secret int32[] arr) {
  for (uint32 i = 0; i < len arr; i += 1) {
    if (arr[i] == 17) {
      return true;
    }
  }
  return false;
}
Clone this wiki locally