scalar_mult_cgo.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //go:build !gofuzz && cgo
  5. // +build !gofuzz,cgo
  6. package secp256k1
  7. import (
  8. "math/big"
  9. "unsafe"
  10. )
  11. /*
  12. #include "libsecp256k1/include/secp256k1.h"
  13. extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar);
  14. */
  15. import "C"
  16. func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
  17. // Ensure scalar is exactly 32 bytes. We pad always, even if
  18. // scalar is 32 bytes long, to avoid a timing side channel.
  19. if len(scalar) > 32 {
  20. panic("can't handle scalars > 256 bits")
  21. }
  22. // NOTE: potential timing issue
  23. padded := make([]byte, 32)
  24. copy(padded[32-len(scalar):], scalar)
  25. scalar = padded
  26. // Do the multiplication in C, updating point.
  27. point := make([]byte, 64)
  28. readBits(Bx, point[:32])
  29. readBits(By, point[32:])
  30. pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
  31. scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
  32. res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr)
  33. // Unpack the result and clear temporaries.
  34. x := new(big.Int).SetBytes(point[:32])
  35. y := new(big.Int).SetBytes(point[32:])
  36. for i := range point {
  37. point[i] = 0
  38. }
  39. for i := range padded {
  40. scalar[i] = 0
  41. }
  42. if res != 1 {
  43. return nil, nil
  44. }
  45. return x, y
  46. }