Sign.smali

.class public Lorg/web3j/crypto/Sign;
.super Ljava/lang/Object;
.source "Sign.java"


# static fields
.field static final CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

.field private static final CURVE_PARAMS:Lorg/spongycastle/asn1/x9/X9ECParameters;

.field static final HALF_CURVE_ORDER:Ljava/math/BigInteger;


# direct methods
.method static constructor <clinit>()V
    .registers 5

    .prologue
    .line 33
    const-string v0, "secp256k1"

    invoke-static {v0}, Lorg/spongycastle/crypto/ec/CustomNamedCurves;->getByName(Ljava/lang/String;)Lorg/spongycastle/asn1/x9/X9ECParameters;

    move-result-object v0

    sput-object v0, Lorg/web3j/crypto/Sign;->CURVE_PARAMS:Lorg/spongycastle/asn1/x9/X9ECParameters;

    .line 34
    new-instance v0, Lorg/spongycastle/crypto/params/ECDomainParameters;

    sget-object v1, Lorg/web3j/crypto/Sign;->CURVE_PARAMS:Lorg/spongycastle/asn1/x9/X9ECParameters;

    invoke-virtual {v1}, Lorg/spongycastle/asn1/x9/X9ECParameters;->getCurve()Lorg/spongycastle/math/ec/ECCurve;

    move-result-object v1

    sget-object v2, Lorg/web3j/crypto/Sign;->CURVE_PARAMS:Lorg/spongycastle/asn1/x9/X9ECParameters;

    invoke-virtual {v2}, Lorg/spongycastle/asn1/x9/X9ECParameters;->getG()Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v2

    sget-object v3, Lorg/web3j/crypto/Sign;->CURVE_PARAMS:Lorg/spongycastle/asn1/x9/X9ECParameters;

    invoke-virtual {v3}, Lorg/spongycastle/asn1/x9/X9ECParameters;->getN()Ljava/math/BigInteger;

    move-result-object v3

    sget-object v4, Lorg/web3j/crypto/Sign;->CURVE_PARAMS:Lorg/spongycastle/asn1/x9/X9ECParameters;

    invoke-virtual {v4}, Lorg/spongycastle/asn1/x9/X9ECParameters;->getH()Ljava/math/BigInteger;

    move-result-object v4

    invoke-direct {v0, v1, v2, v3, v4}, Lorg/spongycastle/crypto/params/ECDomainParameters;-><init>(Lorg/spongycastle/math/ec/ECCurve;Lorg/spongycastle/math/ec/ECPoint;Ljava/math/BigInteger;Ljava/math/BigInteger;)V

    sput-object v0, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    .line 36
    sget-object v0, Lorg/web3j/crypto/Sign;->CURVE_PARAMS:Lorg/spongycastle/asn1/x9/X9ECParameters;

    invoke-virtual {v0}, Lorg/spongycastle/asn1/x9/X9ECParameters;->getN()Ljava/math/BigInteger;

    move-result-object v0

    const/4 v1, 0x1

    invoke-virtual {v0, v1}, Ljava/math/BigInteger;->shiftRight(I)Ljava/math/BigInteger;

    move-result-object v0

    sput-object v0, Lorg/web3j/crypto/Sign;->HALF_CURVE_ORDER:Ljava/math/BigInteger;

    return-void
.end method

.method public constructor <init>()V
    .registers 1

    .prologue
    .line 31
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    .line 222
    return-void
.end method

