zkboo 0.1.0

An implementation of the ZKBoo protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
// SPDX-License-Identifier: LGPL-3.0-or-later

//! Implementation of word references for generic ZKBoo backends.

use crate::{
    backend::{Backend, BooleanWordRef},
    utils::RcPtrMut,
    word::{CompositeWord, Word, WordIdx, WordLike},
};
use alloc::vec::Vec;
use core::ops::{
    AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Mul, MulAssign, Neg,
    Not, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};
use core::{array, ops::Add};

/// Reference to an allocated word in a generic backend.
#[derive(Debug)]
pub struct WordRef<B: Backend, W: Word, const N: usize = 1> {
    backend: RcPtrMut<B>,
    idx: WordIdx<W, N>,
}

impl<B: Backend, W: Word, const N: usize> WordRef<B, W, N> {
    /// Creates a new [WordRef] with the given backend and index.
    /// The reference count for the index in the backend is increased by 1.
    pub(super) fn new(backend: RcPtrMut<B>, idx: WordIdx<W, N>) -> Self {
        backend.borrow_mut().increase_refcount(idx);
        return Self { backend, idx };
    }

    /// Allocates a new input [WordRef] in the same backend as this [WordRef],
    /// initialized to the given word.
    pub(super) fn input<C: WordLike<W, N>>(backend: &RcPtrMut<B>, word: C) -> Self {
        let idx = backend.borrow_mut().input(word.to_word());
        return WordRef::new(backend.clone(), idx);
    }

    /// Allocates a new [WordRef] in the same backend as this [WordRef], without initialisation.
    pub(super) fn alloc(backend: &RcPtrMut<B>) -> Self {
        let idx = backend.borrow_mut().alloc();
        return WordRef::new(backend.clone(), idx);
    }

    /// Allocates a new [WordRef] in the same backend as this [WordRef],
    /// initialised to the given constant word.
    pub(super) fn alloc_constant<U: WordLike<W, N>>(backend: &RcPtrMut<B>, word: U) -> Self {
        let idx = backend.borrow_mut().alloc_constant(word.to_word());
        return WordRef::new(backend.clone(), idx);
    }

    /// Allocates a new [WordRef] in the same backend as this [WordRef],
    /// initialized to zero.
    fn alloc_zero(backend: &RcPtrMut<B>) -> Self {
        let idx = backend
            .borrow_mut()
            .alloc_constant(CompositeWord::<W, N>::ZERO);
        return WordRef::new(backend.clone(), idx);
    }

    /// The type-tagged index of the word in the memory manager.
    pub fn idx(&self) -> WordIdx<W, N> {
        self.idx
    }

    /// Destructures the [WordRef] into its memory manager and index.
    fn destructure(self) -> (RcPtrMut<B>, WordIdx<W, N>) {
        return (self.backend.clone(), self.idx);
    }

    /// Helper function for unary shift operations on [WordRef]s.
    fn unop_shift<F>(in_: WordRef<B, W, N>, shift: usize, op: F) -> WordRef<B, W, N>
    where
        F: Fn(&mut B, WordIdx<W, N>, usize, WordIdx<W, N>),
    {
        let (backend, in_) = in_.destructure();
        let out = Self::alloc(&backend);
        op(&mut backend.borrow_mut(), in_, shift, out.idx);
        return out;
    }

    /// Helper function for unary operations on [WordRef]s.
    fn unop<F>(in_: WordRef<B, W, N>, op: F) -> WordRef<B, W, N>
    where
        F: Fn(&mut B, WordIdx<W, N>, WordIdx<W, N>),
    {
        let (backend, in_) = in_.destructure();
        let out = Self::alloc(&backend);
        op(&mut backend.borrow_mut(), in_, out.idx);
        return out;
    }

    /// Helper function for binary operations on [WordRef]s.
    fn binop<F>(inl: WordRef<B, W, N>, inr: WordRef<B, W, N>, op: F) -> WordRef<B, W, N>
    where
        F: Fn(&mut B, WordIdx<W, N>, WordIdx<W, N>, WordIdx<W, N>),
    {
        let (backend, inl) = inl.destructure();
        let (backend_, inr) = inr.destructure();
        assert_eq!(
            backend, backend_,
            "Backends of both operands must be the same"
        );
        let out = Self::alloc(&backend);
        op(&mut backend.borrow_mut(), inl, inr, out.idx);
        return out;
    }

