golang 实现比特币内核:处理椭圆曲线中的天文数字

在比特币密码学中,我们需要处理天文数字,这个数字是如此巨大,以至于它很容易超出我们宇宙中原子的总数,也许 64 位的值不足以表示这个数字,而像加、乘、幂这样的操作如果使用 64 位整数会导致溢出,因此我们可能需要借助 golang 的 big 包,我们将通过使用 big.Int 来表示其值字段来更改 FieldNumber 的代码,代码将如下所示:

package elliptic_curveimport ("fmt""math/big"
)//using big package to deal with Astronomical figurestype FieldElement struct {order *big.Int //field ordernum   *big.Int //value of the given element in the field
}func NewFieldElement(order *big.Int, num *big.Int) *FieldElement {/*constructor for FieldElement, its the __init__ if you are from python*/if order.Cmp(num) == -1 {err := fmt.Sprintf("Num not in the range from 0 to %v", order)panic(err)}return &FieldElement{order: order,num:   num,}
}func (f *FieldElement) String() string {//format the object to printable string//its __repr__ if you are from pythonreturn fmt.Sprintf("FieldElement{order: %v, num: %v}", *f.order, *f.num)
}func (f *FieldElement) EqualTo(other *FieldElement) bool {/*two field element is equal if their order and value are equal*/return f.order.Cmp(other.order) == 0 && f.num.Cmp(other.num) == 0
}func (f *FieldElement) checkOrder(other *FieldElement) {if f.order.Cmp(other.order) != 0 {panic("add need to do on field element with the same order")}
}func (f *FieldElement) Add(other *FieldElement) *FieldElement {f.checkOrder(other)//remember to do the modulurvar op big.Intreturn NewFieldElement(f.order, op.Mod(op.Add(f.num, other.num), f.order))
}func (f *FieldElement) Negate() *FieldElement {/*for a field element a, its negate is another element b in field such that(a + b) % order= 0(remember the modulur over order), because the value of elementin the field are smaller than its order, we can easily get the negate of a byorder - a,*/var op big.Intreturn NewFieldElement(f.order, op.Sub(f.order, f.num))
}func (f *FieldElement) Subtract(other *FieldElement) *FieldElement {//first find the negate of the other//add this and the negate of the otherreturn f.Add(other.Negate())
}func (f *FieldElement) Multiply(other *FieldElement) *FieldElement {f.checkOrder(other)//multiplie over modulur of ordervar op big.Intmul := op.Mul(f.num, other.num)return NewFieldElement(f.order, op.Mod(mul, f.order))
}func (f *FieldElement) Power(power *big.Int) *FieldElement {var op big.IntpowerRes := op.Exp(f.num, power, nil)modRes := op.Mod(powerRes, f.order)return NewFieldElement(f.order, modRes)
}func (f *FieldElement) ScalarMul(val *big.Int) *FieldElement {var op big.Intres := op.Mul(f.num, val)res = op.Mod(res, f.order)return NewFieldElement(f.order, res)
}

现在我们需要确保这些更改不会破坏我们的逻辑,让我们再次运行测试,在 main.go 中,我们有以下代码:

package mainimport (ecc "elliptic_curve""fmt""math/big""math/rand"
)func SolveField19MultiplieSet() {//randomly select a num from (1, 18)min := 1max := 18k := rand.Intn(max-min) + minfmt.Printf("randomly select k is : %d\n", k)element := ecc.NewFieldElement(big.NewInt(19), big.NewInt(int64(k)))for i := 0; i < 19; i++ {fmt.Printf("element %d multiplie with %d is %v\n", k, i,element.ScalarMul(big.NewInt(int64(i))))}}func main() {f44 := ecc.NewFieldElement(big.NewInt(57), big.NewInt(44))f33 := ecc.NewFieldElement(big.NewInt(57), big.NewInt(33))// 44 + 33 equal to (44+33) % 57 is 20res := f44.Add(f33)fmt.Printf("field element 44 add to field element 33 is : %v\n", res)//-44 is the negate of field element 44, which is 57 - 44 = 13fmt.Printf("negate of field element 44 is : %v\n", f44.Negate())fmt.Printf("field element 44 - 33 is : %v\n", f44.Subtract(f33))fmt.Printf("field element 33 - 44 is : %v\n", f33.Subtract(f44))//it is easy to check (11+33)%57 == 44//check (46 + 44) % 57 == 33fmt.Printf("check 46 + 44 over modulur 57 is %d\n", (46+44)%57)//check by field elementf46 := ecc.NewFieldElement(big.NewInt(57), big.NewInt(46))fmt.Printf("field element 46 + 44 is %v\n", f46.Add(f44))SolveField19MultiplieSet()
}

