1
//! Pseudorandom generator for hash programs and parts thereof
2

            
3
use crate::constraints::{self, Pass, RegisterWriter, Validator};
4
use crate::program::{Instruction, InstructionVec, Opcode};
5
use crate::rand::RngBuffer;
6
use crate::register::{RegisterId, RegisterSet};
7
use crate::scheduler::{InstructionPlan, Scheduler};
8
use crate::Error;
9
use rand_core::RngCore;
10

            
11
/// The `model` attempts to document HashX program generation choices,
12
/// separate from the main body of the program generator.
13
mod model {
14
    use crate::constraints::Pass;
15
    use crate::generator::OpcodeSelector;
16
    use crate::program::Opcode;
17
    use crate::scheduler::SubCycle;
18

            
19
    /// Choose the next [`OpcodeSelector`].
20
    ///
21
    /// HashX uses a repeating pattern of opcode selectors, based on the
22
    /// current sub_cycle timestamp simulated for instruction fetch/decode.
23
    /// This gives a roughly constant program layout since most instructions do
24
    /// only take one sub-cycle to decode, but we do skip a sub-cycle every time
25
    /// there's a 2-op instruction (wide mul, target, branch). The repeating
26
    /// selector pattern is designed to ensure these skipped selectors are
27
    /// always the Normal type, so the overall number of multiply and branch
28
    /// instructions stays constant.
29
    ///
30
    /// The basic pattern is `(Mul, Normal, Normal)` but `Branch`, `Target`,
31
    /// and `WideMul` operations are mixed in at fixed locations in a 36-cycle
32
    /// repetition.
33
    ///
34
    /// Normal cycles are all replaced by `ImmediateSrc` if this is the retry
35
    /// pass, so that retries won't need to attempt source register selection
36
    /// in this case.
37
    #[inline(always)]
38
5054335
    pub(super) fn choose_opcode_selector(pass: Pass, sub_cycle: SubCycle) -> OpcodeSelector {
39
5054335
        let n = sub_cycle.as_usize() % 36;
40
5054335
        if n == 1 {
41
147680
            OpcodeSelector::Target
42
4906655
        } else if n == 19 {
43
147680
            OpcodeSelector::Branch
44
4758975
        } else if n == 12 || n == 24 {
45
295360
            OpcodeSelector::WideMul
46
4463615
        } else if (n % 3) == 0 {
47
1480115
            OpcodeSelector::Mul
48
        } else {
49
2983500
            match pass {
50
2982915
                Pass::Original => OpcodeSelector::Normal,
51
585
                Pass::Retry => OpcodeSelector::ImmediateSrc,
52
            }
53
        }
54
5054335
    }
55

            
56
    /// Opcode choices for [`OpcodeSelector::WideMul`]
57
    pub(super) const WIDE_MUL_OPS_TABLE: [Opcode; 2] = [Opcode::SMulH, Opcode::UMulH];
58

            
59
    /// Opcode choices for [`OpcodeSelector::ImmediateSrc`]
60
    pub(super) const IMMEDIATE_SRC_OPS_TABLE: [Opcode; 4] = [
61
        Opcode::Rotate,
62
        Opcode::XorConst,
63
        Opcode::AddConst,
64
        Opcode::AddConst,
65
    ];
66

            
67
    /// Opcode choices for [`OpcodeSelector::Normal`]
68
    pub(super) const NORMAL_OPS_TABLE: [Opcode; 8] = [
69
        Opcode::Rotate,
70
        Opcode::XorConst,
71
        Opcode::AddConst,
72
        Opcode::AddConst,
73
        Opcode::Sub,
74
        Opcode::Xor,
75
        Opcode::XorConst,
76
        Opcode::AddShift,
77
    ];
78

            
79
    /// Masks for [`super::Instruction::Branch`] always have a constant
80
    /// number of bits set.
81
    ///
82
    /// The probability of taking a branch is approximately
83
    /// `1.0 / (1 << BRANCH_MASK_BIT_WEIGHT)`
84
    pub(super) const BRANCH_MASK_BIT_WEIGHT: usize = 4;
85
}
86

            
87
/// Program generator
88
pub(crate) struct Generator<'r, R: RngCore> {
89
    /// The program generator wraps a random number generator, via [`RngBuffer`].
90
    rng: RngBuffer<'r, R>,
91

            
92
    /// Keep track of when execution units and registers will be ready,
93
    /// and ultimately generate a list of candidate available registers
94
    /// for any particular cycle.