    /// Helper function for binary operations on [WordRef]s with a constant right-hand side.
    fn binop_const<F, RHS: WordLike<W, N>>(
        in_: WordRef<B, W, N>,
        rhs: RHS,
        op: F,
    ) -> WordRef<B, W, N>
    where
        F: Fn(&mut B, WordIdx<W, N>, CompositeWord<W, N>, WordIdx<W, N>),
    {
        let (backend, in_) = in_.destructure();
        let out = Self::alloc(&backend);
        op(&mut backend.borrow_mut(), in_, rhs.to_word(), out.idx);
        return out;
    }
}

impl<B: Backend, W: Word, const N: usize> Clone for WordRef<B, W, N> {
    /// Clones the [WordRef], increasing the reference count in the memory manager.
    fn clone(&self) -> Self {
        return WordRef::new(self.backend.clone(), self.idx);
    }
}

impl<B: Backend, W: Word, const N: usize> Drop for WordRef<B, W, N> {
    /// Drops the [WordRef], decreasing the reference count in the memory manager.
    fn drop(&mut self) {
        self.backend.borrow_mut().decrease_refcount(self.idx);
    }
}

impl<B: Backend, W: Word, const N: usize> WordRef<B, W, N> {
    const WIDTH: usize = W::WIDTH * N;
    const ZERO: CompositeWord<W, N> = CompositeWord::<W, N>::ZERO;
    const ONE: CompositeWord<W, N> = CompositeWord::<W, N>::ONE;
    const MAX: CompositeWord<W, N> = CompositeWord::<W, N>::MAX;

    /// Consumes this [WordRef] to return one set to zero, with the same word width.
    pub fn into_zero(self) -> Self {
        return self & Self::ZERO;
    }

    /// Consumes this [WordRef] to return one set to the given constant word,
    /// with the same word width.
    pub fn into_const_same_width(self, word: CompositeWord<W, N>) -> Self {
        return self.into_zero() ^ word;
    }

    /// Consumes this [WordRef] to return one set to the given machine word,
    /// possibly with different word width.
    pub fn into_const_word<V: Word>(self, word: V) -> WordRef<B, V, 1> {
        return self.lsw().into_zero().cast::<V>() ^ word;
    }

    /// Consumes this [WordRef] to return one set to the given composite word,
    /// possibly with different word width and length.
    pub fn into_const_composite_word<V: Word, const M: usize>(
        self,
        word: CompositeWord<V, M>,
    ) -> WordRef<B, V, M> {
        return self.lsw().into_zero().cast::<V>().tile() ^ word;
    }

    /// Consumes this [WordRef] to return one set to the given boolean value,
    /// with the same word width.
    pub fn into_const_bool(self, value: bool) -> BooleanWordRef<B> {
        return BooleanWordRef::new(self.into_const_word(value as u8));
    }

    /// Allocates a new [WordRef] with the same backend as this [WordRef],
    /// initialized to the given composite word.
    pub fn alloc_new_word<V: Word, const M: usize, U: WordLike<V, M>>(
        &self,
        word: U,
    ) -> WordRef<B, V, M> {
        return WordRef::alloc_constant(&self.backend, word.to_word());
    }

    /// Allocates a new [WordRef] with the same backend as this [WordRef],
    /// initialized to zero, with the given word width and length.
    pub fn alloc_new_zero<V: Word, const M: usize>(&self) -> WordRef<B, V, M> {
        return WordRef::alloc_zero(&self.backend);
    }

    /// Allocates a new [BooleanWordRef] with the same backend as this [WordRef].
    pub fn alloc_new_bool(&self, value: bool) -> BooleanWordRef<B> {
        return BooleanWordRef::new(WordRef::alloc_constant(&self.backend, value as u8));
    }

    /// Bitwise NOT operation.
    pub fn not(self) -> Self {
        return WordRef::unop(self, B::not);
    }

    /// Bitwise XOR operation.
    pub fn bitxor(self, rhs: Self) -> Self {
        return WordRef::binop(self, rhs, B::bitxor);
    }

