Skip to content

Commit cbf0bca

Browse files
author
gefeili
committed
Optimise the initialisation of Ascon Hash
1 parent 242b098 commit cbf0bca

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

crypto/src/crypto/digests/AsconDigest.cs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,33 @@ public enum AsconParameters
1616

1717
public AsconDigest(AsconParameters parameters)
1818
{
19+
asconParameters = parameters;
1920
switch (parameters)
2021
{
2122
case AsconParameters.AsconHash:
2223
ASCON_PB_ROUNDS = 12;
23-
ASCON_IV = (((ulong)(ASCON_HASH_RATE * 8) << 48) |
24-
((ulong)(ASCON_PA_ROUNDS) << 40) |
25-
((ulong)(ASCON_HASH_BYTES * 8)));
2624
algorithmName = "Ascon-Hash";
2725
break;
2826
case AsconParameters.AsconHashA:
2927
ASCON_PB_ROUNDS = 8;
30-
ASCON_IV = (((ulong)(ASCON_HASH_RATE * 8) << 48) |
31-
((ulong)(ASCON_PA_ROUNDS) << 40) |
32-
((ulong)(ASCON_PA_ROUNDS - ASCON_PB_ROUNDS) << 32) |
33-
((ulong)(ASCON_HASH_BYTES * 8)));
3428
algorithmName = "Ascon-HashA";
3529
break;
3630
case AsconParameters.AsconXof:
3731
ASCON_PB_ROUNDS = 12;
38-
ASCON_IV = (((ulong)(ASCON_HASH_RATE * 8) << 48) |
39-
((ulong)(ASCON_PA_ROUNDS) << 40));
4032
algorithmName = "Ascon-Xof";
4133
break;
4234
case AsconParameters.AsconXofA:
4335
ASCON_PB_ROUNDS = 8;
44-
ASCON_IV = (((ulong)(ASCON_HASH_RATE * 8) << 48) |
45-
((ulong)(ASCON_PA_ROUNDS) << 40) |
46-
((ulong)(ASCON_PA_ROUNDS - ASCON_PB_ROUNDS) << 32));
4736
algorithmName = "Ascon-XofA";
4837
break;
4938
default:
5039
throw new ArgumentException("Invalid parameter settings for Ascon Hash");
5140
}
41+
Reset();
5242
}
5343

44+
private AsconParameters asconParameters;
45+
5446
private string algorithmName;
5547

5648
private readonly MemoryStream buffer = new MemoryStream();
@@ -60,14 +52,8 @@ public AsconDigest(AsconParameters parameters)
6052
private ulong x3;
6153
private ulong x4;
6254
private readonly int CRYPTO_BYTES = 32;
63-
private readonly ulong ASCON_IV;
64-
private readonly int ASCON_HASH_RATE = 8;
65-
private readonly int ASCON_PA_ROUNDS = 12;
6655
private int ASCON_PB_ROUNDS;
6756

68-
69-
private uint ASCON_HASH_BYTES = 32;
70-
7157
public string AlgorithmName => algorithmName;
7258

7359
private ulong ROR(ulong x, int n)
@@ -165,14 +151,15 @@ public int DoFinal(byte[] output, int outOff)
165151
byte[] input = buffer.GetBuffer();
166152
int len = (int)buffer.Length;
167153
int inOff = 0;
168-
/* initialize */
169-
x0 = ASCON_IV;
170-
x1 = 0;
171-
x2 = 0;
172-
x3 = 0;
173-
x4 = 0;
174-
P(ASCON_PA_ROUNDS);
154+
///* initialize */
155+
//x0 = ASCON_IV;
156+
//x1 = 0;
157+
//x2 = 0;
158+
//x3 = 0;
159+
//x4 = 0;
160+
//P(ASCON_PA_ROUNDS);
175161
/* absorb full plaintext blocks */
162+
int ASCON_HASH_RATE = 8;
176163
while (len >= ASCON_HASH_RATE)
177164
{
178165
x0 ^= LOADBYTES(input, inOff, 8);
@@ -183,6 +170,7 @@ public int DoFinal(byte[] output, int outOff)
183170
/* absorb readonly plaintext block */
184171
x0 ^= LOADBYTES(input, inOff, len);
185172
x0 ^= PAD(len);
173+
int ASCON_PA_ROUNDS = 12;
186174
P(ASCON_PA_ROUNDS);
187175
/* squeeze full output blocks */
188176
len = CRYPTO_BYTES;
@@ -195,13 +183,46 @@ public int DoFinal(byte[] output, int outOff)
195183
}
196184
/* squeeze readonly output block */
197185
STOREBYTES(output, outOff, x0, len);
186+
Reset();
198187
return CRYPTO_BYTES;
199188
}
200189

201190

202191
public void Reset()
203192
{
204193
buffer.SetLength(0);
194+
/* initialize */
195+
switch (asconParameters)
196+
{
197+
case AsconParameters.AsconHashA:
198+
x0 = 92044056785660070UL;
199+
x1 = 8326807761760157607UL;
200+
x2 = 3371194088139667532UL;
201+
x3 = 15489749720654559101UL;
202+
x4 = 11618234402860862855UL;
203+
break;
204+
case AsconParameters.AsconHash:
205+
x0 = 17191252062196199485UL;
206+
x1 = 10066134719181819906UL;
207+
x2 = 13009371945472744034UL;
208+
x3 = 4834782570098516968UL;
209+
x4 = 3787428097924915520UL;
210+
break;
211+
case AsconParameters.AsconXof:
212+
x0 = 13077933504456348694UL;
213+
x1 = 3121280575360345120UL;
214+
x2 = 7395939140700676632UL;
215+
x3 = 6533890155656471820UL;
216+
x4 = 5710016986865767350UL;
217+
break;
218+
case AsconParameters.AsconXofA:
219+
x0 = 4940560291654768690UL;
220+
x1 = 14811614245468591410UL;
221+
x2 = 17849209150987444521UL;
222+
x3 = 2623493988082852443UL;
223+
x4 = 12162917349548726079UL;
224+
break;
225+
}
205226
}
206227

207228
public int GetByteLength()

0 commit comments

Comments
 (0)