aboutsummaryrefslogtreecommitdiff
path: root/test/test_e2.ml
blob: fe02647cf68b697cb775e5a0ddaaf6a29b75e846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module E2 = Plonkers.Curves.E2

let e2 = Primitives.Testables.e2
let f = Primitives.Literals.e2

let test_strconv () =
  let a =
    f
      ( [|
          "1995966487400060513418002023207574157295305948670062141574847467571155809406706098785462170240887564093686629877623";
          "662886336588505248783096013641071887822498506927882698632762069520277859710519968066621437591913838510294024221845";
        |],
        [|
          "2118133940922730619706689117607342224796346036207279785462459268901402082964459754036268590522181697629582678327818";
          "3180158093976603722423949631887924556492770394116171123059316509806286695323674296949805082557706278821978684422237";
        |] )
  in
  let s =
    "{(1995966487400060513418002023207574157295305948670062141574847467571155809406706098785462170240887564093686629877623,662886336588505248783096013641071887822498506927882698632762069520277859710519968066621437591913838510294024221845);(2118133940922730619706689117607342224796346036207279785462459268901402082964459754036268590522181697629582678327818,3180158093976603722423949631887924556492770394116171123059316509806286695323674296949805082557706278821978684422237)}"
  in
  Alcotest.(check string) "equals" s (E2.to_string a);
  Alcotest.(check e2) "equals" a (E2.of_string s)

let test_curve () =
  Alcotest.(check bool)
    "fails" true
    (try
       E2.of_string "{(0,0);(0,0)}" |> ignore;
       false
     with
    | Invalid_argument _ -> true
    | _ -> false)

let test_subgroup () =
  Alcotest.(check bool)
    "fails" true
    (try
       E2.of_string
         "{(1892439472479153589801064982851113576513855369383752766070453260535595479925033899105674578530308757833348467758334,1754566506869274301718190827735363782447329239031987366461838092103444111363078178418905338941768026382060426750966);(794551614141569136399571058573462200719130525886688829309738555530265772199592670562441013794848828639618754399483,3178059973541833847838400063594433908549328373729351972490312027443775384638577658076883634527704803989808995997410)}"
       |> ignore;
       false
     with
    | Invalid_argument _ -> true
    | _ -> false)

let test_add () =
  Alcotest.(check e2)
    "equals"
    (f
       ( [|
           "499713417605359223674748473758724973948438124705509202302245471290609292306320586538959574203334940457575903991711";
           "3849381523047393672660382657163613807212712961677974554645092181592292011827458146770352521145227167398610078418185";
         |],
         [|
           "3750738009713936929372241922830934361036892789616987325774050932170221795352544491858695643172294211282599110319834";
           "3561628900031869771392652210785168204755113200504180138415139599905464331701794168749876553728218422931049173996790";
         |] ))
    (E2.add
       (f
          ( [|
              "22988558892716969390996985732585240683297793773456313893700754641768024025112560131028908246472269831559878289575";
              "2129706665424714060840367155348060356419393088613610263996150242693619717302217969445633229711091283126869298324191";
            |],
            [|
              "326344468552893907028622086721957524007052308610498663186676327256007196980344862961459048761730914514101678031123";
              "3578939464694739647421989794082523254572164841250176471270721235280032616122779184472701860960655547698666209531163";
            |] ))
       (f
          ( [|
              "3695633206715132410492337765882907918857795221238051472281688705875004174483167692618918438807066911317626485478957";
              "2976733649841946843632279759860096327423816839648446597596824846941966186914175551584845353423964217077348053962065";
            |],
            [|
              "49466427428220367265805901178013626540803904016880618718453870927457307757178707263045811558641515336191874310112";
              "2193129241285505962248559104914181807138658074292296828774608320812063550960488358009160505763316933125829480800311";
            |] )))

let test_double () =
  let a =
    f
      ( [|
          "2630340632582992300068504223042664782235656344249493885516510645984188976796605076720730074664979072939686934257721";
          "3837313288726631479216536661083348837649337798727188764618972962388445003540771017271319539046080622692222479670778";
        |],
        [|
          "1683267476147804034933489382796354199818919863598454325548320805934667239588199593331393365665593904784252069691594";
          "1024386153970382602226736749102780316785763625305478417896374484204124772205537751029991953527668491051585823287162";
        |] )
  in
  Alcotest.(check e2) "equals" (E2.add a a) (E2.double a)

