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.

136 lines
2.5 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 bytesToBits(b []byte) []bool {
  55. var bits []bool
  56. for i := 0; i < len(b); i++ {
  57. for j := 0; j < 8; j++ {
  58. bits = append(bits, b[i]&(1<<j) > 0)
  59. }
  60. }
  61. return bits
  62. }
  63. func bitsToBytes(bits []bool) []byte {
  64. bytesLen := int(math.Ceil(float64(len(bits)) / 8))
  65. b := make([]byte, bytesLen)
  66. for i := 0; i < len(bits); i++ {
  67. if bits[i] {
  68. b[i/8] |= 1 << (i % 8)
  69. }
  70. }
  71. return b
  72. }
  73. func leftShift(a []bool, n int) []bool {
  74. c := make([]bool, len(a))
  75. copy(c[n:], a[:])
  76. // c[0] = a[0]
  77. // for i := 1; i < len(a); i++ {
  78. // if i < n {
  79. // c[i] = a[i]
  80. // } else {
  81. // c[i] = a[i-1]
  82. // }
  83. // }
  84. return c
  85. }
  86. func rightShift(a []bool, n int) []bool {
  87. c := make([]bool, len(a))
  88. copy(c[:], a[n:])
  89. // for i := len(a) - 1 - n; i >= 0; i-- {
  90. // c[i] = a[i+1]
  91. // }
  92. return c
  93. }
  94. // TODO add unit tests
  95. func xorSingle(a []bool) []bool {
  96. c := make([]bool, len(a))
  97. for i := 0; i < len(a); i++ {
  98. c[i] = !a[i]
  99. }
  100. return c
  101. }
  102. func xor(a, b []bool) []bool {
  103. c := make([]bool, len(a))
  104. for i := 0; i < len(a); i++ {
  105. if a[i] != b[i] { // XOR
  106. c[i] = true
  107. }
  108. }
  109. return c
  110. }
  111. func or(a, b []bool) []bool {
  112. c := make([]bool, len(a))
  113. for i := 0; i < len(a); i++ {
  114. if a[i] == false && b[i] == false { // OR
  115. c[i] = false
  116. } else {
  117. c[i] = true
  118. }
  119. }
  120. return c
  121. }
  122. func and(a, b []bool) []bool {
  123. c := make([]bool, len(a))
  124. for i := 0; i < len(a); i++ {
  125. if a[i] == true && b[i] == true {
  126. c[i] = true
  127. }
  128. }
  129. return c
  130. }