|
1 | 1 | import { describe, expect, it } from "bun:test"; |
| 2 | +import { randomBytes } from "node:crypto"; |
2 | 3 | import { |
3 | 4 | shuffleList, |
4 | 5 | unshuffleList, |
5 | 6 | withPollingParams, |
6 | 7 | } from "../../src/shuffle.js"; |
| 8 | +import * as referenceImplementation from "../referenceImplementation.js"; |
| 9 | + |
| 10 | +// start polling right after the call for every 1ms, throw error if after 100ms |
| 11 | +const { asyncShuffleList, asyncUnshuffleList } = withPollingParams(0, 1, 100); |
7 | 12 |
|
8 | 13 | describe("unshuffleList", () => { |
9 | 14 | const testCases: { input: Uint32Array; expected: Uint32Array }[] = [ |
@@ -33,13 +38,6 @@ describe("unshuffleList", () => { |
33 | 38 | expect(result2).toEqual(input); |
34 | 39 | }); |
35 | 40 |
|
36 | | - // start polling right after the call for every 1ms, throw error if after 100ms |
37 | | - const { asyncShuffleList, asyncUnshuffleList } = withPollingParams( |
38 | | - 0, |
39 | | - 1, |
40 | | - 100, |
41 | | - ); |
42 | | - |
43 | 41 | const testWithNFactor = async (n: number) => { |
44 | 42 | let promises: Promise<Uint32Array>[] = []; |
45 | 43 | // call asyncUnshuffleList in parallel n times |
@@ -82,4 +80,133 @@ describe("unshuffleList", () => { |
82 | 80 | } |
83 | 81 | }); |
84 | 82 |
|
85 | | -// TODO: the same tests to @chainsafe/swap-or-not-shuffle |
| 83 | +// the same tests to @chainsafe/swap-or-not-shuffle |
| 84 | +interface ShuffleTestCase { |
| 85 | + id: string; |
| 86 | + rounds: number; |
| 87 | + seed: Uint8Array; |
| 88 | + input: Uint32Array; |
| 89 | + shuffled: string; |
| 90 | + unshuffled: string; |
| 91 | +} |
| 92 | + |
| 93 | +function fromHex(hexInput: string): Uint8Array { |
| 94 | + let hex = hexInput; |
| 95 | + if (typeof hex !== "string") { |
| 96 | + throw new Error(`hex argument type ${typeof hex} must be of type string`); |
| 97 | + } |
| 98 | + |
| 99 | + if (hex.startsWith("0x")) { |
| 100 | + hex = hex.slice(2); |
| 101 | + } |
| 102 | + |
| 103 | + if (hex.length % 2 !== 0) { |
| 104 | + throw new Error(`hex string length ${hex.length} must be multiple of 2`); |
| 105 | + } |
| 106 | + |
| 107 | + const b = Buffer.from(hex, "hex"); |
| 108 | + return new Uint8Array(b.buffer, b.byteOffset, b.length); |
| 109 | +} |
| 110 | + |
| 111 | +function getInputArray(count: number): Uint32Array { |
| 112 | + return Uint32Array.from(Array.from({ length: count }, (_, i) => i)); |
| 113 | +} |
| 114 | + |
| 115 | +function buildReferenceTestCase( |
| 116 | + count: number, |
| 117 | + rounds: number, |
| 118 | +): ShuffleTestCase { |
| 119 | + const seed = randomBytes(32); |
| 120 | + const input = getInputArray(count); |
| 121 | + const shuffled = input.slice(); |
| 122 | + referenceImplementation.shuffleList(shuffled, seed, rounds); |
| 123 | + const unshuffled = input.slice(); |
| 124 | + referenceImplementation.unshuffleList(unshuffled, seed, rounds); |
| 125 | + return { |
| 126 | + id: `TestCase for ${count} indices with seed of $0x${seed.toString("hex")}`, |
| 127 | + seed, |
| 128 | + rounds, |
| 129 | + input, |
| 130 | + shuffled: Buffer.from(shuffled).toString("hex"), |
| 131 | + unshuffled: Buffer.from(unshuffled).toString("hex"), |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +describe("shuffle", () => { |
| 136 | + it("should throw for invalid seed", () => { |
| 137 | + const test = buildReferenceTestCase(10, 10); |
| 138 | + let invalidSeed = Buffer.alloc(31, 0xac); |
| 139 | + expect(() => unshuffleList(test.input, invalidSeed, test.rounds)).toThrow( |
| 140 | + "Shuffling seed must be 32 bytes long", |
| 141 | + ); |
| 142 | + invalidSeed = Buffer.alloc(33, 0xac); |
| 143 | + expect(() => unshuffleList(test.input, invalidSeed, test.rounds)).toThrow( |
| 144 | + "Shuffling seed must be 32 bytes long", |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should throw for invalid number of rounds", () => { |
| 149 | + const test = buildReferenceTestCase(10, 10); |
| 150 | + expect(() => unshuffleList(test.input, test.seed, -1)).toThrow( |
| 151 | + "Rounds must be between 0 and 255", |
| 152 | + ); |
| 153 | + expect(() => unshuffleList(test.input, test.seed, 256)).toThrow( |
| 154 | + "Rounds must be between 0 and 255", |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + /** |
| 159 | + * Leave this test commented for github runners. It fails on memory allocations. Leave in test suite |
| 160 | + * to confirm that it does work though (local tests if you want to verify) |
| 161 | + * TODO: not sure why Bun suspends creating a Buffer of 2**32 bytes |
| 162 | + * but the implementation checks for max length of input already |
| 163 | + */ |
| 164 | + it.skip("should throw for invalid input array length", () => { |
| 165 | + const test = buildReferenceTestCase(10, 10); |
| 166 | + const input = Uint32Array.from(Buffer.alloc(2 ** 32, 0xac)); |
| 167 | + expect(() => unshuffleList(input, test.seed, 100)).toThrow( |
| 168 | + "ActiveIndices must fit in a u32", |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should match spec test results", () => { |
| 173 | + const seed = |
| 174 | + "0x4fe91d85d6bc19b20413659c61f3c690a1c4d48be41cab8363a130cebabada97"; |
| 175 | + const rounds = 10; |
| 176 | + const expected = Buffer.from([ |
| 177 | + 99, 71, 51, 5, 78, 61, 12, 17, 30, 3, 59, 47, 6, 9, 1, 41, 18, 37, 55, 43, |
| 178 | + 20, 31, 38, 79, 29, 69, 70, 54, 53, 36, 34, 62, 77, 87, 39, 96, 56, 92, |
| 179 | + 16, 82, 40, 27, 58, 14, 68, 76, 80, 13, 28, 81, 64, 26, 19, 60, 90, 2, 98, |
| 180 | + 67, 66, 52, 46, 95, 49, 72, 8, 21, 75, 57, 97, 83, 84, 88, 86, 7, 74, 32, |
| 181 | + 63, 85, 23, 65, 24, 91, 0, 48, 35, 15, 44, 25, 22, 73, 93, 45, 4, 33, 89, |
| 182 | + 94, 10, 42, 11, 50, |
| 183 | + ]).toString("hex"); |
| 184 | + |
| 185 | + const result = unshuffleList(getInputArray(100), fromHex(seed), rounds); |
| 186 | + |
| 187 | + expect(Buffer.from(result).toString("hex")).toEqual(expected); |
| 188 | + }); |
| 189 | + |
| 190 | + const testCases: ShuffleTestCase[] = [ |
| 191 | + buildReferenceTestCase(8, 10), |
| 192 | + buildReferenceTestCase(16, 10), |
| 193 | + buildReferenceTestCase(16, 100), |
| 194 | + buildReferenceTestCase(256, 192), |
| 195 | + buildReferenceTestCase(256, 192), |
| 196 | + ]; |
| 197 | + |
| 198 | + for (const { id, seed, rounds, input, shuffled, unshuffled } of testCases) { |
| 199 | + it(`sync - ${id}`, () => { |
| 200 | + const unshuffledResult = unshuffleList(input, seed, rounds); |
| 201 | + const shuffledResult = shuffleList(input, seed, rounds); |
| 202 | + expect(Buffer.from(shuffledResult).toString("hex")).toEqual(shuffled); |
| 203 | + expect(Buffer.from(unshuffledResult).toString("hex")).toEqual(unshuffled); |
| 204 | + }); |
| 205 | + it(`async - ${id}`, async () => { |
| 206 | + const unshuffledResult = await asyncUnshuffleList(input, seed, rounds); |
| 207 | + const shuffledResult = await asyncShuffleList(input, seed, rounds); |
| 208 | + expect(Buffer.from(shuffledResult).toString("hex")).toEqual(shuffled); |
| 209 | + expect(Buffer.from(unshuffledResult).toString("hex")).toEqual(unshuffled); |
| 210 | + }); |
| 211 | + } |
| 212 | +}); |
0 commit comments