let test_sub () =
  Alcotest.(check e2)
    "equals"
    (f
       ( [|
           "2312381534305864038524856410951813412729532000275307896810795592963361639762761897364492993639180834599053426654921";
           "2941946573854091849965088183943891771101444533626394142063742950941901356314086230253727457469500594733301574969126";
         |],
         [|
           "3177038204107211918017473977040505404653337824416553912249365797499278408762074130788569657467795326880900042739300";
           "3911466467618211275615446067985091611817124681549927950440782870032642030586794441825300408067249342511123677227979";
         |] ))
    (E2.sub
       (f
          ( [|
              "3084805454959641404340859217267539384943233503012252203041979288484567877164842557655530989970590180692844056705034";
              "1032626932734486057118525818914103317004948135891523645620202784883454249860761416804451664418540921374537112657339";
            |],
            [|
              "567609968615494874083532393538964631912521339405909452432595170197158682938070010563153437595578466221475239150664";
              "3965723577064707140049831298501225403010472139339519470330943732886849966270836690577682873296098666976942450913027";
            |] ))
       (f
          ( [|
              "1437969629809004608879055525849883700261548412934340004984286831449951101506825523108895949333702907561636531462862";
              "3378050752672563498305643671412654890539264706582196808663756002199076494302859579346688785007817086448260264744631";
            |],
            [|
              "3688003734830478721040484715021921439921663750934892483251989073660346159418502949754053418120146012703162584349321";
              "294600807465112254390070142232849516815757700388555441412905845762862521178052505971852404123896853420248399045909";
            |] )))

let test_scalar_mul () =
  Alcotest.(check e2)
    "equals"
    (f
       ( [|
           "1092894641180253280627824475749822745538993676284211343464536443667704016213631359345660547916686207447599304492990";
           "3324099030654709813055482585412186377145855773893664369906420608900967641626669259098384400094403575314217078271321";
         |],
         [|
           "1687432397129777546869295921172489186821991613986891301985886124965631277467866465780351130557754827159028199904720";
           "2313960103129372774774734178634617919906919386825218848510448126559550974987820849911034145924048313343848913976180";
         |] ))
    (E2.scalar_mul
       (Z.of_string
          "18231751679696756095586214113699656074182459369522013003988412807673749568724")
       (f
          ( [|
              "2514664268577432573560291176315683541072067402533046184786583901096804522469891814129602032754241329292791735547731";
              "1538891981432352671530281965293325684836353821970151860371636345672221993717129428051893539595256156434325115353328";
            |],
            [|
              "1020197595243302833683556943176598276009062246952019251126700930527258255249495123842828836305850271186575937412091";
              "957529907995660594751569367214519238040770457577242766357201002881030298493381525670720382223549185747922813057348";
            |] )))

let test_neg () =
  Alcotest.(check e2)
    "equals"
    (f
       ( [|
           "1545269364848460984490225670755503760473037616438596925671109140482515442704584830885661527842646221605111107590693";
           "1267478445196980059681345977877996221353505182887596481010812841900715170279481217659186985334416192910417976590132";
         |],
         [|
           "2138086320981055187350247210500555749249791226828379441597708773600979851439979713108017155176731805322171737993018";
           "2923510363463074904143568736318190088176676574358556535023319924451820130012383135761860188454542388330577656123700";
         |] ))
    (E2.neg
       (f
          ( [|
              "1545269364848460984490225670755503760473037616438596925671109140482515442704584830885661527842646221605111107590693";
              "1267478445196980059681345977877996221353505182887596481010812841900715170279481217659186985334416192910417976590132";
            |],
            [|
              "1864323234240612206067542615235348407307091593110628443734349362523051799050858151334670473952283858715722534566769";
              "1078899191758592489274221089417714068380206245580451350308738211672211520478454728680827440674473275707316616436087";
            |] )));
  let a =
    f
      ( [|
          "2145319835080253046126157108405019899066932648811331837314642288549615338163523922360993646054737343377628571487304";
          "1358459155514781387151184879883145700362295043361724816677598042273140202854606635229527873146151979028028462660866";
        |],
        [|
          "2261863074976855596039999928645826587478562702008490591716114111186951516904330390847333904655429074513962463071272";
          "2015356306298728612773777903102863638865287815742386042734473839158493228418734519947389685935269331153485256794451";
        |] )
  in
  Alcotest.(check e2) "equals" E2.inf (E2.add a (E2.neg a))

let tests =
  Alcotest.
    [
      test_case "String Conversion" `Quick test_strconv;
      test_case "Is On Curve" `Quick test_curve;
      test_case "Is In Canonical Subgroup" `Quick test_subgroup;
      test_case "Addition" `Quick test_add;
      test_case "Double" `Quick test_double;
      test_case "Subtraction" `Quick test_sub;
      test_case "Scalar Multiplication" `Quick test_scalar_mul;
      test_case "Additive Inverse" `Quick test_neg;
    ]