All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.ntru.sign.SignaturePrivateKey Maven / Gradle / Ivy

/**
 * Copyright (c) 2011, Tim Buktu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package net.sf.ntru.sign;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import net.sf.ntru.exception.NtruException;
import net.sf.ntru.sign.SignatureParameters.BasisType;
import net.sf.ntru.sign.SignatureParameters.TernaryPolynomialType;

/**
 * A NtruSign private key comprises one or more {@link Basis} of three polynomials each,
 * except the zeroth basis for which h is undefined.
 */
public class SignaturePrivateKey {
    int N;
    int q;
    private boolean sparse;
    private TernaryPolynomialType polyType;
    private BasisType basisType;
    private float keyNormBoundSq;
    private List bases;
    
    /**
     * Constructs a new private key from a byte array
     * @param b an encoded private key
     */
    public SignaturePrivateKey(byte[] b) {
        this(new ByteArrayInputStream(b));
    }
    
    /**
     * Constructs a new private key from an input stream
     * @param is an input stream
     * @throws NtruException if an {@link IOException} occurs
     */
    public SignaturePrivateKey(InputStream is) {
        bases = new ArrayList();
        
        DataInputStream dataStream = new DataInputStream(is);
        try {
            N = dataStream.readShort();
            q = dataStream.readShort();
            byte flags = dataStream.readByte();
            sparse = (flags&1) != 0;
            polyType = (flags&4)==0 ? TernaryPolynomialType.SIMPLE : TernaryPolynomialType.PRODUCT;
            basisType = ((flags&8)==0) ? BasisType.STANDARD : BasisType.TRANSPOSE;
            keyNormBoundSq = dataStream.readFloat();
            
            int numBases = is.read();
            for (int i=0; i();
    }
    
    /**
     * Adds a basis to the key.
     * @param b a NtruSign basis
     */
    void add(Basis b) {
        bases.add(b);
    }
    
    /**
     * Returns the i-th basis
     * @param i the index
     * @return the basis at index i
     */
    Basis getBasis(int i) {
        return bases.get(i);
    }
    
    int getNumBases() {
        return bases.size();
    }
    
    /**
     * Converts the key to a byte array
     * @return the encoded key
     */
   public byte[] getEncoded() {
       int numBases = bases.size();
       
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        DataOutputStream dataStream = new DataOutputStream(os);
        try {
            dataStream.writeShort(N);
            dataStream.writeShort(q);
            
            int flags = sparse ? 1 : 0;
            flags |= polyType==TernaryPolynomialType.PRODUCT ? 4 : 0;
            flags |= basisType==BasisType.TRANSPOSE ? 8 : 0;
            dataStream.write(flags);
            
            dataStream.writeFloat(keyNormBoundSq);
            dataStream.write(numBases);   // 1 byte
            
            for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy