Exemplo n.º 1
0
func (p *point) Add(ca, cb abstract.Point) abstract.Point {
	a := ca.(*point)
	b := cb.(*point)
	if C.EC_POINT_add(p.c.g, p.p, a.p, b.p, p.c.ctx) == 0 {
		panic("EC_POINT_add: " + getErrString())
	}
	return p
}
Exemplo n.º 2
0
func (p *point) Sub(ca, cb abstract.Point) abstract.Point {
	a := ca.(*point)
	b := cb.(*point)
	// Add the point inverse.  Must use temporary if p == a.
	t := p
	if p == a {
		t = newPoint(p.c)
	}
	if C.EC_POINT_copy(t.p, b.p) == 0 {
		panic("EC_POINT_copy: " + getErrString())
	}
	if C.EC_POINT_invert(p.c.g, t.p, p.c.ctx) == 0 {
		panic("EC_POINT_invert: " + getErrString())
	}
	if C.EC_POINT_add(p.c.g, p.p, a.p, t.p, p.c.ctx) == 0 {
		panic("EC_POINT_add: " + getErrString())
	}
	return p
}
Exemplo n.º 3
0
func (curve CurveParams) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) {
	bnx1 := C.BN_new()
	if bnx1 == nil {
		return nil, nil
	}
	defer C.BN_free(bnx1)
	bny1 := C.BN_new()
	if bny1 == nil {
		return nil, nil
	}
	defer C.BN_free(bny1)
	bnx2 := C.BN_new()
	if bnx2 == nil {
		return nil, nil
	}
	defer C.BN_free(bnx2)
	bny2 := C.BN_new()
	if bny2 == nil {
		return nil, nil
	}
	defer C.BN_free(bny2)

	x1s := C.CString(x1.String())
	defer C.free(unsafe.Pointer(x1s))
	if C.BN_hex2bn(&bnx1, x1s) == 0 {
		return nil, nil
	}
	y1s := C.CString(y1.String())
	defer C.free(unsafe.Pointer(y1s))
	if C.BN_hex2bn(&bny1, y1s) == 0 {
		return nil, nil
	}
	x2s := C.CString(x2.String())
	defer C.free(unsafe.Pointer(x2s))
	if C.BN_hex2bn(&bnx2, x2s) == 0 {
		return nil, nil
	}
	y2s := C.CString(y2.String())
	defer C.free(unsafe.Pointer(y2s))
	if C.BN_hex2bn(&bny2, y2s) == 0 {
		return nil, nil
	}

	pointR := C.EC_POINT_new(curve.curve)
	if pointR == nil {
		return nil, nil
	}
	defer C.EC_POINT_free(pointR)

	pointA := C.EC_POINT_new(curve.curve)
	if pointA == nil {
		return nil, nil
	}
	defer C.EC_POINT_free(pointA)
	if C.EC_POINT_set_affine_coordinates_GFp(curve.curve, pointA, bnx1, bny1, nil) != 1 {
		return nil, nil
	}

	pointB := C.EC_POINT_new(curve.curve)
	if pointB == nil {
		return nil, nil
	}
	defer C.EC_POINT_free(pointB)
	if C.EC_POINT_set_affine_coordinates_GFp(curve.curve, pointB, bnx2, bny2, nil) != 1 {
		return nil, nil
	}

	if C.EC_POINT_add(curve.curve, pointR, pointA, pointB, nil) == 0 {
		return nil, nil
	}

	bnx := C.BN_new()
	if bnx == nil {
		return nil, nil
	}
	defer C.BN_free(bnx)
	bny := C.BN_new()
	if bny == nil {
		return nil, nil
	}
	defer C.BN_free(bny)

	if C.EC_POINT_get_affine_coordinates_GFp(curve.curve, pointR, bnx, bny, nil) != 1 {
		return nil, nil
	}
	x, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(bnx)), 16)
	y, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(bny)), 16)

	return
}