Coder Social home page Coder Social logo

Comments (1)

mratsim avatar mratsim commented on August 27, 2024

Reproduction on 32-bit, square issue again

import
  # Standard library
  std/[unittest, times],
  # Internals
  ../constantine/config/[common, curves],
  ../constantine/[arithmetic, towers],
  ../constantine/io/[io_bigints, io_fields, io_towers],
  ../constantine/elliptic/[ec_weierstrass_affine, ec_weierstrass_projective]

proc trySetFromCoordsXandZ_debug*[F](P: var ECP_SWei_Proj[F], x, z: F): SecretBool =
  ## Try to create a point the elliptic curve
  ## Y²Z = X³ + aXZ² + bZ³ (projective coordinates)
  ## y² = x³ + a x + b     (affine coordinate)
  ## return true and update `P` if `x` leads to a valid point
  ## return false otherwise, in that case `P` is undefined.
  ##
  ## Note: Dedicated robust procedures for hashing-to-curve
  ##       will be provided, this is intended for testing purposes.
  P.y.curve_eq_rhs(x)

  echo "P.y: ", P.y.toHex()
  echo "P.y.isSquare: ", bool P.y.isSquare
  result = sqrt_if_square(P.y)
  echo "P.y.wasSquare: ", bool result

  P.x.prod(x, z)
  P.y *= z
  P.z = z

var a, b, c: ECP_SWei_Proj[Fp2[BLS12_381]]

var ax, az, bx, bz, cx, cz: Fp2[BLS12_381]
ax.fromHex(
  c0 = "0x0a3cb51c87870ae2dbae8b2541c9e2ce3d8d7399ac27817f6693cc09afc47faf78037c99cdaf982a50f33579a025e8c6",
  c1 = "0x00a139723b314c29bfecf63c19c2900a6973e2f5315e9194145a87b18ae9357830324a5f7f8f2605da22267f1e1145b4"
)

az.fromHex(
  c0 = "0x0c1fe9819c78d075d0bcb9e154998c565e4c0928dfca415fe552ceda584b2adf614a1c4c1c4007c67398471f1c380483",
  c1 = "0x045354e8eae1df51b1fce7608d46141412084a21645c3f1667c628d1d3bddef95804664b1d2db6beae638d66c630de4f"
)

bx.fromHex(
  c0 = "0x02b1597cfa65d8bc46d8f104c7d0fa83918bef778641752b3be9c2a285a21d5f10923f4cf051799b19763a75a7bfbc92",
  c1 = "0x043933ace864e9bf38bde3c37c7d52c59688758fba5ace6aacb3101ac2c1fb5521ed33ffeed4933d0b6cd798d6d0a956"
)

bz.fromHex(
  c0 = "0x03d3dade5c52f71522775fcf84fefa2352b5b7a7b9c5fce68ce0696e0312c5520f7429bfd2cae0ccdc69733baba7ab84",
  c1 = "0x12d0d62574b9ed0794587b7fba9dbdd6d5c34e4f85365ccfb55d873bf83793a3f20c9619dcb45b2b1d2ee8aba85c1051"
)

cx.fromHex(
  c0 = "0x03aeb225779d7298b9769cb8a4629b46d251411cb7460e744a1d91c9501ff53908687cbca5dbdca44868664eed1b3050",
  c1 = "0x10b8dffb7afbbcf5d930bcb369da35fada654aa21042e541f8acf7e79b4cee88685cf58941910bf01c1392fdfdaab9f9"
)

cz.fromHex(
  c0 = "0x0f548d4f9fdca7281e79bf8323296ddb7b13c14382f563572338fa3f769a985c4b3af6056ec6853126e36b7c573f4478",
  c1 = "0x0352c7b0b459518c61339e6d7b64a0e847a322d0aa6dc66a11595c457493fe8d9075df73e82e3624d17715a50514995b"
)


doAssert bool a.trySetFromCoordsXandZ_debug(ax, az)
doAssert bool b.trySetFromCoordsXandZ_debug(bx, bz)
doAssert bool c.trySetFromCoordsXandZ_debug(cx, cz)

echo "a.x: ", a.x.toHex()
echo "a.y: ", a.y.toHex()
echo "a.z: ", a.z.toHex()
echo ""
echo "b.x: ", b.x.toHex()
echo "b.y: ", b.y.toHex()
echo "b.z: ", b.z.toHex()
echo ""
echo "c.x: ", c.x.toHex()
echo "c.y: ", c.y.toHex()
echo "c.z: ", c.z.toHex()

var tmp1{.noInit.}, tmp2{.noInit.}: ECP_SWei_Proj[Fp2[BLS12_381]]

# r0 = (a + b) + c
tmp1.sum(a, b)
tmp2.sum(tmp1, c)
let r0 = tmp2

# r1 = a + (b + c)
tmp1.sum(b, c)
tmp2.sum(a, tmp1)
let r1 = tmp2

# r2 = (a + c) + b
tmp1.sum(a, c)
tmp2.sum(tmp1, b)
let r2 = tmp2

