MathUtils { public static BigInteger sum(List<BigInteger> summands, BigInteger modulus) { return summands.stream() .reduce(BigInteger::add) .orElse(BigInteger.ZERO) .mod(modulus); } private  MathUtils(); static boolean isQuadraticResidue(BigInteger value, BigInteger modulus); static BigInteger modularSqrt(BigInteger value, BigInteger modulus); static BigInteger sum(List<BigInteger> summands, BigInteger modulus);  }
@Test public void testSum() { List<BigInteger> summands = Arrays.asList( BigInteger.ONE, BigInteger.ZERO, modulus.subtract(BigInteger.TEN).mod(modulus), new BigInteger("42").mod(modulus) ); BigInteger expected = BigInteger.ONE.add(BigInteger.ZERO) .add(modulus.subtract(BigInteger.TEN).mod(modulus)).add(new BigInteger("42").mod(modulus)) .mod(modulus); assertEquals(expected, MathUtils.sum(summands, modulus)); }