-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericHashToField.java
More file actions
71 lines (63 loc) · 2.56 KB
/
GenericHashToField.java
File metadata and controls
71 lines (63 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2
package se.digg.crypto.hashtocurve.impl;
import java.math.BigInteger;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.util.Arrays;
import se.digg.crypto.hashtocurve.H2cUtils;
import se.digg.crypto.hashtocurve.HashToField;
import se.digg.crypto.hashtocurve.MessageExpansion;
/**
* Generic implementation of hash to field.
*/
@SuppressWarnings("checkstyle:MemberName")
public class GenericHashToField implements HashToField {
protected final byte[] dst;
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",
justification = "ecParameterSpec is used internally by subclasses")
protected final ECParameterSpec ecParameterSpec;
protected final MessageExpansion messageExpansion;
/** Security parameter for the suite. */
protected int L;
protected int m;
protected BigInteger p;
protected final int count;
@SuppressWarnings("checkstyle:ParameterName")
public GenericHashToField(final byte[] dst, final ECParameterSpec ecParameterSpec,
final MessageExpansion messageExpansion, final int L) {
this(dst, ecParameterSpec, messageExpansion, L, 2);
}
@SuppressWarnings("checkstyle:ParameterName")
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value = "EI_EXPOSE_REP2",
justification = "dst byte array is intentionally stored for internal use")
public GenericHashToField(final byte[] dst, final ECParameterSpec ecParameterSpec,
final MessageExpansion messageExpansion, final int L,
final int count) {
this.dst = dst;
this.ecParameterSpec = ecParameterSpec;
this.count = count;
this.L = L;
this.messageExpansion = messageExpansion;
this.p = ecParameterSpec.getCurve().getField().getCharacteristic();
this.m = ecParameterSpec.getCurve().getField().getDimension();
}
@Override
public BigInteger[][] process(final byte[] message) {
final int byteLen = this.count * this.m * this.L;
final byte[] uniformBytes = this.messageExpansion.expandMessage(message, this.dst, byteLen);
final BigInteger[][] u = new BigInteger[this.count][this.m];
for (int i = 0; i < this.count; i++) {
final BigInteger[] e = new BigInteger[this.m];
for (int j = 0; j < this.m; j++) {
final int elmOffset = this.L * (j + i * this.m);
final byte[] tv = Arrays.copyOfRange(uniformBytes, elmOffset, elmOffset + this.L);
e[j] = H2cUtils.os2ip(tv).mod(this.p);
}
u[i] = e;
}
return u;
}
}