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 needs at least 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 for two bits and , each in three shares, with the option of two fresh bits to make the output uniform.
TI-AND(x_1, x_2, x_3, y_1, y_2, y_3, optional r_1, r_2) ▷ SHARES: 3 for degree 2- 1
z_1 ← x_2 y_2 ⊕ x_2 y_3 ⊕ x_3 y_2 ▷ COMPONENTS: no share 1 - 2
z_2 ← x_3 y_3 ⊕ x_1 y_3 ⊕ x_3 y_1 ▷ no share 2 - 3
z_3 ← x_1 y_1 ⊕ x_1 y_2 ⊕ x_2 y_1 ▷ no share 3 - 4
if UNIFORM needs it then z_1 ← z_1 ⊕ r_1; z_2 ← z_2 ⊕ r_2; z_3 ← z_3 ⊕ r_1 ⊕ r_2 - 5
store (z_1, z_2, z_3) in a REGISTER - 6
return (z_1, z_2, z_3)
The holes are:
- SHARES. The number of input shares. For first-order security of a function of degree , , because a product of shares can use different share indices, and it must go into a component that misses one more index.
- COMPONENTS. The component functions . Correctness: on the shares. Non-completeness: does not depend on share of any input. Lines L1 to L3 are correct, because the 9 products appear once each and their sum is .
- 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 can carry any function of and , and these are independent of . The same holds for and . 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 , the output of lines L1 to L3 always XORs to , and each component does not change when share of both inputs changes. The inputs of each component have the same distribution for all 4 pairs , so a glitch-extended probe on one output share learns nothing.
Uniformity. Without line L4, the output sharing is not uniform: for the sharing appears 7 times out of 16 and each of the other three sharings 3 times, and for the sharings , and appear 5 times each and once. A component of the next stage that reads and then sees different distributions for and , so the non-uniform output breaks the next stage. With the fresh bits of line L4, each of the 4 sharings of appears 16 times out of 64.
A two-share AND without non-completeness. The first-order AND with two shares and one random bit, and , is correct, and each output share alone is uniform, as in the ISW gadget for two shares. But the logic of reads both and , so a glitch can show . The tests find that the inputs of that component have different distributions for and .
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 , 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 shares. Other hardware masking schemes use 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.
# 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
- Check that lines L1 to L3 are correct by expanding .
- Show that no correct and non-complete sharing of with two shares exists.
- Show that adding to any sharing of , with uniform and , gives a uniform sharing of .
- Count the output sharings of lines L1 to L3 for by hand, and find a pair of output shares whose distribution depends on .
- 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 and be bits with uniform sharings: are independent uniform bits and , and the same for with bits independent of those of . Let be independent uniform bits, independent of the rest. Lines L1 to L3 compute
Theorem 1 (correctness). , with or without line L4.
Proof. The nine terms are the products for all , each once: has , has , and has . Their XOR is by the distributive law over GF(2). Line L4 adds .
Theorem 2 (non-completeness). For each , the function that computes , with line L4 included, does not read or .
Proof. By inspection of the terms above; line L4 adds only , or both.
Lemma 3. For a uniform sharing of , any two shares are independent uniform bits, whatever is.
Proof. is uniform by assumption. with uniform and independent of , so given the bit is uniform; hence is uniform. The case is the same with and exchanged.
Theorem 4 (one glitch-extended probe). Model a glitch on output share as a probe that sees all the inputs of the logic of : , and , without the when line L4 is not used. The distribution of each is the same for all four pairs , and so is the distribution of every function of , which includes every value that a glitch on can take.
Proof. By Lemma 3, the two shares of in are uniform and independent, and so are the two shares of . The shares of , the shares of and the bits are mutually independent. So is uniform on its range, whatever is. A function of a random variable whose law does not depend on also has a law that does not depend on .
Theorem 5 (uniformity with fresh bits). With line L4, for each , the output is uniform on the four sharings of .
Proof. Let be the output of lines L1 to L3, a random sharing of by Theorem 1, independent of . The output after line L4 is . For fixed , the map is a bijection onto , and the third share is fixed by the other two and by . So given , the output is uniform on the four sharings of , and that law does not depend on .
Proposition 6 (no uniformity without fresh bits). Without line L4, for the sharing of has probability , not , so the output is not uniform. The pair has a law for that differs from its law for .
Proof. There are equally likely pairs of input sharings. For , the output is only if . Listing the 16 cases gives exactly one, and : then and likewise . For every other case at least one of is 0; the tests enumerate the cases and find the counts 5, 5, 5 and 1 for the sharings , , and . For , the counts are 7, 3, 3 and 3 for , , and . The pair then has probability for and for .
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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 2V6QRX Edit this page History Report an error Markdown