Algorithm Search⌘K

Techniqueresearch

Threshold implementation

Also called TI masking.

Masking based on secret sharing with correctness, non-completeness and uniformity, which stays secure in hardware with glitches.

A threshold implementation (TI) is a form of Boolean masking for hardware. It splits each value into shares, and it splits each function into component functions, so that each component function does not read at least one share of each input. Then even if a glitch in the circuit makes a wire carry any function of the inputs of its component, that wire depends on at most all but one share, and so it is independent of the secret. Nikova, Rechberger and Rijmen proposed the method in 2006, and Nikova, Rijmen and Schläffer (2011) added the uniformity property that lets one stage feed the next. A function of algebraic degree dd needs at least d+1d + 1 shares for a first-order TI, so the AND gate and every quadratic S-box need three.

In plain words

Masking puts a secret into pieces so that no single piece says anything. In software, the device works on one piece at a time. In hardware, a gate gets all its inputs at once, and while the inputs arrive at slightly different times, the output can flicker through values that mix several pieces. Such flickers, called glitches, also use power, so a first-order attack can see a mix of all the pieces of a secret and learn the secret. A threshold implementation makes sure that no gate ever has all the pieces: each output piece is computed from the other pieces only. So even the worst flicker shows at most all but one piece, which looks random. A register stores each output before the next stage starts, so that flickers do not travel from one stage to the next.

When it applies

  • Hardware with glitches. The combinational logic between two registers can leak any function of its inputs during one clock cycle. TI assumes this, and it assumes that registers stop glitches.
  • First-order security. The classic TI targets attacks of order 1, in the model in which a probe on a wire is extended to all the inputs of the logic that drives it.
  • Functions of low degree. The number of shares grows with the algebraic degree of the function. An S-box of degree 3 is often written as a composition of quadratic functions, each with its own TI and a register after it.
  • Uniform inputs. The proof needs each input of a stage to be a uniform sharing. If a stage does not make a uniform output, the next stage needs fresh random bits.

The template

The template is the TI of z=xyz = xy for two bits xx and yy, each in three shares, with the option of two fresh bits to make the output uniform.

  1. TI-AND(x_1, x_2, x_3, y_1, y_2, y_3, optional r_1, r_2) ▷ SHARES: 3 for degree 2
  2. 1z_1 ← x_2 y_2 ⊕ x_2 y_3 ⊕ x_3 y_2 ▷ COMPONENTS: no share 1
  3. 2z_2 ← x_3 y_3 ⊕ x_1 y_3 ⊕ x_3 y_1 ▷ no share 2
  4. 3z_3 ← x_1 y_1 ⊕ x_1 y_2 ⊕ x_2 y_1 ▷ no share 3
  5. 4if UNIFORM needs it then z_1 ← z_1 ⊕ r_1; z_2 ← z_2 ⊕ r_2; z_3 ← z_3 ⊕ r_1 ⊕ r_2
  6. 5store (z_1, z_2, z_3) in a REGISTER
  7. 6return (z_1, z_2, z_3)

The holes are:

  • SHARES. The number ss of input shares. For first-order security of a function of degree dd, s≥d+1s \ge d + 1, because a product of dd shares can use dd different share indices, and it must go into a component that misses one more index.
  • COMPONENTS. The component functions f1,…,fsf_1, \ldots, f_s. Correctness: f1⊕⋯⊕fs=ff_1 \oplus \cdots \oplus f_s = f on the shares. Non-completeness: fif_i does not depend on share ii of any input. Lines L1 to L3 are correct, because the 9 products xiyjx_i y_j appear once each and their sum is (x1⊕x2⊕x3)(y1⊕y2⊕y3)=xy(x_1 \oplus x_2 \oplus x_3)(y_1 \oplus y_2 \oplus y_3) = xy.
  • UNIFORM. Uniformity: for a uniform input sharing, every sharing of the output value is equally likely. Lines L1 to L3 alone are not uniform; line L4 adds a uniform sharing of 0, which makes the output uniform.
  • REGISTER. The register after each stage stops glitches, so that the inputs of the next stage are the stored shares and nothing else.

