Skip to content

Commit

Permalink
docs(tfhe): format regex_engine and add make recipes to run it
Browse files Browse the repository at this point in the history
  • Loading branch information
soonum authored and tmontaigu committed May 24, 2023
1 parent b501cc0 commit d79b1d9
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 74 deletions.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ else
AVX512_FEATURE=
endif

# Variables used only for regex_engine example
REGEX_STRING?=''
REGEX_PATTERN?=''

.PHONY: rs_check_toolchain # Echo the rust toolchain used for checks
rs_check_toolchain:
@echo $(RS_CHECK_TOOLCHAIN)
Expand Down Expand Up @@ -242,6 +246,12 @@ test_user_doc: install_rs_build_toolchain
--features=$(TARGET_ARCH_FEATURE),boolean,shortint,integer,internal-keycache -p tfhe \
-- test_user_docs::

.PHONY: test_regex_engine # Run tests for regex_engine example
test_regex_engine: install_rs_build_toolchain
RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_BUILD_TOOLCHAIN) test --profile $(CARGO_PROFILE) \
--example regex_engine \
--features=$(TARGET_ARCH_FEATURE),integer

.PHONY: doc # Build rust doc
doc: install_rs_check_toolchain
RUSTDOCFLAGS="--html-in-header katex-header.html -Dwarnings" \
Expand Down Expand Up @@ -326,6 +336,13 @@ measure_boolean_key_sizes: install_rs_check_toolchain
--example boolean_key_sizes \
--features=$(TARGET_ARCH_FEATURE),boolean,internal-keycache

.PHONY: regex_engine # Run regex_engine example
regex_engine: install_rs_check_toolchain
RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) run --profile $(CARGO_PROFILE) \
--example regex_engine \
--features=$(TARGET_ARCH_FEATURE),integer \
-- $(REGEX_STRING) $(REGEX_PATTERN)

.PHONY: pcc # pcc stands for pre commit checks
pcc: no_tfhe_typo check_fmt doc clippy_all check_compile_tests

Expand Down
2 changes: 1 addition & 1 deletion tfhe/docs/regex/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ reusing those where possible.
# Trying out the example implementation

The implementation that guided the writing of this tutorial can be found
under `thfe/examples/regex_engine`.
under `tfhe/examples/regex_engine`.

When compiling with `--example regex_engine`, a binary is produced that serves
as a basic demo. Simply call it with first argument the content string and
Expand Down
7 changes: 3 additions & 4 deletions tfhe/examples/regex_engine/ciphertext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::{anyhow, Result};
use tfhe::integer::{gen_keys_radix, RadixCiphertextBig, RadixClientKey, ServerKey};
use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
use tfhe::integer::gen_keys_radix;
use tfhe::integer::{RadixCiphertextBig, RadixClientKey, ServerKey};
use anyhow::{Result, anyhow};

pub type StringCiphertext = Vec<RadixCiphertextBig>;

Expand All @@ -17,5 +16,5 @@ pub fn encrypt_str(client_key: &RadixClientKey, s: &str) -> Result<StringCiphert

pub fn gen_keys() -> (RadixClientKey, ServerKey) {
let num_block = 4;
gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, num_block)
gen_keys_radix(PARAM_MESSAGE_2_CARRY_2, num_block)
}
14 changes: 6 additions & 8 deletions tfhe/examples/regex_engine/engine.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::execution::{Executed, Execution, LazyExecution};
use crate::parser::{parse, RegExpr};
use anyhow::Result;
use std::rc::Rc;
use tfhe::integer::{RadixCiphertextBig, ServerKey};
use crate::parser::{parse, RegExpr};
use crate::execution::{Executed, Execution, LazyExecution};

pub fn has_match(
sk: &ServerKey,
Expand All @@ -25,7 +25,7 @@ pub fn has_match(
.0
} else {
branches[1..]
.into_iter()
.iter()
.fold(branches[0](&mut exec), |res, branch| {
let branch_res = branch(&mut exec);
exec.ct_or(res, branch_res)
Expand All @@ -47,14 +47,14 @@ fn build_branches(
) -> Vec<(LazyExecution, usize)> {
trace!("program pointer: regex={:?}, content pos={}", re, c_pos);
match re {
RegExpr::SOF => {
RegExpr::Sof => {
if c_pos == 0 {
return vec![(Rc::new(|exec| exec.ct_true()), c_pos)];
} else {
return vec![];
}
}
RegExpr::EOF => {
RegExpr::Eof => {
if c_pos == content.len() {
return vec![(Rc::new(|exec| exec.ct_true()), c_pos)];
} else {
Expand Down Expand Up @@ -216,11 +216,9 @@ mod tests {
use crate::engine::has_match;
use test_case::test_case;

use tfhe::integer::{ServerKey, RadixClientKey};
use crate::ciphertext::{encrypt_str, gen_keys, StringCiphertext};
use bincode;
use lazy_static::lazy_static;
use std::io::Write;
use tfhe::integer::{RadixClientKey, ServerKey};

lazy_static! {
pub static ref KEYS: (RadixClientKey, ServerKey) = gen_keys();
Expand Down
5 changes: 1 addition & 4 deletions tfhe/examples/regex_engine/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ impl Execution {

let mut ct_a = a.0.clone();
let mut ct_b = exec.ct_constant(1).0;
(
exec.sk.smart_bitxor(&mut ct_a, &mut ct_b),
ctx.clone(),
)
(exec.sk.smart_bitxor(&mut ct_a, &mut ct_b), ctx.clone())
}),
)
}
Expand Down
4 changes: 2 additions & 2 deletions tfhe/examples/regex_engine/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#[macro_use]
extern crate log;

mod engine;
mod ciphertext;
mod engine;
mod execution;
mod parser;

use std::env;
use env_logger::Env;
use std::env;

fn main() {
let env = Env::default().filter_or("RUST_LOG", "info");
Expand Down
Loading

0 comments on commit d79b1d9

Please sign in to comment.