Skip to content

Commit d2df757

Browse files
authored
[arith]: isSquare on 𝔽r (#576)
1 parent 498504a commit d2df757

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

constantine/math/arithmetic/finite_fields_square_root.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,13 @@ func invsqrt_if_square_vartime*[Name](r: var Fp[Name], a: Fp[Name]): SecretBool
361361
# Legendre symbol / Euler's Criterion / Kronecker's symbol
362362
# ------------------------------------------------------------
363363

364-
func isSquare*(a: Fp): SecretBool =
365-
## Returns true if ``a`` is a square (quadratic residue) in 𝔽p
364+
func isSquare*(a: FF): SecretBool =
365+
## Returns true if ``a`` is a square (quadratic residue) in 𝔽p or 𝔽r
366366
##
367-
## Assumes that the prime modulus ``p`` is public.
368-
var aa {.noInit.}: Fp.getBigInt()
367+
## Assumes that the prime modulus ``p`` (or ``r``) is public.
368+
var aa {.noInit.}: FF.getBigInt()
369369
aa.fromField(a)
370-
let symbol = legendre(aa.limbs, Fp.getModulus().limbs, aa.bits)
370+
let symbol = legendre(aa.limbs, FF.getModulus().limbs, aa.bits)
371371
return not(symbol == MaxWord)
372372

373373
{.pop.} # inline

tests/math_fields/t_finite_fields_sqrt.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,18 @@ proc main() =
199199
a.fromHex"0x7ff7ffffffffffff1dfb7fafc0000000"
200200
testSqrtImpl(a)
201201

202+
suite "isSquare on 𝔽r" & " [" & $WordBitWidth & "-bit words]":
203+
test "𝔽r[BW6_761] is consistent with 𝔽p[BLS12_381]":
204+
block:
205+
var a: Fr[BW6_761]
206+
a.fromHex"0x184d02ce4f24d5e59b4150a57a31b202fd40a4b41d7518c22b84bee475fbcb7763100448ef6b17a6ea603cf062e5db51"
207+
check:
208+
bool(not a.isSquare())
209+
210+
block:
211+
var a: Fr[BW6_761]
212+
a.fromHex"0x0f16d7854229d8804bcadd889f70411d6a482bde840d238033bf868e89558d39d52f9df60b2d745e02584375f16c34a3"
213+
check:
214+
bool(not a.isSquare())
215+
202216
main()

0 commit comments

Comments
 (0)