运行上述代码将获得以下结果:


field element 44 add to field element 33 is : FieldElement{order: 57, num: 20}
negate of field element 44 is : FieldElement{order: 57, num: 13}
field element 44 - 33 is : FieldElement{order: 57, num: 11}
field element 33 - 44 is : FieldElement{order: 57, num: 46}
check 46 + 44 over modulur 57 is 33
field element 46 + 44 is FieldElement{order: 57, num: 33}
randomly select k is : 2
element 2 multiplie with 0 is FieldElement{order: 19, num: 0}
element 2 multiplie with 1 is FieldElement{order: 19, num: 2}
element 2 multiplie with 2 is FieldElement{order: 19, num: 4}
element 2 multiplie with 3 is FieldElement{order: 19, num: 6}
element 2 multiplie with 4 is FieldElement{order: 19, num: 8}
element 2 multiplie with 5 is FieldElement{order: 19, num: 10}
element 2 multiplie with 6 is FieldElement{order: 19, num: 12}
element 2 multiplie with 7 is FieldElement{order: 19, num: 14}
element 2 multiplie with 8 is FieldElement{order: 19, num: 16}
element 2 multiplie with 9 is FieldElement{order: 19, num: 18}
element 2 multiplie with 10 is FieldElement{order: 19, num: 1}
element 2 multiplie with 11 is FieldElement{order: 19, num: 3}
element 2 multiplie with 12 is FieldElement{order: 19, num: 5}
element 2 multiplie with 13 is FieldElement{order: 19, num: 7}
element 2 multiplie with 14 is FieldElement{order: 19, num: 9}
element 2 multiplie with 15 is FieldElement{order: 19, num: 11}
element 2 multiplie with 16 is FieldElement{order: 19, num: 13}
element 2 multiplie with 17 is FieldElement{order: 19, num: 15}
element 2 multiplie with 18 is FieldElement{order: 19, num: 17}

通过检查结果,我们可以确保 FieldElement 中的更改不会破坏我们之前的逻辑。现在让我们考虑以下问题:
p = 7, 11, 17, 19, 31,以下集合会是什么:
{1 ^(p-1), 2 ^ (p-1), … (p-1)^(p-1)}
让我们在 main.go 中编写代码来解决它:


func ComputeFieldOrderPower() {orders := []int{7, 11, 17, 31}for _, p := range orders {fmt.Printf("value of p is: %d\n", p)for i := 1; i < p; i++ {elm := ecc.NewFieldElement(big.NewInt(int64(p)), big.NewInt(int64(i)))fmt.Printf("for element: %v, its power of p - 1 is: %v\n", elm,elm.Power(big.NewInt(int64(p-1))))}fmt.Println("-------------------------------")}
}func main() {ComputeFieldOrderPower()
}

结果如下:

value of p is: 7
for element: FieldElement{order: 7, num: 1}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 2}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 3}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 4}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 5}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 6}, its power of p - 1 is: FieldElement{order: 7, num: 1}
-------------------------------
value of p is: 11
for element: FieldElement{order: 11, num: 1}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 2}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 3}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 4}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 5}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 6}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 7}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 8}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 9}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 10}, its power of p - 1 is: FieldElement{order: 11, num: 1}
-------------------------------
value of p is: 17
for element: FieldElement{order: 17, num: 1}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 2}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 3}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 4}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 5}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 6}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 7}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 8}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 9}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 10}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 11}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 12}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 13}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 14}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 15}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 16}, its power of p - 1 is: FieldElement{order: 17, num: 1}
-------------------------------
value of p is: 31
for element: FieldElement{order: 31, num: 1}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 2}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 3}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 4}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 5}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 6}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 7}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 8}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 9}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 10}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 11}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 12}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 13}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 14}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 15}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 16}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 17}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 18}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 19}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 20}, its power of p - 1 is: FieldElement{order: 31, num: 1}
my@MACdeMacBook-Air bitcoin % go run main.go
value of p is: 7
for element: FieldElement{order: 7, num: 1}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 2}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 3}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 4}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 5}, its power of p - 1 is: FieldElement{order: 7, num: 1}
for element: FieldElement{order: 7, num: 6}, its power of p - 1 is: FieldElement{order: 7, num: 1}
-------------------------------
value of p is: 11
for element: FieldElement{order: 11, num: 1}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 2}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 3}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 4}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 5}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 6}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 7}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 8}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 9}, its power of p - 1 is: FieldElement{order: 11, num: 1}
for element: FieldElement{order: 11, num: 10}, its power of p - 1 is: FieldElement{order: 11, num: 1}
-------------------------------
value of p is: 17
for element: FieldElement{order: 17, num: 1}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 2}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 3}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 4}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 5}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 6}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 7}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 8}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 9}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 10}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 11}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 12}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 13}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 14}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 15}, its power of p - 1 is: FieldElement{order: 17, num: 1}
for element: FieldElement{order: 17, num: 16}, its power of p - 1 is: FieldElement{order: 17, num: 1}
-------------------------------
value of p is: 19
for element: FieldElement{order: 19, num: 1}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 2}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 3}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 4}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 5}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 6}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 7}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 8}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 9}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 10}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 11}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 12}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 13}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 14}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 15}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 16}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 17}, its power of p - 1 is: FieldElement{order: 19, num: 1}
for element: FieldElement{order: 19, num: 18}, its power of p - 1 is: FieldElement{order: 19, num: 1}
-------------------------------
value of p is: 31
for element: FieldElement{order: 31, num: 1}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 2}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 3}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 4}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 5}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 6}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 7}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 8}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 9}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 10}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 11}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 12}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 13}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 14}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 15}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 16}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 17}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 18}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 19}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 20}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 21}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 22}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 23}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 24}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 25}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 26}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 27}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 28}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 29}, its power of p - 1 is: FieldElement{order: 31, num: 1}
for element: FieldElement{order: 31, num: 30}, its power of p - 1 is: FieldElement{order: 31, num: 1}
-------------------------------

