Skip to content

Commit 187c796

Browse files
authored
30-bit bus interaction ID (#2474)
Solves #2470. Depends on #2465. Approval will only works after merging #2465. The following is a response to #2470 and motivation of this PR: Chance of collission: For 24-bits: 100 interactions: 0.000295000053831429 1000 interactions: 0.02933425835911685 10000 interactions: 0.9492338975723106 For 30-bits (theoretically the largest we could get to without changing pil code due to field prime of bb and m31): 100 interactions: 4.610036260510597e-06 1000 interactions: 0.0004650875835883195 10000 interactions: 0.04549425469529611 Neither look ideal, but I think after #2469, bus linker mode should always have 2 bus interactions only. For user defined bus interactions, they can intentionally input IDs that don't clash, so I think just using 30-bit interaction id should be good enough for 29 bits of security (under 2 interactions linker case).
1 parent 7671334 commit 187c796

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

linker/src/bus.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ use std::{
1919
use crate::{call, try_into_namespace_degree, DegreeMode, LinkerBackend, MAIN_OPERATION_NAME};
2020

2121
/// Compute a unique identifier for an interaction
22-
fn interaction_id(link_to: &LinkTo) -> u16 {
22+
fn interaction_id(link_to: &LinkTo) -> u32 {
2323
let mut hasher = Sha256::default();
2424
hasher.update(format!("{}/{}", link_to.machine, link_to.operation));
2525
let result = hasher.finalize();
26-
let mut bytes = [0u8; 2];
27-
bytes.copy_from_slice(&result[..2]);
28-
u16::from_le_bytes(bytes)
26+
let mut bytes = [0u8; 4];
27+
bytes.copy_from_slice(&result[..4]);
28+
// Ensure that interaction_id is at most 30-bits, in order to
29+
// fill in a single field element for BabyBear and M31.
30+
u32::from_le_bytes(bytes) >> 2
2931
}
3032

3133
pub struct BusLinker {
@@ -132,7 +134,7 @@ impl LinkerBackend for BusLinker {
132134
.unwrap()
133135
.into(),
134136
)),
135-
arguments: vec![(interaction_id as u32).into(), tuple, selector],
137+
arguments: vec![interaction_id.into(), tuple, selector],
136138
},
137139
));
138140
}
@@ -274,7 +276,7 @@ impl BusLinker {
274276

275277
let arguments = match selector_index {
276278
// a selector index of None means this operation is called via lookup
277-
None => vec![(interaction_id as u32).into(), latch, tuple, 0.into()],
279+
None => vec![interaction_id.into(), latch, tuple, 0.into()],
278280
// a selector index of Some means this operation is called via permutation
279281
Some(selector_index) => {
280282
let call_selector_array = namespaced_reference(
@@ -287,12 +289,7 @@ impl BusLinker {
287289
let call_selector =
288290
index_access(call_selector_array, Some((*selector_index).into()));
289291
let rhs_selector = latch * call_selector;
290-
vec![
291-
(interaction_id as u32).into(),
292-
rhs_selector,
293-
tuple,
294-
1.into(),
295-
]
292+
vec![interaction_id.into(), rhs_selector, tuple, 1.into()]
296293
}
297294
};
298295

@@ -355,7 +352,7 @@ mod test {
355352
pc' = (1 - first_step') * pc_update;
356353
pol commit call_selectors[0];
357354
std::array::map(call_selectors, std::utils::force_bool);
358-
std::protocols::bus::bus_multi_send([std::protocols::bus::BusInteraction::Send(12064, [0, pc, instr__jump_to_operation, instr__reset, instr__loop, instr_return], 1)]);
355+
std::protocols::bus::bus_multi_send([std::protocols::bus::BusInteraction::Send(454118344, [0, pc, instr__jump_to_operation, instr__reset, instr__loop, instr_return], 1)]);
359356
namespace main__rom(4);
360357
pol constant p_line = [0, 1, 2] + [2]*;
361358
pol constant p_instr__jump_to_operation = [0, 1, 0] + [0]*;
@@ -364,7 +361,7 @@ namespace main__rom(4);
364361
pol constant p_instr_return = [0]*;
365362
pol constant operation_id = [0]*;
366363
pol constant latch = [1]*;
367-
std::protocols::bus::bus_multi_receive_batch_lookup_permutation([(12064, main__rom::latch, [main__rom::operation_id, main__rom::p_line, main__rom::p_instr__jump_to_operation, main__rom::p_instr__reset, main__rom::p_instr__loop, main__rom::p_instr_return], 0)]);
364+
std::protocols::bus::bus_multi_receive_batch_lookup_permutation([(454118344, main__rom::latch, [main__rom::operation_id, main__rom::p_line, main__rom::p_instr__jump_to_operation, main__rom::p_instr__reset, main__rom::p_instr__loop, main__rom::p_instr_return], 0)]);
368365
"#;
369366

370367
let file_name = "../test_data/asm/empty_vm.asm";

0 commit comments

Comments
 (0)