-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bitstring for revocation registry (#188)
Signed-off-by: Cristian G <[email protected]>
- Loading branch information
1 parent
919f2d9
commit 6a0fd8b
Showing
7 changed files
with
196 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ent-sdk/src/commonMain/kotlin/org/hyperledger/identus/walletsdk/pollux/utils/BitString.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.hyperledger.identus.walletsdk.pollux.utils | ||
|
||
import java.util.BitSet | ||
|
||
/** | ||
* The `BitString` class represents a sequence of bits using a from a provided `ByteArray`. | ||
* It reverses the bits from left to right to follow the revocation list specs of left to right indexing. | ||
* | ||
* @property byteArray The byte array to be | ||
* @property size The number of bits in the sequence. | ||
*/ | ||
class BitString(byteArray: ByteArray) { | ||
val bitSet: BitSet | ||
val size: Int | ||
|
||
init { | ||
bitSet = BitSet.valueOf( | ||
byteArray.map { value -> reverseBits(value.toInt()).toByte() }.toByteArray() | ||
) | ||
size = byteArray.size * 8 | ||
} | ||
|
||
/** | ||
* Checks if the bit at the specified index is set (i.e., if the bit is "revoked"). | ||
* | ||
* @param index The index of the bit to check. | ||
* @return `true` if the bit at the specified index is set, `false` otherwise. | ||
* @throws Exception if the index is greater than or equal to the size of the bit sequence. | ||
*/ | ||
fun isRevoked(index: Int): Boolean { | ||
if (index >= size) { | ||
throw Exception("BitString error: index cannot be bigger than the size.") | ||
} | ||
return bitSet.get(index) | ||
} | ||
|
||
// companion object { | ||
/** | ||
* Reverses the bits in an 8-bit integer. | ||
* | ||
* @param b The integer whose bits need to be reversed. | ||
* @return The integer with its bits reversed. | ||
*/ | ||
private fun reverseBits(b: Int): Int { | ||
var result = 0 | ||
for (i in 0 until 8) { | ||
val bit = (b shr i) and 1 | ||
result = (result shl 1) or bit | ||
} | ||
return result | ||
} | ||
// } | ||
} |
44 changes: 0 additions & 44 deletions
44
...ent-sdk/src/commonMain/kotlin/org/hyperledger/identus/walletsdk/pollux/utils/Bitstring.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...sdk/src/commonTest/kotlin/org/hyperledger/identus/walletsdk/pollux/utils/BitStringTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.hyperledger.identus.walletsdk.pollux.utils | ||
|
||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import java.util.* | ||
|
||
class BitStringTest { | ||
|
||
@Test | ||
fun `test BitString with some 0s`() { | ||
val byteArray = byteArrayOf(0.toByte()) | ||
val bitString = BitString(byteArray) | ||
|
||
for (i in 0 until bitString.size) { | ||
assertFalse("Index $i should not be revoked", bitString.isRevoked(i)) | ||
} | ||
} | ||
|
||
@Test | ||
fun `test BitString with some 1s`() { | ||
val byteArray = byteArrayOf(128.toByte(), 3.toByte()) | ||
val bitString = BitString(byteArray) | ||
|
||
assertTrue("Index 0 should be revoked", bitString.isRevoked(0)) | ||
assertTrue("Index 8 should be revoked", bitString.isRevoked(14)) | ||
assertTrue("Index 8 should be revoked", bitString.isRevoked(15)) | ||
for (i in 1 until 14) { | ||
assertFalse("Index $i should not be revoked", bitString.isRevoked(i)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters