Skip to content

FourQ ecc_mul_double implementation #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions ecc/fourq/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,65 @@ func (P *Point) ScalarBaseMult(k *[Size]byte) {
P.fromR1(&_P)
}

// ScalarMultCofactor calculates P = k*Q, where Q is an N-torsion point. Allows for not clearing the cofactor.
func (P *Point) ScalarMultCofactor(k *[Size]byte, Q *Point, clearCofactor bool) {
var _P, _Q pointR1
Q.toR1(&_Q)

if clearCofactor {
_Q.ClearCofactor()
}
_P.ScalarMult(k, &_Q)
P.fromR1(&_P)
}

// DoubleScalarMult calculates P = k*G + l*Q, where the G is the generator.
func (P *Point) DoubleScalarMult(k *[Size]byte, Q *Point, l *[Size]byte) {

//Based on FourQLib's no endomorphism ecc_mul_double:

/*
point_t A;
point_extproj_t T;
point_extproj_precomp_t S;

if (ecc_mul(Q, l, A, false) == false) {
return false;
}
point_setup(A, T);
R1_to_R2(T, S);

ecc_mul_fixed(k, A);
point_setup(A, T);
eccadd(S, T);
eccnorm(T, R);
*/

var _A Point
var _T pointR1
var _S pointR2

// _A = q * l, don't clear cofactor
_A.ScalarMultCofactor(l, Q, false)

// _A -> _T
_A.toR1(&_T)

// _T -> _S
_S.FromR1(&_T)

// _A = k * G
_A.ScalarBaseMult(k)

// _A -> _T
_A.toR1(&_T)

// _T = _T * _S
_T.add(&_S)

P.fromR1(&_T)
}

func (P *Point) fromR1(Q *pointR1) {
Q.ToAffine()
P.X = Q.X
Expand Down
36 changes: 36 additions & 0 deletions ecc/fourq/point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fourq

import (
"crypto/rand"
"encoding/hex"
"math/big"
"testing"

Expand Down Expand Up @@ -123,6 +124,41 @@ func TestScalarMult(t *testing.T) {
})
}

func TestDoubleScalarMult(t *testing.T) {

want := "8cd1b92a5ee686eed483b2fc691a202ba09566e416a8774a2fca098075f2e774"

var k = [32]byte{
0x09, 0x58, 0x2b, 0xb4, 0x34, 0x9a, 0xfd, 0x1c,
0x23, 0xa8, 0xba, 0x06, 0x19, 0x62, 0xfb, 0x34,
0x35, 0x77, 0xe6, 0x77, 0x1e, 0x31, 0x5c, 0x21,
0x81, 0x98, 0x87, 0x01, 0x6d, 0xdb, 0x84, 0x27,
}

var l = [32]byte{
0xd1, 0x68, 0x56, 0xdc, 0x3d, 0xad, 0x5d, 0x45,
0x6c, 0x7b, 0x5e, 0x51, 0x17, 0x22, 0xb7, 0xa6,
0x90, 0x4b, 0x55, 0x76, 0x54, 0xbb, 0x2c, 0xca,
0x33, 0xe8, 0x3e, 0x77, 0xc5, 0x34, 0xb4, 0xff,
}

P := Point{}
P.SetGenerator()

P.DoubleScalarMult(&k, &P, &l)

encoded := [32]byte{}

P.Marshal(&encoded)

got := hex.EncodeToString(encoded[:])

if got != want {
test.ReportError(t, got, want, P)
}

}

func TestScalar(t *testing.T) {
const testTimes = 1 << 12
var xx [5]uint64
Expand Down