Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

curve_test.go 4.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package p503
  2. import (
  3. "bytes"
  4. . "github.com/cloudflare/p751sidh/internal/isogeny"
  5. "testing"
  6. "testing/quick"
  7. )
  8. func TestOne(t *testing.T) {
  9. var tmp Fp2Element
  10. kFieldOps.Mul(&tmp, &P503_OneFp2, &affine_xP)
  11. if !VartimeEqFp2(&tmp, &affine_xP) {
  12. t.Error("Not equal 1")
  13. }
  14. }
  15. // This test is here only to ensure that ScalarMult helper works correctly
  16. func TestScalarMultVersusSage(t *testing.T) {
  17. var xP ProjectivePoint
  18. xP = ProjectivePoint{X: affine_xP, Z: P503_OneFp2}
  19. xP = ScalarMult(&curve, &xP, mScalarBytes[:]) // = x([m]P)
  20. affine_xQ := xP.ToAffine(kCurveOps)
  21. if !VartimeEqFp2(&affine_xaP, affine_xQ) {
  22. t.Error("\nExpected\n", affine_xaP, "\nfound\n", affine_xQ)
  23. }
  24. }
  25. func Test_jInvariant(t *testing.T) {
  26. var curve = ProjectiveCurveParameters{A: curve_A, C: curve_C}
  27. var jbufRes [P503_SharedSecretSize]byte
  28. var jbufExp [P503_SharedSecretSize]byte
  29. // Computed using Sage
  30. // j = 3674553797500778604587777859668542828244523188705960771798425843588160903687122861541242595678107095655647237100722594066610650373491179241544334443939077738732728884873568393760629500307797547379838602108296735640313894560419*i + 3127495302417548295242630557836520229396092255080675419212556702820583041296798857582303163183558315662015469648040494128968509467224910895884358424271180055990446576645240058960358037224785786494172548090318531038910933793845
  31. var known_j = Fp2Element{
  32. A: FpElement{0x2c441d03b72e27c, 0xf2c6748151dbf84, 0x3a774f6191070e, 0xa7c6212c9c800ba6, 0x23921b5cf09abc27, 0x9e1baefbb3cd4265, 0x8cd6a289f12e10dc, 0x3fa364128cf87e},
  33. B: FpElement{0xe7497ac2bf6b0596, 0x629ee01ad23bd039, 0x95ee11587a119fa7, 0x572fb28a24772269, 0x3c00410b6c71567e, 0xe681e83a345f8a34, 0x65d21b1d96bd2d52, 0x7889a47e58901},
  34. }
  35. kCurveOps.Jinvariant(&curve, jbufRes[:])
  36. kCurveOps.Fp2ToBytes(jbufExp[:], &known_j)
  37. if !bytes.Equal(jbufRes[:], jbufExp[:]) {
  38. t.Error("Computed incorrect j-invariant: found\n", jbufRes, "\nexpected\n", jbufExp)
  39. }
  40. }
  41. func TestProjectivePointVartimeEq(t *testing.T) {
  42. var xP ProjectivePoint
  43. xP = ProjectivePoint{X: affine_xP, Z: P503_OneFp2}
  44. xQ := xP
  45. // Scale xQ, which results in the same projective point
  46. kFieldOps.Mul(&xQ.X, &xQ.X, &curve_A)
  47. kFieldOps.Mul(&xQ.Z, &xQ.Z, &curve_A)
  48. if !VartimeEqProjFp2(&xP, &xQ) {
  49. t.Error("Expected the scaled point to be equal to the original")
  50. }
  51. }
  52. func TestPointDoubleVersusSage(t *testing.T) {
  53. var curve = ProjectiveCurveParameters{A: curve_A, C: curve_C}
  54. var params = kCurveOps.CalcCurveParamsEquiv4(&curve)
  55. var xP ProjectivePoint
  56. xP = ProjectivePoint{X: affine_xP, Z: P503_OneFp2}
  57. kCurveOps.Pow2k(&xP, &params, 1)
  58. affine_xQ := xP.ToAffine(kCurveOps)
  59. if !VartimeEqFp2(affine_xQ, &affine_xP2) {
  60. t.Error("\nExpected\n", affine_xP2, "\nfound\n", affine_xQ)
  61. }
  62. }
  63. func TestPointMul4VersusSage(t *testing.T) {
  64. var params = kCurveOps.CalcCurveParamsEquiv4(&curve)
  65. var xP ProjectivePoint
  66. xP = ProjectivePoint{X: affine_xP, Z: P503_OneFp2}
  67. kCurveOps.Pow2k(&xP, &params, 2)
  68. affine_xQ := xP.ToAffine(kCurveOps)
  69. if !VartimeEqFp2(affine_xQ, &affine_xP4) {
  70. t.Error("\nExpected\n", affine_xP4, "\nfound\n", affine_xQ)
  71. }
  72. }
  73. func TestPointMul9VersusSage(t *testing.T) {
  74. var params = kCurveOps.CalcCurveParamsEquiv3(&curve)
  75. var xP ProjectivePoint
  76. xP = ProjectivePoint{X: affine_xP, Z: P503_OneFp2}
  77. kCurveOps.Pow3k(&xP, &params, 2)
  78. affine_xQ := xP.ToAffine(kCurveOps)
  79. if !VartimeEqFp2(affine_xQ, &affine_xP9) {
  80. t.Error("\nExpected\n", affine_xP9, "\nfound\n", affine_xQ)
  81. }
  82. }
  83. func TestPointPow2kVersusScalarMult(t *testing.T) {
  84. var xP, xQ, xR ProjectivePoint
  85. var params = kCurveOps.CalcCurveParamsEquiv4(&curve)
  86. xP = ProjectivePoint{X: affine_xP, Z: P503_OneFp2}
  87. xQ = xP
  88. kCurveOps.Pow2k(&xQ, &params, 5)
  89. xR = ScalarMult(&curve, &xP, []byte{32})
  90. affine_xQ := xQ.ToAffine(kCurveOps) // = x([32]P)
  91. affine_xR := xR.ToAffine(kCurveOps) // = x([32]P)
  92. if !VartimeEqFp2(affine_xQ, affine_xR) {
  93. t.Error("\nExpected\n", affine_xQ, "\nfound\n", affine_xR)
  94. }
  95. }
  96. func TestPointTripleVersusAddDouble(t *testing.T) {
  97. tripleEqualsAddDouble := func(params GeneratedTestParams) bool {
  98. var P2, P3, P2plusP ProjectivePoint
  99. eqivParams4 := kCurveOps.CalcCurveParamsEquiv4(&params.Cparam)
  100. eqivParams3 := kCurveOps.CalcCurveParamsEquiv3(&params.Cparam)
  101. P2 = params.Point
  102. P3 = params.Point
  103. kCurveOps.Pow2k(&P2, &eqivParams4, 1) // = x([2]P)
  104. kCurveOps.Pow3k(&P3, &eqivParams3, 1) // = x([3]P)
  105. P2plusP = AddProjFp2(&P2, &params.Point, &params.Point) // = x([2]P + P)
  106. return VartimeEqProjFp2(&P3, &P2plusP)
  107. }
  108. if err := quick.Check(tripleEqualsAddDouble, quickCheckConfig); err != nil {
  109. t.Error(err)
  110. }
  111. }
  112. func BenchmarkThreePointLadder255BitScalar(b *testing.B) {
  113. var mScalarBytes = [...]uint8{203, 155, 185, 191, 131, 228, 50, 178, 207, 191, 61, 141, 174, 173, 207, 243, 159, 243, 46, 163, 19, 102, 69, 92, 36, 225, 0, 37, 114, 19, 191, 0}
  114. for n := 0; n < b.N; n++ {
  115. kCurveOps.ScalarMul3Pt(&curve, &threePointLadderInputs[0], &threePointLadderInputs[1], &threePointLadderInputs[2], 255, mScalarBytes[:])
  116. }
  117. }