Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

conversion.gno

24.23 Kb · 754 lines
  1// conversions contains methods for converting Uint instances to other types and vice versa.
  2// This includes conversions to and from basic types such as uint64 and int32, as well as string representations
  3// and byte slices. Additionally, it covers marshaling and unmarshaling for JSON and other text formats.
  4package uint256
  5
  6import (
  7	"encoding/binary"
  8	"strconv"
  9)
 10
 11// Uint64 returns the lower 64 bits of z as a uint64.
 12//
 13// Returns:
 14//   - value: the low 64 bits of z
 15func (z *Uint) Uint64() uint64 {
 16	return z[0]
 17}
 18
 19// Int64 returns the lower 64 bits of z as an int64.
 20//
 21// Returns:
 22//   - value: the low 64 bits of z interpreted as int64
 23func (z *Uint) Int64() int64 {
 24	return int64(z.Uint64())
 25}
 26
 27// Uint64WithOverflow returns the lower 64 bits of z and true if overflow occurred.
 28//
 29// Returns:
 30//   - value: the low 64 bits of z
 31//   - overflow: true when any upper 192 bits are non-zero
 32func (z *Uint) Uint64WithOverflow() (uint64, bool) {
 33	return z[0], (z[1] | z[2] | z[3]) != 0
 34}
 35
 36// SetUint64 sets z to the value of x and returns z.
 37//
 38// Parameters:
 39//   - x: the uint64 value assigned to z
 40//
 41// Returns:
 42//   - result: z after setting it to x
 43func (z *Uint) SetUint64(x uint64) *Uint {
 44	z[3], z[2], z[1], z[0] = 0, 0, 0, x
 45	return z
 46}
 47
 48// IsUint64 reports whether z can be represented as a uint64.
 49//
 50// Returns:
 51//   - fits: true when z is representable in 64 bits
 52func (z *Uint) IsUint64() bool {
 53	return (z[1] | z[2] | z[3]) == 0
 54}
 55
 56// Dec returns the decimal representation of z.
 57//
 58// Returns:
 59//   - decimal: the base-10 representation of z
 60func (z *Uint) Dec() string {
 61	if z.IsZero() {
 62		return "0"
 63	}
 64	if z.IsUint64() {
 65		return strconv.FormatUint(z.Uint64(), 10)
 66	}
 67
 68	// The max uint64 value being 18446744073709551615, the largest
 69	// power-of-ten below that is 10000000000000000000.
 70	// When we do a DivMod using that number, the remainder that we
 71	// get back is the lower part of the output.
 72	//
 73	// The ascii-output of remainder will never exceed 19 bytes (since it will be
 74	// below 10000000000000000000).
 75	//
 76	// Algorithm example using 100 as divisor
 77	//
 78	// 12345 % 100 = 45   (rem)
 79	// 12345 / 100 = 123  (quo)
 80	// -> output '45', continue iterate on 123
 81	var (
 82		// out is 98 bytes long: 78 (max size of a string without leading zeroes,
 83		// plus slack so we can copy 19 bytes every iteration).
 84		// We init it with zeroes, because when strconv appends the ascii representations,
 85		// it will omit leading zeroes.
 86		out      [98]byte
 87		divisor  Uint
 88		y        = *z                 // copy z to avoid modifying it
 89		pos      = len(out)           // position to write to
 90		buf      [19]byte             // buffer to write uint64:s to
 91		bufSlice []byte     = buf[:0] // slice for strconv.AppendUint
 92	)
 93
 94	// Initialize out array with '0's
 95	for i := range out {
 96		out[i] = '0'
 97	}
 98
 99	// Set divisor to 10^19
100	divisor.SetUint64(10000000000000000000)
101
102	for {
103		// Obtain Q and R for divisor
104		var quot Uint
105		rem := udivrem(quot[:], y[:], &divisor)
106		y = quot // Set Q for next loop
107		// Convert the R to ascii representation
108		bufSlice = strconv.AppendUint(bufSlice[:0], rem.Uint64(), 10)
109		// Copy in the ascii digits
110		copy(out[pos-len(bufSlice):], bufSlice)
111		if y.IsZero() {
112			break
113		}
114		// Move 19 digits left
115		pos -= 19
116	}
117	// skip leading zeroes by only using the 'used size' of bufSlice
118	return string(out[pos-len(bufSlice):])
119}
120
121// ToString returns the decimal string representation of z.
122// Returns an empty string if z is nil. This method doesn't exist in holiman's uint256.
123//
124// Returns:
125//   - decimal: z in base 10, or an empty string when z is nil
126func (z *Uint) ToString() string {
127	if z == nil {
128		return ""
129	}
130
131	return z.Dec()
132}
133
134// SetBytes interprets buf as a big-endian unsigned integer and sets z to that value.
135// If buf is larger than 32 bytes, uses only the last 32 bytes. Returns z.
136//
137// Parameters:
138//   - buf: the big-endian byte sequence; if longer than 32 bytes, only its final 32 bytes are used
139//
140// Returns:
141//   - result: z after decoding the selected big-endian bytes
142func (z *Uint) SetBytes(buf []byte) *Uint {
143	switch l := len(buf); l {
144	case 0:
145		z.Clear()
146	case 1:
147		z.SetBytes1(buf)
148	case 2:
149		z.SetBytes2(buf)
150	case 3:
151		z.SetBytes3(buf)
152	case 4:
153		z.SetBytes4(buf)
154	case 5:
155		z.SetBytes5(buf)
156	case 6:
157		z.SetBytes6(buf)
158	case 7:
159		z.SetBytes7(buf)
160	case 8:
161		z.SetBytes8(buf)
162	case 9:
163		z.SetBytes9(buf)
164	case 10:
165		z.SetBytes10(buf)
166	case 11:
167		z.SetBytes11(buf)
168	case 12:
169		z.SetBytes12(buf)
170	case 13:
171		z.SetBytes13(buf)
172	case 14:
173		z.SetBytes14(buf)
174	case 15:
175		z.SetBytes15(buf)
176	case 16:
177		z.SetBytes16(buf)
178	case 17:
179		z.SetBytes17(buf)
180	case 18:
181		z.SetBytes18(buf)
182	case 19:
183		z.SetBytes19(buf)
184	case 20:
185		z.SetBytes20(buf)
186	case 21:
187		z.SetBytes21(buf)
188	case 22:
189		z.SetBytes22(buf)
190	case 23:
191		z.SetBytes23(buf)
192	case 24:
193		z.SetBytes24(buf)
194	case 25:
195		z.SetBytes25(buf)
196	case 26:
197		z.SetBytes26(buf)
198	case 27:
199		z.SetBytes27(buf)
200	case 28:
201		z.SetBytes28(buf)
202	case 29:
203		z.SetBytes29(buf)
204	case 30:
205		z.SetBytes30(buf)
206	case 31:
207		z.SetBytes31(buf)
208	default:
209		z.SetBytes32(buf[l-32:])
210	}
211	return z
212}
213
214// SetBytes1 sets z from a 1-byte big-endian slice and returns z.
215// Panics if input is shorter than 1 byte.
216//
217// Parameters:
218//   - in: the first byte is decoded in big-endian order; it must be present
219//
220// Returns:
221//   - result: z after decoding the first byte of in
222func (z *Uint) SetBytes1(in []byte) *Uint {
223	z[3], z[2], z[1] = 0, 0, 0
224	z[0] = uint64(in[0])
225	return z
226}
227
228// SetBytes2 sets z from a 2-byte big-endian slice and returns z.
229// Panics if input is shorter than 2 bytes.
230//
231// Parameters:
232//   - in: the byte sequence whose first 2 bytes are decoded in big-endian order; it must contain at least 2 bytes
233//
234// Returns:
235//   - result: z after decoding the first 2 bytes of in in big-endian order
236func (z *Uint) SetBytes2(in []byte) *Uint {
237	_ = in[1] // bounds check hint to compiler; see golang.org/issue/14808
238	z[3], z[2], z[1] = 0, 0, 0
239	z[0] = uint64(binary.BigEndian.Uint16(in[0:2]))
240	return z
241}
242
243// SetBytes3 sets z from a 3-byte big-endian slice and returns z.
244// Panics if input is shorter than 3 bytes.
245//
246// Parameters:
247//   - in: the byte sequence whose first 3 bytes are decoded in big-endian order; it must contain at least 3 bytes
248//
249// Returns:
250//   - result: z after decoding the first 3 bytes of in in big-endian order
251func (z *Uint) SetBytes3(in []byte) *Uint {
252	_ = in[2] // bounds check hint to compiler; see golang.org/issue/14808
253	z[3], z[2], z[1] = 0, 0, 0
254	z[0] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16
255	return z
256}
257
258// SetBytes4 sets z from a 4-byte big-endian slice and returns z.
259// Panics if input is shorter than 4 bytes.
260//
261// Parameters:
262//   - in: the byte sequence whose first 4 bytes are decoded in big-endian order; it must contain at least 4 bytes
263//
264// Returns:
265//   - result: z after decoding the first 4 bytes of in in big-endian order
266func (z *Uint) SetBytes4(in []byte) *Uint {
267	_ = in[3] // bounds check hint to compiler; see golang.org/issue/14808
268	z[3], z[2], z[1] = 0, 0, 0
269	z[0] = uint64(binary.BigEndian.Uint32(in[0:4]))
270	return z
271}
272
273// SetBytes5 sets z from a 5-byte big-endian slice and returns z.
274// Panics if input is shorter than 5 bytes.
275//
276// Parameters:
277//   - in: the byte sequence whose first 5 bytes are decoded in big-endian order; it must contain at least 5 bytes
278//
279// Returns:
280//   - result: z after decoding the first 5 bytes of in in big-endian order
281func (z *Uint) SetBytes5(in []byte) *Uint {
282	_ = in[4] // bounds check hint to compiler; see golang.org/issue/14808
283	z[3], z[2], z[1] = 0, 0, 0
284	z[0] = bigEndianUint40(in[0:5])
285	return z
286}
287
288// SetBytes6 sets z from a 6-byte big-endian slice and returns z.
289// Panics if input is shorter than 6 bytes.
290//
291// Parameters:
292//   - in: the byte sequence whose first 6 bytes are decoded in big-endian order; it must contain at least 6 bytes
293//
294// Returns:
295//   - result: z after decoding the first 6 bytes of in in big-endian order
296func (z *Uint) SetBytes6(in []byte) *Uint {
297	_ = in[5] // bounds check hint to compiler; see golang.org/issue/14808
298	z[3], z[2], z[1] = 0, 0, 0
299	z[0] = bigEndianUint48(in[0:6])
300	return z
301}
302
303// SetBytes7 sets z from a 7-byte big-endian slice and returns z.
304// Panics if input is shorter than 7 bytes.
305//
306// Parameters:
307//   - in: the byte sequence whose first 7 bytes are decoded in big-endian order; it must contain at least 7 bytes
308//
309// Returns:
310//   - result: z after decoding the first 7 bytes of in in big-endian order
311func (z *Uint) SetBytes7(in []byte) *Uint {
312	_ = in[6] // bounds check hint to compiler; see golang.org/issue/14808
313	z[3], z[2], z[1] = 0, 0, 0
314	z[0] = bigEndianUint56(in[0:7])
315	return z
316}
317
318// SetBytes8 sets z from an 8-byte big-endian slice and returns z.
319// Panics if input is shorter than 8 bytes.
320//
321// Parameters:
322//   - in: the byte sequence whose first 8 bytes are decoded in big-endian order; it must contain at least 8 bytes
323//
324// Returns:
325//   - result: z after decoding the first 8 bytes of in in big-endian order
326func (z *Uint) SetBytes8(in []byte) *Uint {
327	_ = in[7] // bounds check hint to compiler; see golang.org/issue/14808
328	z[3], z[2], z[1] = 0, 0, 0
329	z[0] = binary.BigEndian.Uint64(in[0:8])
330	return z
331}
332
333// SetBytes9 sets z from a 9-byte big-endian slice and returns z.
334// Panics if input is shorter than 9 bytes.
335//
336// Parameters:
337//   - in: the byte sequence whose first 9 bytes are decoded in big-endian order; it must contain at least 9 bytes
338//
339// Returns:
340//   - result: z after decoding the first 9 bytes of in in big-endian order
341func (z *Uint) SetBytes9(in []byte) *Uint {
342	_ = in[8] // bounds check hint to compiler; see golang.org/issue/14808
343	z[3], z[2] = 0, 0
344	z[1] = uint64(in[0])
345	z[0] = binary.BigEndian.Uint64(in[1:9])
346	return z
347}
348
349// SetBytes10 sets z from a 10-byte big-endian slice and returns z.
350// Panics if input is shorter than 10 bytes.
351//
352// Parameters:
353//   - in: the byte sequence whose first 10 bytes are decoded in big-endian order; it must contain at least 10 bytes
354//
355// Returns:
356//   - result: z after decoding the first 10 bytes of in in big-endian order
357func (z *Uint) SetBytes10(in []byte) *Uint {
358	_ = in[9] // bounds check hint to compiler; see golang.org/issue/14808
359	z[3], z[2] = 0, 0
360	z[1] = uint64(binary.BigEndian.Uint16(in[0:2]))
361	z[0] = binary.BigEndian.Uint64(in[2:10])
362	return z
363}
364
365// SetBytes11 sets z from an 11-byte big-endian slice and returns z.
366// Panics if input is shorter than 11 bytes.
367//
368// Parameters:
369//   - in: the byte sequence whose first 11 bytes are decoded in big-endian order; it must contain at least 11 bytes
370//
371// Returns:
372//   - result: z after decoding the first 11 bytes of in in big-endian order
373func (z *Uint) SetBytes11(in []byte) *Uint {
374	_ = in[10] // bounds check hint to compiler; see golang.org/issue/14808
375	z[3], z[2] = 0, 0
376	z[1] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16
377	z[0] = binary.BigEndian.Uint64(in[3:11])
378	return z
379}
380
381// SetBytes12 sets z from a 12-byte big-endian slice and returns z.
382// Panics if input is shorter than 12 bytes.
383//
384// Parameters:
385//   - in: the byte sequence whose first 12 bytes are decoded in big-endian order; it must contain at least 12 bytes
386//
387// Returns:
388//   - result: z after decoding the first 12 bytes of in in big-endian order
389func (z *Uint) SetBytes12(in []byte) *Uint {
390	_ = in[11] // bounds check hint to compiler; see golang.org/issue/14808
391	z[3], z[2] = 0, 0
392	z[1] = uint64(binary.BigEndian.Uint32(in[0:4]))
393	z[0] = binary.BigEndian.Uint64(in[4:12])
394	return z
395}
396
397// SetBytes13 sets z from a 13-byte big-endian slice and returns z.
398// Panics if input is shorter than 13 bytes.
399//
400// Parameters:
401//   - in: the byte sequence whose first 13 bytes are decoded in big-endian order; it must contain at least 13 bytes
402//
403// Returns:
404//   - result: z after decoding the first 13 bytes of in in big-endian order
405func (z *Uint) SetBytes13(in []byte) *Uint {
406	_ = in[12] // bounds check hint to compiler; see golang.org/issue/14808
407	z[3], z[2] = 0, 0
408	z[1] = bigEndianUint40(in[0:5])
409	z[0] = binary.BigEndian.Uint64(in[5:13])
410	return z
411}
412
413// SetBytes14 sets z from a 14-byte big-endian slice and returns z.
414// Panics if input is shorter than 14 bytes.
415//
416// Parameters:
417//   - in: the byte sequence whose first 14 bytes are decoded in big-endian order; it must contain at least 14 bytes
418//
419// Returns:
420//   - result: z after decoding the first 14 bytes of in in big-endian order
421func (z *Uint) SetBytes14(in []byte) *Uint {
422	_ = in[13] // bounds check hint to compiler; see golang.org/issue/14808
423	z[3], z[2] = 0, 0
424	z[1] = bigEndianUint48(in[0:6])
425	z[0] = binary.BigEndian.Uint64(in[6:14])
426	return z
427}
428
429// SetBytes15 sets z from a 15-byte big-endian slice and returns z.
430// Panics if input is shorter than 15 bytes.
431//
432// Parameters:
433//   - in: the byte sequence whose first 15 bytes are decoded in big-endian order; it must contain at least 15 bytes
434//
435// Returns:
436//   - result: z after decoding the first 15 bytes of in in big-endian order
437func (z *Uint) SetBytes15(in []byte) *Uint {
438	_ = in[14] // bounds check hint to compiler; see golang.org/issue/14808
439	z[3], z[2] = 0, 0
440	z[1] = bigEndianUint56(in[0:7])
441	z[0] = binary.BigEndian.Uint64(in[7:15])
442	return z
443}
444
445// SetBytes16 sets z from a 16-byte big-endian slice and returns z.
446// Panics if input is shorter than 16 bytes.
447//
448// Parameters:
449//   - in: the byte sequence whose first 16 bytes are decoded in big-endian order; it must contain at least 16 bytes
450//
451// Returns:
452//   - result: z after decoding the first 16 bytes of in in big-endian order
453func (z *Uint) SetBytes16(in []byte) *Uint {
454	_ = in[15] // bounds check hint to compiler; see golang.org/issue/14808
455	z[3], z[2] = 0, 0
456	z[1] = binary.BigEndian.Uint64(in[0:8])
457	z[0] = binary.BigEndian.Uint64(in[8:16])
458	return z
459}
460
461// SetBytes17 sets z from a 17-byte big-endian slice and returns z.
462// Panics if input is shorter than 17 bytes.
463//
464// Parameters:
465//   - in: the byte sequence whose first 17 bytes are decoded in big-endian order; it must contain at least 17 bytes
466//
467// Returns:
468//   - result: z after decoding the first 17 bytes of in in big-endian order
469func (z *Uint) SetBytes17(in []byte) *Uint {
470	_ = in[16] // bounds check hint to compiler; see golang.org/issue/14808
471	z[3] = 0
472	z[2] = uint64(in[0])
473	z[1] = binary.BigEndian.Uint64(in[1:9])
474	z[0] = binary.BigEndian.Uint64(in[9:17])
475	return z
476}
477
478// SetBytes18 sets z from an 18-byte big-endian slice and returns z.
479// Panics if input is shorter than 18 bytes.
480//
481// Parameters:
482//   - in: the byte sequence whose first 18 bytes are decoded in big-endian order; it must contain at least 18 bytes
483//
484// Returns:
485//   - result: z after decoding the first 18 bytes of in in big-endian order
486func (z *Uint) SetBytes18(in []byte) *Uint {
487	_ = in[17] // bounds check hint to compiler; see golang.org/issue/14808
488	z[3] = 0
489	z[2] = uint64(binary.BigEndian.Uint16(in[0:2]))
490	z[1] = binary.BigEndian.Uint64(in[2:10])
491	z[0] = binary.BigEndian.Uint64(in[10:18])
492	return z
493}
494
495// SetBytes19 sets z from a 19-byte big-endian slice and returns z.
496// Panics if input is shorter than 19 bytes.
497//
498// Parameters:
499//   - in: the byte sequence whose first 19 bytes are decoded in big-endian order; it must contain at least 19 bytes
500//
501// Returns:
502//   - result: z after decoding the first 19 bytes of in in big-endian order
503func (z *Uint) SetBytes19(in []byte) *Uint {
504	_ = in[18] // bounds check hint to compiler; see golang.org/issue/14808
505	z[3] = 0
506	z[2] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16
507	z[1] = binary.BigEndian.Uint64(in[3:11])
508	z[0] = binary.BigEndian.Uint64(in[11:19])
509	return z
510}
511
512// SetBytes20 sets z from a 20-byte big-endian slice and returns z.
513// Panics if input is shorter than 20 bytes.
514//
515// Parameters:
516//   - in: the byte sequence whose first 20 bytes are decoded in big-endian order; it must contain at least 20 bytes
517//
518// Returns:
519//   - result: z after decoding the first 20 bytes of in in big-endian order
520func (z *Uint) SetBytes20(in []byte) *Uint {
521	_ = in[19] // bounds check hint to compiler; see golang.org/issue/14808
522	z[3] = 0
523	z[2] = uint64(binary.BigEndian.Uint32(in[0:4]))
524	z[1] = binary.BigEndian.Uint64(in[4:12])
525	z[0] = binary.BigEndian.Uint64(in[12:20])
526	return z
527}
528
529// SetBytes21 sets z from a 21-byte big-endian slice and returns z.
530// Panics if input is shorter than 21 bytes.
531//
532// Parameters:
533//   - in: the byte sequence whose first 21 bytes are decoded in big-endian order; it must contain at least 21 bytes
534//
535// Returns:
536//   - result: z after decoding the first 21 bytes of in in big-endian order
537func (z *Uint) SetBytes21(in []byte) *Uint {
538	_ = in[20] // bounds check hint to compiler; see golang.org/issue/14808
539	z[3] = 0
540	z[2] = bigEndianUint40(in[0:5])
541	z[1] = binary.BigEndian.Uint64(in[5:13])
542	z[0] = binary.BigEndian.Uint64(in[13:21])
543	return z
544}
545
546// SetBytes22 sets z from a 22-byte big-endian slice and returns z.
547// Panics if input is shorter than 22 bytes.
548//
549// Parameters:
550//   - in: the byte sequence whose first 22 bytes are decoded in big-endian order; it must contain at least 22 bytes
551//
552// Returns:
553//   - result: z after decoding the first 22 bytes of in in big-endian order
554func (z *Uint) SetBytes22(in []byte) *Uint {
555	_ = in[21] // bounds check hint to compiler; see golang.org/issue/14808
556	z[3] = 0
557	z[2] = bigEndianUint48(in[0:6])
558	z[1] = binary.BigEndian.Uint64(in[6:14])
559	z[0] = binary.BigEndian.Uint64(in[14:22])
560	return z
561}
562
563// SetBytes23 sets z from a 23-byte big-endian slice and returns z.
564// Panics if input is shorter than 23 bytes.
565//
566// Parameters:
567//   - in: the byte sequence whose first 23 bytes are decoded in big-endian order; it must contain at least 23 bytes
568//
569// Returns:
570//   - result: z after decoding the first 23 bytes of in in big-endian order
571func (z *Uint) SetBytes23(in []byte) *Uint {
572	_ = in[22] // bounds check hint to compiler; see golang.org/issue/14808
573	z[3] = 0
574	z[2] = bigEndianUint56(in[0:7])
575	z[1] = binary.BigEndian.Uint64(in[7:15])
576	z[0] = binary.BigEndian.Uint64(in[15:23])
577	return z
578}
579
580// SetBytes24 sets z from a 24-byte big-endian slice and returns z.
581// Panics if input is shorter than 24 bytes.
582//
583// Parameters:
584//   - in: the byte sequence whose first 24 bytes are decoded in big-endian order; it must contain at least 24 bytes
585//
586// Returns:
587//   - result: z after decoding the first 24 bytes of in in big-endian order
588func (z *Uint) SetBytes24(in []byte) *Uint {
589	_ = in[23] // bounds check hint to compiler; see golang.org/issue/14808
590	z[3] = 0
591	z[2] = binary.BigEndian.Uint64(in[0:8])
592	z[1] = binary.BigEndian.Uint64(in[8:16])
593	z[0] = binary.BigEndian.Uint64(in[16:24])
594	return z
595}
596
597// SetBytes25 sets z from a 25-byte big-endian slice and returns z.
598// Panics if input is shorter than 25 bytes.
599//
600// Parameters:
601//   - in: the byte sequence whose first 25 bytes are decoded in big-endian order; it must contain at least 25 bytes
602//
603// Returns:
604//   - result: z after decoding the first 25 bytes of in in big-endian order
605func (z *Uint) SetBytes25(in []byte) *Uint {
606	_ = in[24] // bounds check hint to compiler; see golang.org/issue/14808
607	z[3] = uint64(in[0])
608	z[2] = binary.BigEndian.Uint64(in[1:9])
609	z[1] = binary.BigEndian.Uint64(in[9:17])
610	z[0] = binary.BigEndian.Uint64(in[17:25])
611	return z
612}
613
614// SetBytes26 sets z from a 26-byte big-endian slice and returns z.
615// Panics if input is shorter than 26 bytes.
616//
617// Parameters:
618//   - in: the byte sequence whose first 26 bytes are decoded in big-endian order; it must contain at least 26 bytes
619//
620// Returns:
621//   - result: z after decoding the first 26 bytes of in in big-endian order
622func (z *Uint) SetBytes26(in []byte) *Uint {
623	_ = in[25] // bounds check hint to compiler; see golang.org/issue/14808
624	z[3] = uint64(binary.BigEndian.Uint16(in[0:2]))
625	z[2] = binary.BigEndian.Uint64(in[2:10])
626	z[1] = binary.BigEndian.Uint64(in[10:18])
627	z[0] = binary.BigEndian.Uint64(in[18:26])
628	return z
629}
630
631// SetBytes27 sets z from a 27-byte big-endian slice and returns z.
632// Panics if input is shorter than 27 bytes.
633//
634// Parameters:
635//   - in: the byte sequence whose first 27 bytes are decoded in big-endian order; it must contain at least 27 bytes
636//
637// Returns:
638//   - result: z after decoding the first 27 bytes of in in big-endian order
639func (z *Uint) SetBytes27(in []byte) *Uint {
640	_ = in[26] // bounds check hint to compiler; see golang.org/issue/14808
641	z[3] = uint64(binary.BigEndian.Uint16(in[1:3])) | uint64(in[0])<<16
642	z[2] = binary.BigEndian.Uint64(in[3:11])
643	z[1] = binary.BigEndian.Uint64(in[11:19])
644	z[0] = binary.BigEndian.Uint64(in[19:27])
645	return z
646}
647
648// SetBytes28 sets z from a 28-byte big-endian slice and returns z.
649// Panics if input is shorter than 28 bytes.
650//
651// Parameters:
652//   - in: the byte sequence whose first 28 bytes are decoded in big-endian order; it must contain at least 28 bytes
653//
654// Returns:
655//   - result: z after decoding the first 28 bytes of in in big-endian order
656func (z *Uint) SetBytes28(in []byte) *Uint {
657	_ = in[27] // bounds check hint to compiler; see golang.org/issue/14808
658	z[3] = uint64(binary.BigEndian.Uint32(in[0:4]))
659	z[2] = binary.BigEndian.Uint64(in[4:12])
660	z[1] = binary.BigEndian.Uint64(in[12:20])
661	z[0] = binary.BigEndian.Uint64(in[20:28])
662	return z
663}
664
665// SetBytes29 sets z from a 29-byte big-endian slice and returns z.
666// Panics if input is shorter than 29 bytes.
667//
668// Parameters:
669//   - in: the byte sequence whose first 29 bytes are decoded in big-endian order; it must contain at least 29 bytes
670//
671// Returns:
672//   - result: z after decoding the first 29 bytes of in in big-endian order
673func (z *Uint) SetBytes29(in []byte) *Uint {
674	_ = in[28] // bounds check hint to compiler; see golang.org/issue/14808
675	z[3] = bigEndianUint40(in[0:5])
676	z[2] = binary.BigEndian.Uint64(in[5:13])
677	z[1] = binary.BigEndian.Uint64(in[13:21])
678	z[0] = binary.BigEndian.Uint64(in[21:29])
679	return z
680}
681
682// SetBytes30 sets z from a 30-byte big-endian slice and returns z.
683// Panics if input is shorter than 30 bytes.
684//
685// Parameters:
686//   - in: the byte sequence whose first 30 bytes are decoded in big-endian order; it must contain at least 30 bytes
687//
688// Returns:
689//   - result: z after decoding the first 30 bytes of in in big-endian order
690func (z *Uint) SetBytes30(in []byte) *Uint {
691	_ = in[29] // bounds check hint to compiler; see golang.org/issue/14808
692	z[3] = bigEndianUint48(in[0:6])
693	z[2] = binary.BigEndian.Uint64(in[6:14])
694	z[1] = binary.BigEndian.Uint64(in[14:22])
695	z[0] = binary.BigEndian.Uint64(in[22:30])
696	return z
697}
698
699// SetBytes31 sets z from a 31-byte big-endian slice and returns z.
700// Panics if input is shorter than 31 bytes.
701//
702// Parameters:
703//   - in: the byte sequence whose first 31 bytes are decoded in big-endian order; it must contain at least 31 bytes
704//
705// Returns:
706//   - result: z after decoding the first 31 bytes of in in big-endian order
707func (z *Uint) SetBytes31(in []byte) *Uint {
708	_ = in[30] // bounds check hint to compiler; see golang.org/issue/14808
709	z[3] = bigEndianUint56(in[0:7])
710	z[2] = binary.BigEndian.Uint64(in[7:15])
711	z[1] = binary.BigEndian.Uint64(in[15:23])
712	z[0] = binary.BigEndian.Uint64(in[23:31])
713	return z
714}
715
716// SetBytes32 sets z from a 32-byte big-endian slice and returns z.
717// Panics if input is shorter than 32 bytes.
718//
719// Parameters:
720//   - in: the byte sequence whose first 32 bytes are decoded in big-endian order; it must contain at least 32 bytes
721//
722// Returns:
723//   - result: z after decoding the first 32 bytes of in in big-endian order
724func (z *Uint) SetBytes32(in []byte) *Uint {
725	_ = in[31] // bounds check hint to compiler; see golang.org/issue/14808
726	z[3] = binary.BigEndian.Uint64(in[0:8])
727	z[2] = binary.BigEndian.Uint64(in[8:16])
728	z[1] = binary.BigEndian.Uint64(in[16:24])
729	z[0] = binary.BigEndian.Uint64(in[24:32])
730	return z
731}
732
733// Utility methods that are "missing" among the bigEndian.UintXX methods.
734
735// bigEndianUint40 returns the uint64 value represented by the 5 bytes in big-endian order.
736func bigEndianUint40(b []byte) uint64 {
737	_ = b[4] // bounds check hint to compiler; see golang.org/issue/14808
738	return uint64(b[4]) | uint64(b[3])<<8 | uint64(b[2])<<16 | uint64(b[1])<<24 |
739		uint64(b[0])<<32
740}
741
742// bigEndianUint56 returns the uint64 value represented by the 7 bytes in big-endian order.
743func bigEndianUint56(b []byte) uint64 {
744	_ = b[6] // bounds check hint to compiler; see golang.org/issue/14808
745	return uint64(b[6]) | uint64(b[5])<<8 | uint64(b[4])<<16 | uint64(b[3])<<24 |
746		uint64(b[2])<<32 | uint64(b[1])<<40 | uint64(b[0])<<48
747}
748
749// bigEndianUint48 returns the uint64 value represented by the 6 bytes in big-endian order.
750func bigEndianUint48(b []byte) uint64 {
751	_ = b[5] // bounds check hint to compiler; see golang.org/issue/14808
752	return uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 | uint64(b[2])<<24 |
753		uint64(b[1])<<32 | uint64(b[0])<<40
754}