Last active
August 2, 2019 11:16
-
-
Save cyborch/246c481f8c4f7b3befbfd14fecd31df0 to your computer and use it in GitHub Desktop.
Snippet of RSA accumulator which demonstrates adding a value to a set
This file contains hidden or 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
fun add(x: BigInteger): RSACommit { | |
return if (data.containsKey(x)) { | |
commitment | |
} else { | |
val (hashPrime, nonce) = hashToPrime(x, ACCUMULATED_PRIME_SIZE) | |
commitment = commitment.modPow(hashPrime, n) | |
data[x] = nonce | |
commitment | |
} | |
} | |
fun hashToPrime(x: BigInteger, bitLength: Int = 120, initNonce: BigInteger = BigInteger.ZERO): TwoValue<BigInteger> { | |
var nonce = initNonce | |
while (true) { | |
val num = hashToLength(x + nonce, bitLength) | |
if (num.isProbablePrime(primeCertainty)) { | |
return TwoValue(num, nonce) | |
} | |
nonce += BigInteger.ONE | |
} | |
} | |
fun hashToLength(x: BigInteger, bitLength: Int): BigInteger { | |
var hexString = "" | |
val numOfBlocks = Math.ceil(bitLength / 256.00).toInt() | |
for (i in 0 until numOfBlocks) { | |
hexString += sha256.hashBytes((x + i.toBigInteger()).toString(10).toByteArray()).asBytes() | |
.toHexString() | |
} | |
if (bitLength % 256 > 0) { | |
// # we do assume divisible by 4 | |
hexString = | |
hexString.substring((bitLength % 256) / 4) | |
} | |
return BigInteger(hexString, 16) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment