t_finite_fields_conditional_arithmetic.nim
1 # Constantine 2 # Copyright (c) 2018-2019 Status Research & Development GmbH 3 # Copyright (c) 2020-Present Mamy André-Ratsimbazafy 4 # Licensed and distributed under either of 5 # * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). 6 # * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). 7 # at your option. This file may not be copied, modified, or distributed except according to those terms. 8 9 import std/unittest, 10 ../../constantine/platforms/abstractions, 11 ../../constantine/math/arithmetic, 12 ../../constantine/math/io/io_fields, 13 ../../constantine/math/config/curves 14 15 echo "\n------------------------------------------------------\n" 16 17 proc main() = 18 suite "Finite field conditional arithmetic": 19 test "Conditional substraction borrow bug": 20 let a = FP[BN254_Snarks].fromHex"0x14ae3e4392eb3238968c7624ee3d041590392e289e4f0bdfac4b6e56ac8cf768" 21 let b = FP[BN254_Snarks].fromHex"0x24e810017b4c0630a0b35b5c63a377097533928b31fa95d58d0e08d1f98b16c6" 22 23 let expected = FP[BN254_Snarks].fromHex"0x202a7cb4f8d0cc31ae29607f0c1ae569b287062ed4c640975b5df19b8b7edde9" 24 25 var normalsub: Fp[BN254_Snarks] 26 normalsub.diff(a, b) 27 28 var condsub = a 29 condsub.csub(b, CtTrue) 30 31 check: 32 bool(normalsub == expected) 33 bool(condsub == expected) 34 35 main()