|
1 | 1 | package org.bouncycastle.crypto.split.test; |
2 | 2 |
|
| 3 | +import java.security.SecureRandom; |
3 | 4 | import java.util.Arrays; |
4 | 5 |
|
5 | 6 | import junit.framework.TestCase; |
6 | 7 |
|
7 | 8 | import org.bouncycastle.crypto.split.Polynomial; |
8 | 9 | import org.bouncycastle.crypto.split.PolynomialNative; |
9 | 10 | import org.bouncycastle.crypto.split.PolynomialTable; |
| 11 | +import org.bouncycastle.util.test.FixedSecureRandom; |
10 | 12 |
|
11 | 13 | public class PolynomialTest |
12 | 14 | extends TestCase |
@@ -825,67 +827,76 @@ private void testPolynoimial1(PolynomialFactory polynomialFactory) |
825 | 827 | { |
826 | 828 | Polynomial poly = polynomialFactory.newInstance(5, 2, 2); |
827 | 829 | testMatrixMultiplication(poly, TV011B_TV1_SR, TV011B_TV1_SPLITS); |
828 | | - testRecombine(poly, new byte[]{1, 2}, TV011B_TV1_1_2_SPLITS, TV011B_TV1_SECRET); |
| 830 | + testRecombine(poly, new int[]{1, 2}, TV011B_TV1_1_2_SPLITS, TV011B_TV1_SECRET); |
829 | 831 | poly = polynomialFactory.newInstance(5, 2, 4); |
830 | 832 | testMatrixMultiplication(poly, TV011B_TV2_SR, TV011B_TV2_SPLITS); |
831 | | - testRecombine(poly, new byte[]{1, 2}, TV011B_TV2_1_2_SPLITS, TV011B_TV2_SECRET); |
832 | | - testRecombine(poly, new byte[]{1, 4}, TV011B_TV2_1_4_SPLITS, TV011B_TV2_SECRET); |
833 | | - testRecombine(poly, new byte[]{3, 4}, TV011B_TV2_3_4_SPLITS, TV011B_TV2_SECRET); |
| 833 | + testRecombine(poly, new int[]{1, 2}, TV011B_TV2_1_2_SPLITS, TV011B_TV2_SECRET); |
| 834 | + testRecombine(poly, new int[]{1, 4}, TV011B_TV2_1_4_SPLITS, TV011B_TV2_SECRET); |
| 835 | + testRecombine(poly, new int[]{3, 4}, TV011B_TV2_3_4_SPLITS, TV011B_TV2_SECRET); |
834 | 836 | poly = polynomialFactory.newInstance(5, 3, 4); |
835 | 837 | testMatrixMultiplication(poly, TV011B_TV3_SR, TV011B_TV3_SPLITS); |
836 | | - testRecombine(poly, new byte[]{1, 2, 3}, TV011B_TV3_1_2_3_SPLITS, TV011B_TV3_SECRET); |
837 | | - testRecombine(poly, new byte[]{1, 2, 4}, TV011B_TV3_1_2_4_SPLITS, TV011B_TV3_SECRET); |
838 | | - testRecombine(poly, new byte[]{1, 3, 4}, TV011B_TV3_1_3_4_SPLITS, TV011B_TV3_SECRET); |
| 838 | + testRecombine(poly, new int[]{1, 2, 3}, TV011B_TV3_1_2_3_SPLITS, TV011B_TV3_SECRET); |
| 839 | + testRecombine(poly, new int[]{1, 2, 4}, TV011B_TV3_1_2_4_SPLITS, TV011B_TV3_SECRET); |
| 840 | + testRecombine(poly, new int[]{1, 3, 4}, TV011B_TV3_1_3_4_SPLITS, TV011B_TV3_SECRET); |
839 | 841 | poly = polynomialFactory.newInstance(5, 4, 4); |
840 | 842 | testMatrixMultiplication(poly, TV011B_TV4_SR, TV011B_TV4_SPLITS); |
841 | | - testRecombine(poly, new byte[]{1, 2, 3, 4}, TV011B_TV4_1_2_3_4_SPLITS, TV011B_TV4_SECRET); |
| 843 | + testRecombine(poly, new int[]{1, 2, 3, 4}, TV011B_TV4_1_2_3_4_SPLITS, TV011B_TV4_SECRET); |
842 | 844 | poly = polynomialFactory.newInstance(9, 2, 9); |
843 | 845 | testMatrixMultiplication(poly, TV011B_TV5_SR, TV011B_TV5_SPLITS); |
844 | | - testRecombine(poly, new byte[]{1, 2}, TV011B_TV5_1_2_SPLITS, TV011B_TV5_SECRET); |
845 | | - testRecombine(poly, new byte[]{8, 9}, TV011B_TV5_8_9_SPLITS, TV011B_TV5_SECRET); |
| 846 | + testRecombine(poly, new int[]{1, 2}, TV011B_TV5_1_2_SPLITS, TV011B_TV5_SECRET); |
| 847 | + testRecombine(poly, new int[]{8, 9}, TV011B_TV5_8_9_SPLITS, TV011B_TV5_SECRET); |
846 | 848 | poly = polynomialFactory.newInstance(15, 3, 5); |
847 | 849 | testMatrixMultiplication(poly, TV011B_TV6_SR, TV011B_TV6_SPLITS); |
848 | | - testRecombine(poly, new byte[]{1, 2, 3}, TV011B_TV6_1_2_3_SPLITS, TV011B_TV6_SECRET); |
849 | | - testRecombine(poly, new byte[]{2, 3, 4}, TV011B_TV6_2_3_4_SPLITS, TV011B_TV6_SECRET); |
| 850 | + testRecombine(poly, new int[]{1, 2, 3}, TV011B_TV6_1_2_3_SPLITS, TV011B_TV6_SECRET); |
| 851 | + testRecombine(poly, new int[]{2, 3, 4}, TV011B_TV6_2_3_4_SPLITS, TV011B_TV6_SECRET); |
850 | 852 | } |
851 | 853 |
|
852 | 854 | private void testPolynoimial2(PolynomialFactory polynomialFactory) |
853 | 855 | { |
854 | 856 | Polynomial poly = polynomialFactory.newInstance(5, 2, 2); |
855 | 857 | testMatrixMultiplication(poly, TV011D_TV1_SR, TV011D_TV1_SPLITS); |
856 | | - testRecombine(poly, new byte[]{1, 2}, TV011D_TV1_1_2_SPLITS, TV011D_TV1_SECRET); |
| 858 | + testRecombine(poly, new int[]{1, 2}, TV011D_TV1_1_2_SPLITS, TV011D_TV1_SECRET); |
857 | 859 | poly = polynomialFactory.newInstance(5, 2, 4); |
858 | 860 | testMatrixMultiplication(poly, TV011D_TV2_SR, TV011D_TV2_SPLITS); |
859 | | - testRecombine(poly, new byte[]{1, 2}, TV011D_TV2_1_2_SPLITS, TV011D_TV2_SECRET); |
860 | | - testRecombine(poly, new byte[]{1, 4}, TV011D_TV2_1_4_SPLITS, TV011D_TV2_SECRET); |
861 | | - testRecombine(poly, new byte[]{3, 4}, TV011D_TV2_3_4_SPLITS, TV011D_TV2_SECRET); |
| 861 | + testRecombine(poly, new int[]{1, 2}, TV011D_TV2_1_2_SPLITS, TV011D_TV2_SECRET); |
| 862 | + testRecombine(poly, new int[]{1, 4}, TV011D_TV2_1_4_SPLITS, TV011D_TV2_SECRET); |
| 863 | + testRecombine(poly, new int[]{3, 4}, TV011D_TV2_3_4_SPLITS, TV011D_TV2_SECRET); |
862 | 864 | poly = polynomialFactory.newInstance(5, 3, 4); |
863 | 865 | testMatrixMultiplication(poly, TV011D_TV3_SR, TV011D_TV3_SPLITS); |
864 | | - testRecombine(poly, new byte[]{1, 2, 3}, TV011D_TV3_1_2_3_SPLITS, TV011D_TV3_SECRET); |
865 | | - testRecombine(poly, new byte[]{1, 2, 4}, TV011D_TV3_1_2_4_SPLITS, TV011D_TV3_SECRET); |
866 | | - testRecombine(poly, new byte[]{1, 3, 4}, TV011D_TV3_1_3_4_SPLITS, TV011D_TV3_SECRET); |
| 866 | + testRecombine(poly, new int[]{1, 2, 3}, TV011D_TV3_1_2_3_SPLITS, TV011D_TV3_SECRET); |
| 867 | + testRecombine(poly, new int[]{1, 2, 4}, TV011D_TV3_1_2_4_SPLITS, TV011D_TV3_SECRET); |
| 868 | + testRecombine(poly, new int[]{1, 3, 4}, TV011D_TV3_1_3_4_SPLITS, TV011D_TV3_SECRET); |
867 | 869 | poly = polynomialFactory.newInstance(5, 4, 4); |
868 | 870 | testMatrixMultiplication(poly, TV011D_TV4_SR, TV011D_TV4_SPLITS); |
869 | | - testRecombine(poly, new byte[]{1, 2, 3, 4}, TV011D_TV4_1_2_3_4_SPLITS, TV011D_TV4_SECRET); |
| 871 | + testRecombine(poly, new int[]{1, 2, 3, 4}, TV011D_TV4_1_2_3_4_SPLITS, TV011D_TV4_SECRET); |
870 | 872 | poly = polynomialFactory.newInstance(9, 2, 9); |
871 | 873 | testMatrixMultiplication(poly, TV011D_TV5_SR, TV011D_TV5_SPLITS); |
872 | | - testRecombine(poly, new byte[]{1, 2}, TV011D_TV5_1_2_SPLITS, TV011D_TV5_SECRET); |
873 | | - testRecombine(poly, new byte[]{8, 9}, TV011D_TV5_8_9_SPLITS, TV011D_TV5_SECRET); |
| 874 | + testRecombine(poly, new int[]{1, 2}, TV011D_TV5_1_2_SPLITS, TV011D_TV5_SECRET); |
| 875 | + testRecombine(poly, new int[]{8, 9}, TV011D_TV5_8_9_SPLITS, TV011D_TV5_SECRET); |
874 | 876 | poly = polynomialFactory.newInstance(15, 3, 5); |
875 | 877 | testMatrixMultiplication(poly, TV011D_TV6_SR, TV011D_TV6_SPLITS); |
876 | | - testRecombine(poly, new byte[]{1, 2, 3}, TV011D_TV6_1_2_3_SPLITS, TV011D_TV6_SECRET); |
877 | | - testRecombine(poly, new byte[]{2, 3, 4}, TV011D_TV6_2_3_4_SPLITS, TV011D_TV6_SECRET); |
| 878 | + testRecombine(poly, new int[]{1, 2, 3}, TV011D_TV6_1_2_3_SPLITS, TV011D_TV6_SECRET); |
| 879 | + testRecombine(poly, new int[]{2, 3, 4}, TV011D_TV6_2_3_4_SPLITS, TV011D_TV6_SECRET); |
878 | 880 | } |
879 | 881 |
|
880 | 882 | static void testMatrixMultiplication(Polynomial poly, byte[][] sr, byte[][] splits) |
881 | 883 | { |
882 | | - byte[][] result = poly.createShares(sr); |
| 884 | + byte[] source = new byte[sr.length * sr[0].length]; |
| 885 | + int currentIndex = 0; |
| 886 | + |
| 887 | + for (byte[] subArray : sr) |
| 888 | + { |
| 889 | + System.arraycopy(subArray, 0, source, currentIndex, subArray.length); |
| 890 | + currentIndex += subArray.length; |
| 891 | + } |
| 892 | + SecureRandom random = new FixedSecureRandom(new FixedSecureRandom.Source[]{new FixedSecureRandom.Data(source)}); |
| 893 | + byte[][] result = poly.createShares(random); |
883 | 894 | assertEquals(Arrays.deepToString(splits), Arrays.deepToString(result)); |
884 | 895 | } |
885 | 896 |
|
886 | | - public void testRecombine(Polynomial poly, byte[] rr, byte[][] splits, byte[] secret) |
| 897 | + public void testRecombine(Polynomial poly, int[] rr, byte[][] splits, byte[] secret) |
887 | 898 | { |
888 | | - byte[] result = poly.recombine(rr, splits); |
| 899 | + byte[] result = poly.recombineShares(rr, splits); |
889 | 900 | assertTrue(Arrays.equals(secret, result)); |
890 | 901 | } |
891 | 902 | } |
0 commit comments