Fix carry bug in sum/diff

This commit is contained in:
Mamy André-Ratsimbazafy 2020-02-26 01:31:05 +01:00
parent c621355d8d
commit ff98558c4b
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
2 changed files with 22 additions and 22 deletions

View File

@ -356,7 +356,7 @@ func sum*(r: BigIntViewMut, a, b: distinct BigIntViewAny): CTBool[Word] =
for i in 0 ..< a.numLimbs(): for i in 0 ..< a.numLimbs():
r[i] = a[i] + b[i] + Word(result) r[i] = a[i] + b[i] + Word(result)
result = a[i].isMsbSet() result = r[i].isMsbSet()
r[i] = r[i].mask() r[i] = r[i].mask()
func diff*(r: BigIntViewMut, a, b: distinct BigIntViewAny): CTBool[Word] = func diff*(r: BigIntViewMut, a, b: distinct BigIntViewAny): CTBool[Word] =
@ -370,7 +370,7 @@ func diff*(r: BigIntViewMut, a, b: distinct BigIntViewAny): CTBool[Word] =
for i in 0 ..< a.numLimbs(): for i in 0 ..< a.numLimbs():
r[i] = a[i] - b[i] - Word(result) r[i] = a[i] - b[i] - Word(result)
result = a[i].isMsbSet() result = r[i].isMsbSet()
r[i] = r[i].mask() r[i] = r[i].mask()
# ############################################################ # ############################################################

View File

@ -278,16 +278,16 @@ proc mainAdd() =
# echo "rGMP: ", rGMP.toHex() # echo "rGMP: ", rGMP.toHex()
# echo "rConstantine: ", rConstantine.toHex() # echo "rConstantine: ", rConstantine.toHex()
# doAssert rGMP == rConstantine, block: doAssert rGMP == rConstantine, block:
# # Reexport as bigEndian for debugging # Reexport as bigEndian for debugging
# discard mpz_export(aBuf[0].addr, aW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, a) discard mpz_export(aBuf[0].addr, aW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, a)
# discard mpz_export(bBuf[0].addr, bW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, b) discard mpz_export(bBuf[0].addr, bW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, b)
# "\nModular Addition on curve " & $curve & " with operands\n" & "\nModular Addition on curve " & $curve & " with operands\n" &
# " a: " & aBuf.toHex & "\n" & " a: " & aBuf.toHex & "\n" &
# " b: " & bBuf.toHex & "\n" & " b: " & bBuf.toHex & "\n" &
# "failed:" & "\n" & "failed:" & "\n" &
# " GMP: " & rGMP.toHex() & "\n" & " GMP: " & rGMP.toHex() & "\n" &
# " Constantine: " & rConstantine.toHex() " Constantine: " & rConstantine.toHex()
doAssert rGMP == r2Constantine, block: doAssert rGMP == r2Constantine, block:
# Reexport as bigEndian for debugging # Reexport as bigEndian for debugging
@ -375,16 +375,16 @@ proc mainSub() =
# echo "rGMP: ", rGMP.toHex() # echo "rGMP: ", rGMP.toHex()
# echo "rConstantine: ", rConstantine.toHex() # echo "rConstantine: ", rConstantine.toHex()
# doAssert rGMP == rConstantine, block: doAssert rGMP == rConstantine, block:
# # Reexport as bigEndian for debugging # Reexport as bigEndian for debugging
# discard mpz_export(aBuf[0].addr, aW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, a) discard mpz_export(aBuf[0].addr, aW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, a)
# discard mpz_export(bBuf[0].addr, bW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, b) discard mpz_export(bBuf[0].addr, bW.addr, GMP_MostSignificantWordFirst, 1, GMP_WordNativeEndian, 0, b)
# "\nModular Substraction on curve " & $curve & " with operands\n" & "\nModular Substraction on curve " & $curve & " with operands\n" &
# " a: " & aBuf.toHex & "\n" & " a: " & aBuf.toHex & "\n" &
# " b: " & bBuf.toHex & "\n" & " b: " & bBuf.toHex & "\n" &
# "failed:" & "\n" & "failed:" & "\n" &
# " GMP: " & rGMP.toHex() & "\n" & " GMP: " & rGMP.toHex() & "\n" &
# " Constantine: " & rConstantine.toHex() " Constantine: " & rConstantine.toHex()
doAssert rGMP == r2Constantine, block: doAssert rGMP == r2Constantine, block:
# Reexport as bigEndian for debugging # Reexport as bigEndian for debugging