kotlin - 返回 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 是什么?
谢谢奥尤
解决方案
推荐阅读
- java - Agora 一对一通话 - Android 原生
- unity3d - Unity 3d 粒子未在游戏视图中显示
- verification - Yosys 和 JasperGold 如何比较?
- java - 如何通过 Swagger UI 为所有 API 发送请求标头(Origin)
- react-native - 如何在 react native 项目中解决此问题 - “npm ERR!Invalid tag name ">=^16.8.1"” 这个问题?
- flask - 烧瓶应用程序中未显示 Flash 消息
- mongodb - 对集合进行分片的正确方法以及如何解释结果
- node.js - 如何使用用户名和密码以及每个流媒体密钥保护节点媒体服务器管理面板 - 节点媒体服务器
- python - Python Moviepy 模块 - 当我将多个视频(具有不同的纵横比和帧速率)组合成一个视频时,视频已损坏
- flutter - 将位置数据从后台服务发送到 Flutter 中的前台屏幕