0

I'm working on an address generator that's comptaible with the Monero GUI. My seed generator works, however I still don't understand why Crypto Address Test gets a different result when it creates the Private Spend Key from the hexadecimal seed.

With a hexadecimal seed af7a0c64ff778aba9070b7de1bff243aa8496074608fc1c03625da2bf00dba95 Crypto Address Test generates 5a07681f12fce4a107ed022448364e7ea7496074608fc1c03625da2bf00dba05 as the private spend key instead of the original value.

Is there something wrong with my ScReduce32 function?

Here's my code:

fun generateMnemonicSeed(): String {
    val spendKey = generateEdDSAKeyPair()
    val spendPriv = (spendKey.private as Ed25519PrivateKeyParameters).encoded
    println("B4 sc_reduce32: ${spendPriv.toHex()}")
    val reduced =ScReduce32(spendPriv.toHex())
    println("After sc_reduce32: $reduced")
    return hexToSeedPhrase(reduced)
}
fun ScReduce32(hexString: String): String{
    println("hexString:$hexString")
    val j = BigInteger(hexString, 16)
    println("j: $j")
    println("j-hex: ${j.toString(16)}")
    val l = (BigInteger.valueOf(2L).pow(256) + BigInteger("27742317777372353535851937790883648493"))
    println("l: ${l.toString(16)}")
    val reduced = j % l
    val reducedStr = reduced.toString(16)
    println("reducedStr: ${reduced.toString(16)}")
    return reducedStr
}

Resulting Logs

B4 sc_reduce32: af7a0c64ff778aba9070b7de1bff243aa8496074608fc1c03625da2bf00dba95
hexString:af7a0c64ff778aba9070b7de1bff243aa8496074608fc1c03625da2bf00dba95
j: 79370389387829220119965404010432066765120076533697599508186610358111351716501
j-hex: af7a0c64ff778aba9070b7de1bff243aa8496074608fc1c03625da2bf00dba95
l: 10000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed
reducedStr: af7a0c64ff778aba9070b7de1bff243aa8496074608fc1c03625da2bf00dba95
After sc_reduce32: af7a0c64ff778aba9070b7de1bff243aa8496074608fc1c03625da2bf00dba95
toMnemonic: af7a0c64ff778aba9070b7de1bff243aa8496074608fc1c03625da2bf00dba95
n: 1626
new tmpStr: 640c7aafba8a77ffdeb770903a24ff1b746049a8c0c18f602bda253695ba0df0
words: [avidly, water, goldfish, utensils, people, hiding, sawmill, ambush, upper, island, hubcaps, orbit, jellyfish, vary, hinder, vain, baptism, terminal, exotic, morsel, return, lion, oozed, dime]
str: aviwatgolutepeohidsawambuppislhuborbjelvarhinvaibapterexomorretliooozdim
words.joinToString(" "): avidly water goldfish utensils people hiding sawmill ambush upper island hubcaps orbit jellyfish vary hinder vain baptism terminal exotic morsel return lion oozed dime
words.joinToString(" ").length: 167
r2:0
checkSumWord: avidly
resultList.size: 25
mnemonicSeed:avidly water goldfish utensils people hiding sawmill ambush upper island hubcaps orbit jellyfish vary hinder vain baptism terminal exotic morsel return lion oozed dime avidly

1 Answer 1

0

Found the code snippet:

fun scReduce32(s: UByteArray): UByteArray {
    var s0 = 2097151 and load3(s)
    var s1 = 2097151 and (load4(s.copyOfRange(2, 6)) shr 5)
    var s2 = 2097151 and (load3(s.copyOfRange(5, 8)) shr 2)
    var s3 = 2097151 and (load4(s.copyOfRange(7, 11)) shr 7)
    var s4 = 2097151 and (load4(s.copyOfRange(10, 14)) shr 4)
    var s5 = 2097151 and (load3(s.copyOfRange(13, 16)) shr 1)
    var s6 = 2097151 and (load4(s.copyOfRange(15, 19)) shr 6)
    var s7 = 2097151 and (load3(s.copyOfRange(18, 21)) shr 3)
    var s8 = 2097151 and load3(s.copyOfRange(21, 24))
    var s9 = 2097151 and (load4(s.copyOfRange(23, 27)) shr 5)
    var s10 = 2097151 and (load3(s.copyOfRange(26, 29)) shr 2)
    var s11 = load4(s.copyOfRange(28, 32)) shr 7
    var s12 = 0
    val carry = IntArray(12)

    carry[0] = (s0 + (1 shl 20)) shr 21
    s1 += carry[0]
    s0 -= carry[0] shl 21
    carry[2] = (s2 + (1 shl 20)) shr 21
    s3 += carry[2]
    s2 -= carry[2] shl 21
    carry[4] = (s4 + (1 shl 20)) shr 21
    s5 += carry[4]
    s4 -= carry[4] shl 21
    carry[6] = (s6 + (1 shl 20)) shr 21
    s7 += carry[6]
    s6 -= carry[6] shl 21
    carry[8] = (s8 + (1 shl 20)) shr 21
    s9 += carry[8]
    s8 -= carry[8] shl 21
    carry[10] = (s10 + (1 shl 20)) shr 21
    s11 += carry[10]
    s10 -= carry[10] shl 21

    carry[1] = (s1 + (1 shl 20)) shr 21
    s2 += carry[1]
    s1 -= carry[1] shl 21
    carry[3] = (s3 + (1 shl 20)) shr 21
    s4 += carry[3]
    s3 -= carry[3] shl 21
    carry[5] = (s5 + (1 shl 20)) shr 21
    s6 += carry[5]
    s5 -= carry[5] shl 21
    carry[7] = (s7 + (1 shl 20)) shr 21
    s8 += carry[7]
    s7 -= carry[7] shl 21
    carry[9] = (s9 + (1 shl 20)) shr 21
    s10 += carry[9]
    s9 -= carry[9] shl 21
    carry[11] = (s11 + (1 shl 20)) shr 21
    s12 += carry[11]
    s11 -= carry[11] shl 21

    s0 += s12 * 666643
    s1 += s12 * 470296
    s2 += s12 * 654183
    s3 -= s12 * 997805
    s4 += s12 * 136657
    s5 -= s12 * 683901
    s12 = 0

    carry[0] = s0 shr 21
    s1 += carry[0]
    s0 -= carry[0] shl 21
    carry[1] = s1 shr 21
    s2 += carry[1]
    s1 -= carry[1] shl 21
    carry[2] = s2 shr 21
    s3 += carry[2]
    s2 -= carry[2] shl 21
    carry[3] = s3 shr 21
    s4 += carry[3]
    s3 -= carry[3] shl 21
    carry[4] = s4 shr 21
    s5 += carry[4]
    s4 -= carry[4] shl 21
    carry[5] = s5 shr 21
    s6 += carry[5]
    s5 -= carry[5] shl 21
    carry[6] = s6 shr 21
    s7 += carry[6]
    s6 -= carry[6] shl 21
    carry[7] = s7 shr 21
    s8 += carry[7]
    s7 -= carry[7] shl 21
    carry[8] = s8 shr 21
    s9 += carry[8]
    s8 -= carry[8] shl 21
    carry[9] = s9 shr 21
    s10 += carry[9]
    s9 -= carry[9] shl 21
    carry[10] = s10 shr 21
    s11 += carry[10]
    s10 -= carry[10] shl 21
    carry[11] = s11 shr 21
    s12 += carry[11]
    s11 -= carry[11] shl 21

    s0 += s12 * 666643
    s1 += s12 * 470296
    s2 += s12 * 654183
    s3 -= s12 * 997805
    s4 += s12 * 136657
    s5 -= s12 * 683901

    carry[0] = s0 shr 21
    s1 += carry[0]
    s0 -= carry[0] shl 21
    carry[1] = s1 shr 21
    s2 += carry[1]
    s1 -= carry[1] shl 21
    carry[2] = s2 shr 21
    s3 += carry[2]
    s2 -= carry[2] shl 21
    carry[3] = s3 shr 21
    s4 += carry[3]
    s3 -= carry[3] shl 21
    carry[4] = s4 shr 21
    s5 += carry[4]
    s4 -= carry[4] shl 21
    carry[5] = s5 shr 21
    s6 += carry[5]
    s5 -= carry[5] shl 21
    carry[6] = s6 shr 21
    s7 += carry[6]
    s6 -= carry[6] shl 21
    carry[7] = s7 shr 21
    s8 += carry[7]
    s7 -= carry[7] shl 21
    carry[8] = s8 shr 21
    s9 += carry[8]
    s8 -= carry[8] shl 21
    carry[9] = s9 shr 21
    s10 += carry[9]
    s9 -= carry[9] shl 21
    carry[10] = s10 shr 21
    s11 += carry[10]
    s10 -= carry[10] shl 21

    s[0] = s0.toUByte()
    s[1] = (s0 shr 8).toUByte()
    s[2] = ((s0 shr 16) or (s1 shl 5)).toUByte()
    s[3] = (s1 shr 3).toUByte()
    s[4] = (s1 shr 11).toUByte()
    s[5] = ((s1 shr 19) or (s2 shl 2)).toUByte()
    s[6] = (s2 shr 6).toUByte()
    s[7] = ((s2 shr 14) or (s3 shl 7)).toUByte()
    s[8] = (s3 shr 1).toUByte()
    s[9] = (s3 shr 9).toUByte()
    s[10] = ((s3 shr 17) or (s4 shl 4)).toUByte()
    s[11] = (s4 shr 4).toUByte()
    s[12] = (s4 shr 12).toUByte()
    s[13] = ((s4 shr 20) or (s5 shl 1)).toUByte()
    s[14] = (s5 shr 7).toUByte()
    s[15] = ((s5 shr 15) or (s6 shl 6)).toUByte()
    s[16] = (s6 shr 2).toUByte()
    s[17] = (s6 shr 10).toUByte()
    s[18] = ((s6 shr 18) or (s7 shl 3)).toUByte()
    s[19] = (s7 shr 5).toUByte()
    s[20] = (s7 shr 13).toUByte()
    s[21] = s8.toUByte()
    s[22] = (s8 shr 8).toUByte()
    s[23] = ((s8 shr 16) or (s9 shl 5)).toUByte()
    s[24] = (s9 shr 3).toUByte()
    s[25] = (s9 shr 11).toUByte()
    s[26] = ((s9 shr 19) or (s10 shl 2)).toUByte()
    s[27] = (s10 shr 6).toUByte()
    s[28] = ((s10 shr 14) or (s11 shl 7)).toUByte()
    s[29] = (s11 shr 1).toUByte()
    s[30] = (s11 shr 9).toUByte()
    s[31] = (s11 shr 17).toUByte()

    return s
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.