@@ -34,8 +34,8 @@ import org.kotlincrypto.core.xof.Xof
34
34
public sealed class ParallelDigest : SHAKEDigest {
35
35
36
36
private val inner: SHAKEDigest
37
- private val innerBuf : ByteArray
38
- private var innerBufPos : Int
37
+ private val innerBlockSize : Int
38
+ private var innerPos : Int
39
39
private var countLo: Int
40
40
private var countHi: Int
41
41
@@ -60,8 +60,8 @@ public sealed class ParallelDigest: SHAKEDigest {
60
60
BIT_STRENGTH_256 -> CSHAKE256 (N = null , S = null )
61
61
else -> throw IllegalArgumentException (" bitStrength must be $BIT_STRENGTH_128 or $BIT_STRENGTH_256 " )
62
62
}
63
- this .innerBuf = ByteArray ( B )
64
- this .innerBufPos = 0
63
+ this .innerBlockSize = B
64
+ this .innerPos = 0
65
65
this .countLo = 0
66
66
this .countHi = 0
67
67
@@ -72,22 +72,21 @@ public sealed class ParallelDigest: SHAKEDigest {
72
72
73
73
protected constructor (other: ParallelDigest ): super (other) {
74
74
this .inner = other.inner.copy()
75
- this .innerBuf = other.innerBuf.copyOf()
76
- this .innerBufPos = other.innerBufPos
75
+ this .innerBlockSize = other.innerBlockSize
76
+ this .innerPos = other.innerPos
77
77
this .countLo = other.countLo
78
78
this .countHi = other.countHi
79
79
}
80
80
81
81
public abstract override fun copy (): ParallelDigest
82
82
83
83
protected final override fun digestProtected (buf : ByteArray , bufPos : Int ): ByteArray {
84
- val buffered = if (innerBufPos != 0 ) {
84
+ val buffered = if (innerPos != 0 ) {
85
85
// If there's any buffered bytes left,
86
- // process them to append them to the
86
+ // process them and prefix to the
87
87
// buffer here as additional input.
88
- inner.update(innerBuf, 0 , innerBufPos)
89
- increment()
90
- innerBufPos = 0
88
+ innerPos = 0
89
+ incrementCount()
91
90
inner.digest()
92
91
} else {
93
92
ByteArray (0 )
@@ -118,75 +117,67 @@ public sealed class ParallelDigest: SHAKEDigest {
118
117
}
119
118
120
119
protected final override fun updateProtected (input : Byte ) {
121
- val buf = innerBuf
122
- val bufPos = innerBufPos++
123
- buf[bufPos] = input
124
- if (bufPos + 1 != buf.size) return
125
- processBlock(buf, 0 )
126
- innerBufPos = 0
120
+ inner.update(input)
121
+ if (++ innerPos != innerBlockSize) return
122
+ processBlock()
123
+ innerPos = 0
127
124
}
128
125
129
126
protected final override fun updateProtected (input : ByteArray , offset : Int , len : Int ) {
130
- val buf = innerBuf
131
- val blockSize = buf.size
132
127
var inputPos = offset
133
128
val inputLimit = inputPos + len
134
- var bufPos = innerBufPos
129
+ var innerPos = innerPos
135
130
136
- if (bufPos > 0 ) {
137
- if (bufPos + len < blockSize) {
138
- input.copyInto(buf, bufPos, inputPos, inputLimit)
139
- innerBufPos = bufPos + len
131
+ if (innerPos > 0 ) {
132
+ if (innerPos + len < innerBlockSize) {
133
+ // Not enough input to process a block
134
+ inner.update(input, offset, len)
135
+ this .innerPos = innerPos + len
140
136
return
141
137
}
142
138
143
- val needed = blockSize - bufPos
144
- input.copyInto(buf, bufPos, inputPos, inputPos + needed)
145
- processBlock(buf, 0 )
146
- bufPos = 0
139
+ val needed = innerBlockSize - innerPos
140
+ inner.update(input, inputPos, needed)
141
+ processBlock()
142
+ innerPos = 0
147
143
inputPos + = needed
148
144
}
149
145
150
146
while (inputPos < inputLimit) {
151
- val nextPos = inputPos + blockSize
147
+ val nextPos = inputPos + innerBlockSize
152
148
153
149
if (nextPos > inputLimit) {
154
- input.copyInto(buf, 0 , inputPos, inputLimit)
155
- bufPos = inputLimit - inputPos
150
+ innerPos = inputLimit - inputPos
151
+ inner.update(input, inputPos, innerPos)
156
152
break
157
153
}
158
154
159
- processBlock(input, inputPos)
155
+ inner.update(input, inputPos, innerBlockSize)
156
+ processBlock()
160
157
inputPos = nextPos
161
158
}
162
159
163
- innerBufPos = bufPos
160
+ this .innerPos = innerPos
164
161
}
165
162
166
- private inline fun processBlock (input : ByteArray , offset : Int ) {
167
- inner.update(input, offset, innerBuf.size)
163
+ private inline fun processBlock () {
168
164
super .updateProtected(inner.digest(), 0 , inner.digestLength())
169
- increment ()
165
+ incrementCount ()
170
166
}
171
167
172
168
protected final override fun resetProtected () {
173
169
super .resetProtected()
174
- this .innerBuf.fill( 0 )
175
- this .innerBufPos = 0
170
+ this .inner.reset( )
171
+ this .innerPos = 0
176
172
this .countLo = 0
177
173
this .countHi = 0
178
174
179
- // No need to reset inner as digest() is always called
180
- // when processing blocks which leaves it in a perpetually
181
- // reset state.
182
- // this.inner.reset()
183
-
184
175
@OptIn(InternalKotlinCryptoApi ::class )
185
- val encBSize = Xof .Utils .leftEncode(innerBuf.size )
176
+ val encBSize = Xof .Utils .leftEncode(innerBlockSize )
186
177
super .updateProtected(encBSize, 0 , encBSize.size)
187
178
}
188
179
189
- private inline fun increment () { if (++ countLo == 0 ) countHi++ }
180
+ private inline fun incrementCount () { if (++ countLo == 0 ) countHi++ }
190
181
191
182
private companion object {
192
183
private const val PARALLEL_HASH = " ParallelHash"
0 commit comments