You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
2.6 KiB

  1. package keccak
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math"
  6. )
  7. func bytesToU64(v []byte) uint64 {
  8. return uint64(v[0]) |
  9. uint64(v[1])<<8 |
  10. uint64(v[2])<<16 |
  11. uint64(v[3])<<24 |
  12. uint64(v[4])<<32 |
  13. uint64(v[5])<<40 |
  14. uint64(v[6])<<48 |
  15. uint64(v[7])<<56
  16. }
  17. func u64ToBytes(x uint64) []byte {
  18. v := make([]byte, 8)
  19. v[0] = byte(x)
  20. v[1] = byte(x >> 8)
  21. v[2] = byte(x >> 16)
  22. v[3] = byte(x >> 24)
  23. v[4] = byte(x >> 32)
  24. v[5] = byte(x >> 40)
  25. v[6] = byte(x >> 48)
  26. v[7] = byte(x >> 56)
  27. return v
  28. }
  29. func bitsToU64Array(b []bool) []uint64 {
  30. r := make([]uint64, len(b)/64)
  31. for i := 0; i < len(b)/64; i++ {
  32. r[i] = bitsToU64(b[i*64 : i*64+64])
  33. }
  34. return r
  35. }
  36. func u64ArrayToBits(u []uint64) []bool {
  37. r := make([]bool, len(u)*64)
  38. for i := 0; i < len(u); i++ {
  39. copy(r[i*64:i*64+64], u64ToBits(u[i]))
  40. }
  41. return r
  42. }
  43. func bitsToU64(b []bool) uint64 {
  44. if len(b) != 64 {
  45. panic(fmt.Errorf("len(b)=%d, max=64", len(b)))
  46. }
  47. by := bitsToBytes(b)
  48. return binary.LittleEndian.Uint64(by)
  49. }
  50. func u64ToBits(u uint64) []bool {
  51. by := u64ToBytes(u)
  52. return bytesToBits(by)
  53. }
  54. func byteToBits(b byte) []bool {
  55. var bits []bool
  56. for j := 0; j < 8; j++ {
  57. bits = append(bits, b&(1<<j) > 0)
  58. }
  59. return bits
  60. }
  61. func bytesToBits(b []byte) []bool {
  62. var bits []bool
  63. for i := 0; i < len(b); i++ {
  64. for j := 0; j < 8; j++ {
  65. bits = append(bits, b[i]&(1<<j) > 0)
  66. }
  67. }
  68. return bits
  69. }
  70. func bitsToBytes(bits []bool) []byte {
  71. bytesLen := int(math.Ceil(float64(len(bits)) / 8))
  72. b := make([]byte, bytesLen)
  73. for i := 0; i < len(bits); i++ {
  74. if bits[i] {
  75. b[i/8] |= 1 << (i % 8)
  76. }
  77. }
  78. return b
  79. }
  80. func leftShift(a []bool, n int) []bool {
  81. c := make([]bool, len(a))
  82. copy(c[n:], a[:])
  83. // c[0] = a[0]
  84. // for i := 1; i < len(a); i++ {
  85. // if i < n {
  86. // c[i] = a[i]
  87. // } else {
  88. // c[i] = a[i-1]
  89. // }
  90. // }
  91. return c
  92. }
  93. func rightShift(a []bool, n int) []bool {
  94. c := make([]bool, len(a))
  95. copy(c[:], a[n:])
  96. // for i := len(a) - 1 - n; i >= 0; i-- {
  97. // c[i] = a[i+1]
  98. // }
  99. return c
  100. }
  101. // TODO add unit tests
  102. func xorSingle(a []bool) []bool {
  103. c := make([]bool, len(a))
  104. for i := 0; i < len(a); i++ {
  105. c[i] = !a[i]
  106. }
  107. return c
  108. }
  109. func xor(a, b []bool) []bool {
  110. c := make([]bool, len(a))
  111. for i := 0; i < len(a); i++ {
  112. if a[i] != b[i] { // XOR
  113. c[i] = true
  114. }
  115. }
  116. return c
  117. }
  118. func or(a, b []bool) []bool {
  119. c := make([]bool, len(a))
  120. for i := 0; i < len(a); i++ {
  121. if a[i] == false && b[i] == false { // OR
  122. c[i] = false
  123. } else {
  124. c[i] = true
  125. }
  126. }
  127. return c
  128. }
  129. func and(a, b []bool) []bool {
  130. c := make([]bool, len(a))
  131. for i := 0; i < len(a); i++ {
  132. if a[i] == true && b[i] == true {
  133. c[i] = true
  134. }
  135. }
  136. return c
  137. }