I am trying to transform a formula over to a finite-field equivalent of that formula.
The formula can be seen below:
Now I have this implemented and it works correctly, but I need this in a finite-field, which means that I introduce a p, let's say p = 183269
andd take mod p
but how exactly does the above formula change? Do I just mod p
after i'm done calculating the formula normally?
Example:
I have the polynomial: f(x) = 1234 + 631x + 442x^2
I generated 6 random points: (x, f(x) mod p)
1. (108, 93338)
2. (413, 146507)
3. (260, 171647)
4. (819, 98605)
5. (359, 13237)
6. (894, 118490)
Now, what I want is to reconstruct 1234 given any 3 points using the above formula, but it gives me incorrect value.
here is my code:
// x_input = [108, 413, 260]
var reconstructed float64 = 0.0
for _, k := range x_input {
var y float64 = float64(points[k])
var pr_x float64 = 1.0
for _, l := range x_input {
if l != k {
var aux_k float64 = float64(k)
var aux_l float64 = float64(l)
pr_x *= (aux_l / (aux_l - aux_k))
}
}
y *= pr_x
reconstructed += y
}
I'm trying to implement SSSS
EDIT
As pointed out by @user58697
I had some mistakes in my code and understanding of finite fields. I managed to rewrite my formula and it looks like this:
reconstructed := 0
for _, k := range x_input {
y := points[k]
pr_x := 1
for _, l := range x_input {
if l != k {
inv := mod_inverse(l - k, p)
pr_x *= inv
}
}
y *= pr_x
reconstructed += y
}
return reconstructed % p
func mod_inverse(a, p int) int {
if a < 0 { // negative numbers are not allowed
a = a * -1
}
for i := 1; i < p; i++ {
if ((a % p) * (i % p)) % p == 1 {
return i
}
}
return p
}
Unfortunately, it still has one or more bugs because it doesn't produce f(0)