Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

53 rader
1.4 KiB

  1. // +build !amd64 noasm
  2. package sidh
  3. var three238m1 = []uint8{
  4. 0xf8, 0x84, 0x83, 0x82, 0x8a, 0x71, 0xcd, 0xed,
  5. 0x14, 0x7a, 0x42, 0xd4, 0xbf, 0x35, 0x3b, 0x73,
  6. 0x38, 0xcf, 0xd7, 0x94, 0xcf, 0x29, 0x82, 0xf8,
  7. 0xd6, 0x2a, 0x7c, 0x0c, 0x99, 0x6c, 0xc5, 0x63,
  8. 0xc7, 0x22, 0x42, 0x8f, 0x7e, 0xa8, 0x58, 0xb8,
  9. 0xf5, 0xea, 0x25, 0xb5, 0xc6, 0xc9, 0x54, 0x02}
  10. func addc8(cin, a, b uint8) (ret, cout uint8) {
  11. t := a + cin
  12. ret = b + t
  13. cout = ((a & b) | ((a | b) & (^ret))) >> 7
  14. return
  15. }
  16. func subc8(bIn, a, b uint8) (ret, bOut uint8) {
  17. var tmp1 = a - b
  18. ret = tmp1 - bIn
  19. // Set bOut if bIn!=0 and tmp1==0 in constant time
  20. bOut = bIn & (1 ^ ((tmp1 | uint8(0-tmp1)) >> 7))
  21. // Constant time check if a<b
  22. bOut |= (a ^ ((a ^ b) | (uint8(a-b) ^ b))) >> 7
  23. return
  24. }
  25. // Set result to zero if the input scalar is <= 3^238, otherwise result is 1.
  26. // Scalar must be array of 48 bytes. This function is specific to P751.
  27. func checkLessThanThree238(scalar []byte) uint8 {
  28. var borrow uint8
  29. for i := 0; i < len(three238m1); i++ {
  30. _, borrow = subc8(borrow, three238m1[i], scalar[i])
  31. }
  32. return borrow
  33. }
  34. // Multiply 48-byte scalar by 3 to get a scalar in 3*[0,3^238). This
  35. // function is specific to P751.
  36. func multiplyByThree(scalar []byte) {
  37. var carry uint8
  38. var dbl [48]uint8
  39. for i := 0; i < len(scalar); i++ {
  40. dbl[i], carry = addc8(carry, scalar[i], scalar[i])
  41. }
  42. for i := 0; i < len(scalar); i++ {
  43. scalar[i], carry = addc8(carry, dbl[i], scalar[i])
  44. }
  45. }