Skip to content

Commit

Permalink
Switched to uint32 for idx and th
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosz committed Mar 23, 2024
1 parent 83e79eb commit f673a52
Show file tree
Hide file tree
Showing 33 changed files with 358 additions and 385 deletions.
23 changes: 0 additions & 23 deletions changelog.md

This file was deleted.

8 changes: 4 additions & 4 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type Marshaling interface {
// will have different constraints, of course. Two implementations are
// available:
//
// 1. The protobuf encoding using the variable length Google Protobuf encoding
// scheme. The library is available at https://go.dedis.ch/protobuf
// 2. The fixbuf encoding, a fixed length binary encoding of arbitrary
// structures. The library is available at https://go.dedis.ch/fixbuf.
// 1. The protobuf encoding using the variable length Google Protobuf encoding
// scheme. The library is available at https://go.dedis.ch/protobuf
// 2. The fixbuf encoding, a fixed length binary encoding of arbitrary
// structures. The library is available at https://go.dedis.ch/fixbuf.
type Encoding interface {
// Encode and write objects to an io.Writer.
Write(w io.Writer, objs ...interface{}) error
Expand Down
18 changes: 9 additions & 9 deletions examples/dkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func Test_Example_DKG(t *testing.T) {
// default number of node for this test
nStr = "7"
}
nUnszd, err := strconv.Atoi(nStr)
n := uint32(nUnszd)
nUnsz, err := strconv.Atoi(nStr)
n := uint32(nUnsz)
require.NoError(t, err)

type node struct {
Expand Down Expand Up @@ -70,7 +70,7 @@ func Test_Example_DKG(t *testing.T) {

// 2. Create the DKGs on each node
for i, node := range nodes {
dkg, err := dkg.NewDistKeyGenerator(suite, nodes[i].privKey, pubKeys, uint32(n))
dkg, err := dkg.NewDistKeyGenerator(suite, nodes[i].privKey, pubKeys, n)
require.NoError(t, err)
node.dkg = dkg
}
Expand Down Expand Up @@ -112,8 +112,8 @@ func Test_Example_DKG(t *testing.T) {
// 6. Check and print the qualified shares
for _, node := range nodes {
require.True(t, node.dkg.Certified())
require.Equal(t, n, len(node.dkg.QualifiedShares()))
require.Equal(t, n, len(node.dkg.QUAL()))
require.Equal(t, n, uint32(len(node.dkg.QualifiedShares())))
require.Equal(t, n, uint32(len(node.dkg.QUAL())))
t.Log("qualified shares:", node.dkg.QualifiedShares())
t.Log("QUAL", node.dkg.QUAL())
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_Example_DKG(t *testing.T) {
S := suite.Point().Mul(node.secretShare.V, K)
partials[i] = suite.Point().Sub(C, S)
pubShares[i] = &share.PubShare{
I: int32(i), V: partials[i],
I: uint32(i), V: partials[i],
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ func Test_Example_DKG(t *testing.T) {
)
partials[i] = v
pubShares[i] = &share.PubShare{
I: int32(i), V: partials[i],
I: uint32(i), V: partials[i],
}
}

Expand Down Expand Up @@ -252,8 +252,8 @@ func Test_Example_DKG(t *testing.T) {
OldNodes: pubKeys,
NewNodes: pubKeys,
Share: share,
Threshold: uint32(n),
OldThreshold: uint32(n),
Threshold: n,
OldThreshold: n,
}
newDkg, err := dkg.NewDistKeyHandler(c)
require.NoError(t, err)
Expand Down
76 changes: 38 additions & 38 deletions group/edwards25519/fe.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,27 @@ func feFromBytes(dst *fieldElement, src []byte) {

// feToBytes marshals h to s.
// Preconditions:
//
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
//
// Write p=2^255-19; q=floor(h/p).
// Basic claim: q = floor(2^(-255)(h + 19 2^(-25)h9 + 2^(-1))).
//
// Proof:
// Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4.
// Also have |h-2^230 h9|<2^230 so |19 2^(-255)(h-2^230 h9)|<1/4.
//
// Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4.
// Also have |h-2^230 h9|<2^230 so |19 2^(-255)(h-2^230 h9)|<1/4.
//
// Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9).
// Then 0<y<1.
// Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9).
// Then 0<y<1.
//
// Write r=h-pq.
// Have 0<=r<=p-1=2^255-20.
// Thus 0<=r+19(2^-255)r<r+19(2^-255)2^255<=2^255-1.
// Write r=h-pq.
// Have 0<=r<=p-1=2^255-20.
// Thus 0<=r+19(2^-255)r<r+19(2^-255)2^255<=2^255-1.
//
// Write x=r+19(2^-255)r+y.
// Then 0<x<2^255 so floor(2^(-255)x) = 0 so floor(q+2^(-255)x) = q.
// Write x=r+19(2^-255)r+y.
// Then 0<x<2^255 so floor(2^(-255)x) = 0 so floor(q+2^(-255)x) = q.
//
// Have q+2^(-255)x = 2^(-255)(h + 19 2^(-25) h9 + 2^(-1))
// so floor(2^(-255)(h + 19 2^(-25) h9 + 2^(-1))) = q.
// Have q+2^(-255)x = 2^(-255)(h + 19 2^(-25) h9 + 2^(-1))
// so floor(2^(-255)(h + 19 2^(-25) h9 + 2^(-1))) = q.
func feToBytes(s *[32]byte, h *fieldElement) {
var carry [10]int32

Expand Down Expand Up @@ -269,12 +267,10 @@ func feIsNonZero(f *fieldElement) int32 {
// feNeg sets h = -f
//
// Preconditions:
//
// |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
// |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
//
// Postconditions:
//
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
func feNeg(h, f *fieldElement) {
for i := range h {
h[i] = -f[i]
Expand All @@ -285,13 +281,11 @@ func feNeg(h, f *fieldElement) {
// Can overlap h with f or g.
//
// Preconditions:
//
// |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
// |g| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
// |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
// |g| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
//
// Postconditions:
//
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
//
// Notes on implementation strategy:
//
Expand Down Expand Up @@ -546,12 +540,10 @@ func feMul(h, f, g *fieldElement) {
// feSquare calculates h = f*f. Can overlap h with f.
//
// Preconditions:
//
// |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
// |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
//
// Postconditions:
//
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
func feSquare(h, f *fieldElement) {
f0 := f[0]
f1 := f[1]
Expand Down Expand Up @@ -703,13 +695,10 @@ func feSquare(h, f *fieldElement) {
// Can overlap h with f.
//
// Preconditions:
//
// |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
// |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
//
// Postconditions:
//
// |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc.
//
// |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc.
// See fe_mul.c for discussion of implementation strategy.
func feSquare2(h, f *fieldElement) {
f0 := f[0]
Expand Down Expand Up @@ -870,11 +859,13 @@ func feSquare2(h, f *fieldElement) {

func feInvert(out, z *fieldElement) {
var t0, t1, t2, t3 fieldElement
var i int32
var i int

feSquare(&t0, z) // 2^1
feSquare(&t1, &t0) // 2^2
feSquare(&t1, &t1) // 2^3
for i = 1; i < 2; i++ { // 2^3
feSquare(&t1, &t1)
}
feMul(&t1, z, &t1) // 2^3 + 2^0
feMul(&t0, &t0, &t1) // 2^3 + 2^1 + 2^0
feSquare(&t2, &t0) // 2^4 + 2^2 + 2^1
Expand Down Expand Up @@ -923,17 +914,24 @@ func feInvert(out, z *fieldElement) {

func fePow22523(out, z *fieldElement) {
var t0, t1, t2 fieldElement
var i int32
var i int

feSquare(&t0, z)
for i = 1; i < 1; i++ {
feSquare(&t0, &t0)
}
feSquare(&t1, &t0)
feSquare(&t1, &t1)
for i = 1; i < 2; i++ {
feSquare(&t1, &t1)
}
feMul(&t1, z, &t1)
feMul(&t0, &t0, &t1)
feSquare(&t0, &t0)
for i = 1; i < 1; i++ {
feSquare(&t0, &t0)
}
feMul(&t0, &t1, &t0)
feSquare(&t1, &t0)

for i = 1; i < 5; i++ {
feSquare(&t1, &t1)
}
Expand Down Expand Up @@ -969,7 +967,9 @@ func fePow22523(out, z *fieldElement) {
}
feMul(&t0, &t1, &t0)
feSquare(&t0, &t0)
feSquare(&t0, &t0)
for i = 1; i < 2; i++ {
feSquare(&t0, &t0)
}
feMul(out, &t0, z)
}

Expand Down
3 changes: 2 additions & 1 deletion group/edwards25519/ge.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ func geScalarMult(h *extendedGroupElement, a *[32]byte,
var u extendedGroupElement
var r projectiveGroupElement
var c cachedGroupElement
var i int

// Break the exponent into 4-bit nybbles.
var e [64]int8
Expand Down Expand Up @@ -466,7 +467,7 @@ func geScalarMult(h *extendedGroupElement, a *[32]byte,
selectCached(&c, &Ai, int32(e[63]))
t.Add(&u, &c)

for i := 62; i >= 0; i-- {
for i = 62; i >= 0; i-- {

// t <<= 4
t.ToProjective(&r)
Expand Down
2 changes: 1 addition & 1 deletion group/edwards25519/ge_mult_vartime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func geScalarMultVartime(h *extendedGroupElement, a *[32]byte,
var t completedGroupElement
var u, A2 extendedGroupElement
var r projectiveGroupElement
var i int32
var i int

// Slide through the scalar exponent clumping sequences of bits,
// resulting in only zero or odd multipliers between -15 and 15.
Expand Down
16 changes: 8 additions & 8 deletions group/mod/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ func (i *Int) MarshalSize() int {
// MarshalBinary encodes the value of this Int into a byte-slice exactly Len() bytes long.
// It uses i's ByteOrder to determine which byte order to output.
func (i *Int) MarshalBinary() ([]byte, error) {
l := int64(i.MarshalSize())
l := i.MarshalSize()
b := i.V.Bytes() // may be shorter than l
offset := l - int64(len(b))
offset := l - len(b)

if i.BO == LittleEndian {
return i.LittleEndian(l, l), nil
Expand Down Expand Up @@ -365,9 +365,9 @@ func (i *Int) UnmarshalFrom(r io.Reader) (int, error) {
// BigEndian encodes the value of this Int into a big-endian byte-slice
// at least min bytes but no more than max bytes long.
// Panics if max != 0 and the Int cannot be represented in max bytes.
func (i *Int) BigEndian(min, max int64) []byte {
act := int64(i.MarshalSize())
pad, ofs := act, int64(0)
func (i *Int) BigEndian(min, max int) []byte {
act := i.MarshalSize()
pad, ofs := act, 0
if pad < min {
pad, ofs = min, min-act
}
Expand All @@ -394,10 +394,10 @@ func (i *Int) SetBytes(a []byte) kyber.Scalar {
// LittleEndian encodes the value of this Int into a little-endian byte-slice
// at least min bytes but no more than max bytes long.
// Panics if max != 0 and the Int cannot be represented in max bytes.
func (i *Int) LittleEndian(min, max int64) []byte {
act := int64(i.MarshalSize())
func (i *Int) LittleEndian(min, max int) []byte {
act := i.MarshalSize()
vBytes := i.V.Bytes()
vSize := int64(len(vBytes))
vSize := len(vBytes)
if vSize < act {
act = vSize
}
Expand Down
12 changes: 6 additions & 6 deletions proof/deniable.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// the Sigma-protocol proofs of any or all of the other participants.
// Different participants may produce different proofs of varying sizes,
// and may even consist of different numbers of steps.
func DeniableProver(suite Suite, self int64, prover Prover,
func DeniableProver(suite Suite, self uint32, prover Prover,
verifiers []Verifier) Protocol {

return Protocol(func(ctx Context) []error {
Expand All @@ -25,7 +25,7 @@ func DeniableProver(suite Suite, self int64, prover Prover,

type deniableProver struct {
suite Suite // Agreed-on ciphersuite for protocol
self int64 // Our own node number
self uint32 // Our own node number
sc Context // Clique protocol context

// verifiers for other nodes' proofs
Expand All @@ -43,14 +43,14 @@ type deniableProver struct {
err []error
}

func (dp *deniableProver) run(suite Suite, self int64, prv Prover,
func (dp *deniableProver) run(suite Suite, self uint32, prv Prover,
vrf []Verifier, sc Context) []error {
dp.suite = suite
dp.self = self
dp.sc = sc
dp.prirand = sc.Random()

nnodes := int64(len(vrf))
nnodes := uint32(len(vrf))
if self < 0 || self >= nnodes {
return []error{errors.New("out-of-range self node")}
}
Expand All @@ -60,7 +60,7 @@ func (dp *deniableProver) run(suite Suite, self int64, prv Prover,
verr := errors.New("prover or verifier not run")
dp.err = make([]error, nnodes)
for i := range dp.err {
if int64(i) != self {
if uint32(i) != self {
dp.err[i] = verr
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (dp *deniableProver) challengeStep() error {
mix[j] ^= key[j]
}
}
if int64(len(keys)) <= dp.self || !bytes.Equal(keys[dp.self], dp.key) {
if uint32(len(keys)) <= dp.self || !bytes.Equal(keys[dp.self], dp.key) {
return errors.New("our own message was corrupted")
}

Expand Down
Loading

0 comments on commit f673a52

Please sign in to comment.