gofront 1.7.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/README.md +14 -14
  2. package/package.json +10 -38
  3. package/src/backend/js/expressions.js +2060 -389
  4. package/src/backend/js/index.js +586 -174
  5. package/src/backend/js/natives.js +383 -0
  6. package/src/backend/js/runtime.js +936 -1644
  7. package/src/backend/js/statements.js +1349 -132
  8. package/src/backend/js/stdlib/index.js +51 -94
  9. package/src/backend/js/templ.js +67 -118
  10. package/src/backend/js/type-info.js +436 -0
  11. package/src/backend/shared/fixed-float.js +20 -0
  12. package/src/backend/shared/fmt-types.js +80 -0
  13. package/src/backend/shared/host-unicode.js +36 -0
  14. package/src/backend/wasm/boundary.js +753 -872
  15. package/src/backend/wasm/emit-builtins.js +914 -237
  16. package/src/backend/wasm/emit-exprs.js +1456 -195
  17. package/src/backend/wasm/emit-maps.js +78 -380
  18. package/src/backend/wasm/emit-stmts.js +1659 -214
  19. package/src/backend/wasm/emit.js +614 -88
  20. package/src/backend/wasm/encode.js +43 -13
  21. package/src/backend/wasm/globals.js +491 -0
  22. package/src/backend/wasm/glue.js +2 -4
  23. package/src/backend/wasm/index.js +406 -3195
  24. package/src/backend/wasm/method-expr.js +392 -0
  25. package/src/backend/wasm/module-emitter.js +1615 -0
  26. package/src/backend/wasm/monomorph.js +164 -9
  27. package/src/backend/wasm/natives.js +596 -0
  28. package/src/backend/wasm/prescan.js +249 -0
  29. package/src/backend/wasm/prune-funcs.js +89 -0
  30. package/src/backend/wasm/stdlib/index.js +2 -37
  31. package/src/backend/wasm/stdlib/simd.js +293 -28
  32. package/src/backend/wasm/types.js +260 -27
  33. package/src/backend/wasm/variadic.js +93 -0
  34. package/src/backend/wasm/wat.js +47 -20
  35. package/src/{cli-core.js → cli/cli-core.js} +241 -11
  36. package/src/{test-runner.js → cli/test-runner.js} +188 -122
  37. package/src/{vendor.js → cli/vendor.js} +3 -4
  38. package/src/compiler.js +202 -386
  39. package/src/index.js +15 -169
  40. package/src/{lexer.js → lexer/lexer.js} +203 -62
  41. package/src/{templ-lexer.js → lexer/templ-lexer.js} +61 -18
  42. package/src/{tokens.js → lexer/tokens.js} +2 -0
  43. package/src/link/sourcemap.js +97 -0
  44. package/src/link/stdlib.js +737 -0
  45. package/src/link/type-names.js +148 -0
  46. package/src/link/wasm.js +318 -0
  47. package/src/lower/bounds.js +547 -0
  48. package/src/lower/boxing.js +1 -1
  49. package/src/lower/captures.js +267 -64
  50. package/src/lower/embedding.js +32 -3
  51. package/src/lower/errors-as.js +196 -0
  52. package/src/lower/fmt-formatters.js +905 -0
  53. package/src/lower/functions.js +2 -0
  54. package/src/lower/gen-types.js +274 -0
  55. package/src/lower/index.js +33 -19
  56. package/src/lower/json-codecs.js +1379 -0
  57. package/src/lower/ownership.js +47 -8
  58. package/src/lower/range.js +86 -1
  59. package/src/lower/reachability.js +150 -0
  60. package/src/lower/walk.js +12 -0
  61. package/src/parser/declarations.js +83 -15
  62. package/src/parser/expressions.js +110 -18
  63. package/src/parser/index.js +2 -1
  64. package/src/parser/statements.js +38 -22
  65. package/src/{templ-parser.js → parser/templ.js} +179 -17
  66. package/src/parser/types.js +70 -9
  67. package/src/resolver.js +21 -1
  68. package/src/typechecker/assignability.js +291 -19
  69. package/src/typechecker/const-fold.js +399 -0
  70. package/src/typechecker/constants.js +282 -0
  71. package/src/typechecker/expressions.js +1220 -183
  72. package/src/typechecker/index.js +659 -92
  73. package/src/typechecker/init-order.js +426 -0
  74. package/src/typechecker/operand-text.js +40 -0
  75. package/src/typechecker/resolve.js +613 -62
  76. package/src/typechecker/statements.js +212 -20
  77. package/src/typechecker/stdlib/excluded.js +902 -0
  78. package/src/typechecker/stdlib/index.js +38 -0
  79. package/src/typechecker/stdlib/simd.js +65 -15
  80. package/src/typechecker/termination.js +11 -2
  81. package/src/typechecker/types.js +268 -514
  82. package/stdlib/bytes/buffer.go +479 -0
  83. package/stdlib/bytes/bytes.go +1010 -0
  84. package/stdlib/bytes/reader.go +161 -0
  85. package/stdlib/cmp/cmp.go +65 -0
  86. package/stdlib/context/context.go +115 -0
  87. package/stdlib/crypto/rand/rand.go +34 -0
  88. package/stdlib/crypto/sha256/sha256.go +249 -0
  89. package/stdlib/encoding/base64/base64.go +331 -0
  90. package/stdlib/encoding/binary/binary.go +332 -0
  91. package/stdlib/encoding/hex/hex.go +140 -0
  92. package/stdlib/encoding/json/errors.go +31 -0
  93. package/stdlib/encoding/json/indent.go +71 -0
  94. package/stdlib/encoding/json/json.go +62 -0
  95. package/stdlib/encoding/json/number.go +12 -0
  96. package/stdlib/encoding/json/stream.go +173 -0
  97. package/stdlib/errors/errors.go +174 -0
  98. package/stdlib/fmt/errors.go +110 -0
  99. package/stdlib/fmt/format.go +732 -0
  100. package/stdlib/fmt/print.go +1232 -0
  101. package/stdlib/gom/gom.go +293 -0
  102. package/stdlib/hash/hash.go +53 -0
  103. package/stdlib/html/html.go +251 -0
  104. package/stdlib/internal/fmtrt/fmtrt.go +163 -0
  105. package/stdlib/internal/jsonrt/dec.go +871 -0
  106. package/stdlib/internal/jsonrt/enc.go +516 -0
  107. package/stdlib/internal/jsonrt/indent.go +135 -0
  108. package/stdlib/internal/jsonrt/number.go +27 -0
  109. package/stdlib/internal/jsonrt/reader.go +73 -0
  110. package/stdlib/internal/jsonrt/scanner.go +658 -0
  111. package/stdlib/io/io.go +502 -0
  112. package/stdlib/iter/iter.go +18 -0
  113. package/stdlib/maps/maps.go +131 -0
  114. package/stdlib/math/bits/bits.go +507 -0
  115. package/stdlib/math/extra.go +272 -0
  116. package/stdlib/math/math.go +439 -0
  117. package/stdlib/math/rand/rand.go +297 -0
  118. package/stdlib/os/os.go +93 -0
  119. package/stdlib/path/filepath/filepath.go +47 -0
  120. package/stdlib/path/path.go +394 -0
  121. package/stdlib/regexp/regexp.go +231 -0
  122. package/stdlib/slices/slices.go +526 -0
  123. package/stdlib/slices/sort.go +209 -0
  124. package/stdlib/slices/zsort_cmp.go +480 -0
  125. package/stdlib/slices/zsort_ordered.go +482 -0
  126. package/stdlib/sort/sort.go +500 -0
  127. package/stdlib/sort/zsort_func.go +480 -0
  128. package/stdlib/sort/zsort_iface.go +480 -0
  129. package/stdlib/strconv/float.go +1699 -0
  130. package/stdlib/strconv/isprint.go +752 -0
  131. package/stdlib/strconv/quote.go +526 -0
  132. package/stdlib/strconv/strconv.go +616 -0
  133. package/stdlib/strings/builder.go +95 -0
  134. package/stdlib/strings/reader.go +163 -0
  135. package/stdlib/strings/replacer.go +102 -0
  136. package/stdlib/strings/strings.go +807 -0
  137. package/stdlib/sync/sync.go +268 -0
  138. package/stdlib/testing/async_js.go +61 -0
  139. package/stdlib/testing/benchmark.go +297 -0
  140. package/stdlib/testing/testing.go +523 -0
  141. package/stdlib/time/duration.go +233 -0
  142. package/stdlib/time/format.go +745 -0
  143. package/stdlib/time/parse.go +790 -0
  144. package/stdlib/time/sleep_js.go +63 -0
  145. package/stdlib/time/time.go +834 -0
  146. package/stdlib/unicode/unicode.go +250 -0
  147. package/stdlib/unicode/utf8/utf8.go +464 -0
  148. package/src/backend/js/stdlib/base64.js +0 -12
  149. package/src/backend/js/stdlib/binary.js +0 -102
  150. package/src/backend/js/stdlib/bits.js +0 -46
  151. package/src/backend/js/stdlib/builder.js +0 -63
  152. package/src/backend/js/stdlib/bytes.js +0 -122
  153. package/src/backend/js/stdlib/errors.js +0 -44
  154. package/src/backend/js/stdlib/fmt.js +0 -178
  155. package/src/backend/js/stdlib/gom.js +0 -155
  156. package/src/backend/js/stdlib/hex.js +0 -17
  157. package/src/backend/js/stdlib/html.js +0 -20
  158. package/src/backend/js/stdlib/io.js +0 -137
  159. package/src/backend/js/stdlib/json.js +0 -972
  160. package/src/backend/js/stdlib/maps.js +0 -45
  161. package/src/backend/js/stdlib/math.js +0 -69
  162. package/src/backend/js/stdlib/os.js +0 -13
  163. package/src/backend/js/stdlib/path.js +0 -48
  164. package/src/backend/js/stdlib/rand.js +0 -32
  165. package/src/backend/js/stdlib/regexp.js +0 -54
  166. package/src/backend/js/stdlib/sha256.js +0 -12
  167. package/src/backend/js/stdlib/shared.js +0 -17
  168. package/src/backend/js/stdlib/simd.js +0 -24
  169. package/src/backend/js/stdlib/slices.js +0 -77
  170. package/src/backend/js/stdlib/sort.js +0 -30
  171. package/src/backend/js/stdlib/strconv.js +0 -74
  172. package/src/backend/js/stdlib/strings.js +0 -222
  173. package/src/backend/js/stdlib/sync.js +0 -13
  174. package/src/backend/js/stdlib/testing.js +0 -34
  175. package/src/backend/js/stdlib/time.js +0 -128
  176. package/src/backend/js/stdlib/unicode.js +0 -25
  177. package/src/backend/js/stdlib/utf8.js +0 -26
  178. package/src/backend/wasm/const-fold.js +0 -205
  179. package/src/backend/wasm/emit-stdlib.js +0 -13
  180. package/src/backend/wasm/stdlib/bytes.js +0 -5039
  181. package/src/backend/wasm/stdlib/core.js +0 -8903
  182. package/src/backend/wasm/stdlib/io.js +0 -2161
  183. package/src/backend/wasm/stdlib/packages.js +0 -2400
  184. package/src/backend/wasm/stdlib/pure.js +0 -720
  185. package/src/backend/wasm/stdlib/time.js +0 -1200
  186. package/src/typechecker/stdlib/core.js +0 -801
  187. package/src/typechecker/stdlib/extended.js +0 -914
  188. package/src/typechecker/stdlib.js +0 -22
  189. /package/src/{asset-manager.js → cli/asset-manager.js} +0 -0
  190. /package/src/{colors.js → cli/colors.js} +0 -0
  191. /package/src/{dev-server.js → cli/dev-server.js} +0 -0
  192. /package/src/{minifier.js → cli/minifier.js} +0 -0
  193. /package/src/{project-config.js → cli/project-config.js} +0 -0
  194. /package/src/{pwa.js → cli/pwa.js} +0 -0
