shared_addition.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. # This module defines addition on SharedScalars, using the SPDZ algorithm
  2. # for addition [1].
  3. #
  4. # Technically, this method is extremely simple as it follows directly
  5. # from additive secret sharing, and likely predates SPDZ.
  6. #
  7. # [1] "Computations" on pg 6 of https://eprint.iacr.org/2011/535.pdf
  8. # Small hack:
  9. #
  10. # We can't import the SharedScalar class in this module as that would
  11. # create a circular dependency.
  12. #
  13. # However, we'd obviously still like to be able to construct new
  14. # SharedScalars here when doing arithmetic. To be able to do so,
  15. # we can use `type(sh)` to get access to the SharedScalar class &
  16. # constructor.
  17. def add_2sh(sh1, sh2):
  18. '''Implements addition on two SharedScalars.'''
  19. # To do the addition, we add each machine's shares together
  20. sh1._assert_can_operate(sh2)
  21. sum_shares = [sh1.share_of[owner] + sh2.share_of[owner]
  22. for owner in sh1.owners]
  23. return type(sh1)(sum_shares, Q=sh1.Q)
  24. def add_sh_pub(sh, pub):
  25. '''Implements addition on a SharedScalar and a public integer.'''
  26. # To do the addition, we add the integer to one (random) share only
  27. new_shares = [sh.shares[0] + pub] + sh.shares[1:]
  28. return type(sh)(new_shares, sh.Q)