    /// Bitwise XOR operation with a constant.
    pub fn bitxor_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> Self {
        return WordRef::binop_const(self, rhs, B::bitxor_const);
    }

    /// Bitwise AND operation.
    pub fn bitand(self, rhs: Self) -> Self {
        return WordRef::binop(self, rhs, B::bitand);
    }

    /// Bitwise AND operation with a constant.
    pub fn bitand_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> Self {
        return WordRef::binop_const(self, rhs, B::bitand_const);
    }

    /// Bitwise OR operation.
    pub fn bitor(self, rhs: Self) -> Self {
        return self.not().bitand(rhs.not()).not();
    }

    /// Bitwise OR operation with a constant.
    pub fn bitor_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> Self {
        return self.not().bitand_const(rhs.to_word().not()).not();
    }

    /// Left shift operation, where bits shifted out on the left are discarded
    /// and zero bits are shifted in on the right.
    pub fn unbounded_shl(self, shift: usize) -> Self {
        return WordRef::unop_shift(self, shift, Backend::unbounded_shl);
    }

    /// Right shift operation, where bits shifted out on the right are discarded
    /// and zero bits are shifted in on the left.
    pub fn unbounded_shr(self, shift: usize) -> Self {
        return WordRef::unop_shift(self, shift, Backend::unbounded_shr);
    }

    /// Rotate left operation.
    pub fn rotate_left(self, shift: usize) -> Self {
        return WordRef::unop_shift(self, shift, Backend::rotate_left);
    }

    /// Rotate right operation.
    pub fn rotate_right(self, shift: usize) -> Self {
        return WordRef::unop_shift(self, shift, Backend::rotate_right);
    }

    /// Overflowing left shift operation, which returns the shifted result and
    /// the bits shifted out on the left for an additional word width.
    pub fn overflowing_shl(self, shift: usize) -> (Self, Self) {
        if shift >= 2 * Self::WIDTH {
            let lo = WordRef::alloc_zero(&self.backend);
            let hi = WordRef::alloc_zero(&self.backend);
            return (lo, hi);
        }
        if shift >= Self::WIDTH {
            let lo = WordRef::alloc_zero(&self.backend);
            let hi = self << (shift - Self::WIDTH);
            return (lo, hi);
        }
        let pieces = self.rotate_left(shift);
        let mask_lo = Self::MAX << shift;
        let mask_hi = !mask_lo;
        return (pieces.clone() & mask_lo, pieces & mask_hi);
    }

    /// Reverse bits operation.
    pub fn reverse_bits(self) -> Self {
        return WordRef::unop(self, Backend::reverse_bits);
    }

    /// Swap bytes operation.
    pub fn swap_bytes(self) -> Self {
        return WordRef::unop(self, Backend::swap_bytes);
    }

    /// Returns the least significant word.
    pub fn lsw(self) -> WordRef<B, W, 1> {
        return self.into_le_words().into_iter().next().unwrap();
    }

    /// Returns the most significant word.
    pub fn msw(self) -> WordRef<B, W, 1> {
        return self.into_le_words().into_iter().last().unwrap();
    }

    /// Returns the word at the given index, where the least significant word is at index 0.
    /// If the index is out of bounds, returns a zero word.
    pub fn word_at(self, idx: usize) -> WordRef<B, W, 1> {
        let backend = self.backend.clone();
        return self
            .into_le_words()
            .into_iter()
            .nth(idx)
            .unwrap_or_else(|| WordRef::alloc_zero(&backend));
    }

    /// Returns the boolean value of the least significant bit in this word.
    pub fn lsb(self) -> BooleanWordRef<B> {
        return BooleanWordRef::new(self.lsw().cast() & 1u8);
    }

    /// Returns the boolean value of the most significant bit in this word.
    pub fn msb(self) -> BooleanWordRef<B> {
        return BooleanWordRef::new(((self.msw() >> (W::WIDTH - 1)) & W::ONE).cast());
    }

    /// Returns the boolean value of the bit at the given index in this word,
    /// where 0 is the least significant bit.
    pub fn bit_at(self, idx: usize) -> BooleanWordRef<B> {
        let word_idx = idx / W::WIDTH;
        let bit_idx = idx % W::WIDTH;
        return BooleanWordRef::new(((self.word_at(word_idx) >> bit_idx) & W::ONE).cast());
    }

