|
| 1 | +package org.tron.common.zksnark; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertThrows; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import org.bouncycastle.util.encoders.Hex; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | +import org.springframework.test.util.ReflectionTestUtils; |
| 18 | +import org.tron.common.zksnark.LibrustzcashParam.SpendProofParams; |
| 19 | +import org.tron.common.zksnark.LibrustzcashParam.Zip32XfvkAddressParams; |
| 20 | +import org.tron.common.zksnark.LibrustzcashParam.Zip32XskDeriveParams; |
| 21 | +import org.tron.common.zksnark.LibrustzcashParam.Zip32XskMasterParams; |
| 22 | +import org.tron.core.exception.ZksnarkException; |
| 23 | + |
| 24 | +public class ShieldedNativeValidationTest { |
| 25 | + |
| 26 | + private Librustzcash original; |
| 27 | + private Librustzcash nativeApi; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setUp() { |
| 31 | + original = (Librustzcash) ReflectionTestUtils.getField(JLibrustzcash.class, "INSTANCE"); |
| 32 | + nativeApi = mock(Librustzcash.class); |
| 33 | + ReflectionTestUtils.setField(JLibrustzcash.class, "INSTANCE", nativeApi); |
| 34 | + } |
| 35 | + |
| 36 | + @After |
| 37 | + public void tearDown() { |
| 38 | + ReflectionTestUtils.setField(JLibrustzcash.class, "INSTANCE", original); |
| 39 | + } |
| 40 | + |
| 41 | + private static byte[] validAk() { |
| 42 | + return Hex.decode("db8c9492f599646369a4855b4260fe3b957cec8e1c33de7036ec61d8059d52d1"); |
| 43 | + } |
| 44 | + |
| 45 | + private static byte[] identity(boolean sign) { |
| 46 | + byte[] ak = new byte[32]; |
| 47 | + ak[0] = 1; |
| 48 | + ak[31] = sign ? (byte) 0x80 : 0; |
| 49 | + return ak; |
| 50 | + } |
| 51 | + |
| 52 | + private static SpendProofParams spend(byte[] ak) throws ZksnarkException { |
| 53 | + byte[] path = new byte[1065]; |
| 54 | + path[0] = 32; |
| 55 | + for (int i = 0; i < 32; i++) { |
| 56 | + path[1 + i * 33] = 32; |
| 57 | + } |
| 58 | + return new SpendProofParams(1, ak, new byte[32], new byte[11], new byte[32], |
| 59 | + new byte[32], 0, new byte[32], path, new byte[32], new byte[32], new byte[192]); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void rejectsBothIdentityEncodingsBeforeNativeProof() { |
| 64 | + for (boolean sign : new boolean[]{false, true}) { |
| 65 | + ZksnarkException error = assertThrows(ZksnarkException.class, |
| 66 | + () -> spend(identity(sign))); |
| 67 | + assertEquals("Invalid spend ak: identity point", error.getMessage()); |
| 68 | + } |
| 69 | + verifyNoInteractions(nativeApi); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void rejectsInvalidAkLengths() { |
| 74 | + for (byte[] ak : new byte[][]{null, new byte[0], new byte[31], new byte[33]}) { |
| 75 | + assertThrows(ZksnarkException.class, () -> spend(ak)); |
| 76 | + } |
| 77 | + verifyNoInteractions(nativeApi); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void revalidatesSetterAndArrayMutationsAtNativeBoundary() throws Exception { |
| 82 | + SpendProofParams params = spend(validAk()); |
| 83 | + params.setAk(identity(true)); |
| 84 | + assertThrows(ZksnarkException.class, |
| 85 | + () -> JLibrustzcash.librustzcashSaplingSpendProof(params)); |
| 86 | + |
| 87 | + byte[] ak = validAk(); |
| 88 | + SpendProofParams aliased = spend(ak); |
| 89 | + System.arraycopy(identity(false), 0, ak, 0, ak.length); |
| 90 | + assertThrows(ZksnarkException.class, |
| 91 | + () -> JLibrustzcash.librustzcashSaplingSpendProof(aliased)); |
| 92 | + verifyNoInteractions(nativeApi); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void rejectsIdentityDerivedFromZeroAsk() { |
| 97 | + byte[] ak = new byte[32]; |
| 98 | + original.librustzcashAskToAk(new byte[32], ak); |
| 99 | + assertThrows(ZksnarkException.class, () -> spend(ak)); |
| 100 | + verifyNoInteractions(nativeApi); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void acceptsNormalAkWithSignBitAndPreservesNativeFailure() throws Exception { |
| 105 | + SpendProofParams params = spend(validAk()); |
| 106 | + when(nativeApi.librustzcashSaplingSpendProof( |
| 107 | + org.mockito.ArgumentMatchers.anyLong(), any(), any(), any(), any(), any(), |
| 108 | + org.mockito.ArgumentMatchers.anyLong(), any(), any(), any(), any(), any())) |
| 109 | + .thenReturn(true, false); |
| 110 | + assertTrue(JLibrustzcash.librustzcashSaplingSpendProof(params)); |
| 111 | + org.junit.Assert.assertFalse(JLibrustzcash.librustzcashSaplingSpendProof(params)); |
| 112 | + org.junit.Assert.assertArrayEquals(validAk(), params.getAk()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void validatesZip32MasterSeedBoundsAndRechecksMutation() throws Exception { |
| 117 | + assertThrows(ZksnarkException.class, |
| 118 | + () -> new Zip32XskMasterParams(null, 0, new byte[169])); |
| 119 | + assertThrows(ZksnarkException.class, |
| 120 | + () -> new Zip32XskMasterParams(new byte[32], -1, new byte[169])); |
| 121 | + assertThrows(ZksnarkException.class, |
| 122 | + () -> new Zip32XskMasterParams(new byte[32], 33, new byte[169])); |
| 123 | + Zip32XskMasterParams params = new Zip32XskMasterParams(new byte[32], 32, new byte[169]); |
| 124 | + params.setSize(33); |
| 125 | + assertThrows(ZksnarkException.class, |
| 126 | + () -> JLibrustzcash.librustzcashZip32XskMaster(params)); |
| 127 | + verifyNoInteractions(nativeApi); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void preservesEmptyAndPrefixSeedsAndHardenedChildIndices() throws Exception { |
| 132 | + byte[] output = new byte[169]; |
| 133 | + byte[] seed = new byte[32]; |
| 134 | + JLibrustzcash.librustzcashZip32XskMaster(new Zip32XskMasterParams(seed, 16, output)); |
| 135 | + verify(nativeApi).librustzcashZip32XskMaster(seed, 16, output); |
| 136 | + byte[] emptySeed = new byte[0]; |
| 137 | + JLibrustzcash.librustzcashZip32XskMaster(new Zip32XskMasterParams(emptySeed, 0, output)); |
| 138 | + verify(nativeApi).librustzcashZip32XskMaster(emptySeed, 0, output); |
| 139 | + byte[] parent = new byte[169]; |
| 140 | + JLibrustzcash.librustzcashZip32XskDerive( |
| 141 | + new Zip32XskDeriveParams(parent, Integer.MIN_VALUE, output)); |
| 142 | + verify(nativeApi).librustzcashZip32XskDerive(parent, Integer.MIN_VALUE, output); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void rejectsShortZip32KeysAndOutputBuffers() throws Exception { |
| 147 | + assertThrows(ZksnarkException.class, |
| 148 | + () -> new Zip32XskDeriveParams(new byte[168], 0, new byte[169])); |
| 149 | + assertThrows(ZksnarkException.class, |
| 150 | + () -> new Zip32XskDeriveParams(new byte[169], 0, new byte[168])); |
| 151 | + for (int field = 0; field < 4; field++) { |
| 152 | + byte[][] arrays = {new byte[169], new byte[11], new byte[11], new byte[43]}; |
| 153 | + arrays[field] = Arrays.copyOf(arrays[field], arrays[field].length - 1); |
| 154 | + assertThrows(ZksnarkException.class, |
| 155 | + () -> new Zip32XfvkAddressParams(arrays[0], arrays[1], arrays[2], arrays[3])); |
| 156 | + } |
| 157 | + Zip32XskDeriveParams derive = new Zip32XskDeriveParams(new byte[169], 0, new byte[169]); |
| 158 | + derive.setData(new byte[1]); |
| 159 | + assertThrows(ZksnarkException.class, |
| 160 | + () -> JLibrustzcash.librustzcashZip32XskDerive(derive)); |
| 161 | + Zip32XfvkAddressParams address = |
| 162 | + new Zip32XfvkAddressParams(new byte[169], new byte[11], new byte[11], new byte[43]); |
| 163 | + address.setAddr_ret(new byte[1]); |
| 164 | + assertThrows(ZksnarkException.class, |
| 165 | + () -> JLibrustzcash.librustzcashZip32XfvkAddress(address)); |
| 166 | + verifyNoInteractions(nativeApi); |
| 167 | + } |
| 168 | +} |
0 commit comments