|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | +Provides functions for manipulating sequences of trits. |
| 4 | +
|
| 5 | +Based on: |
| 6 | +https://github.com/iotaledger/iota.lib.js/blob/v0.4.2/lib/crypto/helpers/adder.js |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import absolute_import, division, print_function, \ |
| 10 | + unicode_literals |
| 11 | + |
| 12 | +from typing import Iterable, List, Optional, Sequence, Tuple |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + 'add_trits', |
| 16 | + 'int_from_trits', |
| 17 | + 'trits_from_int', |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def add_trits(left, right): |
| 22 | + # type: (Sequence[int], Sequence[int]) -> List[int] |
| 23 | + """ |
| 24 | + Adds two sequences of trits together. |
| 25 | +
|
| 26 | + The result is a list of trits equal in length to the longer of the |
| 27 | + two sequences. |
| 28 | +
|
| 29 | + Note: Overflow is possible. |
| 30 | + For example, ``add_trits([1], [1])`` returns ``[-1]``. |
| 31 | + """ |
| 32 | + target_len = max(len(left), len(right)) |
| 33 | + |
| 34 | + res = [0] * target_len |
| 35 | + left += [0] * (target_len - len(left)) |
| 36 | + right += [0] * (target_len - len(right)) |
| 37 | + |
| 38 | + carry = 0 |
| 39 | + for i in range(len(res)): |
| 40 | + res[i], carry = _full_add_trits(left[i], right[i], carry) |
| 41 | + |
| 42 | + return res |
| 43 | + |
| 44 | + |
| 45 | +def int_from_trits(trits): |
| 46 | + # type: (Iterable[int]) -> int |
| 47 | + """ |
| 48 | + Converts a sequence of trits into an integer value. |
| 49 | + """ |
| 50 | + # Normally we'd have to wrap ``enumerate`` inside ``reversed``, but |
| 51 | + # balanced ternary puts least significant digits first. |
| 52 | + return sum(base * (3 ** power) for power, base in enumerate(trits)) |
| 53 | + |
| 54 | + |
| 55 | +def trits_from_int(n, pad=1): |
| 56 | + # type: (int, Optional[int]) -> List[int] |
| 57 | + """ |
| 58 | + Returns a trit representation of an integer value. |
| 59 | +
|
| 60 | + :param n: |
| 61 | + Integer value to convert. |
| 62 | +
|
| 63 | + :param pad: |
| 64 | + Ensure the result has at least this many trits. |
| 65 | +
|
| 66 | + References: |
| 67 | + - https://dev.to/buntine/the-balanced-ternary-machines-of-soviet-russia |
| 68 | + - https://en.wikipedia.org/wiki/Balanced_ternary |
| 69 | + - https://rosettacode.org/wiki/Balanced_ternary#Python |
| 70 | + """ |
| 71 | + if n == 0: |
| 72 | + trits = [] |
| 73 | + else: |
| 74 | + quotient, remainder = divmod(n, 3) |
| 75 | + |
| 76 | + if remainder == 2: |
| 77 | + # Lend 1 to the next place so we can make this trit negative. |
| 78 | + quotient += 1 |
| 79 | + remainder = -1 |
| 80 | + |
| 81 | + trits = [remainder] + trits_from_int(quotient, pad=0) |
| 82 | + |
| 83 | + if pad: |
| 84 | + trits += [0] * max(0, pad - len(trits)) |
| 85 | + |
| 86 | + return trits |
| 87 | + |
| 88 | + |
| 89 | +def _cons_trits(left, right): |
| 90 | + # type: (int, int) -> int |
| 91 | + """ |
| 92 | + Compares two trits. If they have the same value, returns that value. |
| 93 | + Otherwise, returns 0. |
| 94 | + """ |
| 95 | + return left if left == right else 0 |
| 96 | + |
| 97 | + |
| 98 | +def _add_trits(left, right): |
| 99 | + # type: (int, int) -> int |
| 100 | + """ |
| 101 | + Adds two individual trits together. |
| 102 | +
|
| 103 | + The result is always a single trit. |
| 104 | + """ |
| 105 | + res = left + right |
| 106 | + return res if -2 < res < 2 else (res < 0) - (res > 0) |
| 107 | + |
| 108 | + |
| 109 | +def _any_trits(left, right): |
| 110 | + # type: (int, int) -> int |
| 111 | + """ |
| 112 | + Adds two individual trits together and returns a single trit |
| 113 | + indicating whether the result is positive or negative. |
| 114 | + """ |
| 115 | + res = left + right |
| 116 | + return (res > 0) - (res < 0) |
| 117 | + |
| 118 | + |
| 119 | +def _full_add_trits(left, right, carry): |
| 120 | + # type: (int, int, int) -> Tuple[int, int] |
| 121 | + """ |
| 122 | + Adds two trits together, with support for a carry trit. |
| 123 | + """ |
| 124 | + sum_both = _add_trits(left, right) |
| 125 | + cons_left = _cons_trits(left, right) |
| 126 | + cons_right = _cons_trits(sum_both, carry) |
| 127 | + |
| 128 | + return _add_trits(sum_both, carry), _any_trits(cons_left, cons_right) |
0 commit comments