你可以看到集合中的所有元素都是1,无论字段的顺序如何,这意味着对于任何有限字段中的任意元素k和顺序p,我们会有:
k ^(p-1) % p == 1
这是一个重要结论,我们将在后续视频中使用它来驱动我们的加密算法。

有限域元素上最难的操作是除法,我们有乘法操作,对于字段中的元素3和7(顺序为19),它们的乘积是(3 * 7) % 19 = 2。现在给定两个字段元素2和7,我们如何得到7?我们定义一个除法操作,它是乘法的逆运算,即2 / 7 = 3,这相当直观。这里我们需要确保分母不是0。

记住在有限的定义中,如果a在字段中,那么还有一个b在字段中,使得a * b = 1。对于3 7 = 2(注意表示模顺序的乘法),如果我们能找到b,使得b * 7 = 1,那么我们就会有3 * 7 * b = 2 * b => 3 * (7 * b) = 2 * b => 3 = 2 * b,这意味着2 / 7是2乘以b的结果,b. 也就是说,如果我们想做除法a / b,我们可以找到b的乘法逆元,称之为c,并使用c与模顺序相乘。

现在问题来了,我们如何找到b的乘法逆元?记住我们上面的问题吗?b ^ (p - 1) % p = 1 => b * b ^(p-2) % p = 1 => b的乘法逆元是b ^ (p-2)。

如果你不能确定为什么对于给定元素b在字段中且b^(p-1) % p = 1,我们有一个小代码片段来获得结果,我们需要使其数学上稳固,然后我们就有了它的证明,结论b^(p-1) % p = 1被称为费马小定理:

对于任何字段元素k(k!=0)和顺序p,我们有{1, 2, 3 …, p-1} <=> {k 1 % p, …, k (p-1) %p} =>
[1 2 3… (p-1)] % p == (k1) (k2) … (k* (p-1)) % p = k^(p-1) * [1 2 … p-1] % p,两边消去[12…p-1]我们得到1 % p == k ^(p-1) % p => 1 == k^(p-1)%p

现在让我们看看如何使用代码实现除法操作:


func (f *FieldElement) Multiply(other *FieldElement) *FieldElement {f.checkOrder(other)// 模顺序进行乘法var op big.Intmul := op.Mul(f.num, other.num)return NewFieldElement(f.order, op.Mod(mul, f.order))
}

因为b ^ (p - 1) % p = 1,所以当我们计算字段元素k的T次方时,我们可以优化为首先获取t = T % (p-1),然后计算k^(t) % p,这里是代码:


func (f *FieldElement) Power(power *big.Int) *FieldElement {/*k ^ (p-1) % p = 1,我们可以计算t = power % (p-1)然后k ^ power % p == k ^ t %p*/var op big.Intt := op.Mod(power, op.Sub(f.order, big.NewInt(int64(1))))powerRes := op.Exp(f.num, t, nil)modRes := op.Mod(powerRes, f.order)return NewFieldElement(f.order, modRes)
}

现在我们可以在main.go中检查我们的代码:


package mainimport (ecc "elliptic_curve""fmt""math/big""math/rand"
)func main() {f2 := ecc.NewFieldElement(big.NewInt(int64(19)), big.NewInt(int64(2)))f7 := ecc.NewFieldElement(big.NewInt(int64(19)), big.NewInt(int64(7)))fmt.Printf("field element 2 / 7 with order 19 is %v\n", f2.Divide(f7))f46 := ecc.NewFieldElement(big.NewInt(57), big.NewInt(46))fmt.Printf("field element 46 * 46 with order 57: %v\n", f46.Multiply(f46))fmt.Printf("field element 46 ^ (58) is %v\n", f46.Power(big.NewInt(int64(58))))
}

运行上述代码我们得到以下结果:

``go
复制代码
field element 2 / 7 with order 19 is FieldElement{order: 19, num: 3}
field element 46 * 46 with order 57: FieldElement{order: 57, num: 7}
field element 46 ^ (58) is FieldElement{order: 57, num: 7}

    
这正是我们所期望的,这就是字段元素的实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/465002.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深度体验SCNet超算平台:SCNet「AI跃升季」·谁是下一个“AI”跃人?

平时做大模型训练的时候总是苦于没有服务器资源来做微调实验&#xff0c;于是这次深度体验了一下SCNet超算平台。 SCNet超算平台是一个超算互联网计算服务平台&#xff0c;有着更大更全更专业的超级算力。显卡从异构加速卡到A800都有。 本次我尝试了大模型的推理和微调。 第一…

求助帖【如何学习核磁共振的原理】

最近提前进组了 我完全不懂磁共振的相关知识 想问问各位大佬有没有推荐的学习路线 或者是学习资料、论坛都可以的&#xff08;我做的方向是磁共振成像技术&#xff09; 老师给了一本书&#xff0c;但是有点看不懂&#xff0c;全英文的 叫Principles Of Magnetic Resonance …

MySQL查询where中包含多个in条件问题

示例&#xff1a; select * from x_table where a in (1,2,3) and b in (4,8) 上面这种查询方法&#xff0c;如果可以通过a和b唯一确定一条数据&#xff0c;但a和b列可以有相同值时&#xff0c;会造成查询数据不准确。 验证&#xff1a; 假设有以下数据&#xff08;手机号为…

HiveSQL 中判断字段是否包含某个值的方法

HiveSQL 中判断字段是否包含某个值的方法 在 HiveSQL 中&#xff0c;有时我们需要判断一个字段是否包含某个特定的值。下面将介绍几种常用的方法来实现这个功能。 一、创建示例表并插入数据 首先&#xff0c;我们创建一个名为employee的表&#xff0c;并插入一些示例数据&am…

python-读写Excel:openpyxl-(4)下拉选项设置

使用openpyxl库的DataValidation对象方法可添加下拉选择列表。 DataValidation参数说明&#xff1a; type&#xff1a; 数据类型("whole", "decimal", "list", "date", "time", "textLength", "custom"…

求平面连接线段组成的所有最小闭合区间

这个功能确实非常实用&#xff0c;我在过去开发地面分区编辑器时就曾应用过这一算法。最近&#xff0c;在新产品的开发中再次遇到了类似的需求。尽管之前已经实现过&#xff0c;但由于长时间未接触&#xff0c;对算法的具体细节有所遗忘&#xff0c;导致重新编写时耗费了不少时…

springboot - 定时任务

定时任务是企业级应用中的常见操作 定时任务是企业级开发中必不可少的组成部分&#xff0c;诸如长周期业务数据的计算&#xff0c;例如年度报表&#xff0c;诸如系统脏数据的处理&#xff0c;再比如系统性能监控报告&#xff0c;还有抢购类活动的商品上架&#xff0c;这些都离不…

ES管理工具Cerebro 0.8.5 Windows版本安装及启动

前言&#xff1a; Cerebro 的下载地址 https://github.com/lmenezes/cerebro/releases Cerebro 默认监听IP 0.0.0.0 &#xff0c;默认端口9000&#xff0c;访问地址&#xff1a;http://localhost:9000 启动 cmd命令到安装目录下&#xff1a;cerebro-0.8.5\bin 执行命令 ce…

Flutter 正在切换成 Monorepo 和支持 workspaces

其实关于 Monorepo 和 workspaces 相关内容在之前《Dart 3.5 发布&#xff0c;全新 Dart Roadmap Update》 和 《Flutter 之 ftcon24usa 大会&#xff0c;创始人分享 Flutter 十年发展史》 就有简单提到过&#xff0c;而目前来说刚好看到 flaux 这个新进展&#xff0c;所以就再…

[论文][环境]3DGS+Colmap环境搭建_WSL2_Ubuntu22.04 - 副本