    /// Consumes this [WordRef] and applies the given function to each bit in this word,
    /// from least significant to most significant.
    pub fn map_bits<F: FnMut(BooleanWordRef<B>)>(self, mut f: F) {
        for i in 0..Self::WIDTH {
            f(self.clone().bit_at(i));
        }
    }

    /// Creates a boolean mask from the given input boolean word,
    /// The mask bits are all set to the same value:
    ///
    /// - 0 if the input word is zero
    /// - 1 if the input word is non-zero
    pub fn mask(bool: BooleanWordRef<B>) -> WordRef<B, W, N> {
        let bool = bool.into();
        let mut res = bool.clone();
        // Fill one byte [8 * (SHL + XOR)]:
        for _ in 0..7 {
            res = (res << 1) ^ bool.clone();
        }
        return res.tile();
    }

    /// Creates a [WordRef] where all bits are set to zero except the least significant bit,
    /// which is set to the given boolean value.
    pub fn from_bool(bool: BooleanWordRef<B>) -> Self {
        return bool.select_const_const(Self::ONE, Self::ZERO);
    }

    /// Creates a [WordRef] from an array of [WordRef]s representing the little-endian words.
    pub fn from_le_words(refs: [WordRef<B, W, 1>; N]) -> WordRef<B, W, N> {
        assert!(N > 0, "N must be greater than 0");
        let backend = refs[0].backend.clone();
        for word_ref in refs.iter() {
            assert_eq!(
                word_ref.backend, backend,
                "All WordRefs must have the same backend"
            );
        }
        let ins = refs.map(|word_ref| word_ref.idx.into());
        let out = WordRef::alloc(&backend);
        backend.borrow_mut().from_le_words(ins, out.idx);
        return out;
    }

    /// Unpacks this [WordRef] into an array of [WordRef]s representing the little-endian words.
    pub fn into_le_words(self) -> [WordRef<B, W, 1>; N] {
        let backend = self.backend.clone();
        let in_ = self.idx;
        let outs: [_; N] = array::from_fn(|_| WordRef::<B, W, 1>::alloc(&backend));
        backend
            .borrow_mut()
            .to_le_words(in_, array::from_fn(|i| outs[i].idx));
        return outs;
    }
}

impl<B: Backend, W: Word, const N: usize> WordRef<B, W, N> {
    /// Simple carry calculation, with fixed carry-in and no carry-out.
    pub fn carry(self, g: Self, carry_in: bool) -> Self {
        let (backend, in_idx) = self.destructure();
        let out = WordRef::alloc(&backend);
        backend.borrow_mut().carry(in_idx, g.idx, carry_in, out.idx);
        return out;
    }

    /// Wrapping addition (with fixed initial carry).
    fn _wrapping_add_with_carry(self, rhs: Self, carry: bool) -> Self {
        let p = self.clone().bitxor(rhs.clone());
        let g = self.bitand(rhs);
        let carry = p.clone().carry(g, carry);
        return p.bitxor(carry);
    }

    /// Wrapping addition with a constant (with fixed initial carry).
    fn _wrapping_add_with_carry_const<RHS: WordLike<W, N>>(self, rhs: RHS, carry: bool) -> Self {
        let p = self.clone().bitxor_const(rhs);
        let g = self.bitand_const(rhs);
        let carry = p.clone().carry(g, carry);
        return p.bitxor(carry);
    }

    /// Two's complement negation.
    pub fn wrapping_neg(self) -> Self {
        return (!self)._wrapping_add_with_carry_const(Self::ZERO, true);
    }

    /// Wrapping addition.
    pub fn wrapping_add(self, rhs: Self) -> Self {
        return self._wrapping_add_with_carry(rhs, false);
    }

    /// Wrapping addition with a constant.
    pub fn wrapping_add_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> Self {
        return self._wrapping_add_with_carry_const(rhs, false);
    }