95
    scheduler: Scheduler,
96

            
97
    /// Additional constraints on the entire program and pieces thereof
98
    /// are implemented in this separate Validator module.
99
    validator: Validator,
100

            
101
    /// Last [`Opcode`] chosen by an instruction selector
102
    ///
103
    /// Some of the instruction selectors have the notion of avoiding
104
    /// duplicates, but `HashX` designs this check based on the sequence of
105
    /// selector results rather than the sequence of committed instructions.
106
    last_selector_result_op: Option<Opcode>,
107
}
108

            
109
impl<'r, R: RngCore> Generator<'r, R> {
110
    /// Create a fresh program generator from a random number generator state.
111
    #[inline(always)]
112
9230
    pub(crate) fn new(rng: &'r mut R) -> Self {
113
9230
        Generator {
114
9230
            rng: RngBuffer::new(rng),
115
9230
            scheduler: Scheduler::new(),
116
9230
            validator: Validator::new(),
117
9230
            last_selector_result_op: None,
118
9230
        }
119
9230
    }
120

            
121
    /// Pick a pseudorandom register from a RegisterSet.
122
    ///
123
    /// Returns `Err(())` if the set is empty. Consumes one `u32` from the `Rng`
124
    /// only if the set contains more than one item.
125
    ///
126
    /// The choice is perfectly uniform only if the register set is a power of
127
    /// two length. Uniformity is not critical here.
128
    #[inline(always)]
129
7212010
    fn select_register(&mut self, reg_options: &RegisterSet) -> Result<RegisterId, ()> {
130
7212010
        match reg_options.len() {
131
5070
            0 => Err(()),
132
75140
            1 => Ok(reg_options.index(0)),
133
7131800
            num_options => {
134
7131800
                let num_options: u32 = num_options
135
7131800
                    .try_into()
136
7131800
                    .expect("register set length always fits in u32");
137
7131800
                let index = self.rng.next_u32() % num_options;
138
7131800
                Ok(reg_options.index(
139
7131800
                    index
140
7131800
                        .try_into()
141
7131800
                        .expect("register set length always fits in usize"),
142
7131800
                ))
143
            }
144
        }
145
7212010
    }
146

            
147
    /// Pick a pseudorandom operation from a list of options.
148
    ///
149
    /// The `options` slice must be between 2 and 255 options in length.
150
    /// For uniformity and efficiency it's best if the length is also a power
151
    /// of two. All actual operation lists used by HashX have power-of-two
152
    /// lengths.
153
    #[inline(always)]
154
3278860
    fn select_op<'a, T, const SIZE: usize>(&mut self, options: &'a [T; SIZE]) -> &'a T {
155
3278860
        &options[(self.rng.next_u8() as usize) % options.len()]
156
3278860
    }
157

            
158
    /// Generate a random u32 bit mask, with a constant number of bits set.
159
    ///
160
    /// This uses an iterative algorithm that selects one bit at a time
161
    /// using a u8 from the Rng for each, discarding duplicates.
162
    #[inline(always)]
163
147680
    fn select_constant_weight_bit_mask(&mut self, num_ones: usize) -> u32 {
164
147680
        let mut result = 0_u32;
165
147680
        let mut count = 0;
166
768690
        while count < num_ones {
167
621010
            let bit = 1 << ((self.rng.next_u8() as usize) % 32);
168
621010
            if (result & bit) == 0 {
169
590720
                result |= bit;
170
590720
                count += 1;
171
590720
            }
172
        }
173
147680
        result
174
147680
    }
175

            
176
    /// Generate random nonzero values.
177
    ///
178
    /// Iteratively picks a random u32, masking it, and discarding results that
179
    /// would be all zero.
180
    #[inline(always)]
181
1651910
    fn select_nonzero_u32(&mut self, mask: u32) -> u32 {
182
        loop {
183
1657955
            let result = self.rng.next_u32() & mask;
184
1657955
            if result != 0 {
185
1651910
                return result;
186
6045
            }
187
        }
188
1651910
    }
189

            
190
    /// Generate an entire program.
191
    ///
192
    /// Generates instructions into a provided [`Vec`] until the generator
193
    /// state can't be advanced any further. Runs the whole-program validator.
194
    /// Returns with [`Error::ProgramConstraints`] if the program fails these
195
    /// checks. This happens in normal use on a small fraction of seed values.
196
    #[inline(always)]