The first-order argument: for a uniform sharing, any two of the three shares of a value are uniform and independent of it. A glitch on z1z_1 can carry any function of x2,x3,y2,y3x_2, x_3, y_2, y_3 and r1r_1, and these are independent of (x,y)(x, y). The same holds for z2z_2 and z3z_3. The proof file gives the details.

The cost is 9 AND gates and 6 XOR gates for the three components, with 2 random bits and 4 more XOR gates for line L4, and one register stage, against one AND gate unmasked.

Classic examples

The code of this page is a small abstract model of a masked gate. It works on bits in tuples, models a glitch as a probe that sees all the inputs of one component function, and enumerates all sharings; it never reads a device, a file or a clock. The tests check every claim below.

The AND gate. Over the 16 pairs of input sharings for each of the 4 pairs (x,y)(x, y), the output of lines L1 to L3 always XORs to xyxy, and each component does not change when share ii of both inputs changes. The inputs of each component have the same distribution for all 4 pairs (x,y)(x, y), so a glitch-extended probe on one output share learns nothing.

Uniformity. Without line L4, the output sharing is not uniform: for xy=0xy = 0 the sharing (0,0,0)(0, 0, 0) appears 7 times out of 16 and each of the other three sharings 3 times, and for x=y=1x = y = 1 the sharings (0,0,1)(0, 0, 1), (0,1,0)(0, 1, 0) and (1,0,0)(1, 0, 0) appear 5 times each and (1,1,1)(1, 1, 1) once. A component of the next stage that reads z1z_1 and z2z_2 then sees different distributions for xy=0xy = 0 and xy=1xy = 1, so the non-uniform output breaks the next stage. With the fresh bits of line L4, each of the 4 sharings of xyxy appears 16 times out of 64.

A two-share AND without non-completeness. The first-order AND with two shares and one random bit, c1=a1b1⊕a1b2⊕rc_1 = a_1 b_1 \oplus a_1 b_2 \oplus r and c2=a2b1⊕a2b2⊕rc_2 = a_2 b_1 \oplus a_2 b_2 \oplus r, is correct, and each output share alone is uniform, as in the ISW gadget for two shares. But the logic of c1c_1 reads both b1b_1 and b2b_2, so a glitch can show bb. The tests find that the inputs of that component have different distributions for b=0b = 0 and b=1b = 1.

Block ciphers. The 4-bit S-box of PRESENT has degree 3. With three shares, it is computed as two quadratic functions, each with a TI and a register, so that each stage has degree 2. TIs of the AES S-box use its decomposition into operations in smaller fields, with fresh bits where uniformity fails. Such designs are tested with TVLA: the expected result for a first-order TI is a first-order pass and a second-order fail.

Variations

  • More shares, no randomness. For some functions, a uniform sharing exists with more shares than d+1d + 1, or for a different split of the monomials. Classic TI searches for such sharings to avoid fresh random bits.
  • Decomposition. Write a function of high degree as a composition of quadratic or cubic functions, each with a uniform TI, so that fewer shares are needed.
  • Higher orders. Later work extends the rules to security against more probes, with more shares. The extension needs care, because an argument for one clock cycle does not cover attacks that combine values from several cycles.
  • Gadgets with t+1t + 1 shares. Other hardware masking schemes use t+1t + 1 shares and fresh random bits in each AND gadget, as in ISW, with registers placed so that no glitch combines all shares of a value. They use fewer shares than TI at the cost of more randomness.

Pitfalls

  • Missing registers. If the output of a stage goes into the next stage without a register, a glitch can travel through both stages, and a wire of the second stage can depend on all shares.
  • Non-uniform outputs. Composing non-uniform stages can leak at first order in a later stage, as the example shows. Check uniformity, or add fresh bits.
  • Shares that meet in the place and route. Two shares on wires close to each other can couple, so that the physical leak depends on both. Non-completeness on the netlist does not prevent this.
  • The synthesis tool. A tool that optimizes the logic can merge component functions, or share gates between them, and break non-completeness. Keep each component in its own module, and stop optimizations across modules.
  • Only first order. A three-share TI is first-order secure. A second-order attack, which combines two shares, finds the secret with enough traces.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