@@ -0,0 +1,161 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Overlay on the emitter-backed bytes package: Reader written in Go over the
6
+ // real package's algorithm.
7
+ package bytes
8
+
9
+ import (
10
+ "errors"
11
+ "io"
12
+ "unicode/utf8"
13
+ )
14
+
15
+ // A Reader implements the [io.Reader], [io.ReaderAt], [io.WriterTo], [io.Seeker],
16
+ // [io.ByteScanner], and [io.RuneScanner] interfaces by reading from
17
+ // a byte slice.
18
+ // Unlike a [Buffer], a Reader is read-only and supports seeking.
19
+ // The zero value for Reader operates like a Reader of an empty slice.
20
+ type Reader struct {
21
+ s []byte
22
+ i int64 // current reading index
23
+ prevRune int // index of previous rune; or < 0
24
+ }
25
+
26
+ // Len returns the number of bytes of the unread portion of the
27
+ // slice.
28
+ func (r *Reader) Len() int {
29
+ if r.i >= int64(len(r.s)) {
30
+ return 0
31
+ }
32
+ return int(int64(len(r.s)) - r.i)
33
+ }
34
+
35
+ // Size returns the original length of the underlying byte slice.
36
+ // Size is the number of bytes available for reading via [Reader.ReadAt].
37
+ // The result is unaffected by any method calls except [Reader.Reset].
38
+ func (r *Reader) Size() int64 { return int64(len(r.s)) }
39
+
40
+ // Read implements the [io.Reader] interface.
41
+ func (r *Reader) Read(b []byte) (n int, err error) {
42
+ if r.i >= int64(len(r.s)) {
43
+ return 0, io.EOF
44
+ }
45
+ r.prevRune = -1
46
+ n = copy(b, r.s[r.i:])
47
+ r.i += int64(n)
48
+ return
49
+ }
50
+
51
+ // ReadAt implements the [io.ReaderAt] interface.
52
+ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
53
+ // cannot modify state - see io.ReaderAt
54
+ if off < 0 {
55
+ return 0, errors.New("bytes.Reader.ReadAt: negative offset")
56
+ }
57
+ if off >= int64(len(r.s)) {
58
+ return 0, io.EOF
59
+ }
60
+ n = copy(b, r.s[off:])
61
+ if n < len(b) {
62
+ err = io.EOF
63
+ }
64
+ return
65
+ }
66
+
67
+ // ReadByte implements the [io.ByteReader] interface.
68
+ func (r *Reader) ReadByte() (byte, error) {
69
+ r.prevRune = -1
70
+ if r.i >= int64(len(r.s)) {
71
+ return 0, io.EOF
72
+ }
73
+ b := r.s[r.i]
74
+ r.i++
75
+ return b, nil
76
+ }
77
+
78
+ // UnreadByte complements [Reader.ReadByte] in implementing the [io.ByteScanner] interface.
79
+ func (r *Reader) UnreadByte() error {
80
+ if r.i <= 0 {
81
+ return errors.New("bytes.Reader.UnreadByte: at beginning of slice")
82
+ }
83
+ r.prevRune = -1
84
+ r.i--
85
+ return nil
86
+ }
87
+
88
+ // ReadRune implements the [io.RuneReader] interface.
89
+ func (r *Reader) ReadRune() (ch rune, size int, err error) {
90
+ if r.i >= int64(len(r.s)) {
91
+ r.prevRune = -1
92
+ return 0, 0, io.EOF
93
+ }
94
+ r.prevRune = int(r.i)
95
+ if c := r.s[r.i]; c < utf8.RuneSelf {
96
+ r.i++
97
+ return rune(c), 1, nil
98
+ }
99
+ ch, size = utf8.DecodeRune(r.s[r.i:])
100
+ r.i += int64(size)
101
+ return
102
+ }
103
+
104
+ // UnreadRune complements [Reader.ReadRune] in implementing the [io.RuneScanner] interface.
105
+ func (r *Reader) UnreadRune() error {
106
+ if r.i <= 0 {
107
+ return errors.New("bytes.Reader.UnreadRune: at beginning of slice")
108
+ }
109
+ if r.prevRune < 0 {
110
+ return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune")
111
+ }
112
+ r.i = int64(r.prevRune)
113
+ r.prevRune = -1
114
+ return nil
115
+ }
116
+
117
+ // Seek implements the [io.Seeker] interface.
118
+ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
119
+ r.prevRune = -1
120
+ var abs int64
121
+ switch whence {
122
+ case io.SeekStart:
123
+ abs = offset
124
+ case io.SeekCurrent:
125
+ abs = r.i + offset
126
+ case io.SeekEnd:
127
+ abs = int64(len(r.s)) + offset
128
+ default:
129
+ return 0, errors.New("bytes.Reader.Seek: invalid whence")
130
+ }
131
+ if abs < 0 {
132
+ return 0, errors.New("bytes.Reader.Seek: negative position")
133
+ }
134
+ r.i = abs
135
+ return abs, nil
136
+ }
137
+
138
+ // WriteTo implements the [io.WriterTo] interface.
139
+ func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
140
+ r.prevRune = -1
141
+ if r.i >= int64(len(r.s)) {
142
+ return 0, nil
143
+ }
144
+ b := r.s[r.i:]
145
+ m, err := w.Write(b)
146
+ if m > len(b) {
147
+ panic("bytes.Reader.WriteTo: invalid Write count")
148
+ }
149
+ r.i += int64(m)
150
+ n = int64(m)
151
+ if m != len(b) && err == nil {
152
+ err = io.ErrShortWrite
153
+ }
154
+ return
155
+ }
156
+
157
+ // Reset resets the [Reader] to be reading from b.
158
+ func (r *Reader) Reset(b []byte) { *r = Reader{b, 0, -1} }
159
+
160
+ // NewReader returns a new [Reader] reading from b.
161
+ func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }
@@ -0,0 +1,65 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Package cmp provides types and functions related to comparing ordered
6
+ // values. Same API as Go's cmp package.
7
+ package cmp
8
+
9
+ // Ordered is a constraint that permits any ordered type: any type that
10
+ // supports the operators < <= >= >.
11
+ type Ordered interface {
12
+ ~int | ~int8 | ~int16 | ~int32 | ~int64 |
13
+ ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
14
+ ~float32 | ~float64 |
15
+ ~string
16
+ }
17
+
18
+ // Less reports whether x is less than y. A NaN is less than any non-NaN,
19
+ // and -0.0 is not less than 0.0.
20
+ func Less[T Ordered](x, y T) bool {
21
+ return (isNaNValue(x) && !isNaNValue(y)) || x < y
22
+ }
23
+
24
+ // Compare returns -1 if x is less than y, 0 if x equals y, and +1 if x is
25
+ // greater than y. A NaN is less than any non-NaN, a NaN equals a NaN, and
26
+ // -0.0 equals 0.0.
27
+ func Compare[T Ordered](x, y T) int {
28
+ xNaN := isNaNValue(x)
29
+ yNaN := isNaNValue(y)
30
+ if xNaN {
31
+ if yNaN {
32
+ return 0
33
+ }
34
+ return -1
35
+ }
36
+ if yNaN {
37
+ return +1
38
+ }
39
+ if x < y {
40
+ return -1
41
+ }
42
+ if x > y {
43
+ return +1
44
+ }
45
+ return 0
46
+ }
47
+
48
+ // isNaNValue reports whether x is a NaN without requiring the math package
49
+ // (named so it does not collide with the browser global isNaN until Task 0.9).
50
+ // This will always return false if T is not floating-point.
51
+ func isNaNValue[T Ordered](x T) bool {
52
+ return x != x
53
+ }
54
+
55
+ // Or returns the first of its arguments that is not equal to the zero
56
+ // value. If no argument is non-zero, it returns the zero value.
57
+ func Or[T comparable](vals ...T) T {
58
+ var zero T
59
+ for _, v := range vals {
60
+ if v != zero {
61
+ return v
62
+ }
63
+ }
64
+ return zero
65
+ }
@@ -0,0 +1,115 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Package context carries a cancellation signal and request-scoped values
6
+ // across API boundaries.
7
+ //
8
+ // It is the part of the real package that needs no channels or goroutines:
9
+ // Background, TODO, WithCancel and WithValue. The Context interface has no
10
+ // Done method and there are no deadlines or timeouts; poll Err instead.
11
+ package context
12
+
13
+ import (
14
+ "errors"
15
+ "time"
16
+ )
17
+
18
+ // A Context carries a cancellation signal and values across API boundaries.
19
+ type Context interface {
20
+ // Deadline returns the time when work done on behalf of this context
21
+ // should be canceled. No context has a deadline, so ok is always false.
22
+ Deadline() (deadline time.Time, ok bool)
23
+
24
+ // Err returns nil while the context is live, and Canceled once it has
25
+ // been canceled.
26
+ Err() error
27
+
28
+ // Value returns the value associated with key, or nil.
29
+ Value(key any) any
30
+ }
31
+
32
+ // Canceled is the error returned by Context.Err when the context is canceled.
33
+ var Canceled = errors.New("context canceled")
34
+
35
+ // A CancelFunc tells an operation to abandon its work.
36
+ type CancelFunc func()
37
+
38
+ type emptyCtx struct {
39
+ name string
40
+ }
41
+
42
+ func (*emptyCtx) Deadline() (deadline time.Time, ok bool) { return }
43
+ func (*emptyCtx) Err() error { return nil }
44
+ func (*emptyCtx) Value(key any) any { return nil }
45
+ func (e *emptyCtx) String() string { return e.name }
46
+
47
+ var (
48
+ background = &emptyCtx{"context.Background"}
49
+ todo = &emptyCtx{"context.TODO"}
50
+ )
51
+
52
+ // Background returns a non-nil, empty Context. It is never canceled and has
53
+ // no values.
54
+ func Background() Context {
55
+ return background
56
+ }
57
+
58
+ // TODO returns a non-nil, empty Context, for code that has not yet decided
59
+ // which Context to use.
60
+ func TODO() Context {
61
+ return todo
62
+ }
63
+
64
+ type cancelCtx struct {
65
+ parent Context
66
+ canceled bool
67
+ }
68
+
69
+ func (c *cancelCtx) Deadline() (deadline time.Time, ok bool) { return }
70
+
71
+ func (c *cancelCtx) Err() error {
72
+ if c.canceled {
73
+ return Canceled
74
+ }
75
+ return c.parent.Err()
76
+ }
77
+
78
+ func (c *cancelCtx) Value(key any) any { return c.parent.Value(key) }
79
+
80
+ // WithCancel returns a derived context that is canceled when the returned
81
+ // cancel function is called, or when the parent is canceled.
82
+ func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
83
+ if parent == nil {
84
+ panic("cannot create context from nil parent")
85
+ }
86
+ c := &cancelCtx{parent: parent}
87
+ return c, func() { c.canceled = true }
88
+ }
89
+
90
+ type valueCtx struct {
91
+ Context
92
+ key, val any
93
+ }
94
+
95
+ func (c *valueCtx) Deadline() (deadline time.Time, ok bool) { return c.Context.Deadline() }
96
+
97
+ func (c *valueCtx) Err() error { return c.Context.Err() }
98
+
99
+ func (c *valueCtx) Value(key any) any {
100
+ if c.key == key {
101
+ return c.val
102
+ }
103
+ return c.Context.Value(key)
104
+ }
105
+
106
+ // WithValue returns a derived context in which key is associated with val.
107
+ func WithValue(parent Context, key, val any) Context {
108
+ if parent == nil {
109
+ panic("cannot create context from nil parent")
110
+ }
111
+ if key == nil {
112
+ panic("nil key")
113
+ }
114
+ return &valueCtx{parent, key, val}
115
+ }
@@ -0,0 +1,34 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Package rand implements a cryptographically secure random number
6
+ // generator over the host's secure source (crypto.getRandomValues).
7
+ package rand
8
+
9
+ import "io"
10
+
11
+ // hostU32 returns four random bytes from the host's secure source.
12
+ //
13
+ //gofront:intrinsic crypto_rand_u32
14
+ func hostU32() uint32
15
+
16
+ type reader struct{}
17
+
18
+ func (reader) Read(b []byte) (int, error) { return Read(b) }
19
+
20
+ // Reader is a global, shared instance of a cryptographically secure random
21
+ // number generator.
22
+ var Reader io.Reader = reader{}
23
+
24
+ // Read fills b with cryptographically secure random bytes. It never returns
25
+ // an error, and always fills b entirely.
26
+ func Read(b []byte) (n int, err error) {
27
+ for i := 0; i < len(b); i += 4 {
28
+ v := hostU32()
29
+ for j := 0; j < 4 && i+j < len(b); j++ {
30
+ b[i+j] = byte(v >> (8 * j))
31
+ }
32
+ }
33
+ return len(b), nil
34
+ }
@@ -0,0 +1,249 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Copyright 2009 The Go Authors. All rights reserved.
6
+ // Use of this source code is governed by a BSD-style
7
+ // license that can be found in the LICENSE file.
8
+
9
+ // Package sha256 implements the SHA224 and SHA256 hash algorithms as defined
10
+ // in FIPS 180-4.
11
+ package sha256
12
+
13
+ import "hash"
14
+
15
+ // The size of a SHA256 checksum in bytes.
16
+ const Size = 32
17
+
18
+ // The size of a SHA224 checksum in bytes.
19
+ const Size224 = 28
20
+
21
+ // The blocksize of SHA256 and SHA224 in bytes.
22
+ const BlockSize = 64
23
+
24
+ const (
25
+ chunk = 64
26
+ init0 = 0x6A09E667
27
+ init1 = 0xBB67AE85
28
+ init2 = 0x3C6EF372
29
+ init3 = 0xA54FF53A
30
+ init4 = 0x510E527F
31
+ init5 = 0x9B05688C
32
+ init6 = 0x1F83D9AB
33
+ init7 = 0x5BE0CD19
34
+ init0_224 = 0xC1059ED8
35
+ init1_224 = 0x367CD507
36
+ init2_224 = 0x3070DD17
37
+ init3_224 = 0xF70E5939
38
+ init4_224 = 0xFFC00B31
39
+ init5_224 = 0x68581511
40
+ init6_224 = 0x64F98FA7
41
+ init7_224 = 0xBEFA4FA4
42
+ )
43
+
44
+ var _K = [...]uint32{
45
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
46
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
47
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
48
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
49
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
50
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
51
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
52
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
53
+ }
54
+
55
+ // digest represents the partial evaluation of a checksum.
56
+ type digest struct {
57
+ h [8]uint32
58
+ x [64]byte
59
+ nx int
60
+ len uint64
61
+ is224 bool // mark if this digest is SHA-224
62
+ }
63
+
64
+ func (d *digest) Reset() {
65
+ if !d.is224 {
66
+ d.h[0] = init0
67
+ d.h[1] = init1
68
+ d.h[2] = init2
69
+ d.h[3] = init3
70
+ d.h[4] = init4
71
+ d.h[5] = init5
72
+ d.h[6] = init6
73
+ d.h[7] = init7
74
+ } else {
75
+ d.h[0] = init0_224
76
+ d.h[1] = init1_224
77
+ d.h[2] = init2_224
78
+ d.h[3] = init3_224
79
+ d.h[4] = init4_224
80
+ d.h[5] = init5_224
81
+ d.h[6] = init6_224
82
+ d.h[7] = init7_224
83
+ }
84
+ d.nx = 0
85
+ d.len = 0
86
+ }
87
+
88
+ // New returns a new [hash.Hash] computing the SHA256 checksum.
89
+ func New() hash.Hash {
90
+ d := new(digest)
91
+ d.Reset()
92
+ return d
93
+ }
94
+
95
+ // New224 returns a new [hash.Hash] computing the SHA224 checksum.
96
+ func New224() hash.Hash {
97
+ d := new(digest)
98
+ d.is224 = true
99
+ d.Reset()
100
+ return d
101
+ }
102
+
103
+ func (d *digest) Size() int {
104
+ if !d.is224 {
105
+ return Size
106
+ }
107
+ return Size224
108
+ }
109
+
110
+ func (d *digest) BlockSize() int { return BlockSize }
111
+
112
+ func (d *digest) Write(p []byte) (nn int, err error) {
113
+ nn = len(p)
114
+ d.len += uint64(nn)
115
+ if d.nx > 0 {
116
+ n := copy(d.x[d.nx:], p)
117
+ d.nx += n
118
+ if d.nx == chunk {
119
+ block(d, d.x[:])
120
+ d.nx = 0
121
+ }
122
+ p = p[n:]
123
+ }
124
+ if len(p) >= chunk {
125
+ n := len(p) &^ (chunk - 1)
126
+ block(d, p[:n])
127
+ p = p[n:]
128
+ }
129
+ if len(p) > 0 {
130
+ d.nx = copy(d.x[:], p)
131
+ }
132
+ return
133
+ }
134
+
135
+ func (d *digest) Sum(in []byte) []byte {
136
+ // Make a copy of d so that caller can keep writing and summing.
137
+ d0 := *d
138
+ sum := d0.checkSum()
139
+ if d0.is224 {
140
+ return append(in, sum[:Size224]...)
141
+ }
142
+ return append(in, sum[:]...)
143
+ }
144
+
145
+ func (d *digest) checkSum() [32]byte {
146
+ length := d.len
147
+ // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
148
+ var tmp [64 + 8]byte // padding + length buffer
149
+ tmp[0] = 0x80
150
+ var t uint64
151
+ if length%64 < 56 {
152
+ t = 56 - length%64
153
+ } else {
154
+ t = 64 + 56 - length%64
155
+ }
156
+
157
+ // Length in bits.
158
+ length <<= 3
159
+ padlen := tmp[:t+8]
160
+ for i := uint64(0); i < 8; i++ {
161
+ padlen[t+i] = byte(length >> (56 - 8*i))
162
+ }
163
+ d.Write(padlen)
164
+
165
+ if d.nx != 0 {
166
+ panic("d.nx != 0")
167
+ }
168
+
169
+ var sum [32]byte
170
+ for i := 0; i < 8; i++ {
171
+ v := d.h[i]
172
+ sum[4*i] = byte(v >> 24)
173
+ sum[4*i+1] = byte(v >> 16)
174
+ sum[4*i+2] = byte(v >> 8)
175
+ sum[4*i+3] = byte(v)
176
+ }
177
+
178
+ return sum
179
+ }
180
+
181
+ // Sum256 returns the SHA256 checksum of the data.
182
+ func Sum256(data []byte) [32]byte {
183
+ var d digest
184
+ d.Reset()
185
+ d.Write(data)
186
+ return d.checkSum()
187
+ }
188
+
189
+ // Sum224 returns the SHA224 checksum of the data.
190
+ func Sum224(data []byte) [28]byte {
191
+ var d digest
192
+ d.is224 = true
193
+ d.Reset()
194
+ d.Write(data)
195
+ sum := d.checkSum()
196
+ var out [28]byte
197
+ copy(out[:], sum[:Size224])
198
+ return out
199
+ }
200
+
201
+ func block(dig *digest, p []byte) {
202
+ var w [64]uint32
203
+ h0, h1, h2, h3, h4, h5, h6, h7 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4], dig.h[5], dig.h[6], dig.h[7]
204
+ for len(p) >= chunk {
205
+ // Can interlace the computation of w with the
206
+ // rounds below if needed for speed.
207
+ for i := 0; i < 16; i++ {
208
+ j := i * 4
209
+ w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
210
+ }
211
+ for i := 16; i < 64; i++ {
212
+ v1 := w[i-2]
213
+ t1 := (v1>>17 | v1<<(32-17)) ^ (v1>>19 | v1<<(32-19)) ^ (v1 >> 10)
214
+ v2 := w[i-15]
215
+ t2 := (v2>>7 | v2<<(32-7)) ^ (v2>>18 | v2<<(32-18)) ^ (v2 >> 3)
216
+ w[i] = t1 + w[i-7] + t2 + w[i-16]
217
+ }
218
+
219
+ a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
220
+
221
+ for i := 0; i < 64; i++ {
222
+ t1 := h + ((e>>6 | e<<(32-6)) ^ (e>>11 | e<<(32-11)) ^ (e>>25 | e<<(32-25))) + ((e & f) ^ (^e & g)) + _K[i] + w[i]
223
+
224
+ t2 := ((a>>2 | a<<(32-2)) ^ (a>>13 | a<<(32-13)) ^ (a>>22 | a<<(32-22))) + ((a & b) ^ (a & c) ^ (b & c))
225
+
226
+ h = g
227
+ g = f
228
+ f = e
229
+ e = d + t1
230
+ d = c
231
+ c = b
232
+ b = a
233
+ a = t1 + t2
234
+ }
235
+
236
+ h0 += a
237
+ h1 += b
238
+ h2 += c
239
+ h3 += d
240
+ h4 += e
241
+ h5 += f
242
+ h6 += g
243
+ h7 += h
244
+
245
+ p = p[chunk:]
246
+ }
247
+
248
+ dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4], dig.h[5], dig.h[6], dig.h[7] = h0, h1, h2, h3, h4, h5, h6, h7
249
+ }