197
9230
    pub(crate) fn generate_program(&mut self, output: &mut InstructionVec) -> Result<(), Error> {
198
9230
        assert!(output.is_empty());
199
4722250
        while !output.is_full() {
200
4722250
            match self.generate_instruction() {
201
                Err(()) => break,
202
4722250
                Ok((inst, regw)) => {
203
4722250
                    let state_advance = self.commit_instruction_state(&inst, regw);
204
4722250
                    output.push(inst);
205
4722250
                    if let Err(()) = state_advance {
206
9230
                        break;
207
4713020
                    }
208
                }
209
            }
210
        }
211
9230
        self.validator
212
9230
            .check_whole_program(&self.scheduler, output)
213
9248
            .map_err(|()| Error::ProgramConstraints)
214
9230
    }
215

            
216
    /// Generate the next instruction.
217
    ///
218
    /// This is a multi-pass approach, starting with a [`Pass::Original`] that
219
    /// normally succeeds, followed by a [`Pass::Retry`] with simplifications
220
    /// to improve success rate, followed by a timing stall that advances the
221
    /// simulated cycle count forward and tries again with the benefit of newly
222
    /// available registers in the schedule.
223
    ///
224
    /// This only returns `Err(())` if we've hit a stopping condition for the
225
    /// program.
226
    #[inline(always)]
227
4722250
    fn generate_instruction(&mut self) -> Result<(Instruction, RegisterWriter), ()> {
228
        loop {
229
4723420
            if let Ok(result) = self.instruction_gen_attempt(Pass::Original) {
230
4719520
                return Ok(result);
231
3900
            }
232
3900
            if let Ok(result) = self.instruction_gen_attempt(Pass::Retry) {
233
2730
                return Ok(result);
234
1170
            }
235
1170
            self.scheduler.stall()?;
236
        }
237
4722250
    }
238

            
239
    /// Choose an opcode using the current [`OpcodeSelector`], subject to
240
    /// stateful constraints on adjacent opcode choices.
241
    #[inline(always)]
242
4727320
    fn choose_opcode(&mut self, pass: Pass) -> Opcode {
243
4727320
        let op = loop {
244
5054335
            let sub_cycle = self.scheduler.instruction_stream_sub_cycle();
245
5054335
            let op = model::choose_opcode_selector(pass, sub_cycle).apply(self);
246
5054335
            if let Ok(()) = constraints::opcode_pair_allowed(self.last_selector_result_op, op) {
247
4727320
                break op;
248
327015
            }
249
        };
250
4727320
        self.last_selector_result_op = Some(op);
251
4727320
        op
252
4727320
    }
253

            
254
    /// Make one attempt at instruction generation.
255
    ///
256
    /// This picks an [`OpcodeSelector`], chooses an opcode, then finishes
257
    /// choosing the opcode-specific parts of the instruction. Each of these
258
    /// choices affects the Rng state, and may fail if conditions are not met.
259
    #[inline(always)]
260
4727320
    fn instruction_gen_attempt(&mut self, pass: Pass) -> Result<(Instruction, RegisterWriter), ()> {
261
4727320
        let op = self.choose_opcode(pass);
262
4727320
        let plan = self.scheduler.instruction_plan(op)?;
263
4727320
        let (inst, regw) = self.choose_instruction_with_opcode_plan(op, pass, &plan)?;
264
4722250
        debug_assert_eq!(inst.opcode(), op);
265
4722250
        self.scheduler.commit_instruction_plan(&plan, &inst);
266
4722250
        Ok((inst, regw))
267
4727320
    }
268

            
269
    /// Choose only a source register, depending on the opcode and timing plan
270
    #[inline(never)]
271
2780050
    fn choose_src_reg(
272
2780050
        &mut self,
273
2780050
        op: Opcode,
274
2780050
        timing_plan: &InstructionPlan,
275
2780050
    ) -> Result<RegisterId, ()> {
276
22240400
        let src_set = RegisterSet::from_filter(|src| {
277
22240400
            self.scheduler
278
22240400
                .register_available(src, timing_plan.cycle_issued())
279
22240400
        });
280
2780050
        let src_set = constraints::src_registers_allowed(src_set, op);
281
2780050
        self.select_register(&src_set)
282
2780050
    }
283

            
284
    /// Choose both a source and destination register using a normal
285
    /// [`RegisterWriter`] for two-operand instructions.
286
    #[inline(always)]
