Principle, construction and code implementation of linear secret sharing scheme based on sharing matrix

1 Linear secret sharing scheme

Linear secret sharing scheme is an encryption technology used to split a secret message into multiple fragments and distribute these fragments to multiple participants. Only when a sufficient number of participants cooperate, the complete secret information can be restored Secret information.
The basic principle of linear secret sharing scheme is to use polynomial interpolation. Suppose we have a polynomial of order (t-1), where t is the threshold (the minimum number of participants required to recover the secret information). We choose an integer n greater than or equal to t as the number of participants.
When generating a linear secret sharing scheme, first use the original secret information as the constant term of the polynomial, and randomly select other coefficients of the polynomial (which can be random numbers or other values). This generates a polynomial of order t.
Then, we calculate the value of the polynomial at the ordinal position of each participant in turn to obtain the shared value of each participant. These shared values constitute multiple distributed fragments of the secret, distributed to different participants.
When recovering secret information, at least t participants need to cooperate, and they combine the shared values they hold. Through the polynomial interpolation algorithm, based on these shared value data points, we can reconstruct the original polynomial and extract the secret information from it.
The advantages of the linear secret sharing scheme are security and scalability. Since the secret information is divided into multiple shared value fragments and distributed to different participants, a single participant cannot obtain the complete secret information, ensuring the security of the information. At the same time, the scalability of the scheme can be achieved by adjusting the threshold and the number of participants.
Linear secret sharing schemes have applications in many fields, especially in scenarios that require multi-party cooperation, such as distributed system security, multi-party collaboration encryption, data transmission confidentiality, etc. It provides a reliable way to protect secret information from potential attacks and leaks.

2 Linear secret sharing scheme based on secret sharing matrix

Linear secret sharing scheme based on secret sharing matrix is an improved linear secret sharing scheme, in which matrix operations are used to achieve the segmentation and recovery of secret information.

2.1 Principle

  • Suppose we want to split a secret information into multiple shared values, first represent the secret information as a vector.
  • Then, a t × n matrix is generated, where t is the threshold (the minimum number of participants required to recover the secret information) and n is the number of participants.
  • Each row of the matrix is a random (irreversible) vector, and each participant gets a row of the matrix as its corresponding shared value.
  • When a sufficient number of participants cooperate, these shared values can be used to recover the original secret information through matrix operations.

2.2 Structure

  • Generate a shared matrix:

    • Computes a t × n matrix in which each element is randomly selected.
    • Each row represents a shared value, where t shared values can be used to recover the secret information.
    • Distribute each row to the appropriate participant.
  • Recover secret information:

    • At least t participants cooperate to collect corresponding shared values.
    • Construct a t × t submatrix using these t shared values.
    • Use the sub-matrix to perform inverse matrix operations to obtain the inverse matrix.
    • Represent the remaining n-t shared values as an n-t column vector.
    • Multiply the column vector with the inverse matrix to get the original secret message.

2.3 Features

  • Shared values in the scheme are represented as row vectors of matrices rather than as values of polynomials.
  • The construction of the shared matrix requires guarantees of randomness and irreversibility to increase security.
  • When recovering secret information, matrix operations and matrix inverse operations are required.
  • The scheme still follows the threshold requirement, requiring the cooperation of at least t participants to recover the secret information.

The linear secret sharing scheme based on the secret sharing matrix can provide higher security and difficulty in cracking, because matrix-based operations involve more complex mathematical operations and have stricter mathematical guarantees. This solution is widely used in scenarios that require a higher level of confidentiality and security, such as confidential file transmission, blockchain security, etc.

3 Code implementation

3.1 Python implementation

#Python implementation example code of linear secret sharing scheme based on secret sharing matrix, with detailed annotations:
import numpy as np
import random

class LinearSecretSharing:
    def __init__(self, num_shares, threshold, secret):
        self.num_shares = num_shares # Number of shares
        self.threshold = threshold # Threshold
        self.secret = secret # Secret value
        
    def generate_share_matrix(self):
        # Generate a secret sharing matrix of num_shares x threshold
        # Each element corresponds to the secret shared value
        
        # Generate a random polynomial coefficient matrix
        coefficients = np.random.randint(1, 1000, size=(self.threshold-1,))
        
        # Build secret sharing matrix
        share_matrix = np.zeros((self.num_shares, self.threshold))
        for i in range(self.num_shares):
            for j in range(self.threshold):
                # Calculate the value of the i-th share
                share_matrix[i][j] = self.secret + np.sum(coefficients * self.power(i + 1, j + 1))
        
        return share_matrix
    
    def reconstruct_secret(self, share_matrix):
        # Reconstruct the secret value according to the secret sharing matrix
        
        assert share_matrix.shape[0] >= self.threshold, "Not enough shares for reconstruction"
        
        reconstructed_secret = 0
        for i in range(self.threshold):
            numerator = 1
            denominator = 1
            for j in range(self.threshold):
                if i != j:
                    numerator *= (0 - j)
                    denominator *= (i - j)
            share = share_matrix[i][0]
            
            # Multiply by the Lagrangian interpolation polynomial coefficients
            for k in range(self.threshold - 1):
                share *= numerator / denominator
            reconstructed_secret + = share
        
        return int(reconstructed_secret)
    
    def power(self, base, exponent):
        # exponentiation
        return base ** exponent
    
