Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GLVTypeBEndomorphism.java « endo « ec « math « bouncycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 45b21722dd85b200287310e2f677fe1033d7600a (plain)
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
package org.bouncycastle.math.ec.endo;

import java.math.BigInteger;

import org.bouncycastle.math.ec.ECConstants;
import org.bouncycastle.math.ec.ECPointMap;
import org.bouncycastle.math.ec.GLVEndomorphism;
import org.bouncycastle.math.ec.ScaleXPointMap;

public class GLVTypeBEndomorphism implements GLVEndomorphism
{
    private final GLVTypeBParameters parameters;
    private final ECPointMap pointMap;

    public GLVTypeBEndomorphism(GLVTypeBParameters parameters)
    {
        this.parameters = parameters;
        this.pointMap = new ScaleXPointMap(parameters.getBeta());
    }

    public BigInteger[] decomposeScalar(BigInteger k)
    {
        int bits = parameters.getBits();
        BigInteger b1 = calculateB(k, parameters.getG1(), bits);
        BigInteger b2 = calculateB(k, parameters.getG2(), bits);

        BigInteger[] v1 = parameters.getV1(), v2 = parameters.getV2();
        BigInteger a = k.subtract((b1.multiply(v1[0])).add(b2.multiply(v2[0])));
        BigInteger b = (b1.multiply(v1[1])).add(b2.multiply(v2[1])).negate();

        return new BigInteger[]{ a, b };
    }

    public ECPointMap getPointMap()
    {
        return pointMap;
    }

    public boolean hasEfficientPointMap()
    {
        return true;
    }

    protected BigInteger calculateB(BigInteger k, BigInteger g, int t)
    {
        boolean negative = (g.signum() < 0);
        BigInteger b = k.multiply(g.abs());
        boolean extra = b.testBit(t - 1);
        b = b.shiftRight(t);
        if (extra)
        {
            b = b.add(ECConstants.ONE);
        }
        return negative ? b.negate() : b;
    }
}