首页 > 解决方案 > 返回 shamir 方案的 n 次多项式的函数

问题描述

我正在编写一个用于执行 shamirs 秘密共享方案的 kotlin 程序。我正在用 kotlin 写这个,但我很乐意在任何编程语言或伪代码中得到答案。

因此,我需要编写一个执行以下操作的函数:

Implement a function to split the secret into N shares with the polynomial definition from the       
lecture on a field F and allow reconstruction with at least k shares.

所以我必须有一个可以接受一些输入的函数,并将其拆分为 N 个份额,然后我应该能够通过收集 k 个份额来重建原始输入,其中 k <= N。

到目前为止,我已经用一个二次多项式实现了这个方案,这样秘密就可以用 3 份来重建。这基本上是为这样的二次函数创建一个类:

class quadratic(val A: Int, val B: Int, val C : BigInteger) {

val a = A.toDouble().toBigDecimal()
val b = B.toDouble().toBigDecimal()
val c = C.toBigDecimal()

override fun toString() : String{
return this.a.toString() + "* x ^2 +" + this.b.toString() + " * x + " + this.c.toString()
}
// This is the important part, this is where the quadratic function is actually implemented:
private fun getter() : (BigDecimal) -> BigDecimal = { x : BigDecimal -> (this.a * x.pow(2))+(this.b*x)+this.c}

fun YforX(x : Int) : BigDecimal {
    val valueeeeee = getter().invoke(x.toDouble().toBigDecimal())
    return valueeeeee
}}

然后我有一个名为 lagrange 的函数,它可以从 3 点重构有问题的二次函数:

fun langraged(cord0 : Pair<BigDecimal,BigDecimal>,cord1 : Pair<BigDecimal,BigDecimal>,cord2 : Pair<BigDecimal,BigDecimal>) : (BigDecimal) -> BigDecimal{
    val L0: (BigDecimal) -> BigDecimal = { x -> ((x - cord1.first)*(x - cord2.first)) / ((cord0.first - cord1.first)*(cord0.first - cord2.first))}
    val L1: (BigDecimal) -> BigDecimal = { x -> ((x - cord0.first)*(x - cord2.first)) / ((cord1.first - cord0.first)*(cord1.first - cord2.first))}
    val L2: (BigDecimal) -> BigDecimal = { x -> ((x - cord0.first)*(x - cord1.first)) / ((cord2.first - cord0.first)*(cord2.first - cord1.first))}

    return { x -> cord0.second * L0(x) + cord1.second * L1(x)  + cord2.second * L2(x) }
}

这两个代码应该给出我正在做的事情的要点。但!正如我的练习所说,我需要一个可以具有任何 k 阈值的函数。

那么,有什么方法可以动态生成 n 度的函数,具体取决于输入 n 是什么?

谢谢奥尤

标签: kotlincryptography

解决方案


推荐阅读