Skip to content

Commit

Permalink
Refactor and reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-lj committed Nov 10, 2024
1 parent 92498a3 commit 82d552f
Show file tree
Hide file tree
Showing 28 changed files with 132 additions and 105 deletions.
52 changes: 27 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,30 @@ concrete implementations of a vector space over a field.
```java
public class GramSchmidt<V, S, F extends Field<S>> implements Function<List<V>, List<V>> {

private final VectorSpace<V, S, F> vectorSpace;
private final BiFunction<V, V, S> innerProduct;

public GramSchmidt(VectorSpace<V, S, F> V,
BiFunction<V, V, S> innerProduct) {
this.vectorSpace = V;
this.innerProduct = innerProduct;
}

@Override
public List<V> apply(List<V> vectors) {
List<V> U = new ArrayList<>();

Projection<V, S, F> proj = new Projection<>(vectorSpace, innerProduct);

for (V v : vectors) {
for (V u : U) {
V p = proj.apply(v, u);
v = vectorSpace.subtract(v, p);
}
U.add(v);
private final VectorSpace<V, S, F> vectorSpace;
private final BiFunction<V, V, S> innerProduct;

public GramSchmidt(VectorSpace<V, S, F> V,
BiFunction<V, V, S> innerProduct) {
this.vectorSpace = V;
this.innerProduct = innerProduct;
}

@Override
public List<V> apply(List<V> vectors) {
List<V> U = new ArrayList<>();

Projection<V, S, F> proj = new Projection<>(vectorSpace, innerProduct);

for (V v : vectors) {
for (V u : U) {
V p = proj.apply(v, u);
v = vectorSpace.subtract(v, p);
}
U.add(v);
}
return U;
}
return U;
}

}
```
Expand All @@ -133,9 +133,11 @@ There are a few demo applications showing some capabilities of the library in th

