Skip to content

Commit abd2166

Browse files
committed
refactor(scheduler): clarify pattern tag naming
1 parent b1703ed commit abd2166

File tree

4 files changed

+45
-44
lines changed

4 files changed

+45
-44
lines changed

coprocessor/fhevm-engine/scheduler/src/dfg/pattern/encoding.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use crate::dfg::types::DFGTaskInput;
1212
use crate::dfg::{DFGOp, OpEdge};
1313
use fhevm_engine_common::common::FheOperation;
1414

15-
/// Encoding version byte (self-describing, decodable).
16-
pub(super) const ENCODING_VERSION: u8 = 0x01;
15+
/// Tag byte for the inline, self-describing pattern encoding.
16+
pub(super) const INLINE_PATTERN_TAG: u8 = 0x01;
1717

18-
/// Hash version byte (Keccak-256 truncated, not decodable).
19-
pub(super) const HASH_VERSION: u8 = 0x02;
18+
/// Tag byte for the hashed pattern encoding (Keccak-256 truncated, not decodable).
19+
pub(super) const HASHED_PATTERN_TAG: u8 = 0x02;
2020

2121
/// Number of bytes to keep from the Keccak-256 digest.
2222
const HASH_DIGEST_LEN: usize = 20;
@@ -48,14 +48,14 @@ pub fn pattern_to_base64url(bytes: &[u8]) -> String {
4848

4949
/// Decode a binary pattern encoding into a [`PatternDescription`].
5050
///
51-
/// Returns `None` if the encoding is malformed (wrong version, truncated, etc.)
52-
/// or if the pattern is a hashed v2 form (hashes are not decodable).
51+
/// Returns `None` if the encoding is malformed (wrong tag, truncated, etc.)
52+
/// or if the pattern is a hashed form (hashes are not decodable).
5353
pub fn decode_pattern(bytes: &[u8]) -> Option<PatternDescription> {
5454
if bytes.len() < 2 {
5555
return None;
5656
}
57-
// Only v1 encodings are decodable; hashed (v2) and unknown versions are not.
58-
if bytes[0] != ENCODING_VERSION {
57+
// Only inline encodings are decodable; hashed and unknown tags are not.
58+
if bytes[0] != INLINE_PATTERN_TAG {
5959
return None;
6060
}
6161
let node_count = bytes[1] as usize;
@@ -116,9 +116,9 @@ pub fn decode_pattern(bytes: &[u8]) -> Option<PatternDescription> {
116116
// Two-tier finalization (encoding vs hash)
117117
// ---------------------------------------------------------------------------
118118

119-
/// Returns `true` if the pattern bytes represent a hashed (v2) pattern.
119+
/// Returns `true` if the pattern bytes represent a hashed pattern.
120120
pub fn is_hashed_pattern(bytes: &[u8]) -> bool {
121-
!bytes.is_empty() && bytes[0] == HASH_VERSION
121+
!bytes.is_empty() && bytes[0] == HASHED_PATTERN_TAG
122122
}
123123

124124
/// Hash threshold read once from `FHEVM_PATTERN_HASH_THRESHOLD` env var,
@@ -130,11 +130,11 @@ pub(super) static PATTERN_HASH_THRESHOLD: LazyLock<usize> = LazyLock::new(|| {
130130
.unwrap_or(DEFAULT_PATTERN_HASH_THRESHOLD)
131131
});
132132

133-
/// If the v1 encoding has `node_count ≤ threshold`, return it as-is.
134-
/// Otherwise hash it into a compact 23-byte v2 form and log the full
133+
/// If the inline encoding has `node_count ≤ threshold`, return it as-is.
134+
/// Otherwise hash it into a compact 23-byte form and log the full
135135
/// encoding once (per unique hash) for operator linkability.
136136
pub(super) fn finalize_pattern(encoding: Vec<u8>, threshold: usize) -> Vec<u8> {
137-
debug_assert!(encoding.len() >= 2 && encoding[0] == ENCODING_VERSION);
137+
debug_assert!(encoding.len() >= 2 && encoding[0] == INLINE_PATTERN_TAG);
138138
let node_count = encoding[1] as usize;
139139

140140
if node_count <= threshold {
@@ -144,12 +144,12 @@ pub(super) fn finalize_pattern(encoding: Vec<u8>, threshold: usize) -> Vec<u8> {
144144
build_hash_pattern(&encoding, node_count, true)
145145
}
146146

147-
/// Build a 23-byte v2 hashed pattern from arbitrary encoding bytes.
147+
/// Build a 23-byte hashed pattern from arbitrary encoding bytes.
148148
///
149149
/// ## Hashed pattern binary layout
150150
///
151151
/// ```text
152-
/// Byte 0: 0x02 (HASH_VERSION)
152+
/// Byte 0: 0x02 (HASHED_PATTERN_TAG)
153153
/// Bytes 1-2: node_count as u16 big-endian
154154
/// Bytes 3-22: first 20 bytes of Keccak-256(encoding)
155155
/// Total: 23 bytes
@@ -162,7 +162,7 @@ pub(super) fn build_hash_pattern(
162162
let digest = Keccak256::digest(encoding);
163163

164164
let mut buf = Vec::with_capacity(1 + 2 + HASH_DIGEST_LEN);
165-
buf.push(HASH_VERSION);
165+
buf.push(HASHED_PATTERN_TAG);
166166
let node_count_u16 = u16::try_from(node_count).unwrap_or(u16::MAX);
167167
buf.extend_from_slice(&node_count_u16.to_be_bytes());
168168
buf.extend_from_slice(&digest[..HASH_DIGEST_LEN]);
@@ -220,10 +220,10 @@ pub(super) fn compute_subgraph_layout(
220220
Some((local_topo, topo_pos))
221221
}
222222

223-
/// Maximum number of nodes that a v1 encoding can represent (node_count is u8).
223+
/// Maximum number of nodes that an inline encoding can represent (node_count is u8).
224224
const V1_MAX_NODES: usize = 255;
225225

226-
/// Maximum number of inputs per node that a v1 encoding can represent
226+
/// Maximum number of inputs per node that an inline encoding can represent
227227
/// (3-bit field in the flags byte).
228228
const V1_MAX_INPUTS_PER_NODE: usize = 7;
229229

@@ -243,14 +243,14 @@ const V1_MAX_INTERNAL_REF: usize = 127;
243243
/// nodes, allowed nodes from other groups, DB handles) are treated as external
244244
/// (byte 0x00).
245245
///
246-
/// Returns `None` if the group exceeds the v1 encoding limits (\>255 nodes,
246+
/// Returns `None` if the group exceeds the inline encoding limits (\>255 nodes,
247247
/// \>7 inputs per node, or \>127 internal-ref position). Callers can fall back
248248
/// to wide-format hashing so oversized groups are still attributable via hash.
249249
///
250-
/// ## Binary layout (version 1)
250+
/// ## Inline binary layout
251251
///
252252
/// ```text
253-
/// Byte 0: 0x01 (version)
253+
/// Byte 0: 0x01 (inline pattern tag)
254254
/// Byte 1: node_count (u8, max 255)
255255
///
256256
/// Per node in canonical topological order:
@@ -276,11 +276,11 @@ pub(super) fn encode_subgraph(
276276
return None;
277277
}
278278

279-
// Pre-allocate: version(1) + count(1) + per-node ~4 bytes average
279+
// Pre-allocate: tag(1) + count(1) + per-node ~4 bytes average
280280
let mut buf: Vec<u8> = Vec::with_capacity(2 + node_count * 4);
281281

282282
// Header
283-
buf.push(ENCODING_VERSION);
283+
buf.push(INLINE_PATTERN_TAG);
284284
buf.push(node_count as u8);
285285

286286
// Nodes
@@ -326,7 +326,7 @@ pub(super) fn encode_subgraph(
326326
Some(buf)
327327
}
328328

329-
/// Wide-format encoding used only as hash input when a group exceeds v1 limits.
329+
/// Wide-format encoding used only as hash input when a group exceeds inline encoding limits.
330330
///
331331
/// This is intentionally not emitted as a pattern attribute directly: only the
332332
/// resulting hash is used.

coprocessor/fhevm-engine/scheduler/src/dfg/pattern/grouping.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ pub fn compute_logical_pattern_ids(
226226

227227
// Encode each group and assign pattern_ids.
228228
//
229-
// Try compact v1 first. If it succeeds and is above threshold, we hash and
230-
// log full encoding once. If v1 fails (group too large), hash wide encoding
231-
// without logging the full payload.
229+
// Try the compact inline encoding first. If it succeeds and is above the
230+
// threshold, we hash and log the full encoding once. If inline encoding
231+
// fails (group too large), hash the wide encoding without logging the full
232+
// payload.
232233
let threshold = *PATTERN_HASH_THRESHOLD;
233234
let mut result: HashMap<usize, Vec<u8>> = HashMap::new();
234235
for group in groups.values() {

coprocessor/fhevm-engine/scheduler/src/dfg/pattern/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub use grouping::{compute_logical_pattern_ids, compute_transaction_pattern_id};
2828
pub use types::{PatternDescription, PatternInput, PatternNode};
2929

3030
#[cfg(test)]
31-
use encoding::{ENCODING_VERSION, HASH_VERSION};
31+
use encoding::{HASHED_PATTERN_TAG, INLINE_PATTERN_TAG};
3232

3333
#[cfg(test)]
3434
mod tests;

coprocessor/fhevm-engine/scheduler/src/dfg/pattern/tests.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::collections::HashMap;
66
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
77

88
use super::{
9-
decode_pattern, is_hashed_pattern, pattern_to_base64url, ENCODING_VERSION, HASH_VERSION,
9+
decode_pattern, is_hashed_pattern, pattern_to_base64url, HASHED_PATTERN_TAG, INLINE_PATTERN_TAG,
1010
};
1111

1212
/// Build a pre-partition graph + produced_handles from `Vec<DFGOp>` and call
@@ -56,22 +56,22 @@ fn collect_op_pattern_ids(component: &super::super::ComponentNode) -> Vec<Vec<u8
5656
.collect()
5757
}
5858

59-
/// Assert that a pattern_id is a valid pattern — either a v1 encoding
60-
/// (decodable) or a v2 hash (23 bytes, starts with HASH_VERSION).
59+
/// Assert that a pattern_id is a valid pattern — either an inline encoding
60+
/// (decodable) or a hashed encoding (23 bytes, starts with `HASHED_PATTERN_TAG`).
6161
fn assert_valid_pattern(bytes: &[u8]) {
6262
assert!(!bytes.is_empty(), "pattern_id should not be empty");
6363
match bytes[0] {
64-
ENCODING_VERSION => {
65-
let desc = decode_pattern(bytes).expect("v1 pattern_id should be decodable");
64+
INLINE_PATTERN_TAG => {
65+
let desc = decode_pattern(bytes).expect("inline pattern_id should be decodable");
6666
assert!(
6767
!desc.nodes.is_empty(),
6868
"decoded pattern should have at least one node"
6969
);
7070
}
71-
HASH_VERSION => {
71+
HASHED_PATTERN_TAG => {
7272
assert_eq!(bytes.len(), 23, "hashed pattern should be exactly 23 bytes");
7373
}
74-
other => panic!("unexpected pattern version byte: 0x{other:02x}"),
74+
other => panic!("unexpected pattern tag byte: 0x{other:02x}"),
7575
}
7676
}
7777

@@ -1110,7 +1110,7 @@ fn oversize_group_hashes_for_transaction_pattern() {
11101110

11111111
let tx_id = vec![0xFFu8; 32];
11121112
let (components, _) = build_component_nodes(ops2, &tx_id).unwrap();
1113-
// >255 nodes: v1 encoding fails, wide encoding is hashed.
1113+
// >255 nodes: inline encoding fails, wide encoding is hashed.
11141114
let tx_pat = &components[0].transaction_pattern_id;
11151115
assert!(
11161116
is_hashed_pattern(tx_pat),
@@ -1167,22 +1167,22 @@ fn build_chain(n: usize, prefix: u8) -> Vec<DFGOp> {
11671167

11681168
#[test]
11691169
fn pattern_below_threshold_is_encoding() {
1170-
// A group with ≤ DEFAULT_PATTERN_HASH_THRESHOLD nodes stays v1.
1170+
// A group with ≤ DEFAULT_PATTERN_HASH_THRESHOLD nodes stays inline.
11711171
let ops = build_chain(10, 0xA0);
11721172
let ids = compute_logical_ids(ops);
11731173
assert!(!ids.is_empty());
11741174
let pattern = ids.values().next().unwrap();
11751175
assert_eq!(
1176-
pattern[0], ENCODING_VERSION,
1177-
"small group should produce v1 encoding"
1176+
pattern[0], INLINE_PATTERN_TAG,
1177+
"small group should produce inline encoding"
11781178
);
11791179
assert!(
11801180
decode_pattern(pattern).is_some(),
1181-
"v1 encoding should be decodable"
1181+
"inline encoding should be decodable"
11821182
);
11831183
assert!(
11841184
!is_hashed_pattern(pattern),
1185-
"v1 encoding should not be identified as hashed"
1185+
"inline encoding should not be identified as hashed"
11861186
);
11871187
}
11881188

@@ -1195,8 +1195,8 @@ fn pattern_above_threshold_is_hashed() {
11951195
assert!(!ids.is_empty());
11961196
let pattern = ids.values().next().unwrap();
11971197
assert_eq!(
1198-
pattern[0], HASH_VERSION,
1199-
"large group should produce v2 hashed pattern"
1198+
pattern[0], HASHED_PATTERN_TAG,
1199+
"large group should produce hashed pattern"
12001200
);
12011201
assert_eq!(
12021202
pattern.len(),
@@ -1208,7 +1208,7 @@ fn pattern_above_threshold_is_hashed() {
12081208
assert_eq!(node_count, 30, "hashed pattern should encode node_count=30");
12091209
assert!(
12101210
is_hashed_pattern(pattern),
1211-
"v2 pattern should be identified as hashed"
1211+
"hashed pattern should be identified as hashed"
12121212
);
12131213
assert!(
12141214
decode_pattern(pattern).is_none(),

0 commit comments

Comments
 (0)