threshold_implementation.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/threshold-implementation/. The authors are listed in prov/2V6QRX.jsonl.
"""A threshold implementation of the AND of two bits with three shares.

This is a small abstract model of a masked gate, not of a device. Shares
are bits in tuples. To model glitches, a probe on the output of a
component function is extended to all the inputs of that function: the
function ``component_inputs`` returns those inputs, and the tests compare
their distributions over all input sharings. Random bits come from the
caller or from a seeded ``random.Random``.
"""

import itertools
import random

Shares = tuple[int, ...]


def sharings(v: int, n: int) -> list[Shares]:
    """All n-share Boolean sharings of the bit v, that is 2^(n-1) tuples."""
    return [s for s in itertools.product((0, 1), repeat=n) if sum(s) % 2 == v]


def ti_and(x: Shares, y: Shares, r: tuple[int, int] | None = None) -> Shares:
    """Three output shares of x AND y from three shares of x and of y.

    Component i does not read share i of x or y (non-completeness). If r
    is given, the bits (r1, r2, r1 xor r2) are added to make the output
    sharing uniform. Cost: Theta(1); 9 AND gates and 2 random bits.
    """
    x1, x2, x3 = x
    y1, y2, y3 = y
    z1 = (x2 & y2) ^ (x2 & y3) ^ (x3 & y2)  # @line L1
    z2 = (x3 & y3) ^ (x1 & y3) ^ (x3 & y1)  # @line L2
    z3 = (x1 & y1) ^ (x1 & y2) ^ (x2 & y1)  # @line L3
    if r is not None:  # @line L4
        z1, z2, z3 = z1 ^ r[0], z2 ^ r[1], z3 ^ r[0] ^ r[1]
    out = (z1, z2, z3)  # @line L5
    return out  # @line L6


def component_inputs(x: Shares, y: Shares, r: tuple[int, int] | None = None) -> list[tuple[int, ...]]:
    """What a glitch-extended probe on each output share of ti_and can see."""
    x1, x2, x3 = x
    y1, y2, y3 = y
    rr = () if r is None else r
    return [
        (x2, x3, y2, y3) + rr[:1],
        (x1, x3, y1, y3) + rr[1:2],
        (x1, x2, y1, y2) + rr,
    ]


def two_share_and(a: Shares, b: Shares, r: int) -> tuple[Shares, list[tuple[int, ...]]]:
    """A first-order AND with two shares and one random bit, computed without a register.

    It is correct, and each output share alone is uniform, but each
    component reads both shares of b, so a glitch can combine them.
    Returns the output shares and the inputs of each component.
    """
    a1, a2 = a
    b1, b2 = b
    c1 = (a1 & b1) ^ (a1 & b2) ^ r
    c2 = (a2 & b1) ^ (a2 & b2) ^ r
    return (c1, c2), [(a1, b1, b2, r), (a2, b1, b2, r)]


def masked_and(rng: random.Random, x: int, y: int) -> Shares:
    """Share the bits x and y uniformly, apply ti_and with fresh bits, and return the shares."""
    def share(v: int) -> Shares:
        s1, s2 = rng.randrange(2), rng.randrange(2)
        return (s1, s2, v ^ s1 ^ s2)

    return ti_and(share(x), share(y), (rng.randrange(2), rng.randrange(2)))
test_threshold_implementation.py 121 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/threshold-implementation/. The authors are listed in prov/2V6QRX.jsonl.

import itertools
import random
from collections import Counter

from threshold_implementation import component_inputs, masked_and, sharings, ti_and, two_share_and

BITS = list(itertools.product((0, 1), repeat=2))
RANDS = list(itertools.product((0, 1), repeat=2))


def xor(s):
    return sum(s) % 2


def test_sharings() -> None:
    for v in (0, 1):
        s = sharings(v, 3)
        assert len(s) == 4 and all(xor(t) == v for t in s)