These include an implementation of
the [AKS primality testing algorithm](https://en.wikipedia.org/wiki/AKS_primality_test),
computing the [optimal Ate pairing over the BLS12-381 elliptic construction](https://hackmd.io/@benjaminion/bls12-381), a
computing the [optimal Ate pairing over the BLS12-381 elliptic construction](https://hackmd.io/@benjaminion/bls12-381),
a
demonstration of arbitrary precision arithmetic with real numbers inspired
by [the work of Hans-J Boehm](https://www.hboehm.info/new_crcalc/CRCalc.html) and an implementation of the [Poseidon hash function over BN254](https://www.poseidon-hash.info/).
by [the work of Hans-J Boehm](https://www.hboehm.info/new_crcalc/CRCalc.html) and an implementation of
the [Poseidon hash function over BN254](https://www.poseidon-hash.info/).

<!-- CONTRIBUTING -->

Expand Down
29 changes: 29 additions & 0 deletions class-group/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>dk.jonaslindstrom.ruffini</groupId>
<version>0.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>class-group</artifactId>

<dependencies>
<dependency>
<groupId>dk.jonaslindstrom.ruffini</groupId>
<artifactId>common</artifactId>
<version>0.4-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dk.jonaslindstrom.ruffini</groupId>
<artifactId>integers</artifactId>
<version>0.4-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package dk.jonaslindstrom.ruffini.finitefields.quadraticform;
package dk.jonaslindstrom.ruffini.quadraticform;

import dk.jonaslindstrom.ruffini.common.abstractions.Group;
import dk.jonaslindstrom.ruffini.common.algorithms.BigLegendreSymbol;
import dk.jonaslindstrom.ruffini.common.algorithms.ChineseRemainderTheorem;
import dk.jonaslindstrom.ruffini.common.util.SamplingUtils;
import dk.jonaslindstrom.ruffini.common.vector.Vector;
import dk.jonaslindstrom.ruffini.integers.algorithms.ModularSquareRoot;
import dk.jonaslindstrom.ruffini.integers.structures.BigIntegers;

Expand Down Expand Up @@ -35,6 +32,11 @@ public ClassGroup(BigInteger discriminant) {
k.multiply(k).subtract(discriminant).divide(BigInteger.valueOf(4)));
}

private static int legendre(BigInteger a, BigInteger p) {
BigInteger l = a.modPow(p.subtract(BigInteger.ONE).divide(BigInteger.TWO), p);
return l.equals(BigInteger.ONE) ? 1 : -1;
}

@Override
public QuadraticForm<BigInteger, BigIntegers> invert(QuadraticForm<BigInteger, BigIntegers> a) {
if (!a.discriminant().equals(discriminant)) {
Expand Down Expand Up @@ -107,9 +109,4 @@ public QuadraticForm<BigInteger, BigIntegers> sample(Random random) {
System.out.println("Discriminant: " + result.discriminant().equals(discriminant));
return result.reduce();
}

private static int legendre(BigInteger a, BigInteger p) {
BigInteger l = a.modPow(p.subtract(BigInteger.ONE).divide(BigInteger.TWO), p);
return l.equals(BigInteger.ONE) ? 1 : -1;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dk.jonaslindstrom.ruffini.finitefields.quadraticform;
package dk.jonaslindstrom.ruffini.quadraticform;

import dk.jonaslindstrom.ruffini.common.abstractions.EuclideanDomain;
import dk.jonaslindstrom.ruffini.common.abstractions.OrderedSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ default E doubling(E e) {
return add(e, e);
}

/** Returns the sum of a list of elements. */
/**
* Returns the sum of a list of elements.
*/
default E sum(List<E> terms) {
return new Sum<>(this).apply(terms);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DFT(Ring<F> ring, F a, int n) {
this.ring = ring;
this.n = n;
Power<F> power = new Power<>(ring);
this.powers = ArrayUtils.populate(n*n, j -> power.apply(a, j));
this.powers = ArrayUtils.populate(n * n, j -> power.apply(a, j));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.IntStream;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ static <F> Matrix<F> column(Vector<F> vector) {
return Matrix.lazy(vector.size(), 1, (i, j) -> vector.get(i));
}

static <E> Matrix<E> diagonal(int n, IntFunction<E> populator, E zero) {
return Matrix.lazy(n, n, (i, j) -> i.equals(j) ? populator.apply(i) : zero);
}

Vector<E> getColumn(int j);

Vector<E> getRow(int i);
Expand Down Expand Up @@ -222,9 +226,5 @@ default Stream<E> stream() {
.flatMap(i -> IntStream.range(0, getWidth()).mapToObj(j -> get(i, j)));
}

static <E> Matrix<E> diagonal(int n, IntFunction<E> populator, E zero) {
return Matrix.lazy(n, n, (i, j) -> i.equals(j) ? populator.apply(i) : zero);
}

String toString(Function<E, String> toString);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dk.jonaslindstrom.ruffini.common.util;

import dk.jonaslindstrom.ruffini.common.algorithms.Power;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package dk.jonaslindstrom.ruffini.common.util;

import org.checkerframework.checker.units.qual.A;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

import static dk.jonaslindstrom.ruffini.common.util.MathUtils.bigLog2;

public class SamplingUtils {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public class TestUtils {
public static class TestField implements Field<Integer> {
private final Integer modulus;

private Integer reduce(int a) {
return Math.floorMod(a, modulus);
}

public TestField(Integer modulus) {
this.modulus = modulus;
}

private Integer reduce(int a) {
return Math.floorMod(a, modulus);
}

@Override
public Integer negate(Integer a) {
return reduce(-a);
Expand Down
2 changes: 1 addition & 1 deletion common/src/test/java/AlgorithmsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void testBinaryGCD() {
for (int i = 0; i < tests; i++) {
BigInteger a = new BigInteger(16, random);
BigInteger b = new BigInteger(16, random);
BigInteger gcd = BinaryGCD.apply(a,b);
BigInteger gcd = BinaryGCD.apply(a, b);
Assert.assertEquals(a.gcd(b), gcd);
}
}
Expand Down
2 changes: 1 addition & 1 deletion demos/src/main/java/demo/ConstructiveRealsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void main(String[] arguments) {
System.out.println("Demo of constructive reals");

// Number of bits of precision
int m = 512;
int m = 1024;

ConstructiveReals = new ConstructiveReals();
DotProduct<ConstructiveReal> dotProduct = new DotProduct<>();
Expand Down
12 changes: 9 additions & 3 deletions demos/src/main/java/demo/PolynomialMultiplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,25 @@ public static void main(String[] arguments) {
PolynomialRing<BigInteger> polynomialRing = new PolynomialRing<>(field);
field.reset();
Polynomial<BigInteger> expected = polynomialRing.multiply(p, q);

System.out.println("Straight-forward multiplication");
System.out.println("===============================");
System.out.println(field);

System.out.println();

field.reset();
Vector<BigInteger> pHatCoefficients = fdft.apply(pCoefficients);
System.out.println(field);
Vector<BigInteger> qHatCoefficients = fdft.apply(qCoefficients);
System.out.println(field);
Vector<BigInteger> sHatCoefficients = pHatCoefficients.coordinateWise(qHatCoefficients, field::multiply);
System.out.println(field);
Vector<BigInteger> sCoefficients = ifdft.apply(sHatCoefficients);
System.out.println("With DFT");
System.out.println("========");
System.out.println(field);

Polynomial<BigInteger> actual = new Polynomial<>(sCoefficients, field);

// Check that the result is the same with and without using DFT
System.out.println(actual.equals(expected));


Expand Down
2 changes: 1 addition & 1 deletion demos/src/main/java/demo/StrassenDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Random;

/**
* Compute the product of two 8x8 matrices using the traditional algorithm and Strassens algorithm
* Compute the product of two 8x8 matrices using the traditional algorithm and Strassen's algorithm
* and compare the number of operations used for each.
*/
public class StrassenDemo {
Expand Down
2 changes: 1 addition & 1 deletion demos/src/main/java/demo/poseidon/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static Matrix<BigInteger> getMatrix(int t) {
try {
List<String> rows = getMatrixStrings(t);
int n = t + 2;
return Matrix.of(n, n, (i,j) -> new BigInteger(rows.get(i * n + j)));
return Matrix.of(n, n, (i, j) -> new BigInteger(rows.get(i * n + j)));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
2 changes: 1 addition & 1 deletion demos/src/main/java/demo/poseidon/Poseidon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dk.jonaslindstrom.ruffini.common.abstractions.Field;
import dk.jonaslindstrom.ruffini.common.algorithms.Power;
import dk.jonaslindstrom.ruffini.common.helpers.PerformanceLoggingField;
import dk.jonaslindstrom.ruffini.common.matrices.elements.Matrix;
import dk.jonaslindstrom.ruffini.common.vector.Vector;
import dk.jonaslindstrom.ruffini.finitefields.BigPrimeField;
Expand Down Expand Up @@ -46,6 +45,7 @@ public static class PoseidonHash<E> {
private final Field<E> field;
private final Power<E> power;
private ArrayList<E> state = new ArrayList<>();

public PoseidonHash(Matrix<E> m,
List<E> c,
int fullRounds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class BN254 {
public static AffinePoint<Polynomial<BigInteger>> G2_GENERATOR = new AffinePoint<>(
Polynomial.of(
new BigInteger("61A10BB519EB62FEB8D8C7E8C61EDB6A4648BBB4898BF0D91EE4224C803FB2B", 16),
new BigInteger("516AAF9BA737833310AA78C5982AA5B1F4D746BAE3784B70D8C34C1E7D54CF3", 16)),
new BigInteger("516AAF9BA737833310AA78C5982AA5B1F4D746BAE3784B70D8C34C1E7D54CF3", 16)),
Polynomial.of(
new BigInteger("21897A06BAF93439A90E096698C822329BD0AE6BDBE09BD19F0E07891CD2B9A", 16),
new BigInteger("EBB2B0E7C8B15268F6D4456F5F38D37B09006FFD739C9578A2D1AEC6B3ACE9B", 16))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public BigElement<E> add(BigElement<E> a, BigElement<E> b) {
carry = qr.first;
limbs.add(qr.second);
}
if (!ring.isZero(ring.zero())) {
limbs.add(carry);
}
if (!ring.isZero(ring.zero())) {
limbs.add(carry);
}
return new BigElement<>(limbs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ public L evaluateFromLeafs(List<L> values, BiFunction<Pair<L, L>, Pair<L, L>, L>
return root.evaluateFromLeafs(0, values, operator);
}

/**
* A binary tree where the leafs are x-l_i for a set of constants (l_0, ..., l_{n-1}) and internal nodes are the product
* of their children.
*/
public static class SubproductTree<E> extends BinaryTree<Polynomial<E>> {

private final PolynomialRingOverRing<E> polynomialRing;

public SubproductTree(List<E> leafs, PolynomialRingOverRing<E> polynomialRing) {
super(leafs.stream().map(leaf -> Polynomial.of(
polynomialRing.getRing().negate(leaf),
polynomialRing.getRing().identity())).collect(Collectors.toList()), polynomialRing::multiply);
this.polynomialRing = polynomialRing;
}

/**
* Evaluate from the root. For each node set the value to be the value from the parent modulo the label on the node.
* Return the values from the leafs.
*/
public List<E> evaluate(Polynomial<E> polynomial) {
return super.evaluate(polynomial, new Remainder<>(polynomialRing)).stream().map(Polynomial::getConstant).toList();
}
}

private class Node {

final Node left, right;
Expand Down Expand Up @@ -83,28 +107,4 @@ private L evaluateFromLeafs(int index, List<L> leafs, BiFunction<Pair<L, L>, Pai
return operator.apply(leftOp, rightOp);
}
}

/**
* A binary tree where the leafs are x-l_i for a set of constants (l_0, ..., l_{n-1}) and internal nodes are the product
* of their children.
*/
public static class SubproductTree<E> extends BinaryTree<Polynomial<E>> {

private final PolynomialRingOverRing<E> polynomialRing;

public SubproductTree(List<E> leafs, PolynomialRingOverRing<E> polynomialRing) {
super(leafs.stream().map(leaf -> Polynomial.of(
polynomialRing.getRing().negate(leaf),
polynomialRing.getRing().identity())).collect(Collectors.toList()), polynomialRing::multiply);
this.polynomialRing = polynomialRing;
}

/**
* Evaluate from the root. For each node set the value to be the value from the parent modulo the label on the node.
* Return the values from the leafs.
*/
public List<E> evaluate(Polynomial<E> polynomial) {
return super.evaluate(polynomial, new Remainder<>(polynomialRing)).stream().map(Polynomial::getConstant).toList();
}
}
}
Loading

0 comments on commit 82d552f

Please sign in to comment.