Debian Codegraph @

scalar.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package ristretto255
6
7 import (
8 "encoding/base64"
9 "errors"
10
11 "filippo.io/edwards25519"
12 )
13
14 // A Scalar is an element of the ristretto255 scalar field, as specified in
15 // RFC 9496, Section 4.4. That is, an integer modulo
16 //
17 // l = 2^252 + 27742317777372353535851937790883648493
18 //
19 // The zero value is a valid zero element.
20 type Scalar struct {
21 s edwards25519.Scalar
22 }
23
24 // NewScalar returns a Scalar set to the value 0.
25 func NewScalar() *Scalar {
26 return &Scalar{}
27 }
28
29 // Set sets the value of s to x and returns s.
30 func (s *Scalar) Set(x *Scalar) *Scalar {
31 *s = *x
32 return s
33 }
34
35 // Add sets s = x + y mod l and returns s.
36 func (s *Scalar) Add(x, y *Scalar) *Scalar {
37 s.s.Add(&x.s, &y.s)
38 return s
39 }
40
41 // Subtract sets s = x - y mod l and returns s.
42 func (s *Scalar) Subtract(x, y *Scalar) *Scalar {
43 s.s.Subtract(&x.s, &y.s)
44 return s
45 }
46
47 // Negate sets s = -x mod l and returns s.
48 func (s *Scalar) Negate(x *Scalar) *Scalar {
49 s.s.Negate(&x.s)
50 return s
51 }
52
53 // Multiply sets s = x * y mod l and returns s.
54 func (s *Scalar) Multiply(x, y *Scalar) *Scalar {
55 s.s.Multiply(&x.s, &y.s)
56 return s
57 }
58
59 // Invert sets s = 1 / x such that s * x = 1 mod l and returns s.
60 //
61 // If x is 0, the result is undefined.
62 func (s *Scalar) Invert(x *Scalar) *Scalar {
63 s.s.Invert(&x.s)
64 return s
65 }
66
67 // FromUniformBytes sets s to a uniformly distributed value given 64 uniformly
68 // distributed random bytes.
69 //
70 // Deprecated: use SetUniformBytes. This API will be removed before v1.0.0.
71 func (s *Scalar) FromUniformBytes(x []byte) *Scalar {
72 if _, err := s.SetUniformBytes(x); err != nil {
73 panic(err.Error())
74 }
75 return s
76 }
77
78 // SetUniformBytes sets s to a uniformly distributed value given 64 uniformly
79 // distributed random bytes by interpreting the 64-byte string as a 512-bit
80 // unsigned integer in little-endian order and reducing the integer modulo l.
81 //
82 // If x is not of the right length, SetUniformBytes returns nil and an error,
83 // and the receiver is unchanged.
84 func (s *Scalar) SetUniformBytes(x []byte) (*Scalar, error) {
85 if _, err := s.s.SetUniformBytes(x); err != nil {
86 return nil, errors.New("ristretto255: SetUniformBytes input is not 64 bytes long")
87 }
88 return s, nil
89 }
90
91 // Decode sets s = x, where x is a 32 bytes little-endian encoding of s. If x is
92 // not a canonical encoding of s, Decode returns an error and the receiver is
93 // unchanged.
94 //
95 // Deprecated: use SetCanonicalBytes. This API will be removed before v1.0.0.
96 func (s *Scalar) Decode(x []byte) error {
97 _, err := s.SetCanonicalBytes(x)
98 return err
99 }
100
101 // SetCanonicalBytes sets s = x, where x is a 32 bytes little-endian encoding of
102 // s. If x is not a canonical encoding of s, SetCanonicalBytes returns nil and
103 // an error and the receiver is unchanged.
104 func (s *Scalar) SetCanonicalBytes(x []byte) (*Scalar, error) {
105 if _, err := s.s.SetCanonicalBytes(x); err != nil {
106 return nil, errors.New("ristretto255: " + err.Error())
107 }
108 return s, nil
109 }
110
111 // Encode appends a 32 bytes little-endian encoding of s to b.
112 //
113 // Deprecated: use Bytes. This API will be removed before v1.0.0.
114 func (s *Scalar) Encode(b []byte) []byte {
115 ret, out := sliceForAppend(b, 32)
116 copy(out, s.s.Bytes())
117 return ret
118 }
119
120 // Bytes returns the 32 bytes little-endian canonical encoding of s.
121 func (s *Scalar) Bytes() []byte {
122 return s.s.Bytes()
123 }
124
125 // Equal returns 1 if v and u are equal, and 0 otherwise.
126 func (s *Scalar) Equal(u *Scalar) int {
127 return s.s.Equal(&u.s)
128 }
129
130 // Zero sets s = 0 and returns s.
131 func (s *Scalar) Zero() *Scalar {
132 s.s = edwards25519.Scalar{}
133 return s
134 }
135
136 // MarshalText implements encoding/TextMarshaler interface
137 func (s *Scalar) MarshalText() (text []byte, err error) {
138 b := s.Encode([]byte{})
139 return []byte(base64.StdEncoding.EncodeToString(b)), nil
140 }
141
142 // UnmarshalText implements encoding/TextMarshaler interface
143 func (s *Scalar) UnmarshalText(text []byte) error {
144 sb, err := base64.StdEncoding.DecodeString(string(text))
145 if err == nil {
146 return s.Decode(sb)
147 }
148 return err
149 }
150
151 // String implements the Stringer interface
152 func (s *Scalar) String() string {
153 result, _ := s.MarshalText()
154 return string(result)
155 }