def test_correct_for_all_sharings() -> None:
    for x, y in BITS:
        for xs in sharings(x, 3):
            for ys in sharings(y, 3):
                assert xor(ti_and(xs, ys)) == x & y
                for r in RANDS:
                    assert xor(ti_and(xs, ys, r)) == x & y


def test_non_completeness() -> None:
    # Output share i does not change when share i of both inputs changes.
    for xs in itertools.product((0, 1), repeat=3):
        for ys in itertools.product((0, 1), repeat=3):
            z = ti_and(xs, ys)
            for i in range(3):
                for fx, fy in BITS:
                    xs2 = tuple(v ^ (fx if j == i else 0) for j, v in enumerate(xs))
                    ys2 = tuple(v ^ (fy if j == i else 0) for j, v in enumerate(ys))
                    assert ti_and(xs2, ys2)[i] == z[i]


def glitch_views(component, rands):
    """For each secret pair, the distribution of what one extended probe sees."""
    return {
        (x, y): Counter(
            component_inputs(xs, ys, r)[component]
            for xs in sharings(x, 3)
            for ys in sharings(y, 3)
            for r in rands
        )
        for x, y in BITS
    }


def test_glitch_extended_probe_is_secure() -> None:
    for rands in ([None], RANDS):
        for i in range(3):
            views = glitch_views(i, rands)
            first = views[(0, 0)]
            assert all(v == first for v in views.values())


def test_two_share_and_leaks_with_glitches() -> None:
    views = {}
    for a, b in BITS:
        outs = Counter()
        seen = Counter()
        for a_s in sharings(a, 2):
            for b_s in sharings(b, 2):
                for r in (0, 1):
                    c, comps = two_share_and(a_s, b_s, r)
                    assert xor(c) == a & b
                    outs[c[0]] += 1
                    seen[comps[0]] += 1
        assert outs == Counter({0: 4, 1: 4})  # each output share alone is uniform
        views[(a, b)] = seen
    # The inputs of one component give b = b1 xor b2.
    assert views[(0, 0)] != views[(0, 1)]


def output_counts(x, y, rands):
    return Counter(
        ti_and(xs, ys, r) for xs in sharings(x, 3) for ys in sharings(y, 3) for r in rands
    )


def test_output_is_not_uniform_without_fresh_bits() -> None:
    for x, y in BITS[:3]:
        assert output_counts(x, y, [None]) == Counter(
            {(0, 0, 0): 7, (0, 1, 1): 3, (1, 0, 1): 3, (1, 1, 0): 3}
        )
    assert output_counts(1, 1, [None]) == Counter(
        {(0, 0, 1): 5, (0, 1, 0): 5, (1, 0, 0): 5, (1, 1, 1): 1}
    )


def test_non_uniform_output_leaks_in_the_next_stage() -> None:
    # A next component that reads output shares 1 and 2 sees different laws for x AND y = 0 and 1.
    def pair_law(x, y, rands):
        return Counter(z[:2] for z, n in output_counts(x, y, rands).items() for _ in range(n))

    assert pair_law(0, 0, [None]) != pair_law(1, 1, [None])
    assert pair_law(0, 0, RANDS) == pair_law(1, 1, RANDS) == Counter(
        {(0, 0): 16, (0, 1): 16, (1, 0): 16, (1, 1): 16}
    )


def test_fresh_bits_make_the_output_uniform() -> None:
    for x, y in BITS:
        counts = output_counts(x, y, RANDS)
        assert counts == Counter({s: 16 for s in sharings(x & y, 3)})


def test_random_oracle() -> None:
    rng = random.Random(20240509)
    for _ in range(1000):
        x, y = rng.randrange(2), rng.randrange(2)
        assert xor(masked_and(rng, x, y)) == x & y

Exercises

  1. Check that lines L1 to L3 are correct by expanding (x1⊕x2⊕x3)(y1⊕y2⊕y3)(x_1 \oplus x_2 \oplus x_3)(y_1 \oplus y_2 \oplus y_3).
  2. Show that no correct and non-complete sharing of xyxy with two shares exists.
  3. Show that adding (r1,r2,r1⊕r2)(r_1, r_2, r_1 \oplus r_2) to any sharing of zz, with uniform r1r_1 and r2r_2, gives a uniform sharing of zz.
  4. Count the output sharings of lines L1 to L3 for x=y=1x = y = 1 by hand, and find a pair of output shares whose distribution depends on xyxy.
  5. A function has degree 3. How many shares does a first-order TI need, and how can a decomposition into quadratic functions reduce this?
