From ff98558c4b8b3c2957aa815d90d600958cbcab7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Wed, 26 Feb 2020 01:31:05 +0100 Subject: [PATCH] Fix carry bug in sum/diff --- constantine/arithmetic/bigints_raw.nim | 4 +-- tests/test_finite_fields_vs_gmp.nim | 40 +++++++++++++------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/constantine/arithmetic/bigints_raw.nim b/constantine/arithmetic/bigints_raw.nim index 57f59ce..9712e68 100644 --- a/constantine/arithmetic/bigints_raw.nim +++ b/constantine/arithmetic/bigints_raw.nim @@ -356,7 +356,7 @@ func sum*(r: BigIntViewMut, a, b: distinct BigIntViewAny): CTBool[Word] = for i in 0 ..< a.numLimbs(): r[i] = a[i] + b[i] + Word(result) - result = a[i].isMsbSet() + result = r[i].isMsbSet() r[i] = r[i].mask() 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(): r[i] = a[i] - b[i] - Word(result) - result = a[i].isMsbSet() + result = r[i].isMsbSet() r[i] = r[i].mask() # ############################################################ diff --git a/tests/test_finite_fields_vs_gmp.nim b/tests/test_finite_fields_vs_gmp.nim index 2310eab..d05bb63 100644 --- a/tests/test_finite_fields_vs_gmp.nim +++ b/tests/test_finite_fields_vs_gmp.nim @@ -278,16 +278,16 @@ proc mainAdd() = # echo "rGMP: ", rGMP.toHex() # echo "rConstantine: ", rConstantine.toHex() - # doAssert rGMP == rConstantine, block: - # # Reexport as bigEndian for debugging - # 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) - # "\nModular Addition on curve " & $curve & " with operands\n" & - # " a: " & aBuf.toHex & "\n" & - # " b: " & bBuf.toHex & "\n" & - # "failed:" & "\n" & - # " GMP: " & rGMP.toHex() & "\n" & - # " Constantine: " & rConstantine.toHex() + doAssert rGMP == rConstantine, block: + # Reexport as bigEndian for debugging + 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) + "\nModular Addition on curve " & $curve & " with operands\n" & + " a: " & aBuf.toHex & "\n" & + " b: " & bBuf.toHex & "\n" & + "failed:" & "\n" & + " GMP: " & rGMP.toHex() & "\n" & + " Constantine: " & rConstantine.toHex() doAssert rGMP == r2Constantine, block: # Reexport as bigEndian for debugging @@ -375,16 +375,16 @@ proc mainSub() = # echo "rGMP: ", rGMP.toHex() # echo "rConstantine: ", rConstantine.toHex() - # doAssert rGMP == rConstantine, block: - # # Reexport as bigEndian for debugging - # 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) - # "\nModular Substraction on curve " & $curve & " with operands\n" & - # " a: " & aBuf.toHex & "\n" & - # " b: " & bBuf.toHex & "\n" & - # "failed:" & "\n" & - # " GMP: " & rGMP.toHex() & "\n" & - # " Constantine: " & rConstantine.toHex() + doAssert rGMP == rConstantine, block: + # Reexport as bigEndian for debugging + 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) + "\nModular Substraction on curve " & $curve & " with operands\n" & + " a: " & aBuf.toHex & "\n" & + " b: " & bBuf.toHex & "\n" & + "failed:" & "\n" & + " GMP: " & rGMP.toHex() & "\n" & + " Constantine: " & rConstantine.toHex() doAssert rGMP == r2Constantine, block: # Reexport as bigEndian for debugging