Debian Codegraph @

src/bbbd.rs

raw symbols SCIP
1 //! The Bergeron-Berstel-Brlek-Duboc algorithm for finding short addition chains.
2 //!
3 //! References:
4 //! - Bergeron, Berstel, Brlek, Duboc.
5 //! ["Addition chains using continued fractions."][BBBD1989]
6 //! - [Handbook of Elliptic and Hyperelliptic Curve Cryptography][HEHCC], Chapter 9:
7 //! Exponentiation
8 //!
9 //! [BBBD1989]: https://doi.org/10.1016/0196-6774(89)90036-9
10 //! [HEHCC]: https://www.hyperelliptic.org/HEHCC/index.html
11
12 use num_bigintrust-analyzer cargo num-bigint 0.4.4 crate/::BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#;
13 use num_integerrust-analyzer cargo num-integer 0.1.46 crate/::Integerrust-analyzer cargo num-integer 0.1.46 Integer#;
14 use num_traitsrust-analyzer cargo num-traits 0.2.18 crate/::{Onerust-analyzer cargo num-traits 0.2.18 identities/One#, Zerorust-analyzer cargo num-traits 0.2.18 identities/Zero#};
15 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#, Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#};
16
17 /// A wrapper around an addition chain. Addition and multiplication operations are defined
18 /// according to the BBBD algorithm.
19 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
20 pub(superrust-analyzer cargo addchain 0.3.0 crate/) struct Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#>);
21
22 impl Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#> for Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain# {
23 type Outputrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]Output# = Selfrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`];
24
25 fn addrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]add().(mut self, k: BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#) -> Selfrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`] {
26 self.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(k rust-analyzer cargo num-bigint 0.4.4 biguint/addition/impl#[BigUint][`Add<&BigUint>`]add().+rust-analyzer cargo num-bigint 0.4.4 biguint/addition/impl#[BigUint][`Add<&BigUint>`]add(). rust-analyzer cargo num-bigint 0.4.4 biguint/addition/impl#[BigUint][`Add<&BigUint>`]add().self.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0..lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("chain is not empty"));
27 self
28 }
29 }
30
31 impl Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain#> for Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain# {
32 type Outputrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]Output# = Selfrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`];
33
34 fn mulrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul().(mut self, mut other: Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain#) -> Selfrust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`] {
35 let last = self.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0..lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("chain is not empty");
36
37 // The first element of every chain is 1, so we skip it to prevent duplicate
38 // entries in the resulting chain.
39 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(other.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0..removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]remove().(0).is_onerust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][One]is_one().());
40
41 for w in other.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0..iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() {
42 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().w rust-analyzer cargo num-bigint 0.4.4 biguint/multiplication/impl#[BigUint][`MulAssign<&BigUint>`]mul_assign().*=rust-analyzer cargo num-bigint 0.4.4 biguint/multiplication/impl#[BigUint][`MulAssign<&BigUint>`]mul_assign(). rust-analyzer cargo num-bigint 0.4.4 biguint/multiplication/impl#[BigUint][`MulAssign<&BigUint>`]mul_assign().last;
43 }
44 self.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0..appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut other.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0.);
45
46 self
47 }
48 }
49
50 pub(superrust-analyzer cargo addchain 0.3.0 crate/) fn find_shortest_chainrust-analyzer cargo addchain 0.3.0 bbbd/find_shortest_chain().(n: BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#> {
51 minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(n).0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0.
52 }
53
54 fn minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(n: BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#) -> Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain# {
55 let log_n = n.bitsrust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint]bits().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().1;
56 if n rust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][`PartialEq<Self>`]eq().==rust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][`PartialEq<Self>`]eq(). rust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][`PartialEq<Self>`]eq().BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::onerust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][One]one().() rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl().<<rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl(). rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl().log_n {
57 Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain#((0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#log_n).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|i| BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::onerust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][One]one().() rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl().<<rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl(). rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl().i).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().())
58 } else if n rust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][`PartialEq<Self>`]eq().==rust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][`PartialEq<Self>`]eq(). rust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][`PartialEq<Self>`]eq().BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(3u32) {
59 Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::onerust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][One]one().(), BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(2u32), n])
60 } else {
61 // The minchain() algorithm on page 162 of HEHCC indicates that k should be set to
62 // 2^(log(n) / 2) in the call to chain(). This is at odds with the definition of k
63 // at the bottom of page 161; the latter gives the intended result.
64 let k = &n rust-analyzer cargo num-bigint 0.4.4 biguint/division/impl#[`&BigUint`][`Div<BigUint>`]div()./rust-analyzer cargo num-bigint 0.4.4 biguint/division/impl#[`&BigUint`][`Div<BigUint>`]div(). rust-analyzer cargo num-bigint 0.4.4 biguint/division/impl#[`&BigUint`][`Div<BigUint>`]div().(BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::onerust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][One]one().() rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl().<<rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl(). rust-analyzer cargo num-bigint 0.4.4 biguint/shift/impl#[BigUint][`Shl<u64>`]shl().(log_n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Div<Self>`]div().2));
65 chainrust-analyzer cargo addchain 0.3.0 bbbd/chain().(&n, k)
66 }
67 }
68
69 fn chainrust-analyzer cargo addchain 0.3.0 bbbd/chain().(n: &BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#, k: BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#) -> Chainrust-analyzer cargo addchain 0.3.0 bbbd/Chain# {
70 let (q, r) = n.div_remrust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][Integer]div_rem().(&k);
71 if r.is_zerorust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][Zero]is_zero().() {
72 minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(k) rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul().*rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul(). rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul().minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(q)
73 } else if r.is_onerust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][One]is_one().() {
74 // We handle the r = 1 case here to prevent unnecessary recursion.
75 minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(k) rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul().*rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul(). rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul().minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(q) rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]add().+rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]add(). rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]add().r
76 } else {
77 chainrust-analyzer cargo addchain 0.3.0 bbbd/chain().(&k, r.clonerust-analyzer cargo num-bigint 0.4.4 biguint/impl#[BigUint][Clone]clone().()) rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul().*rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul(). rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Mul<Self>`]mul().minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(q) rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]add().+rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]add(). rust-analyzer cargo addchain 0.3.0 bbbd/impl#[Chain][`Add<BigUint>`]add().r
78 }
79 }
80
81 #[cfg(test)]
82 mod testsrust-analyzer cargo addchain 0.3.0 bbbd/tests/ {
83 use num_bigintrust-analyzer cargo num-bigint 0.4.4 crate/::BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#;
84
85 use superrust-analyzer cargo addchain 0.3.0 bbbd/::minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().;
86
87 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
88 fn minchain_87rust-analyzer cargo addchain 0.3.0 bbbd/tests/minchain_87().() {
89 // Example 9.37 from HEHCC.
90 let chain = minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(87u32));
91 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
92 chain.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0.,
93 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
94 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(1u32),
95 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(2u32),
96 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(3u32),
97 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(6u32),
98 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(7u32),
99 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(10u32),
100 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(20u32),
101 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(40u32),
102 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(80u32),
103 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(87u32),
104 ]
105 );
106 }
107
108 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
109 fn minchain_384rust-analyzer cargo addchain 0.3.0 bbbd/tests/minchain_384().() {
110 let chain = minchainrust-analyzer cargo addchain 0.3.0 bbbd/minchain().(BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(384u32));
111 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
112 chain.0rust-analyzer cargo addchain 0.3.0 bbbd/Chain#0.,
113 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
114 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(1u32),
115 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(2u32),
116 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(3u32),
117 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(6u32),
118 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(12u32),
119 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(24u32),
120 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(48u32),
121 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(96u32),
122 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(192u32),
123 BigUintrust-analyzer cargo num-bigint 0.4.4 biguint/BigUint#::fromrust-analyzer cargo num-bigint 0.4.4 biguint/convert/impl#[BigUint][`From<u32>`]from().(384u32),
124 ]
125 );
126 }
127 }