Proof: The three-share AND is correct and non-complete, resists one glitch-extended probe, and is uniform with two fresh bitsfull

Let xx and yy be bits with uniform sharings: x1,x2x_1, x_2 are independent uniform bits and x3=x⊕x1⊕x2x_3 = x \oplus x_1 \oplus x_2, and the same for yy with bits independent of those of xx. Let r1,r2r_1, r_2 be independent uniform bits, independent of the rest. Lines L1 to L3 compute

z1=x2y2⊕x2y3⊕x3y2,z2=x3y3⊕x1y3⊕x3y1,z3=x1y1⊕x1y2⊕x2y1.z_1 = x_2 y_2 \oplus x_2 y_3 \oplus x_3 y_2, \quad z_2 = x_3 y_3 \oplus x_1 y_3 \oplus x_3 y_1, \quad z_3 = x_1 y_1 \oplus x_1 y_2 \oplus x_2 y_1.

Theorem 1 (correctness). z1⊕z2⊕z3=xyz_1 \oplus z_2 \oplus z_3 = xy, with or without line L4.

Proof. The nine terms are the products xiyjx_i y_j for all i,j∈{1,2,3}i, j \in \{1, 2, 3\}, each once: z1z_1 has (2,2),(2,3),(3,2)(2,2), (2,3), (3,2), z2z_2 has (3,3),(1,3),(3,1)(3,3), (1,3), (3,1), and z3z_3 has (1,1),(1,2),(2,1)(1,1), (1,2), (2,1). Their XOR is (x1⊕x2⊕x3)(y1⊕y2⊕y3)=xy(x_1 \oplus x_2 \oplus x_3)(y_1 \oplus y_2 \oplus y_3) = xy by the distributive law over GF(2). Line L4 adds r1⊕r2⊕(r1⊕r2)=0r_1 \oplus r_2 \oplus (r_1 \oplus r_2) = 0. □\square

Theorem 2 (non-completeness). For each ii, the function that computes ziz_i, with line L4 included, does not read xix_i or yiy_i.

Proof. By inspection of the terms above; line L4 adds only r1r_1, r2r_2 or both. □\square

Lemma 3. For a uniform sharing (x1,x2,x3)(x_1, x_2, x_3) of xx, any two shares are independent uniform bits, whatever xx is.

Proof. (x1,x2)(x_1, x_2) is uniform by assumption. x3=(x⊕x1)⊕x2x_3 = (x \oplus x_1) \oplus x_2 with x2x_2 uniform and independent of x1x_1, so given x1x_1 the bit x3x_3 is uniform; hence (x1,x3)(x_1, x_3) is uniform. The case (x2,x3)(x_2, x_3) is the same with x1x_1 and x2x_2 exchanged. □\square

Theorem 4 (one glitch-extended probe). Model a glitch on output share ii as a probe that sees all the inputs of the logic of ziz_i: V1=(x2,x3,y2,y3,r1)V_1 = (x_2, x_3, y_2, y_3, r_1), V2=(x1,x3,y1,y3,r2)V_2 = (x_1, x_3, y_1, y_3, r_2) and V3=(x1,x2,y1,y2,r1,r2)V_3 = (x_1, x_2, y_1, y_2, r_1, r_2), without the rr when line L4 is not used. The distribution of each ViV_i is the same for all four pairs (x,y)(x, y), and so is the distribution of every function of ViV_i, which includes every value that a glitch on ziz_i can take.

Proof. By Lemma 3, the two shares of xx in ViV_i are uniform and independent, and so are the two shares of yy. The shares of xx, the shares of yy and the bits rr are mutually independent. So ViV_i is uniform on its range, whatever (x,y)(x, y) is. A function of a random variable whose law does not depend on (x,y)(x, y) also has a law that does not depend on (x,y)(x, y). □\square