# r3 = a + (c + b)
tmp1.sum(c, b)
tmp2.sum(a, tmp1)
let r3 = tmp2

# r4 = (c + a) + b
tmp1.sum(c, a)
tmp2.sum(tmp1, b)
let r4 = tmp2

# ...

doAssert bool(r0 == r1)
doAssert bool(r0 == r2)
doAssert bool(r0 == r3)
doAssert bool(r0 == r4)
P.y: Fp2(c0: 0x15fdcea1f41db0855ef99545d8971b48a44aa75bd612b405bd4deda4b0946ad030c765d459d667db9861a98d6bc65907, c1: 0x0e7f92a96e7bcbf15cc5a4a1b58f6fe9d40518cec77f39c6e29731d3a0a224eb4193d82ff6297edd310f7f0c989570bd)
P.y.isSquare: true
P.y.wasSquare: true
P.y: Fp2(c0: 0x0c7effa8bb2ea8f6d163783a7851752587d52ff6f816dc754dfd08c7779060f793a88bebf9354a8d7e83befffb0d95da, c1: 0x16da288efa43f7a70ea71cfd3385bb76d79bc611e9c54657762b80516da616d15b50204433cf23bdbc4252fb77388853)
P.y.isSquare: true
P.y.wasSquare: true
P.y: Fp2(c0: 0x09f7034e1d37628dec7be400ddd098110c9160e1de63637d73bd93796f311fb50d438ef357a9349d245fbcfcb6fccf01, c1: 0x033c9b2f17988d8bea494fde020f54fb33cc780bba53e4f6746783ac659d472d9f616516fcf87f0d9a980243d38afeee)
P.y.isSquare: false
P.y.wasSquare: true
a.x: Fp2(c0: 0x077f15045670b4cbcae17e2c402c1ce25fe5d9ee26f71b20de33a23bcf2f6e4883c9427b14655095484928acdb491674, c1: 0x0c28a3d98a13283ef175e39e06de526fc4fd8dcf3e81685c9fe1c266e934996f027206edb263a2a2e42575f30f3274c8)
a.y: Fp2(c0: 0x1826ef0dd0e228f360e707dc3ac704fad5db8d23d8e7515eb47a64c95013d0affc6fe9be08d3f2319d273ca9c34828fc, c1: 0x06038b73a5a99ef59c96846c6d2f562c7a4140893110bfdd38ad357721a74aeab44b67ca4644e6e6b37fc4008b888685)
a.z: Fp2(c0: 0x0c1fe9819c78d075d0bcb9e154998c565e4c0928dfca415fe552ceda584b2adf614a1c4c1c4007c67398471f1c380483, c1: 0x045354e8eae1df51b1fce7608d46141412084a21645c3f1667c628d1d3bddef95804664b1d2db6beae638d66c630de4f)

b.x: Fp2(c0: 0x11e972fb755fa9f0446f6cdd815d8275be62d4b2ee66a1ecea6d0c47c9ac28cd437839007a7dd8210db2d936c06d2eaf, c1: 0x1808bcfe0f92ff0c082d1bb9cdd9185a97d2214158e591ad84c08a771f747f59a94063c174694803af7bda62caae9663)
b.y: Fp2(c0: 0x03e0b8344b9b05e7c375794f21e3aeb6d7d02a7fa4a2f7c7b275177724cc3bbd258bac5748ea18a6b81dac2302cc0576, c1: 0x11c3ea888162242587ae7851754f3e0194e0e2f487b8190585debc6d2e934fd87b8779921ee9febfbdcc73405736ba5a)
b.z: Fp2(c0: 0x03d3dade5c52f71522775fcf84fefa2352b5b7a7b9c5fce68ce0696e0312c5520f7429bfd2cae0ccdc69733baba7ab84, c1: 0x12d0d62574b9ed0794587b7fba9dbdd6d5c34e4f85365ccfb55d873bf83793a3f20c9619dcb45b2b1d2ee8aba85c1051)

c.x: Fp2(c0: 0x16e7cb0e8005ef0907ddaca75f1d3ce4d092252d682413f710dfc145623e5c106309e645d12dc9269a60972a0354c945, c1: 0x0137cfb2e1aecf444ecd6b62ce17692a2a91326dfdfb12d080bac36a8166fa63df1bc5d6ce2da7e922aa92219f33a506)
c.y: Fp2(c0: 0x0540fd95dbf8d64c97317161201f6e0fcc1c6ab96e42afe628223543a952e4559922db2c8aa36a1b5c8911e611d2d5a5, c1: 0x18ef5d022d8c19036f025bd3492ae2df926a52294009da00a4839a2b159c84caa8c996ddd0f1d4c1de05dd29a8415776)
c.z: Fp2(c0: 0x0f548d4f9fdca7281e79bf8323296ddb7b13c14382f563572338fa3f769a985c4b3af6056ec6853126e36b7c573f4478, c1: 0x0352c7b0b459518c61339e6d7b64a0e847a322d0aa6dc66a11595c457493fe8d9075df73e82e3624d17715a50514995b)

from constantine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.