0. 前言 仅使用Ubuntu进行场景编译&#xff0c;场景渲染查看则使用Windows下官方提供的编译好的预编译包打开即可&#xff0c;非常方便&#xff08;要注意即使是预编译版本&#xff0c;Windows端也应该安装VS和CUDA Toolkit&#xff0c;要注意的是&#xff0c;最新的SIBR预编译…

json-server的使用(根据json数据一键生成接口)

一.使用目的 在前端开发初期&#xff0c;后端 API 可能还未完成&#xff0c;json-server 可以快速创建模拟的 RESTful API&#xff0c;帮助前端开发者进行开发和测试。 二.安装 npm install json-server //局部安装npm i json-server -g //全局安装 三.使用教程 1.准备一…

导入和部署自定义 LLM 大模型

本文以【Qwen2-7B-Instruct】模型为例&#xff0c;指导如何将自定义大模型导入到 TI 平台&#xff0c;并使用平台内置推理镜像部署大模型对话推理服务。 前置要求 申请 CFS 本文所涉及到的操作需要通过 CFS 存储模型文件&#xff0c;详情请查看创建文件系统及挂载点。 操作…

开源办公软件 ONLYOFFICE 深入探索

文章目录 引言1. ONLYOFFICE 创建的背景1. 1 ONLYOFFICE 项目启动1. 2 ONLYOFFICE 的发展历程 2. 核心功能介绍2. 1 桌面编辑器2. 1. 1 文档2. 1. 2 表格2. 1. 3 幻灯片 2. 2 协作空间2. 3 文档编辑器 - 本地部署版 3. 技术介绍4. 安装5. 优势与挑战6. 个人体验7. 强大但不止于…

HTTP慢速攻击原理及解决办法

目录 引言 HTTP慢速攻击原理 解决办法 Nginx Tomcat 华宇TAS IIS 结论 引言 HTTP慢速攻击&#xff08;Slow HTTP Attack&#xff09;是一种拒绝服务攻击&#xff08;DoS&#xff09;&#xff0c;攻击者通过故意缓慢地发送HTTP请求来耗尽服务器资源&#xff0c;导致合法…

[mysql]修改表和课后练习

目录 DDL数据定义语言 添加一个字段 添加一个字段到最后一个 添加到表中的第一个一个字段 选择其中一个位置: 修改一个字段:数据类型,长度,默认值(略) 重命名一个字段 删除一个字段 重命名表 删除表 清空表 DCL中事务相关内容 DCL中COMMIT和ROLLBACK的讲解 对比TR…

SpringBoot+ClickHouse集成

前面已经完成ClickHouse的搭建&#xff0c;创建账号&#xff0c;创建数据库&#xff0c;保存数据库等&#xff0c;接下来就是在SpringBoot项目中集成ClickHouse。 一&#xff0c;引入依赖 <!-- SpringBoot集成ClickHouse --> <dependency><groupId>com.baom…

搜维尔科技:【煤矿虚拟仿真】煤矿企业、高校、科研单位-多语言支持、数字孪生、交互式学习体验

品牌&#xff1a;SouVR 发票&#xff1a;支持专票、普票 单位&#xff1a;套 版本号&#xff1a;1.0 包装清单&#xff1a;软件1套 软件形式&#xff1a;U盘、光盘 运行环境&#xff1a;windows 应用对象&#xff1a;煤矿企业、高校、科研单位 系统配置&#xff1a;…

(五)Spark大数据开发实战:灵活运用PySpark常用DataFrame API

目录 一、PySpark 二、数据介绍 三、PySpark大数据开发实战 1、数据文件上传HDFS 2、导入模块及数据 3、数据统计与分析 ①、计算演员参演电影数 ②、依次罗列电影番位前十的演员 ③、按照番位计算演员参演电影数 ④、求每位演员所有参演电影中的最早、最晚上映时间及…

单链表的实现(数据结构)

一. 单链表的实现 我们在上一篇中简单的认识了链表的组成和结构&#xff0c;并打印出链表&#xff0c;那么今天就来具体实现一下单链表对于数据增加、删减、插入等。 接下来就是我们在链表中对于数据的增、删、插的实现&#xff0c;对于我们的链表来说在任何地方增加数据都需…

Golang | Leetcode Golang题解之第540题有序数组中的单一元素

题目&#xff1a; 题解&#xff1a; func singleNonDuplicate(nums []int) int {low, high : 0, len(nums)-1for low < high {mid : low (high-low)/2mid - mid & 1if nums[mid] nums[mid1] {low mid 2} else {high mid}}return nums[low] }