Skip to content

Commit 08b8d05

Browse files
committed
Add Bits and Longs classes from bc-java
1 parent e53939f commit 08b8d05

File tree

8 files changed

+169
-60
lines changed

8 files changed

+169
-60
lines changed

crypto/BouncyCastle.Android.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@
13951395
<Compile Include="src\math\field\IPolynomial.cs" />
13961396
<Compile Include="src\math\field\IPolynomialExtensionField.cs" />
13971397
<Compile Include="src\math\field\PrimeField.cs" />
1398+
<Compile Include="src\math\raw\Bits.cs" />
13981399
<Compile Include="src\math\raw\Interleave.cs" />
13991400
<Compile Include="src\math\raw\Mod.cs" />
14001401
<Compile Include="src\math\raw\Nat.cs" />
@@ -1557,6 +1558,7 @@
15571558
<Compile Include="src\util\Enums.cs" />
15581559
<Compile Include="src\util\IMemoable.cs" />
15591560
<Compile Include="src\util\Integers.cs" />
1561+
<Compile Include="src\util\Longs.cs" />
15601562
<Compile Include="src\util\MemoableResetException.cs" />
15611563
<Compile Include="src\util\Platform.cs" />
15621564
<Compile Include="src\util\Strings.cs" />

crypto/BouncyCastle.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,7 @@
13891389
<Compile Include="src\math\field\IPolynomial.cs" />
13901390
<Compile Include="src\math\field\IPolynomialExtensionField.cs" />
13911391
<Compile Include="src\math\field\PrimeField.cs" />
1392+
<Compile Include="src\math\raw\Bits.cs" />
13921393
<Compile Include="src\math\raw\Interleave.cs" />
13931394
<Compile Include="src\math\raw\Mod.cs" />
13941395
<Compile Include="src\math\raw\Nat.cs" />
@@ -1551,6 +1552,7 @@
15511552
<Compile Include="src\util\Enums.cs" />
15521553
<Compile Include="src\util\IMemoable.cs" />
15531554
<Compile Include="src\util\Integers.cs" />
1555+
<Compile Include="src\util\Longs.cs" />
15541556
<Compile Include="src\util\MemoableResetException.cs" />
15551557
<Compile Include="src\util\Platform.cs" />
15561558
<Compile Include="src\util\Strings.cs" />
@@ -1662,4 +1664,4 @@
16621664
<None Include="checklist.txt" />
16631665
</ItemGroup>
16641666
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
1665-
</Project>
1667+
</Project>

crypto/BouncyCastle.iOS.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,7 @@
13901390
<Compile Include="src\math\field\IPolynomial.cs" />
13911391
<Compile Include="src\math\field\IPolynomialExtensionField.cs" />
13921392
<Compile Include="src\math\field\PrimeField.cs" />
1393+
<Compile Include="src\math\raw\Bits.cs" />
13931394
<Compile Include="src\math\raw\Interleave.cs" />
13941395
<Compile Include="src\math\raw\Mod.cs" />
13951396
<Compile Include="src\math\raw\Nat.cs" />
@@ -1552,6 +1553,7 @@
15521553
<Compile Include="src\util\Enums.cs" />
15531554
<Compile Include="src\util\IMemoable.cs" />
15541555
<Compile Include="src\util\Integers.cs" />
1556+
<Compile Include="src\util\Longs.cs" />
15551557
<Compile Include="src\util\MemoableResetException.cs" />
15561558
<Compile Include="src\util\Platform.cs" />
15571559
<Compile Include="src\util\Strings.cs" />

crypto/crypto.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6833,6 +6833,11 @@
68336833
SubType = "Code"
68346834
BuildAction = "Compile"
68356835
/>
6836+
<File
6837+
RelPath = "src\math\raw\Bits.cs"
6838+
SubType = "Code"
6839+
BuildAction = "Compile"
6840+
/>
68366841
<File
68376842
RelPath = "src\math\raw\Interleave.cs"
68386843
SubType = "Code"
@@ -7643,6 +7648,11 @@
76437648
SubType = "Code"
76447649
BuildAction = "Compile"
76457650
/>
7651+
<File
7652+
RelPath = "src\util\Longs.cs"
7653+
SubType = "Code"
7654+
BuildAction = "Compile"
7655+
/>
76467656
<File
76477657
RelPath = "src\util\MemoableResetException.cs"
76487658
SubType = "Code"

crypto/src/math/raw/Bits.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace Org.BouncyCastle.Math.Raw
4+
{
5+
internal abstract class Bits
6+
{
7+
internal static uint BitPermuteStep(uint x, uint m, int s)
8+
{
9+
uint t = (x ^ (x >> s)) & m;
10+
return (t ^ (t << s)) ^ x;
11+
}
12+
13+
internal static ulong BitPermuteStep(ulong x, ulong m, int s)
14+
{
15+
ulong t = (x ^ (x >> s)) & m;
16+
return (t ^ (t << s)) ^ x;
17+
}
18+
19+
internal static uint BitPermuteStepSimple(uint x, uint m, int s)
20+
{
21+
return ((x & m) << s) | ((x >> s) & m);
22+
}
23+
24+
internal static ulong BitPermuteStepSimple(ulong x, ulong m, int s)
25+
{
26+
return ((x & m) << s) | ((x >> s) & m);
27+
}
28+
}
29+
}

crypto/src/math/raw/Interleave.cs

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,22 @@ internal static uint Expand16to32(uint x)
7070
internal static ulong Expand32to64(uint x)
7171
{
7272
// "shuffle" low half to even bits and high half to odd bits
73-
uint t;
74-
t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
75-
t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
76-
t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
77-
t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
73+
x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
74+
x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
75+
x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2);
76+
x = Bits.BitPermuteStep(x, 0x22222222U, 1);
7877

7978
return ((x >> 1) & M32) << 32 | (x & M32);
8079
}
8180

8281
internal static void Expand64To128(ulong x, ulong[] z, int zOff)
8382
{
8483
// "shuffle" low half to even bits and high half to odd bits
85-
ulong t;
86-
t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
87-
t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
88-
t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
89-
t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
90-
t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
84+
x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
85+
x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
86+
x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
87+
x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
88+
x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
9189

9290
z[zOff ] = (x ) & M64;
9391
z[zOff + 1] = (x >> 1) & M64;
@@ -97,28 +95,19 @@ internal static void Expand64To128(ulong[] xs, int xsOff, int xsLen, ulong[] zs,
9795
{
9896
for (int i = 0; i < xsLen; ++i)
9997
{
100-
// "shuffle" low half to even bits and high half to odd bits
101-
ulong x = xs[xsOff + i], t;
102-
t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
103-
t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
104-
t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
105-
t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
106-
t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
107-
108-
zs[zsOff++] = (x ) & M64;
109-
zs[zsOff++] = (x >> 1) & M64;
98+
Expand64To128(xs[xsOff + i], zs, zsOff);
99+
zsOff += 2;
110100
}
111101
}
112102

113103
internal static void Expand64To128Rev(ulong x, ulong[] z, int zOff)
114104
{
115105
// "shuffle" low half to even bits and high half to odd bits
116-
ulong t;
117-
t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
118-
t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
119-
t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
120-
t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
121-
t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
106+
x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
107+
x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
108+
x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
109+
x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
110+
x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
122111

123112
z[zOff] = (x ) & M64R;
124113
z[zOff + 1] = (x << 1) & M64R;
@@ -127,68 +116,62 @@ internal static void Expand64To128Rev(ulong x, ulong[] z, int zOff)
127116
internal static uint Shuffle(uint x)
128117
{
129118
// "shuffle" low half to even bits and high half to odd bits
130-
uint t;
131-
t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
132-
t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
133-
t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
134-
t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
119+
x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
120+
x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
121+
x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2);
122+
x = Bits.BitPermuteStep(x, 0x22222222U, 1);
135123
return x;
136124
}
137125

138126
internal static ulong Shuffle(ulong x)
139127
{
140128
// "shuffle" low half to even bits and high half to odd bits
141-
ulong t;
142-
t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
143-
t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
144-
t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
145-
t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
146-
t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
129+
x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
130+
x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
131+
x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
132+
x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
133+
x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
147134
return x;
148135
}
149136

150137
internal static uint Shuffle2(uint x)
151138
{
152139
// "shuffle" (twice) low half to even bits and high half to odd bits
153-
uint t;
154-
t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7));
155-
t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14));
156-
t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
157-
t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
140+
x = Bits.BitPermuteStep(x, 0x00AA00AAU, 7);
141+
x = Bits.BitPermuteStep(x, 0x0000CCCCU, 14);
142+
x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
143+
x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
158144
return x;
159145
}
160146

161147
internal static uint Unshuffle(uint x)
162148
{
163149
// "unshuffle" even bits to low half and odd bits to high half
164-
uint t;
165-
t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
166-
t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
167-
t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
168-
t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
150+
x = Bits.BitPermuteStep(x, 0x22222222U, 1);
151+
x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2);
152+
x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
153+
x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
169154
return x;
170155
}
171156

172157
internal static ulong Unshuffle(ulong x)
173158
{
174159
// "unshuffle" even bits to low half and odd bits to high half
175-
ulong t;
176-
t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
177-
t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
178-
t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
179-
t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
180-
t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
160+
x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
161+
x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
162+
x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
163+
x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
164+
x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
181165
return x;
182166
}
183167

184168
internal static uint Unshuffle2(uint x)
185169
{
186170
// "unshuffle" (twice) even bits to low half and odd bits to high half
187-
uint t;
188-
t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
189-
t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
190-
t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14));
191-
t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7));
171+
x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
172+
x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
173+
x = Bits.BitPermuteStep(x, 0x0000CCCCU, 14);
174+
x = Bits.BitPermuteStep(x, 0x00AA00AAU, 7);
192175
return x;
193176
}
194177
}

crypto/src/util/Integers.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using Org.BouncyCastle.Math.Raw;
4+
35
namespace Org.BouncyCastle.Utilities
46
{
57
public abstract class Integers
@@ -29,6 +31,20 @@ public static int NumberOfTrailingZeros(int i)
2931
return DeBruijnTZ[(uint)((i & -i) * 0x04D7651F) >> 27];
3032
}
3133

34+
public static int Reverse(int i)
35+
{
36+
i = (int)Bits.BitPermuteStepSimple((uint)i, 0x55555555U, 1);
37+
i = (int)Bits.BitPermuteStepSimple((uint)i, 0x33333333U, 2);
38+
i = (int)Bits.BitPermuteStepSimple((uint)i, 0x0F0F0F0FU, 4);
39+
return ReverseBytes(i);
40+
}
41+
42+
public static int ReverseBytes(int i)
43+
{
44+
return RotateLeft((int)((uint)i & 0xFF00FF00U), 8) |
45+
RotateLeft((int)((uint)i & 0x00FF00FFU), 24);
46+
}
47+
3248
public static int RotateLeft(int i, int distance)
3349
{
3450
return (i << distance) ^ (int)((uint)i >> -distance);

crypto/src/util/Longs.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
3+
using Org.BouncyCastle.Math.Raw;
4+
5+
namespace Org.BouncyCastle.Utilities
6+
{
7+
public abstract class Longs
8+
{
9+
public static long Reverse(long i)
10+
{
11+
i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x5555555555555555UL, 1);
12+
i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x3333333333333333UL, 2);
13+
i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x0F0F0F0F0F0F0F0FUL, 4);
14+
return ReverseBytes(i);
15+
}
16+
17+
[CLSCompliantAttribute(false)]
18+
public static ulong Reverse(ulong i)
19+
{
20+
i = Bits.BitPermuteStepSimple(i, 0x5555555555555555UL, 1);
21+
i = Bits.BitPermuteStepSimple(i, 0x3333333333333333UL, 2);
22+
i = Bits.BitPermuteStepSimple(i, 0x0F0F0F0F0F0F0F0FUL, 4);
23+
return ReverseBytes(i);
24+
}
25+
26+
public static long ReverseBytes(long i)
27+
{
28+
return RotateLeft((long)((ulong)i & 0xFF000000FF000000UL), 8) |
29+
RotateLeft((long)((ulong)i & 0x00FF000000FF0000UL), 24) |
30+
RotateLeft((long)((ulong)i & 0x0000FF000000FF00UL), 40) |
31+
RotateLeft((long)((ulong)i & 0x000000FF000000FFUL), 56);
32+
}
33+
34+
[CLSCompliantAttribute(false)]
35+
public static ulong ReverseBytes(ulong i)
36+
{
37+
return RotateLeft(i & 0xFF000000FF000000UL, 8) |
38+
RotateLeft(i & 0x00FF000000FF0000UL, 24) |
39+
RotateLeft(i & 0x0000FF000000FF00UL, 40) |
40+
RotateLeft(i & 0x000000FF000000FFUL, 56);
41+
}
42+
43+
public static long RotateLeft(long i, int distance)
44+
{
45+
return (i << distance) ^ (long)((ulong)i >> -distance);
46+
}
47+
48+
[CLSCompliantAttribute(false)]
49+
public static ulong RotateLeft(ulong i, int distance)
50+
{
51+
return (i << distance) ^ (i >> -distance);
52+
}
53+
54+
public static long RotateRight(long i, int distance)
55+
{
56+
return (long)((ulong)i >> distance) ^ (i << -distance);
57+
}
58+
59+
[CLSCompliantAttribute(false)]
60+
public static ulong RotateRight(ulong i, int distance)
61+
{
62+
return (i >> distance) ^ (i << -distance);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)