    /// Overflowing addition.
    pub fn overflowing_add(self, rhs: Self) -> (Self, BooleanWordRef<B>) {
        let sum = self.clone().wrapping_add(rhs.clone());
        let self_msb = self.msb();
        let rhs_msb = rhs.msb();
        let sum_msb = sum.clone().msb();
        let carry = (self_msb.clone() & rhs_msb.clone()) | ((self_msb ^ rhs_msb) & !sum_msb);
        return (sum, carry);
    }

    /// Overflowing addition with a constant.
    pub fn overflowing_add_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> (Self, BooleanWordRef<B>) {
        let rhs = rhs.to_word();
        let sum = self.clone().wrapping_add_const(rhs.clone());
        let self_msb = self.msb();
        let rhs_msb = rhs.msb();
        let sum_msb = sum.clone().msb();
        let carry = (self_msb.clone() & rhs_msb.clone()) | ((self_msb ^ rhs_msb) & !sum_msb);
        return (sum, carry);
    }

    /// Wrapping subtraction.
    pub fn wrapping_sub(self, rhs: Self) -> Self {
        return self._wrapping_add_with_carry(rhs.not(), true);
    }

    /// Wrapping subtraction.
    pub fn wrapping_sub_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> Self {
        let rhs = rhs.to_word();
        return self._wrapping_add_with_carry_const(!rhs, true);
    }

    /// Wrapping subtraction with borrow flag.
    pub fn overflowing_sub(self, rhs: Self) -> (Self, BooleanWordRef<B>) {
        let diff = self.clone().wrapping_sub(rhs.clone());
        // Borrow: either lhs_msb == 0 and rhs_msb == 1, or lhs_msb == rhs_msb, and diff_msb == 1.
        let self_msb = self.msb();
        let rhs_msb = rhs.msb();
        let diff_msb = diff.clone().msb();
        let borrow = (!self_msb.clone() & rhs_msb.clone()) | (!(self_msb ^ rhs_msb) & diff_msb);
        return (diff, borrow);
    }

    /// Wrapping subtraction with borrow flag and constant RHS.
    pub fn overflowing_sub_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> (Self, BooleanWordRef<B>) {
        let rhs = rhs.to_word();
        let diff = self.clone() - rhs;
        // Borrow: either lhs_msb == 0 and rhs_msb == 1, or lhs_msb == rhs_msb, and diff_msb == 1.
        let self_msb = self.msb();
        let rhs_msb = rhs.msb();
        let diff_msb = diff.clone().msb();
        let borrow = (!self_msb.clone() & rhs_msb.clone()) | (!(self_msb ^ rhs_msb) & diff_msb);
        return (diff, borrow);
    }

    /// Wrapping subtraction with borrow flag and constant LHS.
    pub fn overflowing_sub_from_const<RHS: WordLike<W, N>>(
        self,
        rhs: RHS,
    ) -> (Self, BooleanWordRef<B>) {
        let rhs = rhs.to_word();
        let diff = self.clone().wrapping_neg().wrapping_add_const(rhs);
        // Borrow: either lhs_msb == 0 and rhs_msb == 1, or lhs_msb == rhs_msb, and diff_msb == 1.
        let self_msb = self.msb();
        let rhs_msb = rhs.msb();
        let diff_msb = diff.clone().msb();
        let borrow = (self_msb.clone() & !rhs_msb.clone()) | (!(self_msb ^ rhs_msb) & diff_msb);
        return (diff, borrow);
    }

    /// Wrapping multiplication.
    pub fn wrapping_mul(mut self, mut rhs: Self) -> Self {
        let mut acc = WordRef::alloc_zero(&self.backend);
        for _ in 0..W::WIDTH {
            let rhs_bit = rhs.clone().lsb();
            acc = acc.wrapping_add(rhs_bit.select_var_const(self.clone(), Self::ZERO));
            self = self << 1;
            rhs = rhs >> 1;
        }
        return acc;
    }

    /// Wrapping multiplication with a constant.
    pub fn wrapping_mul_const<RHS: WordLike<W, N>>(mut self, rhs: RHS) -> Self {
        let mut rhs = rhs.to_word();
        let mut acc = WordRef::alloc_zero(&self.backend);
        for _ in 0..W::WIDTH {
            if rhs.lsb() {
                acc = acc.wrapping_add(self.clone());
            }
            self = self << 1;
            rhs = rhs >> 1;
        }
        return acc;
    }