Theorem 5 (uniformity with fresh bits). With line L4, for each (x,y)(x, y), the output (z1,z2,z3)(z_1, z_2, z_3) is uniform on the four sharings of xyxy.

Proof. Let (w1,w2,w3)(w_1, w_2, w_3) be the output of lines L1 to L3, a random sharing of xyxy by Theorem 1, independent of (r1,r2)(r_1, r_2). The output after line L4 is (w1⊕r1,w2⊕r2,w3⊕r1⊕r2)(w_1 \oplus r_1, w_2 \oplus r_2, w_3 \oplus r_1 \oplus r_2). For fixed ww, the map (r1,r2)↦(w1⊕r1,w2⊕r2)(r_1, r_2) \mapsto (w_1 \oplus r_1, w_2 \oplus r_2) is a bijection onto {0,1}2\{0, 1\}^2, and the third share is fixed by the other two and by xyxy. So given ww, the output is uniform on the four sharings of xyxy, and that law does not depend on ww. □\square

Proposition 6 (no uniformity without fresh bits). Without line L4, for (x,y)=(1,1)(x, y) = (1, 1) the sharing (1,1,1)(1, 1, 1) of xy=1xy = 1 has probability 116\frac{1}{16}, not 14\frac14, so the output is not uniform. The pair (z1,z2)(z_1, z_2) has a law for (x,y)=(0,0)(x, y) = (0, 0) that differs from its law for (1,1)(1, 1).

Proof. There are 4×4=164 \times 4 = 16 equally likely pairs of input sharings. For x=y=1x = y = 1, the output is (1,1,1)(1, 1, 1) only if z1=z2=1z_1 = z_2 = 1. Listing the 16 cases gives exactly one, x=(1,1,1)x = (1, 1, 1) and y=(1,1,1)y = (1, 1, 1): then z1=1⊕1⊕1=1z_1 = 1 \oplus 1 \oplus 1 = 1 and likewise z2=z3=1z_2 = z_3 = 1. For every other case at least one of z1,z2z_1, z_2 is 0; the tests enumerate the cases and find the counts 5, 5, 5 and 1 for the sharings (0,0,1)(0,0,1), (0,1,0)(0,1,0), (1,0,0)(1,0,0) and (1,1,1)(1,1,1). For (x,y)=(0,0)(x, y) = (0, 0), the counts are 7, 3, 3 and 3 for (0,0,0)(0,0,0), (0,1,1)(0,1,1), (1,0,1)(1,0,1) and (1,1,0)(1,1,0). The pair (z1,z2)=(1,1)(z_1, z_2) = (1, 1) then has probability 316\frac{3}{16} for (0,0)(0, 0) and 116\frac{1}{16} for (1,1)(1, 1). ■\blacksquare

So lines L1 to L6 with the fresh bits give a correct and non-complete sharing that resists one glitch-extended probe for uniform input sharings, and whose output is a uniform sharing that the next stage can use with the same argument. The code computes the same expressions, and its tests enumerate all input sharings and random bits to confirm each statement.

References

  1. Svetla Nikova, Christian Rechberger and Vincent Rijmen, Threshold implementations against side-channel attacks and glitches, ICICS 2006, LNCS 4307, pages 529 to 545, the definition of a threshold implementation by correctness and non-completeness, and the proof that it resists first-order attacks with glitches
  2. Svetla Nikova, Vincent Rijmen and Martin Schläffer, Secure hardware implementation of nonlinear functions in the presence of glitches, Journal of Cryptology 24(2), 2011, pages 292 to 321, the properties of correctness, non-completeness and uniformity, the bound of d + 1 shares for a function of degree d, and the sharing of the AND gate
  3. Yuval Ishai, Amit Sahai and David Wagner, Private circuits: securing hardware against probing attacks, CRYPTO 2003, LNCS 2729, pages 463 to 481, the t-probing model and the masked AND gate with fresh random bits, which threshold implementations are compared with

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH2 full proof
  • correctness: no review record reviews/2V6QRX/proof-correctness.yaml
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples