mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/luxfi/crypto/ipa/bandersnatch/fr"
|
|
"github.com/luxfi/crypto/ipa/banderwagon"
|
|
)
|
|
|
|
// VectorLength is the number of elements in the vector. This value is fixed.
|
|
// Note that this means that the degree of the polynomial is one less than this value.
|
|
const VectorLength = 256
|
|
|
|
// PowersOf returns powers of x from 0 to degree-1: <1, x, x^2, ..., x^(degree-1)>.
|
|
// Used for polynomial evaluation and computing challenge powers in IPA proofs.
|
|
func PowersOf(x fr.Element, degree int) []fr.Element {
|
|
result := make([]fr.Element, degree)
|
|
result[0] = fr.One()
|
|
|
|
for i := 1; i < degree; i++ {
|
|
result[i].Mul(&result[i-1], &x)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func ReadPoint(r io.Reader) (*banderwagon.Element, error) {
|
|
var x = make([]byte, 32)
|
|
if _, err := io.ReadAtLeast(r, x, 32); err != nil {
|
|
return nil, fmt.Errorf("reading x coordinate: %w", err)
|
|
}
|
|
var p = &banderwagon.Element{}
|
|
if err := p.SetBytes(x); err != nil {
|
|
return nil, fmt.Errorf("deserializing point: %w", err)
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func ReadScalar(r io.Reader) (*fr.Element, error) {
|
|
var x = make([]byte, 32)
|
|
if _, err := io.ReadAtLeast(r, x, 32); err != nil {
|
|
return nil, fmt.Errorf("reading scalar: %w", err)
|
|
}
|
|
var scalar = &fr.Element{}
|
|
if _, err := scalar.SetBytesLECanonical(x); err != nil {
|
|
return nil, fmt.Errorf("deserializing scalar: %s", err)
|
|
}
|
|
|
|
return scalar, nil
|
|
}
|