    /// Wide multiplication.
    pub fn wide_mul(self, mut rhs: Self) -> (Self, Self) {
        let mut acc_hi = WordRef::alloc_zero(&self.backend);
        let mut acc_lo = WordRef::alloc_zero(&self.backend);
        let mut add_hi = WordRef::alloc_zero(&self.backend);
        let mut add_lo = self;
        let mut add_hi_lo: Self;
        let mut carry: BooleanWordRef<B>;
        for _ in 0..Self::WIDTH {
            let rhs_bit = rhs.clone().lsb();
            (acc_lo, carry) = acc_lo
                .overflowing_add(rhs_bit.clone().select_var_const(add_lo.clone(), Self::ZERO));
            acc_hi = acc_hi
                .wrapping_add(rhs_bit.select_var_const(add_hi.clone(), Self::ZERO))
                .wrapping_add(Self::from_bool(carry));
            (add_lo, add_hi_lo) = add_lo.overflowing_shl(1);
            add_hi = (add_hi << 1).bitxor(add_hi_lo);
            rhs = rhs >> 1;
        }
        return (acc_lo, acc_hi);
    }

    /// Carrying multiplication with constant rhs.
    pub fn wide_mul_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> (Self, Self) {
        let mut rhs = rhs.to_word();
        let mut acc_hi = WordRef::alloc_zero(&self.backend);
        let mut acc_lo = WordRef::alloc_zero(&self.backend);
        let mut add_hi = WordRef::alloc_zero(&self.backend);
        let mut add_lo = self;
        let mut add_hi_lo: Self;
        let mut carry: BooleanWordRef<B>;
        for _ in 0..Self::WIDTH {
            if rhs.lsb() {
                (acc_lo, carry) = acc_lo.overflowing_add(add_lo.clone());
                acc_hi = acc_hi.wrapping_add(add_hi.clone().wrapping_add(Self::from_bool(carry)));
            }
            (add_lo, add_hi_lo) = add_lo.overflowing_shl(1);
            add_hi = (add_hi << 1).bitxor(add_hi_lo);
            rhs = rhs >> 1;
        }
        return (acc_lo, acc_hi);
    }

    /// Less than comparison.
    pub fn lt(self, rhs: Self) -> BooleanWordRef<B> {
        let (_, borrow) = self.overflowing_sub(rhs);
        return borrow;
    }

    /// Greater than or equal comparison.
    pub fn ge(self, rhs: Self) -> BooleanWordRef<B> {
        return !self.lt(rhs);
    }

    /// Less than or equal comparison.
    pub fn le(self, rhs: Self) -> BooleanWordRef<B> {
        return !rhs.lt(self);
    }

    /// Greater than comparison.
    pub fn gt(self, rhs: Self) -> BooleanWordRef<B> {
        return rhs.lt(self);
    }

    /// Less than comparison with a constant.
    pub fn lt_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> BooleanWordRef<B> {
        let (_, borrow) = self.overflowing_sub_const(rhs);
        return borrow;
    }

    /// Greater than or equal comparison with a constant.
    pub fn ge_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> BooleanWordRef<B> {
        return !self.lt_const(rhs);
    }

    /// Less than or equal comparison with a constant.
    pub fn le_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> BooleanWordRef<B> {
        return !self.gt_const(rhs);
    }

    /// Greater than comparison with a constant.
    pub fn gt_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> BooleanWordRef<B> {
        let (_, borrow) = self.overflowing_sub_from_const(rhs);
        return borrow;
    }

    /// Non-zero check operation.
    pub fn is_nonzero(self) -> BooleanWordRef<B> {
        return (self.clone() | -self).msb();
    }

    /// Zero check operation.
    pub fn is_zero(self) -> BooleanWordRef<B> {
        return !self.is_nonzero();
    }

    /// Negative equality comparison.
    pub fn ne(self, rhs: Self) -> BooleanWordRef<B> {
        return (self ^ rhs).is_nonzero();
    }

    /// Equality comparison.
    pub fn eq(self, rhs: Self) -> BooleanWordRef<B> {
        return !self.ne(rhs);
    }

    /// Negative equality comparison with a constant.
    pub fn ne_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> BooleanWordRef<B> {
        return (self ^ rhs).is_nonzero();
    }

