group.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package kyber
  2. import (
  3. "crypto/cipher"
  4. )
  5. // Scalar represents a scalar value by which
  6. // a Point (group element) may be encrypted to produce another Point.
  7. // This is an exponent in DSA-style groups,
  8. // in which security is based on the Discrete Logarithm assumption,
  9. // and a scalar multiplier in elliptic curve groups.
  10. type Scalar interface {
  11. Marshaling
  12. // Equality test for two Scalars derived from the same Group.
  13. Equal(s2 Scalar) bool
  14. // Set sets the receiver equal to another Scalar a.
  15. Set(a Scalar) Scalar
  16. // Clone creates a new Scalar with the same value.
  17. Clone() Scalar
  18. // SetInt64 sets the receiver to a small integer value.
  19. SetInt64(v int64) Scalar
  20. // Set to the additive identity (0).
  21. Zero() Scalar
  22. // Set to the modular sum of scalars a and b.
  23. Add(a, b Scalar) Scalar
  24. // Set to the modular difference a - b.
  25. Sub(a, b Scalar) Scalar
  26. // Set to the modular negation of scalar a.
  27. Neg(a Scalar) Scalar
  28. // Set to the multiplicative identity (1).
  29. One() Scalar
  30. // Set to the modular product of scalars a and b.
  31. Mul(a, b Scalar) Scalar
  32. // Set to the modular division of scalar a by scalar b.
  33. Div(a, b Scalar) Scalar
  34. // Set to the modular inverse of scalar a.
  35. Inv(a Scalar) Scalar
  36. // Set to a fresh random or pseudo-random scalar.
  37. Pick(rand cipher.Stream) Scalar
  38. // SetBytes sets the scalar from a byte-slice,
  39. // reducing if necessary to the appropriate modulus.
  40. // The endianess of the byte-slice is determined by the
  41. // implementation.
  42. SetBytes([]byte) Scalar
  43. }
  44. // Point represents an element of a public-key cryptographic Group.
  45. // For example,
  46. // this is a number modulo the prime P in a DSA-style Schnorr group,
  47. // or an (x, y) point on an elliptic curve.
  48. // A Point can contain a Diffie-Hellman public key, an ElGamal ciphertext, etc.
  49. type Point interface {
  50. Marshaling
  51. // Equality test for two Points derived from the same Group.
  52. Equal(s2 Point) bool
  53. // Null sets the receiver to the neutral identity element.
  54. Null() Point
  55. // Base sets the receiver to this group's standard base point.
  56. Base() Point
  57. // Pick sets the receiver to a fresh random or pseudo-random Point.
  58. Pick(rand cipher.Stream) Point
  59. // Set sets the receiver equal to another Point p.
  60. Set(p Point) Point
  61. // Clone clones the underlying point.
  62. Clone() Point
  63. // Maximum number of bytes that can be embedded in a single
  64. // group element via Pick().
  65. EmbedLen() int
  66. // Embed encodes a limited amount of specified data in the
  67. // Point, using r as a source of cryptographically secure
  68. // random data. Implementations only embed the first EmbedLen
  69. // bytes of the given data.
  70. Embed(data []byte, r cipher.Stream) Point
  71. // Extract data embedded in a point chosen via Embed().
  72. // Returns an error if doesn't represent valid embedded data.
  73. Data() ([]byte, error)
  74. // Add points so that their scalars add homomorphically.
  75. Add(a, b Point) Point
  76. // Subtract points so that their scalars subtract homomorphically.
  77. Sub(a, b Point) Point
  78. // Set to the negation of point a.
  79. Neg(a Point) Point
  80. // Multiply point p by the scalar s.
  81. // If p == nil, multiply with the standard base point Base().
  82. Mul(s Scalar, p Point) Point
  83. }
  84. // AllowsVarTime allows callers to determine if a given kyber.Scalar
  85. // or kyber.Point supports opting-in to variable time operations. If
  86. // an object implements AllowsVarTime, then the caller can use
  87. // AllowVarTime(true) in order to allow variable time operations on
  88. // that object until AllowVarTime(false) is called. Variable time
  89. // operations may be faster, but also risk leaking information via a
  90. // timing side channel. Thus they are only safe to use on public
  91. // Scalars and Points, never on secret ones.
  92. type AllowsVarTime interface {
  93. AllowVarTime(bool)
  94. }
  95. // Group interface represents a mathematical group
  96. // usable for Diffie-Hellman key exchange, ElGamal encryption,
  97. // and the related body of public-key cryptographic algorithms
  98. // and zero-knowledge proof methods.
  99. // The Group interface is designed in particular to be a generic front-end
  100. // to both traditional DSA-style modular arithmetic groups
  101. // and ECDSA-style elliptic curves:
  102. // the caller of this interface's methods
  103. // need not know or care which specific mathematical construction
  104. // underlies the interface.
  105. //
  106. // The Group interface is essentially just a "constructor" interface
  107. // enabling the caller to generate the two particular types of objects
  108. // relevant to DSA-style public-key cryptography;
  109. // we call these objects Points and Scalars.
  110. // The caller must explicitly initialize or set a new Point or Scalar object
  111. // to some value before using it as an input to some other operation
  112. // involving Point and/or Scalar objects.
  113. // For example, to compare a point P against the neutral (identity) element,
  114. // you might use P.Equal(suite.Point().Null()),
  115. // but not just P.Equal(suite.Point()).
  116. //
  117. // It is expected that any implementation of this interface
  118. // should satisfy suitable hardness assumptions for the applicable group:
  119. // e.g., that it is cryptographically hard for an adversary to
  120. // take an encrypted Point and the known generator it was based on,
  121. // and derive the Scalar with which the Point was encrypted.
  122. // Any implementation is also expected to satisfy
  123. // the standard homomorphism properties that Diffie-Hellman
  124. // and the associated body of public-key cryptography are based on.
  125. type Group interface {
  126. String() string
  127. ScalarLen() int // Max length of scalars in bytes
  128. Scalar() Scalar // Create new scalar
  129. PointLen() int // Max length of point in bytes
  130. Point() Point // Create new point
  131. }