All files / kyber/lib/sign/schnorr schnorr.js

100% Statements 59/59
100% Branches 18/18
100% Functions 3/3
100% Lines 59/59
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x   1x 1x 1x 1x   1x                   5x 1x   4x 1x   3x 1x       2x 2x 2x     2x     2x       2x 2x     2x 2x 2x 2x 2x                             8x 1x   7x 1x   6x 1x   5x 1x       4x 4x 4x 4x 1x       3x 3x 3x   3x 3x 3x     3x 3x     3x   3x 3x   3x 2x   1x                     5x 5x 15x   5x 5x 5x     1x 1x  
const group = require("./../../group");
// XXX to be changed to an interface style
const nist = group.nist;      
const curve = nist.Curve;
const scalarCons = nist.Scalar;
const pointCons = nist.Point;
 
const hash = require("hash.js");
/**
 *
 * Sign computes a Schnorr signature over the given message.
 * 
 * @param privateKey private key scalar to sign with
 * @param message message over which the signature is computed
 * @return signature as a Uint8Array
 * */
function Sign(suite, privateKey, message) {
    if (suite.constructor !== curve) {
        throw "first argument must be a suite";
    }
    if (privateKey.constructor !== scalarCons) {
        throw "second argument must be a scalar";
    }
    if (message.constructor !== Uint8Array) {
        throw "third argument must be Uint8Array";
    }
 
    // generate r & R
    const r = suite.scalar().pick();
    const R = suite.point().mul(r,null);
    const buffR = R.marshalBinary();
 
    // generate public key
    const pub = suite.point().mul(privateKey,null);
    
    // generate challenge
    const challenge = hashSchnorr(suite,buffR, pub.marshalBinary(), 
                         message) 
 
    // generate signature
    const s = suite.scalar().mul(privateKey,challenge)
    s.add(s,r)
 
    // concatenate R || s
    const buffS = s.marshalBinary();
    const buffSig = new Uint8Array(buffR.length + buffS.length);
    buffSig.set(buffR);
    buffSig.set(buffS,buffR.length);
    return buffSig;
}
 
/**
 * 
 * Verify verify if the signature of the message is valid under the given public
 * key.
 *
 * @params suite suite to use
 * @params publicKey public key under which to verify the signature
 * @params message message that is signed
 * @params signature signature made over the given message
 * @return boolean true if signature is valid or false otherwise.
 * */
function Verify(suite, publicKey, message, signature) {
    if (suite.constructor !== curve) {
        throw "first argument must be a suite";
    }
    if (publicKey.constructor !== pointCons) {
        throw "second argument must be a point";
    }
    if (message.constructor !== Uint8Array) {
        throw "third argument must be a Uint8Array";
    }
    if (signature.constructor !== Uint8Array) {
        throw "fourth argument must be a Uint8Array";
    }
 
    // check the signature size
    const plen = suite.pointLen();
    const slen = suite.scalarLen();
    const totalSize = plen + slen;
    if (signature.length != totalSize) {
        return false;
    }
   
    // unmarshal R || s
    const buffR = signature.slice(0,plen);
    const R = suite.point();
    R.unmarshalBinary(buffR);
 
    const buffs = signature.slice(plen,signature.lengh);
    const s = suite.scalar();
    s.unmarshalBinary(buffs);
 
    // recompute challenge = H(R || P || M)
    const buffPub = publicKey.marshalBinary();
    const challenge = hashSchnorr(suite, buffR, buffPub, message);
 
    // compute sG
    const left = suite.point().mul(s,null);
    // compute R + challenge * Public
    const right = suite.point().mul(challenge,publicKey)
    right.add(right,R);
 
    if (!right.equal(left)) {
        return false; 
    }
    return true;
}
 
/**
 * 
 * hashSchnorr returns a scalar out of hashing the given inputs.
 * @params inputs a list of Uint8Array
 * @return a scalar 
 *
 **/
function hashSchnorr(suite, ...inputs) {
    const h = hash.sha512();
    for(let i of inputs) {
        h.update(i); 
    }
    const scalar = suite.scalar()
    scalar.setBytes(Uint8Array.from(h.digest()));
    return scalar
}
 
module.exports.sign = Sign;
module.exports.verify = Verify;