|
| 1 | +package org.bouncycastle.pqc.crypto.slhdsa; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | + |
| 5 | +import org.bouncycastle.util.Arrays; |
| 6 | + |
| 7 | +class Fors |
| 8 | +{ |
| 9 | + SLHDSAEngine engine; |
| 10 | + |
| 11 | + public Fors(SLHDSAEngine engine) |
| 12 | + { |
| 13 | + this.engine = engine; |
| 14 | + } |
| 15 | + |
| 16 | + // Input: Secret seed SK.seed, start index s, target node height z, public seed PK.seed, address ADRS |
| 17 | + // Output: n-byte root node - top node on Stack |
| 18 | + byte[] treehash(byte[] skSeed, int s, int z, byte[] pkSeed, ADRS adrsParam) |
| 19 | + { |
| 20 | + LinkedList<NodeEntry> stack = new LinkedList<NodeEntry>(); |
| 21 | + |
| 22 | + if (s % (1 << z) != 0) |
| 23 | + { |
| 24 | + return null; |
| 25 | + } |
| 26 | + |
| 27 | + ADRS adrs = new ADRS(adrsParam); |
| 28 | + |
| 29 | + for (int idx = 0; idx < (1 << z); idx++) |
| 30 | + { |
| 31 | + adrs.setType(ADRS.FORS_PRF); |
| 32 | + adrs.setKeyPairAddress(adrsParam.getKeyPairAddress()); |
| 33 | + adrs.setTreeHeight(0); |
| 34 | + adrs.setTreeIndex(s + idx); |
| 35 | + |
| 36 | + byte[] sk = engine.PRF(pkSeed, skSeed, adrs); |
| 37 | + |
| 38 | + adrs.changeType(ADRS.FORS_TREE); |
| 39 | + |
| 40 | + byte[] node = engine.F(pkSeed, adrs, sk); |
| 41 | + |
| 42 | + adrs.setTreeHeight(1); |
| 43 | + |
| 44 | + // while ( Top node on Stack has same height as node ) |
| 45 | + while (!stack.isEmpty() |
| 46 | + && ((NodeEntry)stack.get(0)).nodeHeight == adrs.getTreeHeight()) |
| 47 | + { |
| 48 | + adrs.setTreeIndex((adrs.getTreeIndex() - 1) / 2); |
| 49 | + NodeEntry current = ((NodeEntry)stack.remove(0)); |
| 50 | + |
| 51 | + node = engine.H(pkSeed, adrs, current.nodeValue, node); |
| 52 | + //topmost node is now one layer higher |
| 53 | + adrs.setTreeHeight(adrs.getTreeHeight() + 1); |
| 54 | + } |
| 55 | + |
| 56 | + stack.add(0, new NodeEntry(node, adrs.getTreeHeight())); |
| 57 | + } |
| 58 | + |
| 59 | + return ((NodeEntry)stack.get(0)).nodeValue; |
| 60 | + } |
| 61 | + |
| 62 | + public SIG_FORS[] sign(byte[] md, byte[] skSeed, byte[] pkSeed, ADRS paramAdrs) |
| 63 | + { |
| 64 | + ADRS adrs = new ADRS(paramAdrs); |
| 65 | + |
| 66 | + int[] idxs = message_to_idxs(md, engine.K, engine.A); |
| 67 | + SIG_FORS[] sig_fors = new SIG_FORS[engine.K]; |
| 68 | +// compute signature elements |
| 69 | + int t = engine.T; |
| 70 | + for (int i = 0; i < engine.K; i++) |
| 71 | + { |
| 72 | +// get next index |
| 73 | + int idx = idxs[i]; |
| 74 | +// pick private key element |
| 75 | + adrs.setType(ADRS.FORS_PRF); |
| 76 | + adrs.setKeyPairAddress(paramAdrs.getKeyPairAddress()); |
| 77 | + adrs.setTreeHeight(0); |
| 78 | + adrs.setTreeIndex(i * t + idx); |
| 79 | + |
| 80 | + byte[] sk = engine.PRF(pkSeed, skSeed, adrs); |
| 81 | + |
| 82 | + adrs.changeType(ADRS.FORS_TREE); |
| 83 | + |
| 84 | + byte[][] authPath = new byte[engine.A][]; |
| 85 | +// compute auth path |
| 86 | + for (int j = 0; j < engine.A; j++) |
| 87 | + { |
| 88 | + int s = (idx / (1 << j)) ^ 1; |
| 89 | + authPath[j] = treehash(skSeed, i * t + s * (1 << j), j, pkSeed, adrs); |
| 90 | + } |
| 91 | + sig_fors[i] = new SIG_FORS(sk, authPath); |
| 92 | + } |
| 93 | + return sig_fors; |
| 94 | + } |
| 95 | + |
| 96 | + public byte[] pkFromSig(SIG_FORS[] sig_fors, byte[] message, byte[] pkSeed, ADRS adrs) |
| 97 | + { |
| 98 | + byte[][] node = new byte[2][]; |
| 99 | + byte[][] root = new byte[engine.K][]; |
| 100 | + int t = engine.T; |
| 101 | + |
| 102 | + int[] idxs = message_to_idxs(message, engine.K, engine.A); |
| 103 | + // compute roots |
| 104 | + for (int i = 0; i < engine.K; i++) |
| 105 | + { |
| 106 | + // get next index |
| 107 | + int idx = idxs[i]; |
| 108 | + // compute leaf |
| 109 | + byte[] sk = sig_fors[i].getSK(); |
| 110 | + adrs.setTreeHeight(0); |
| 111 | + adrs.setTreeIndex(i * t + idx); |
| 112 | + node[0] = engine.F(pkSeed, adrs, sk); |
| 113 | + // compute root from leaf and AUTH |
| 114 | + byte[][] authPath = sig_fors[i].getAuthPath(); |
| 115 | + |
| 116 | + adrs.setTreeIndex(i * t + idx); |
| 117 | + for (int j = 0; j < engine.A; j++) |
| 118 | + { |
| 119 | + adrs.setTreeHeight(j + 1); |
| 120 | + if (((idx / (1 << j)) % 2) == 0) |
| 121 | + { |
| 122 | + adrs.setTreeIndex(adrs.getTreeIndex() / 2); |
| 123 | + node[1] = engine.H(pkSeed, adrs, node[0], authPath[j]); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + adrs.setTreeIndex((adrs.getTreeIndex() - 1) / 2); |
| 128 | + node[1] = engine.H(pkSeed, adrs, authPath[j], node[0]); |
| 129 | + } |
| 130 | + node[0] = node[1]; |
| 131 | + } |
| 132 | + root[i] = node[0]; |
| 133 | + } |
| 134 | + ADRS forspkADRS = new ADRS(adrs); // copy address to create FTS public key address |
| 135 | + forspkADRS.setType(ADRS.FORS_PK); |
| 136 | + forspkADRS.setKeyPairAddress(adrs.getKeyPairAddress()); |
| 137 | + return engine.T_l(pkSeed, forspkADRS, Arrays.concatenate(root)); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Interprets m as SPX_FORS_HEIGHT-bit unsigned integers. |
| 142 | + * Assumes m contains at least SPX_FORS_HEIGHT * SPX_FORS_TREES bits. |
| 143 | + * Assumes indices has space for SPX_FORS_TREES integers. |
| 144 | + */ |
| 145 | + static int[] message_to_idxs(byte[] msg, int fors_trees, int fors_height) |
| 146 | + { |
| 147 | + int offset = 0; |
| 148 | + int[] idxs = new int[fors_trees]; |
| 149 | + for (int i = 0; i < fors_trees; i++) |
| 150 | + { |
| 151 | + idxs[i] = 0; |
| 152 | + for (int j = 0; j < fors_height; j++) |
| 153 | + { |
| 154 | + idxs[i] ^= ((msg[offset >> 3] >> (offset & 0x7)) & 0x1) << j; |
| 155 | + offset++; |
| 156 | + } |
| 157 | + } |
| 158 | + return idxs; |
| 159 | + } |
| 160 | +} |
0 commit comments