@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"testing"
5
+ "crypto/rand"
5
6
"net/http"
6
7
"io/ioutil"
7
8
"encoding/json"
@@ -334,6 +335,88 @@ func TestGenerateCommitment(t *testing.T) {
334
335
}
335
336
}
336
337
338
+ func TestGenerateSchnorr (t * testing.T ) {
339
+ x , _ := rand .Int (rand .Reader , bn256 .Order )
340
+ P := new (bn256.G1 ).ScalarBaseMult (x )
341
+ m := "This is the message to be signed"
342
+ generateSchnorrInputs := GenerateSchnorrInputs {P : NewCurvePoint (P ), X : fmt .Sprintf ("0x%064x" , x ), M : m }
343
+ marshalledJSON , _ := json .Marshal (generateSchnorrInputs )
344
+ response , err := http .Post ("http://localhost:" + port + "/generate/schnorr/" , "application/json" , bytes .NewBuffer (marshalledJSON ))
345
+ if err != nil {
346
+ t .Errorf ("An error occurred while making request to API: %s\n " , err )
347
+ return
348
+ }
349
+ defer response .Body .Close ()
350
+ contents , err := ioutil .ReadAll (response .Body )
351
+ if err != nil {
352
+ t .Errorf ("An error occurred while reading response body: %s\n " , err )
353
+ return
354
+ }
355
+ var res Response
356
+ err = json .Unmarshal (contents , & res )
357
+ if err != nil {
358
+ t .Errorf ("An error occurred while reading into JSON object: %s\n " , err )
359
+ return
360
+ }
361
+ if res .Err != nil && res .Err .Msg != "" {
362
+ t .Errorf (fmt .Sprintf ("An error occurred: %s\n " , res .Err .Msg ))
363
+ return
364
+ }
365
+ sig := res .Sig
366
+ P_out := new (bn256.G1 )
367
+ marshalledPoint := sig .P .X [2 :] + sig .P .Y [2 :]
368
+ marshalledBytes , err := hex .DecodeString (marshalledPoint )
369
+ if err != nil {
370
+ t .Errorf ("An error occurred while decoding hex string: %s\n " , err )
371
+ return
372
+ }
373
+ _ , err = P_out .Unmarshal (marshalledBytes )
374
+ if err != nil {
375
+ t .Errorf ("An error occurred while unmarshalling BN256 curve point: %s\n " , err )
376
+ return
377
+ }
378
+ E_out , _ := new (big.Int ).SetString (sig .E [2 :], 16 )
379
+ S_out , _ := new (big.Int ).SetString (sig .S [2 :], 16 )
380
+ M_out := sig .M
381
+ isValid , err := VerifySchnorrSignature (P_out , M_out , E_out , S_out , err )
382
+ if (! isValid ) {
383
+ t .Errorf ("Invalid Schnorr signature generated" )
384
+ }
385
+ }
386
+
387
+ func TestVerifySchnorr (t * testing.T ) {
388
+ x , _ := rand .Int (rand .Reader , bn256 .Order )
389
+ P := new (bn256.G1 ).ScalarBaseMult (x )
390
+ m := "This is the message to be signed"
391
+ P_out , M_out , E_out , S_out , err := GenerateSchnorrSignature (P , m , x , nil )
392
+ schnorrSignature := SchnorrSignature {P : NewCurvePoint (P_out ), M : M_out , E : fmt .Sprintf ("0x%064x" , E_out ), S : fmt .Sprintf ("0x%064x" , S_out )}
393
+ marshalledJSON , _ := json .Marshal (schnorrSignature )
394
+ response , err := http .Post ("http://localhost:" + port + "/verify/schnorr/" , "application/json" , bytes .NewBuffer (marshalledJSON ))
395
+ if err != nil {
396
+ t .Errorf ("An error occurred while making request to API: %s\n " , err )
397
+ return
398
+ }
399
+ defer response .Body .Close ()
400
+ contents , err := ioutil .ReadAll (response .Body )
401
+ if err != nil {
402
+ t .Errorf ("An error occurred while reading response body: %s\n " , err )
403
+ return
404
+ }
405
+ var res Response
406
+ err = json .Unmarshal (contents , & res )
407
+ if err != nil {
408
+ t .Errorf ("An error occurred while reading into JSON object: %s\n " , err )
409
+ return
410
+ }
411
+ if res .Err != nil && res .Err .Msg != "" {
412
+ t .Errorf (fmt .Sprintf ("An error occurred: %s\n " , res .Err .Msg ))
413
+ return
414
+ }
415
+ if (res .Text != "true" ) {
416
+ t .Errorf ("Invalid Schnorr signature generated" )
417
+ }
418
+ }
419
+
337
420
func TestBigAdd (t * testing.T ) {
338
421
a , _ := new (big.Int ).SetString ("20222222222222222222222222222222222222222222222222222222222222222222222222222" , 10 )
339
422
b , _ := new (big.Int ).SetString ("11111111111111111111111111111111111111111111111111111111111111111111111111111" , 10 )
0 commit comments