summaryrefslogtreecommitdiff
path: root/bcprov/src/main/java/org/bouncycastle/asn1/x9/X9ECPoint.java
blob: 95fdc672a88d7b593a6f1bbfcc2966eb3ce36a76 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.bouncycastle.asn1.x9;

import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.Arrays;

/**
 * class for describing an ECPoint as a DER object.
 */
public class X9ECPoint
    extends ASN1Object
{
    private final ASN1OctetString encoding;

    private ECCurve c;
    private ECPoint p;

    public X9ECPoint(
        ECPoint p)
    {
        this(p, false);
    }

    public X9ECPoint(
        ECPoint p,
        boolean compressed)
    {
        this.p = p.normalize();
        this.encoding = new DEROctetString(p.getEncoded(compressed));
    }

    public X9ECPoint(
        ECCurve          c,
        byte[]           encoding)
    {
        this.c = c;
        this.encoding = new DEROctetString(Arrays.clone(encoding));
    }

    public X9ECPoint(
        ECCurve          c,
        ASN1OctetString  s)
    {
        this(c, s.getOctets());
    }

    public byte[] getPointEncoding()
    {
        return Arrays.clone(encoding.getOctets());
    }

    public synchronized ECPoint getPoint()
    {
        if (p == null)
        {
            p = c.decodePoint(encoding.getOctets()).normalize();
        }

        return p;
    }

    public boolean isPointCompressed()
    {
        byte[] octets = encoding.getOctets();
        return octets != null && octets.length > 0 && (octets[0] == 2 || octets[0] == 3);
    }

    /**
     * Produce an object suitable for an ASN1OutputStream.
     * <pre>
     *  ECPoint ::= OCTET STRING
     * </pre>
     * <p>
     * Octet string produced using ECPoint.getEncoded().
     */
    public ASN1Primitive toASN1Primitive()
    {
        return encoding;
    }
}