.method private static decompressKey(Ljava/math/BigInteger;Z)Lorg/spongycastle/math/ec/ECPoint;
    .registers 5

    .prologue
    .line 150
    new-instance v0, Lorg/spongycastle/asn1/x9/X9IntegerConverter;

    invoke-direct {v0}, Lorg/spongycastle/asn1/x9/X9IntegerConverter;-><init>()V

    .line 151
    sget-object v1, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    invoke-virtual {v1}, Lorg/spongycastle/crypto/params/ECDomainParameters;->getCurve()Lorg/spongycastle/math/ec/ECCurve;

    move-result-object v1

    invoke-virtual {v0, v1}, Lorg/spongycastle/asn1/x9/X9IntegerConverter;->getByteLength(Lorg/spongycastle/math/ec/ECCurve;)I

    move-result v1

    add-int/lit8 v1, v1, 0x1

    invoke-virtual {v0, p0, v1}, Lorg/spongycastle/asn1/x9/X9IntegerConverter;->integerToBytes(Ljava/math/BigInteger;I)[B

    move-result-object v1

    .line 152
    const/4 v2, 0x0

    if-eqz p1, :cond_27

    const/4 v0, 0x3

    :goto_19
    int-to-byte v0, v0

    aput-byte v0, v1, v2

    .line 153
    sget-object v0, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    invoke-virtual {v0}, Lorg/spongycastle/crypto/params/ECDomainParameters;->getCurve()Lorg/spongycastle/math/ec/ECCurve;

    move-result-object v0

    invoke-virtual {v0, v1}, Lorg/spongycastle/math/ec/ECCurve;->decodePoint([B)Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v0

    return-object v0

    .line 152
    :cond_27
    const/4 v0, 0x2

    goto :goto_19
.end method

.method public static publicKeyFromPrivate(Ljava/math/BigInteger;)Ljava/math/BigInteger;
    .registers 5

    .prologue
    const/4 v3, 0x1

    .line 202
    invoke-static {p0}, Lorg/web3j/crypto/Sign;->publicPointFromPrivate(Ljava/math/BigInteger;)Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v0

    .line 204
    const/4 v1, 0x0

    invoke-virtual {v0, v1}, Lorg/spongycastle/math/ec/ECPoint;->getEncoded(Z)[B

    move-result-object v0

    .line 205
    new-instance v1, Ljava/math/BigInteger;

    array-length v2, v0

    invoke-static {v0, v3, v2}, Ljava/util/Arrays;->copyOfRange([BII)[B

    move-result-object v0

    invoke-direct {v1, v3, v0}, Ljava/math/BigInteger;-><init>(I[B)V

    return-object v1
.end method

.method private static publicPointFromPrivate(Ljava/math/BigInteger;)Lorg/spongycastle/math/ec/ECPoint;
    .registers 3

    .prologue
    .line 216
    invoke-virtual {p0}, Ljava/math/BigInteger;->bitLength()I

    move-result v0

    sget-object v1, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    invoke-virtual {v1}, Lorg/spongycastle/crypto/params/ECDomainParameters;->getN()Ljava/math/BigInteger;

    move-result-object v1

    invoke-virtual {v1}, Ljava/math/BigInteger;->bitLength()I

    move-result v1

    if-le v0, v1, :cond_1a

    .line 217
    sget-object v0, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    invoke-virtual {v0}, Lorg/spongycastle/crypto/params/ECDomainParameters;->getN()Ljava/math/BigInteger;

    move-result-object v0

    invoke-virtual {p0, v0}, Ljava/math/BigInteger;->mod(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object p0

    .line 219
    :cond_1a
    new-instance v0, Lorg/spongycastle/math/ec/FixedPointCombMultiplier;

    invoke-direct {v0}, Lorg/spongycastle/math/ec/FixedPointCombMultiplier;-><init>()V

    sget-object v1, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    invoke-virtual {v1}, Lorg/spongycastle/crypto/params/ECDomainParameters;->getG()Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v1

    invoke-virtual {v0, v1, p0}, Lorg/spongycastle/math/ec/FixedPointCombMultiplier;->multiply(Lorg/spongycastle/math/ec/ECPoint;Ljava/math/BigInteger;)Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v0

    return-object v0
.end method

.method private static recoverFromSignature(ILorg/web3j/crypto/ECDSASignature;[B)Ljava/math/BigInteger;
    .registers 13

    .prologue
    const/4 v3, 0x0

    const/4 v2, 0x0

    const/4 v1, 0x1

    .line 91
    if-ltz p0, :cond_51

    move v0, v1

    :goto_6
    const-string v4, "recId must be positive"

    invoke-static {v0, v4}, Lorg/web3j/utils/Assertions;->verifyPrecondition(ZLjava/lang/String;)V

    .line 92
    iget-object v0, p1, Lorg/web3j/crypto/ECDSASignature;->r:Ljava/math/BigInteger;

    invoke-virtual {v0}, Ljava/math/BigInteger;->signum()I

    move-result v0

    if-ltz v0, :cond_53

    move v0, v1

    :goto_14
    const-string v4, "r must be positive"

    invoke-static {v0, v4}, Lorg/web3j/utils/Assertions;->verifyPrecondition(ZLjava/lang/String;)V

    .line 93
    iget-object v0, p1, Lorg/web3j/crypto/ECDSASignature;->s:Ljava/math/BigInteger;

    invoke-virtual {v0}, Ljava/math/BigInteger;->signum()I

    move-result v0

    if-ltz v0, :cond_55

    move v0, v1

    :goto_22
    const-string v4, "s must be positive"

    invoke-static {v0, v4}, Lorg/web3j/utils/Assertions;->verifyPrecondition(ZLjava/lang/String;)V

    .line 94
    if-eqz p2, :cond_57

    move v0, v1

    :goto_2a
    const-string v4, "message cannot be null"

    invoke-static {v0, v4}, Lorg/web3j/utils/Assertions;->verifyPrecondition(ZLjava/lang/String;)V

    .line 98
    sget-object v0, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    invoke-virtual {v0}, Lorg/spongycastle/crypto/params/ECDomainParameters;->getN()Ljava/math/BigInteger;

    move-result-object v4

    .line 99
    int-to-long v6, p0

    const-wide/16 v8, 0x2

    div-long/2addr v6, v8

    invoke-static {v6, v7}, Ljava/math/BigInteger;->valueOf(J)Ljava/math/BigInteger;

    move-result-object v0

    .line 100
    iget-object v5, p1, Lorg/web3j/crypto/ECDSASignature;->r:Ljava/math/BigInteger;

    invoke-virtual {v0, v4}, Ljava/math/BigInteger;->multiply(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v0

    invoke-virtual {v5, v0}, Ljava/math/BigInteger;->add(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v5

    .line 108
    sget-object v0, Lorg/spongycastle/math/ec/custom/sec/SecP256K1Curve;->q:Ljava/math/BigInteger;

    .line 109
    invoke-virtual {v5, v0}, Ljava/math/BigInteger;->compareTo(Ljava/math/BigInteger;)I

    move-result v0

    if-ltz v0, :cond_59

    move-object v0, v3

    .line 145
    :goto_50
    return-object v0

    :cond_51
    move v0, v2

    .line 91
    goto :goto_6

    :cond_53
    move v0, v2

    .line 92
    goto :goto_14

    :cond_55
    move v0, v2

    .line 93
    goto :goto_22

    :cond_57
    move v0, v2

    .line 94
    goto :goto_2a

    .line 115
    :cond_59
    and-int/lit8 v0, p0, 0x1

    if-ne v0, v1, :cond_6e

    move v0, v1

    :goto_5e
    invoke-static {v5, v0}, Lorg/web3j/crypto/Sign;->decompressKey(Ljava/math/BigInteger;Z)Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v0

    .line 118
    invoke-virtual {v0, v4}, Lorg/spongycastle/math/ec/ECPoint;->multiply(Ljava/math/BigInteger;)Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v5

    invoke-virtual {v5}, Lorg/spongycastle/math/ec/ECPoint;->isInfinity()Z

    move-result v5

    if-nez v5, :cond_70

    move-object v0, v3

    .line 119
    goto :goto_50

    :cond_6e
    move v0, v2

    .line 115
    goto :goto_5e

    .line 122
    :cond_70
    new-instance v3, Ljava/math/BigInteger;

    invoke-direct {v3, v1, p2}, Ljava/math/BigInteger;-><init>(I[B)V

    .line 137
    sget-object v5, Ljava/math/BigInteger;->ZERO:Ljava/math/BigInteger;

    invoke-virtual {v5, v3}, Ljava/math/BigInteger;->subtract(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v3

    invoke-virtual {v3, v4}, Ljava/math/BigInteger;->mod(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v3

    .line 138
    iget-object v5, p1, Lorg/web3j/crypto/ECDSASignature;->r:Ljava/math/BigInteger;

    invoke-virtual {v5, v4}, Ljava/math/BigInteger;->modInverse(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v5

    .line 139
    iget-object v6, p1, Lorg/web3j/crypto/ECDSASignature;->s:Ljava/math/BigInteger;

    invoke-virtual {v5, v6}, Ljava/math/BigInteger;->multiply(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v6

    invoke-virtual {v6, v4}, Ljava/math/BigInteger;->mod(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v6

    .line 140
    invoke-virtual {v5, v3}, Ljava/math/BigInteger;->multiply(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v3

    invoke-virtual {v3, v4}, Ljava/math/BigInteger;->mod(Ljava/math/BigInteger;)Ljava/math/BigInteger;

    move-result-object v3

    .line 141
    sget-object v4, Lorg/web3j/crypto/Sign;->CURVE:Lorg/spongycastle/crypto/params/ECDomainParameters;

    invoke-virtual {v4}, Lorg/spongycastle/crypto/params/ECDomainParameters;->getG()Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v4

    invoke-static {v4, v3, v0, v6}, Lorg/spongycastle/math/ec/ECAlgorithms;->sumOfTwoMultiplies(Lorg/spongycastle/math/ec/ECPoint;Ljava/math/BigInteger;Lorg/spongycastle/math/ec/ECPoint;Ljava/math/BigInteger;)Lorg/spongycastle/math/ec/ECPoint;

    move-result-object v0

    .line 143
    invoke-virtual {v0, v2}, Lorg/spongycastle/math/ec/ECPoint;->getEncoded(Z)[B

    move-result-object v2

    .line 145
    new-instance v0, Ljava/math/BigInteger;

    array-length v3, v2

    invoke-static {v2, v1, v3}, Ljava/util/Arrays;->copyOfRange([BII)[B

    move-result-object v2

    invoke-direct {v0, v1, v2}, Ljava/math/BigInteger;-><init>(I[B)V

    goto :goto_50
.end method

.method public static signMessage([BLorg/web3j/crypto/ECKeyPair;)Lorg/web3j/crypto/Sign$SignatureData;
    .registers 9

    .prologue
    const/16 v6, 0x20

    const/4 v1, -0x1

    .line 39
    invoke-virtual {p1}, Lorg/web3j/crypto/ECKeyPair;->getPublicKey()Ljava/math/BigInteger;

    move-result-object v2

    .line 41
    invoke-static {p0}, Lorg/web3j/crypto/Hash;->sha3([B)[B

    move-result-object v3

    .line 43
    invoke-virtual {p1, v3}, Lorg/web3j/crypto/ECKeyPair;->sign([B)Lorg/web3j/crypto/ECDSASignature;

    move-result-object v4

    .line 46
    const/4 v0, 0x0

    :goto_10
    const/4 v5, 0x4

    if-ge v0, v5, :cond_41

    .line 47
    invoke-static {v0, v4, v3}, Lorg/web3j/crypto/Sign;->recoverFromSignature(ILorg/web3j/crypto/ECDSASignature;[B)Ljava/math/BigInteger;

    move-result-object v5

    .line 48
    if-eqz v5, :cond_29

    invoke-virtual {v5, v2}, Ljava/math/BigInteger;->equals(Ljava/lang/Object;)Z

    move-result v5

    if-eqz v5, :cond_29

    .line 53
    :goto_1f
    if-ne v0, v1, :cond_2c

    .line 54
    new-instance v0, Ljava/lang/RuntimeException;

    const-string v1, "Could not construct a recoverable key. This should never happen."

    invoke-direct {v0, v1}, Ljava/lang/RuntimeException;-><init>(Ljava/lang/String;)V

    throw v0

    .line 46
    :cond_29
    add-int/lit8 v0, v0, 0x1

    goto :goto_10

    .line 58
    :cond_2c
    add-int/lit8 v0, v0, 0x1b

    .line 61
    int-to-byte v0, v0

    .line 62
    iget-object v1, v4, Lorg/web3j/crypto/ECDSASignature;->r:Ljava/math/BigInteger;

    invoke-static {v1, v6}, Lorg/web3j/utils/Numeric;->toBytesPadded(Ljava/math/BigInteger;I)[B

    move-result-object v1

    .line 63
    iget-object v2, v4, Lorg/web3j/crypto/ECDSASignature;->s:Ljava/math/BigInteger;

    invoke-static {v2, v6}, Lorg/web3j/utils/Numeric;->toBytesPadded(Ljava/math/BigInteger;I)[B

    move-result-object v2

    .line 65
    new-instance v3, Lorg/web3j/crypto/Sign$SignatureData;

    invoke-direct {v3, v0, v1, v2}, Lorg/web3j/crypto/Sign$SignatureData;-><init>(B[B[B)V

    return-object v3

    :cond_41
    move v0, v1

    goto :goto_1f
.end method

.method public static signedMessageToKey([BLorg/web3j/crypto/Sign$SignatureData;)Ljava/math/BigInteger;
    .registers 8

    .prologue
    const/16 v5, 0x20

    const/4 v2, 0x0

    const/4 v1, 0x1

    .line 170
    invoke-virtual {p1}, Lorg/web3j/crypto/Sign$SignatureData;->getR()[B

    move-result-object v0

    .line 171
    invoke-virtual {p1}, Lorg/web3j/crypto/Sign$SignatureData;->getS()[B

    move-result-object v3

    .line 172
    if-eqz v0, :cond_45

    array-length v0, v0

    if-ne v0, v5, :cond_45

    move v0, v1

    :goto_12
    const-string v4, "r must be 32 bytes"

    invoke-static {v0, v4}, Lorg/web3j/utils/Assertions;->verifyPrecondition(ZLjava/lang/String;)V

    .line 173
    if-eqz v3, :cond_1d

    array-length v0, v3

    if-ne v0, v5, :cond_1d

    move v2, v1

    :cond_1d
    const-string v0, "s must be 32 bytes"

    invoke-static {v2, v0}, Lorg/web3j/utils/Assertions;->verifyPrecondition(ZLjava/lang/String;)V

    .line 175
    invoke-virtual {p1}, Lorg/web3j/crypto/Sign$SignatureData;->getV()B

    move-result v0

    and-int/lit16 v0, v0, 0xff

    .line 178
    const/16 v2, 0x1b

    if-lt v0, v2, :cond_30

    const/16 v2, 0x22

    if-le v0, v2, :cond_47

    .line 179
    :cond_30
    new-instance v1, Ljava/security/SignatureException;

    new-instance v2, Ljava/lang/StringBuilder;

    const-string v3, "Header byte out of range: "

    invoke-direct {v2, v3}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V

    invoke-virtual {v2, v0}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;

    move-result-object v0

    invoke-virtual {v0}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v0

    invoke-direct {v1, v0}, Ljava/security/SignatureException;-><init>(Ljava/lang/String;)V

    throw v1

    :cond_45
    move v0, v2

    .line 172
    goto :goto_12

    .line 182
    :cond_47
    new-instance v2, Lorg/web3j/crypto/ECDSASignature;

    new-instance v3, Ljava/math/BigInteger;

    invoke-virtual {p1}, Lorg/web3j/crypto/Sign$SignatureData;->getR()[B

    move-result-object v4

    invoke-direct {v3, v1, v4}, Ljava/math/BigInteger;-><init>(I[B)V

    new-instance v4, Ljava/math/BigInteger;

    invoke-virtual {p1}, Lorg/web3j/crypto/Sign$SignatureData;->getS()[B

    move-result-object v5

    invoke-direct {v4, v1, v5}, Ljava/math/BigInteger;-><init>(I[B)V

    invoke-direct {v2, v3, v4}, Lorg/web3j/crypto/ECDSASignature;-><init>(Ljava/math/BigInteger;Ljava/math/BigInteger;)V

    .line 186
    invoke-static {p0}, Lorg/web3j/crypto/Hash;->sha3([B)[B

    move-result-object v1

    .line 187
    add-int/lit8 v0, v0, -0x1b

    .line 188
    invoke-static {v0, v2, v1}, Lorg/web3j/crypto/Sign;->recoverFromSignature(ILorg/web3j/crypto/ECDSASignature;[B)Ljava/math/BigInteger;

    move-result-object v0

    .line 189
    if-nez v0, :cond_72

    .line 190
    new-instance v0, Ljava/security/SignatureException;

    const-string v1, "Could not recover public key from signature"

    invoke-direct {v0, v1}, Ljava/security/SignatureException;-><init>(Ljava/lang/String;)V

    throw v0

    .line 192
    :cond_72
    return-object v0
.end method