    /// Equality comparison with a constant.
    pub fn eq_const<RHS: WordLike<W, N>>(self, rhs: RHS) -> BooleanWordRef<B> {
        return !self.ne_const(rhs);
    }
}

impl<B: Backend, W: Word> WordRef<B, W, 1> {
    /// Tiles this word into a larger target word size.
    ///
    /// If the target word size is smaller, this behaves as a [WordRef::cast] instead.
    /// If the target word size is the same as this word size, the result is the same as this word.
    ///
    pub fn tile<U: Word, const M: usize>(self) -> WordRef<B, U, M> {
        // Presumes (correctly) that word sizes come in powers of two.
        let mut res: WordRef<B, U, 1> = self.cast();
        let mut width = W::WIDTH;
        while width < U::WIDTH {
            res = (res.clone() << width) ^ res;
            width *= 2;
        }
        return WordRef::from_le_words(array::from_fn(|_| res.clone()));
    }

    /// Cast to another [Word] type.
    pub fn cast<T: Word>(self) -> WordRef<B, T, 1> {
        let (backend, in_idx) = self.destructure();
        let out = WordRef::alloc(&backend);
        backend.borrow_mut().cast::<W, T>(in_idx, out.idx);
        return out;
    }

    /// Converts a word to a big-endian byte array.
    pub fn into_le_bytes(mut self) -> Vec<WordRef<B, u8, 1>> {
        let mut res = Vec::new();
        for _ in 0..(W::WIDTH / 8) {
            res.push(self.clone().cast());
            self >>= 8;
        }
        return res;
    }

    /// Converts a word to a little-endian byte array.
    pub fn into_be_bytes(self) -> Vec<WordRef<B, u8, 1>> {
        return self.into_le_bytes().into_iter().rev().collect();
    }

    /// Converts a big-endian byte array to a word of given type.
    pub fn from_be_bytes(
        bytes: Vec<WordRef<B, u8, 1>>,
    ) -> Result<WordRef<B, W, 1>, Vec<WordRef<B, u8, 1>>> {
        if bytes.len() * 8 != W::WIDTH as usize {
            return Err(bytes);
        }
        let mut it = bytes.into_iter();
        let mut res: WordRef<B, W, 1> = it.next().unwrap().cast();
        for b in it {
            res <<= 8;
            res ^= b.cast();
        }
        return Ok(res);
    }

    /// Converts a little-endian byte array to a word of given type.
    pub fn from_le_bytes(
        bytes: Vec<WordRef<B, u8, 1>>,
    ) -> Result<WordRef<B, W, 1>, Vec<WordRef<B, u8, 1>>> {
        return Self::from_be_bytes(bytes.into_iter().rev().collect());
    }
}

impl<B: Backend, W: Word, const N: usize> Not for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Bitwise NOT operation.
    fn not(self) -> Self::Output {
        return self.not();
    }
}