if __name__ == "__main__":
    num_shares = 5
    threshold = 3
    secret=42
    
    #Create a LinearSecretSharing object
    lss = LinearSecretSharing(num_shares, threshold, secret)
    
    # Generate secret sharing matrix
    share_matrix = lss.generate_share_matrix()
    
    # Output secret sharing matrix
    print("Share Matrix:")
    for row in share_matrix:
        print(" ".join(str(int(share)) for share in row))
    
    # Reconstruct the secret value
    reconstructed_secret = lss.reconstruct_secret(share_matrix)
    
    # Output the reconstructed secret value
    print("Reconstructed Secret:", reconstructed_secret)

This code implements a linear secret sharing scheme based on a secret sharing matrix. Among them, the generate_share_matrix method is used to generate the secret sharing matrix, and the reconstruct_secret method is used to reconstruct the secret value based on the given secret sharing matrix.

In the main program, we can specify the number of shares, thresholds and secret values, and demonstrate the process of generating a secret sharing matrix and reconstructing the secret value based on this matrix.

Requires error handling, data validation, and more complex algorithms to meet your actual needs.

3.2 Java implementation

//Java implementation example code of linear secret sharing scheme based on secret sharing matrix, with detailed annotations:
import java.security.SecureRandom;

public class LinearSecretSharing {<!-- -->
    private int numShares; // Number of shares
    private int threshold; // threshold
    private int secret; // secret value
    private SecureRandom random; // Random number generator

    // Constructor
    public LinearSecretSharing(int numShares, int threshold, int secret) {<!-- -->
        this.numShares = numShares;
        this.threshold = threshold;
        this.secret = secret;
        this.random = new SecureRandom();
    }

    // Generate secret sharing matrix
    public int[][] generateShareMatrix() {<!-- -->
        int[][] shareMatrix = new int[numShares][threshold];

        // Generate a polynomial for each share
        for (int i = 0; i < numShares; i + + ) {<!-- -->
            int[] coefficients = generateCoefficients();
            shareMatrix[i] = generateShares(coefficients);
        }

        return shareMatrix;
    }

    // Generate polynomial coefficients
    private int[] generateCoefficients() {<!-- -->
        int[] coefficients = new int[threshold - 1];

        // Randomly generate polynomial coefficients
        for (int i = 0; i <threshold - 1; i + + ) {<!-- -->
            coefficients[i] = random.nextInt();
        }

        return coefficients;
    }

    // Generate shared values based on polynomial coefficients
    private int[] generateShares(int[] coefficients) {<!-- -->
        int[] shares = new int[threshold];

        // Calculate the value of each share
        for (int i = 0; i <threshold; i + + ) {<!-- -->
            int share = secret;

            // Calculate the value of each term: coefficient * (i + 1)^exponent
            for (int j = 0; j <threshold - 1; j + + ) {<!-- -->
                share + = coefficients[j] * power(i + 1, j + 1);
            }

            //Storage shared value
            shares[i] = share;
        }

        return shares;
    }

    // exponentiation
    private int power(int base, int exponent) {<!-- -->
        int result = 1;

        for (int i = 0; i < exponent; i + + ) {<!-- -->
            result *= base;
        }

        return result;
    }

    // Reconstruct the secret value based on the shared value
    public int reconstructSecret(int[][] shareMatrix) {<!-- -->
        int reconstructedSecret = 0;

        // Use Lagrangian interpolation to reconstruct the secret value
        for (int i = 0; i < numShares; i + + ) {<!-- -->
            int numerator = 1;
            int denominator = 1;

            for (int j = 0; j <threshold; j + + ) {<!-- -->
                if (i != j) {<!-- -->
                    numerator *= (0 - j);
                    denominator *= (i - j);
                }
            }

            int share = shareMatrix[i][0];

            // Multiply by the Lagrangian interpolation polynomial coefficients
            for (int k = 0; k <threshold - 1; k + + ) {<!-- -->
                share *= numerator / denominator;
            }

            reconstructedSecret + = share;
        }

        return reconstructedSecret;
    }

    public static void main(String[] args) {<!-- -->
        int numShares = 5;
        int threshold = 3;
        int secret = 42;

        LinearSecretSharing lss = new LinearSecretSharing(numShares, threshold, secret);

        // Generate secret sharing matrix
        int[][] shareMatrix = lss.generateShareMatrix();

        // Output secret sharing matrix
        System.out.println("Share Matrix: ");
        for (int[] row : shareMatrix) {<!-- -->
            for (int share : row) {<!-- -->
                System.out.print(share + " ");
            }
            System.out.println();
        }

        //Reconstruct the secret value
        int reconstructedSecret = lss.reconstructSecret(shareMatrix);

        // Output the reconstructed secret value
        System.out.println("Reconstructed Secret: " + reconstructedSecret);
    }
}

This code implements a linear secret sharing scheme based on a secret sharing matrix. Among them, the generateShareMatrix method is used to generate the secret sharing matrix, and the reconstructSecret method is used to reconstruct the secret value based on the given secret sharing matrix.

In the main method, we can specify the number of shares, threshold and secret value, and demonstrate the process of generating a secret sharing matrix and reconstructing the secret value based on this matrix.

Error handling, data validation, and more complex algorithms are also needed to meet your actual needs.