287
2484690
    fn choose_src_dst_regs(
288
2484690
        &mut self,
289
2484690
        op: Opcode,
290
2484690
        pass: Pass,
291
2484690
        writer_info_fn: fn(RegisterId) -> RegisterWriter,
292
2484690
        timing_plan: &InstructionPlan,
293
2484690
    ) -> Result<(RegisterId, RegisterId, RegisterWriter), ()> {
294
2484690
        let src = self.choose_src_reg(op, timing_plan)?;
295
2484690
        let writer_info = writer_info_fn(src);
296
2484690
        let dst = self.choose_dst_reg(op, pass, writer_info, Some(src), timing_plan)?;
297
2479815
        Ok((src, dst, writer_info))
298
2484690
    }
299

            
300
    /// Choose both a source and destination register, with a custom
301
    /// [`RegisterWriter`] constraint that doesn't depend on source
302
    /// register choice.
303
    #[inline(always)]
304
295360
    fn choose_src_dst_regs_with_writer_info(
305
295360
        &mut self,
306
295360
        op: Opcode,
307
295360
        pass: Pass,
308
295360
        writer_info: RegisterWriter,
309
295360
        timing_plan: &InstructionPlan,
310
295360
    ) -> Result<(RegisterId, RegisterId), ()> {
311
295360
        let src = self.choose_src_reg(op, timing_plan)?;
312
295360
        let dst = self.choose_dst_reg(op, pass, writer_info, Some(src), timing_plan)?;
313
295360
        Ok((src, dst))
314
295360
    }
315

            
316
    /// Choose a destination register only, using source and writer info
317
    /// as well as the current state of the validator.
318
    #[inline(never)]
319
4431960
    fn choose_dst_reg(
320
4431960
        &mut self,
321
4431960
        op: Opcode,
322
4431960
        pass: Pass,
323
4431960
        writer_info: RegisterWriter,
324
4431960
        src: Option<RegisterId>,
325
4431960
        timing_plan: &InstructionPlan,
326
4431960
    ) -> Result<RegisterId, ()> {
327
4431960
        let validator = self
328
4431960
            .validator
329
4431960
            .dst_registers_allowed(op, pass, writer_info, src);
330
35455680
        let dst_set = RegisterSet::from_filter(|dst| {
331
35455680
            self.scheduler
332
35455680
                .register_available(dst, timing_plan.cycle_issued())
333
21918975
                && validator.check(dst)
334
35455680
        });
335
4431960
        self.select_register(&dst_set)
336
4431960
    }
337

            
338
    /// With an [`Opcode`] and an execution unit timing plan already in mind,
339
    /// generate the other pieces necessary to fully describe an
340
    /// [`Instruction`].
341
    ///
342
    /// This can fail if register selection fails.
343
    #[inline(always)]
344
4727320
    fn choose_instruction_with_opcode_plan(
345
4727320
        &mut self,
346
4727320
        op: Opcode,
347
4727320
        pass: Pass,
348
4727320
        plan: &InstructionPlan,
349
4727320
    ) -> Result<(Instruction, RegisterWriter), ()> {
350
4727320
        Ok(match op {
351
147680
            Opcode::Target => (Instruction::Target, RegisterWriter::None),
352

            
353
147680
            Opcode::Branch => (
354
147680
                Instruction::Branch {
355
147680
                    mask: self.select_constant_weight_bit_mask(model::BRANCH_MASK_BIT_WEIGHT),
356
147680
                },
357
147680
                RegisterWriter::None,
358
147680
            ),
359

            
360
            Opcode::UMulH => {
361
148005
                let regw = RegisterWriter::UMulH(self.rng.next_u32());
362
148005
                let (src, dst) = self.choose_src_dst_regs_with_writer_info(op, pass, regw, plan)?;
363
148005
                (Instruction::UMulH { src, dst }, regw)
364
            }
365

            
366
            Opcode::SMulH => {
367
147355
                let regw = RegisterWriter::SMulH(self.rng.next_u32());
368
147355
                let (src, dst) = self.choose_src_dst_regs_with_writer_info(op, pass, regw, plan)?;
369
147355
                (Instruction::SMulH { src, dst }, regw)
370
            }
371

            
372
            Opcode::Mul => {
373
1480115
                let regw = RegisterWriter::Mul;
374
1480115
                let (src, dst, regw) = self.choose_src_dst_regs(op, pass, regw, plan)?;
375
1475630
                (Instruction::Mul { src, dst }, regw)
376
            }
377

            
378
            Opcode::Sub => {
379
328770
                let regw = RegisterWriter::AddSub;
380
328770
                let (src, dst, regw) = self.choose_src_dst_regs(op, pass, regw, plan)?;
381
328770
                (Instruction::Sub { src, dst }, regw)
382
            }
383

            
384
            Opcode::Xor => {
385
357435
                let regw = RegisterWriter::Xor;
386
357435
                let (src, dst, regw) = self.choose_src_dst_regs(op, pass, regw, plan)?;
387
357435
                (Instruction::Xor { src, dst }, regw)
388
            }
389

            
390
            Opcode::AddShift => {
391
318370
                let regw = RegisterWriter::AddSub;
392
318370
                let left_shift = (self.rng.next_u32() & 3) as u8;
393
318370
                let (src, dst, regw) = self.choose_src_dst_regs(op, pass, regw, plan)?;
394
317980
                (
395
317980
                    Instruction::AddShift {
396
317980
                        src,
397
317980
                        dst,
398
317980
                        left_shift,
399
317980
                    },
400
317980
                    regw,
401
317980
                )
402
            }
403

            
404
            Opcode::AddConst => {
405
641680
                let regw = RegisterWriter::AddConst;
406
641680
                let src = self.select_nonzero_u32(u32::MAX) as i32;
407
641680
                let dst = self.choose_dst_reg(op, pass, regw, None, plan)?;
408
641550
                (Instruction::AddConst { src, dst }, regw)
409
            }
410

            
411
            Opcode::XorConst => {
412
649415
                let regw = RegisterWriter::XorConst;
413
649415
                let src = self.select_nonzero_u32(u32::MAX) as i32;
414
649415
                let dst = self.choose_dst_reg(op, pass, regw, None, plan)?;
415
649350
                (Instruction::XorConst { src, dst }, regw)
416
            }
417

            
418
            Opcode::Rotate => {
419
360815
                let regw = RegisterWriter::Rotate;
420
360815
                let right_rotate: u8 = self.select_nonzero_u32(63) as u8;
421
360815
                let dst = self.choose_dst_reg(op, pass, regw, None, plan)?;
422
360815
                (Instruction::Rotate { dst, right_rotate }, regw)
423
            }
424
        })
425
4727320
    }
426

            
427
    /// Commit all state modifications associated with a chosen instruction
428
    /// that's certainly being written to the final program.
429
    ///
430
    /// Returns `Ok(())` on success or `Err(())` if the new state would no
431
    /// longer be valid for program generation and we're done writing code.
432
    #[inline(always)]
433
4722250
    fn commit_instruction_state(
434
4722250
        &mut self,
435
4722250
        inst: &Instruction,
436
4722250
        regw: RegisterWriter,
437
4722250
    ) -> Result<(), ()> {
438
4722250
        self.validator.commit_instruction(inst, regw);
439
4722250
        self.scheduler.advance_instruction_stream(inst.opcode())
440
4722250
    }
441
}
442

            
443
/// HashX uses a limited number of different instruction selection strategies,
444
/// chosen based on the sub-cycle timing of our position in the
445
/// instruction stream.
446
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
447
enum OpcodeSelector {
448
    /// Main ALU instruction chooser, picks from Add/Sub/Xor/Rotate
449
    Normal,
450
    /// Retry pass if Normal fails, instructions with immediate source only
451
    ImmediateSrc,
452
    /// Only multiply instructions, no additional register selection work.
453
    Mul,
454
    /// Wide multiply instructions, randomly choosing signedness
455
    WideMul,
456
    /// Only branch targets
457
    Target,
458
    /// Only branch instructions
459
    Branch,
460
}
461

            
462
impl OpcodeSelector {
463
    /// Apply the selector, advancing the Rng state and returning an Opcode.
464
    #[inline(always)]
465
5054335
    fn apply<R: RngCore>(&self, gen: &mut Generator<'_, R>) -> Opcode {
466
5054335
        match self {
467
147680
            OpcodeSelector::Target => Opcode::Target,
468
147680
            OpcodeSelector::Branch => Opcode::Branch,
469
1480115
            OpcodeSelector::Mul => Opcode::Mul,
470
2982915
            OpcodeSelector::Normal => *gen.select_op(&model::NORMAL_OPS_TABLE),
471
585
            OpcodeSelector::ImmediateSrc => *gen.select_op(&model::IMMEDIATE_SRC_OPS_TABLE),
472
295360
            OpcodeSelector::WideMul => *gen.select_op(&model::WIDE_MUL_OPS_TABLE),
473
        }
474
5054335
    }
475
}