impl<B: Backend, W: Word, const N: usize> BitXor for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Bitwise XOR operation.
    fn bitxor(self, rhs: Self) -> Self::Output {
        return self.bitxor(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize> BitXorAssign for WordRef<B, W, N> {
    /// Bitwise XOR assignment operation.
    fn bitxor_assign(&mut self, rhs: Self) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this ^ rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> BitXor<RHS> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Bitwise XOR operation with a constant.
    fn bitxor(self, rhs: RHS) -> Self::Output {
        return self.bitxor_const(rhs.to_word());
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> BitXorAssign<RHS>
    for WordRef<B, W, N>
{
    /// Bitwise XOR assignment operation with a constant.
    fn bitxor_assign(&mut self, rhs: RHS) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this ^ rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize> BitAnd for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Bitwise AND operation.
    fn bitand(self, rhs: Self) -> Self::Output {
        return self.bitand(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize> BitAndAssign for WordRef<B, W, N> {
    /// Bitwise AND assignment operation.
    fn bitand_assign(&mut self, rhs: Self) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this & rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> BitAnd<RHS> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Bitwise AND operation with a constant.
    fn bitand(self, rhs: RHS) -> Self::Output {
        return self.bitand_const(rhs.to_word());
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> BitAndAssign<RHS>
    for WordRef<B, W, N>
{
    /// Bitwise AND assignment operation with a constant.
    fn bitand_assign(&mut self, rhs: RHS) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this & rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize> BitOr for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Bitwise OR operation.
    fn bitor(self, rhs: Self) -> Self::Output {
        return self.bitor(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize> BitOrAssign for WordRef<B, W, N> {
    /// Bitwise OR assignment operation.
    fn bitor_assign(&mut self, rhs: Self) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this | rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> BitOr<RHS> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Bitwise OR operation with a constant.
    fn bitor(self, rhs: RHS) -> Self::Output {
        return self.bitor_const(rhs.to_word());
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> BitOrAssign<RHS>
    for WordRef<B, W, N>
{
    /// Bitwise OR assignment operation with a constant.
    fn bitor_assign(&mut self, rhs: RHS) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this | rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize> Shl<usize> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Checked left shift operation.
    fn shl(self, shift: usize) -> Self::Output {
        return self.unbounded_shl(shift);
    }
}

impl<B: Backend, W: Word, const N: usize> ShlAssign<usize> for WordRef<B, W, N> {
    /// Checked left shift assignment operation.
    fn shl_assign(&mut self, shift: usize) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this.unbounded_shl(shift)) }
    }
}

impl<B: Backend, W: Word, const N: usize> Shr<usize> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Checked right shift operation.
    fn shr(self, shift: usize) -> Self::Output {
        return self.unbounded_shr(shift);
    }
}

impl<B: Backend, W: Word, const N: usize> ShrAssign<usize> for WordRef<B, W, N> {
    /// Checked right shift assignment operation.
    fn shr_assign(&mut self, shift: usize) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this.unbounded_shr(shift)) }
    }
}

impl<B: Backend, W: Word, const N: usize> Add for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Wrapping addition.
    fn add(self, rhs: Self) -> Self::Output {
        return self.wrapping_add(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize> AddAssign for WordRef<B, W, N> {
    /// Wrapping addition assignment.
    fn add_assign(&mut self, rhs: Self) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this + rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> Add<RHS> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Wrapping addition with a constant.
    fn add(self, rhs: RHS) -> Self::Output {
        return self.wrapping_add_const(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> AddAssign<RHS> for WordRef<B, W, N> {
    /// Wrapping addition assignment with a constant.
    fn add_assign(&mut self, rhs: RHS) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this + rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize> Neg for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Two's complement negation.
    fn neg(self) -> Self::Output {
        return self.wrapping_neg();
    }
}

impl<B: Backend, W: Word, const N: usize> Sub for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Wrapping subtraction.
    fn sub(self, rhs: Self) -> Self::Output {
        return self.wrapping_sub(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize> SubAssign for WordRef<B, W, N> {
    /// Wrapping subtraction assignment.
    fn sub_assign(&mut self, rhs: Self) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this - rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> Sub<RHS> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Wrapping subtraction with a constant.
    fn sub(self, rhs: RHS) -> Self::Output {
        return self.wrapping_sub_const(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> SubAssign<RHS> for WordRef<B, W, N> {
    /// Wrapping subtraction assignment with a constant.
    fn sub_assign(&mut self, rhs: RHS) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this - rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize> Mul for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Wrapping multiplication.
    fn mul(self, rhs: Self) -> Self::Output {
        return self.wrapping_mul(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize> MulAssign for WordRef<B, W, N> {
    /// Wrapping multiplication assignment.
    fn mul_assign(&mut self, rhs: Self) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this * rhs) }
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> Mul<RHS> for WordRef<B, W, N> {
    type Output = WordRef<B, W, N>;
    /// Wrapping multiplication with a constant.
    fn mul(self, rhs: RHS) -> Self::Output {
        return self.wrapping_mul_const(rhs);
    }
}

impl<B: Backend, W: Word, const N: usize, RHS: WordLike<W, N>> MulAssign<RHS> for WordRef<B, W, N> {
    /// Wrapping multiplication assignment with a constant.
    fn mul_assign(&mut self, rhs: RHS) {
        let this = unsafe { core::ptr::read(self) };
        unsafe { core::ptr::write(self, this * rhs) }
    }
}